henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #ifndef WEBRTC_BASE_COMMON_H_ // NOLINT |
| 12 | #define WEBRTC_BASE_COMMON_H_ |
| 13 | |
| 14 | #include "webrtc/base/basictypes.h" |
| 15 | #include "webrtc/base/constructormagic.h" |
| 16 | |
| 17 | #if defined(_MSC_VER) |
| 18 | // warning C4355: 'this' : used in base member initializer list |
| 19 | #pragma warning(disable:4355) |
| 20 | #endif |
| 21 | |
| 22 | ////////////////////////////////////////////////////////////////////// |
| 23 | // General Utilities |
| 24 | ////////////////////////////////////////////////////////////////////// |
| 25 | |
henrike@webrtc.org | 14abcc7 | 2014-05-16 16:54:44 +0000 | [diff] [blame] | 26 | #ifndef RTC_UNUSED |
| 27 | #define RTC_UNUSED(x) RtcUnused(static_cast<const void*>(&x)) |
| 28 | #define RTC_UNUSED2(x, y) RtcUnused(static_cast<const void*>(&x)); \ |
| 29 | RtcUnused(static_cast<const void*>(&y)) |
| 30 | #define RTC_UNUSED3(x, y, z) RtcUnused(static_cast<const void*>(&x)); \ |
| 31 | RtcUnused(static_cast<const void*>(&y)); \ |
| 32 | RtcUnused(static_cast<const void*>(&z)) |
| 33 | #define RTC_UNUSED4(x, y, z, a) RtcUnused(static_cast<const void*>(&x)); \ |
| 34 | RtcUnused(static_cast<const void*>(&y)); \ |
| 35 | RtcUnused(static_cast<const void*>(&z)); \ |
| 36 | RtcUnused(static_cast<const void*>(&a)) |
| 37 | #define RTC_UNUSED5(x, y, z, a, b) RtcUnused(static_cast<const void*>(&x)); \ |
| 38 | RtcUnused(static_cast<const void*>(&y)); \ |
| 39 | RtcUnused(static_cast<const void*>(&z)); \ |
| 40 | RtcUnused(static_cast<const void*>(&a)); \ |
| 41 | RtcUnused(static_cast<const void*>(&b)) |
| 42 | inline void RtcUnused(const void*) {} |
| 43 | #endif // RTC_UNUSED |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 44 | |
| 45 | #if !defined(WEBRTC_WIN) |
| 46 | |
| 47 | #ifndef strnicmp |
| 48 | #define strnicmp(x, y, n) strncasecmp(x, y, n) |
| 49 | #endif |
| 50 | |
| 51 | #ifndef stricmp |
| 52 | #define stricmp(x, y) strcasecmp(x, y) |
| 53 | #endif |
| 54 | |
andresp@webrtc.org | ff689be | 2015-02-12 11:54:26 +0000 | [diff] [blame] | 55 | #endif // !defined(WEBRTC_WIN) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 56 | |
| 57 | #define ARRAY_SIZE(x) (static_cast<int>(sizeof(x) / sizeof(x[0]))) |
| 58 | |
| 59 | ///////////////////////////////////////////////////////////////////////////// |
| 60 | // Assertions |
| 61 | ///////////////////////////////////////////////////////////////////////////// |
| 62 | |
| 63 | #ifndef ENABLE_DEBUG |
Tommi | e502bbe | 2015-10-31 22:41:43 +0100 | [diff] [blame^] | 64 | #if !defined(NDEBUG) |
| 65 | #define ENABLE_DEBUG 1 |
| 66 | #else |
| 67 | #define ENABLE_DEBUG 0 |
| 68 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 69 | #endif // !defined(ENABLE_DEBUG) |
| 70 | |
| 71 | // Even for release builds, allow for the override of LogAssert. Though no |
| 72 | // macro is provided, this can still be used for explicit runtime asserts |
| 73 | // and allow applications to override the assert behavior. |
| 74 | |
| 75 | namespace rtc { |
| 76 | |
| 77 | |
| 78 | // If a debugger is attached, triggers a debugger breakpoint. If a debugger is |
| 79 | // not attached, forces program termination. |
| 80 | void Break(); |
| 81 | |
| 82 | // LogAssert writes information about an assertion to the log. It's called by |
| 83 | // Assert (and from the ASSERT macro in debug mode) before any other action |
| 84 | // is taken (e.g. breaking the debugger, abort()ing, etc.). |
| 85 | void LogAssert(const char* function, const char* file, int line, |
| 86 | const char* expression); |
| 87 | |
| 88 | typedef void (*AssertLogger)(const char* function, |
| 89 | const char* file, |
| 90 | int line, |
| 91 | const char* expression); |
| 92 | |
| 93 | // Sets a custom assert logger to be used instead of the default LogAssert |
| 94 | // behavior. To clear the custom assert logger, pass NULL for |logger| and the |
| 95 | // default behavior will be restored. Only one custom assert logger can be set |
| 96 | // at a time, so this should generally be set during application startup and |
| 97 | // only by one component. |
| 98 | void SetCustomAssertLogger(AssertLogger logger); |
| 99 | |
henrike@webrtc.org | c00ca62 | 2014-06-23 16:15:27 +0000 | [diff] [blame] | 100 | bool IsOdd(int n); |
| 101 | |
| 102 | bool IsEven(int n); |
| 103 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 104 | } // namespace rtc |
| 105 | |
| 106 | #if ENABLE_DEBUG |
| 107 | |
| 108 | namespace rtc { |
| 109 | |
| 110 | inline bool Assert(bool result, const char* function, const char* file, |
| 111 | int line, const char* expression) { |
| 112 | if (!result) { |
| 113 | LogAssert(function, file, line, expression); |
| 114 | Break(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 115 | } |
henrike@webrtc.org | b614d06 | 2014-07-10 22:47:02 +0000 | [diff] [blame] | 116 | return result; |
| 117 | } |
| 118 | |
| 119 | // Same as Assert above, but does not call Break(). Used in assert macros |
| 120 | // that implement their own breaking. |
| 121 | inline bool AssertNoBreak(bool result, const char* function, const char* file, |
| 122 | int line, const char* expression) { |
| 123 | if (!result) |
| 124 | LogAssert(function, file, line, expression); |
| 125 | return result; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | } // namespace rtc |
| 129 | |
| 130 | #if defined(_MSC_VER) && _MSC_VER < 1300 |
| 131 | #define __FUNCTION__ "" |
| 132 | #endif |
| 133 | |
| 134 | #ifndef ASSERT |
henrike@webrtc.org | b614d06 | 2014-07-10 22:47:02 +0000 | [diff] [blame] | 135 | #if defined(WIN32) |
| 136 | // Using debugbreak() inline on Windows directly in the ASSERT macro, has the |
| 137 | // benefit of breaking exactly where the failing expression is and not two |
| 138 | // calls up the stack. |
| 139 | #define ASSERT(x) \ |
| 140 | (rtc::AssertNoBreak((x), __FUNCTION__, __FILE__, __LINE__, #x) ? \ |
| 141 | (void)(1) : __debugbreak()) |
| 142 | #else |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 143 | #define ASSERT(x) \ |
| 144 | (void)rtc::Assert((x), __FUNCTION__, __FILE__, __LINE__, #x) |
| 145 | #endif |
henrike@webrtc.org | b614d06 | 2014-07-10 22:47:02 +0000 | [diff] [blame] | 146 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 147 | |
| 148 | #ifndef VERIFY |
henrike@webrtc.org | b614d06 | 2014-07-10 22:47:02 +0000 | [diff] [blame] | 149 | #if defined(WIN32) |
| 150 | #define VERIFY(x) \ |
| 151 | (rtc::AssertNoBreak((x), __FUNCTION__, __FILE__, __LINE__, #x) ? \ |
| 152 | true : (__debugbreak(), false)) |
| 153 | #else |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 154 | #define VERIFY(x) rtc::Assert((x), __FUNCTION__, __FILE__, __LINE__, #x) |
| 155 | #endif |
henrike@webrtc.org | b614d06 | 2014-07-10 22:47:02 +0000 | [diff] [blame] | 156 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 157 | |
| 158 | #else // !ENABLE_DEBUG |
| 159 | |
| 160 | namespace rtc { |
| 161 | |
| 162 | inline bool ImplicitCastToBool(bool result) { return result; } |
| 163 | |
| 164 | } // namespace rtc |
| 165 | |
| 166 | #ifndef ASSERT |
| 167 | #define ASSERT(x) (void)0 |
| 168 | #endif |
| 169 | |
| 170 | #ifndef VERIFY |
| 171 | #define VERIFY(x) rtc::ImplicitCastToBool(x) |
| 172 | #endif |
| 173 | |
| 174 | #endif // !ENABLE_DEBUG |
| 175 | |
| 176 | #define COMPILE_TIME_ASSERT(expr) char CTA_UNIQUE_NAME[expr] |
| 177 | #define CTA_UNIQUE_NAME CTA_MAKE_NAME(__LINE__) |
| 178 | #define CTA_MAKE_NAME(line) CTA_MAKE_NAME2(line) |
| 179 | #define CTA_MAKE_NAME2(line) constraint_ ## line |
| 180 | |
| 181 | // Forces compiler to inline, even against its better judgement. Use wisely. |
| 182 | #if defined(__GNUC__) |
| 183 | #define FORCE_INLINE __attribute__((always_inline)) |
| 184 | #elif defined(WEBRTC_WIN) |
| 185 | #define FORCE_INLINE __forceinline |
| 186 | #else |
| 187 | #define FORCE_INLINE |
| 188 | #endif |
| 189 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 190 | // Annotate a function indicating the caller must examine the return value. |
| 191 | // Use like: |
| 192 | // int foo() WARN_UNUSED_RESULT; |
| 193 | // To explicitly ignore a result, see |ignore_result()| in <base/basictypes.h>. |
| 194 | // TODO(ajm): Hack to avoid multiple definitions until the base/ of webrtc and |
| 195 | // libjingle are merged. |
| 196 | #if !defined(WARN_UNUSED_RESULT) |
dcheng | 3402bcd | 2015-10-12 16:28:16 -0700 | [diff] [blame] | 197 | #if defined(__GNUC__) || defined(__clang__) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 198 | #define WARN_UNUSED_RESULT __attribute__((warn_unused_result)) |
| 199 | #else |
| 200 | #define WARN_UNUSED_RESULT |
| 201 | #endif |
| 202 | #endif // WARN_UNUSED_RESULT |
| 203 | |
| 204 | #endif // WEBRTC_BASE_COMMON_H_ // NOLINT |