henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2004 Google Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
wu@webrtc.org | 5c9dd59 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 28 | #ifndef TALK_BASE_COMMON_H_ // NOLINT |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 29 | #define TALK_BASE_COMMON_H_ |
| 30 | |
| 31 | #include "talk/base/basictypes.h" |
| 32 | #include "talk/base/constructormagic.h" |
| 33 | |
| 34 | #if defined(_MSC_VER) |
| 35 | // warning C4355: 'this' : used in base member initializer list |
| 36 | #pragma warning(disable:4355) |
| 37 | #endif |
| 38 | |
| 39 | ////////////////////////////////////////////////////////////////////// |
| 40 | // General Utilities |
| 41 | ////////////////////////////////////////////////////////////////////// |
| 42 | |
| 43 | // Note: UNUSED is also defined in basictypes.h |
| 44 | #ifndef UNUSED |
| 45 | #define UNUSED(x) Unused(static_cast<const void*>(&x)) |
| 46 | #define UNUSED2(x, y) Unused(static_cast<const void*>(&x)); \ |
| 47 | Unused(static_cast<const void*>(&y)) |
| 48 | #define UNUSED3(x, y, z) Unused(static_cast<const void*>(&x)); \ |
| 49 | Unused(static_cast<const void*>(&y)); \ |
| 50 | Unused(static_cast<const void*>(&z)) |
| 51 | #define UNUSED4(x, y, z, a) Unused(static_cast<const void*>(&x)); \ |
| 52 | Unused(static_cast<const void*>(&y)); \ |
| 53 | Unused(static_cast<const void*>(&z)); \ |
| 54 | Unused(static_cast<const void*>(&a)) |
| 55 | #define UNUSED5(x, y, z, a, b) Unused(static_cast<const void*>(&x)); \ |
| 56 | Unused(static_cast<const void*>(&y)); \ |
| 57 | Unused(static_cast<const void*>(&z)); \ |
| 58 | Unused(static_cast<const void*>(&a)); \ |
| 59 | Unused(static_cast<const void*>(&b)) |
| 60 | inline void Unused(const void*) {} |
| 61 | #endif // UNUSED |
| 62 | |
| 63 | #ifndef WIN32 |
wu@webrtc.org | 2a81a38 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 64 | |
| 65 | #ifndef strnicmp |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 66 | #define strnicmp(x, y, n) strncasecmp(x, y, n) |
wu@webrtc.org | 2a81a38 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 67 | #endif |
| 68 | |
| 69 | #ifndef stricmp |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 70 | #define stricmp(x, y) strcasecmp(x, y) |
wu@webrtc.org | 2a81a38 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 71 | #endif |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 72 | |
wu@webrtc.org | 5c9dd59 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 73 | // TODO(fbarchard): Remove this. std::max should be used everywhere in the code. |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 74 | // NOMINMAX must be defined where we include <windows.h>. |
| 75 | #define stdmax(x, y) std::max(x, y) |
| 76 | #else |
| 77 | #define stdmax(x, y) talk_base::_max(x, y) |
| 78 | #endif |
| 79 | |
| 80 | #define ARRAY_SIZE(x) (static_cast<int>(sizeof(x) / sizeof(x[0]))) |
| 81 | |
| 82 | ///////////////////////////////////////////////////////////////////////////// |
| 83 | // Assertions |
| 84 | ///////////////////////////////////////////////////////////////////////////// |
| 85 | |
| 86 | #ifndef ENABLE_DEBUG |
| 87 | #define ENABLE_DEBUG _DEBUG |
| 88 | #endif // !defined(ENABLE_DEBUG) |
| 89 | |
| 90 | // Even for release builds, allow for the override of LogAssert. Though no |
| 91 | // macro is provided, this can still be used for explicit runtime asserts |
| 92 | // and allow applications to override the assert behavior. |
| 93 | |
| 94 | namespace talk_base { |
| 95 | |
jiayl@webrtc.org | 13a42bc | 2014-01-29 17:45:53 +0000 | [diff] [blame] | 96 | |
| 97 | // If a debugger is attached, triggers a debugger breakpoint. If a debugger is |
| 98 | // not attached, forces program termination. |
| 99 | void Break(); |
| 100 | |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 101 | // LogAssert writes information about an assertion to the log. It's called by |
| 102 | // Assert (and from the ASSERT macro in debug mode) before any other action |
| 103 | // is taken (e.g. breaking the debugger, abort()ing, etc.). |
| 104 | void LogAssert(const char* function, const char* file, int line, |
| 105 | const char* expression); |
| 106 | |
| 107 | typedef void (*AssertLogger)(const char* function, |
| 108 | const char* file, |
| 109 | int line, |
| 110 | const char* expression); |
| 111 | |
| 112 | // Sets a custom assert logger to be used instead of the default LogAssert |
| 113 | // behavior. To clear the custom assert logger, pass NULL for |logger| and the |
| 114 | // default behavior will be restored. Only one custom assert logger can be set |
| 115 | // at a time, so this should generally be set during application startup and |
| 116 | // only by one component. |
| 117 | void SetCustomAssertLogger(AssertLogger logger); |
| 118 | |
| 119 | } // namespace talk_base |
| 120 | |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 121 | #if ENABLE_DEBUG |
| 122 | |
| 123 | namespace talk_base { |
| 124 | |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 125 | inline bool Assert(bool result, const char* function, const char* file, |
| 126 | int line, const char* expression) { |
| 127 | if (!result) { |
| 128 | LogAssert(function, file, line, expression); |
| 129 | Break(); |
| 130 | return false; |
| 131 | } |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | } // namespace talk_base |
| 136 | |
| 137 | #if defined(_MSC_VER) && _MSC_VER < 1300 |
| 138 | #define __FUNCTION__ "" |
| 139 | #endif |
| 140 | |
| 141 | #ifndef ASSERT |
| 142 | #define ASSERT(x) \ |
| 143 | (void)talk_base::Assert((x), __FUNCTION__, __FILE__, __LINE__, #x) |
| 144 | #endif |
| 145 | |
| 146 | #ifndef VERIFY |
| 147 | #define VERIFY(x) talk_base::Assert((x), __FUNCTION__, __FILE__, __LINE__, #x) |
| 148 | #endif |
| 149 | |
| 150 | #else // !ENABLE_DEBUG |
| 151 | |
| 152 | namespace talk_base { |
| 153 | |
| 154 | inline bool ImplicitCastToBool(bool result) { return result; } |
| 155 | |
| 156 | } // namespace talk_base |
| 157 | |
| 158 | #ifndef ASSERT |
| 159 | #define ASSERT(x) (void)0 |
| 160 | #endif |
| 161 | |
| 162 | #ifndef VERIFY |
| 163 | #define VERIFY(x) talk_base::ImplicitCastToBool(x) |
| 164 | #endif |
| 165 | |
| 166 | #endif // !ENABLE_DEBUG |
| 167 | |
| 168 | #define COMPILE_TIME_ASSERT(expr) char CTA_UNIQUE_NAME[expr] |
| 169 | #define CTA_UNIQUE_NAME CTA_MAKE_NAME(__LINE__) |
| 170 | #define CTA_MAKE_NAME(line) CTA_MAKE_NAME2(line) |
| 171 | #define CTA_MAKE_NAME2(line) constraint_ ## line |
| 172 | |
| 173 | // Forces compiler to inline, even against its better judgement. Use wisely. |
| 174 | #if defined(__GNUC__) |
| 175 | #define FORCE_INLINE __attribute__((always_inline)) |
| 176 | #elif defined(WIN32) |
| 177 | #define FORCE_INLINE __forceinline |
| 178 | #else |
| 179 | #define FORCE_INLINE |
| 180 | #endif |
| 181 | |
| 182 | // Borrowed from Chromium's base/compiler_specific.h. |
| 183 | // Annotate a virtual method indicating it must be overriding a virtual |
| 184 | // method in the parent class. |
| 185 | // Use like: |
| 186 | // virtual void foo() OVERRIDE; |
| 187 | #if defined(WIN32) |
| 188 | #define OVERRIDE override |
| 189 | #elif defined(__clang__) |
wu@webrtc.org | 5c9dd59 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 190 | // Clang defaults to C++03 and warns about using override. Squelch that. |
| 191 | // Intentionally no push/pop here so all users of OVERRIDE ignore the warning |
| 192 | // too. This is like passing -Wno-c++11-extensions, except that GCC won't die |
| 193 | // (because it won't see this pragma). |
| 194 | #pragma clang diagnostic ignored "-Wc++11-extensions" |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 195 | #define OVERRIDE override |
andrew@webrtc.org | 43f871c | 2013-12-11 00:07:11 +0000 | [diff] [blame] | 196 | #elif defined(__GNUC__) && __cplusplus >= 201103 && \ |
| 197 | (__GNUC__ * 10000 + __GNUC_MINOR__ * 100) >= 40700 |
| 198 | // GCC 4.7 supports explicit virtual overrides when C++11 support is enabled. |
| 199 | #define OVERRIDE override |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 200 | #else |
| 201 | #define OVERRIDE |
| 202 | #endif |
| 203 | |
wu@webrtc.org | 5c9dd59 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 204 | // Annotate a function indicating the caller must examine the return value. |
| 205 | // Use like: |
| 206 | // int foo() WARN_UNUSED_RESULT; |
| 207 | // To explicitly ignore a result, see |ignore_result()| in <base/basictypes.h>. |
| 208 | // TODO(ajm): Hack to avoid multiple definitions until the base/ of webrtc and |
| 209 | // libjingle are merged. |
| 210 | #if !defined(WARN_UNUSED_RESULT) |
wu@webrtc.org | 4646ae6 | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 211 | #if defined(__GNUC__) |
wu@webrtc.org | 5c9dd59 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 212 | #define WARN_UNUSED_RESULT __attribute__((warn_unused_result)) |
| 213 | #else |
| 214 | #define WARN_UNUSED_RESULT |
| 215 | #endif |
| 216 | #endif // WARN_UNUSED_RESULT |
| 217 | |
| 218 | #endif // TALK_BASE_COMMON_H_ // NOLINT |