henrike@webrtc.org | 47be73b | 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 | 5199d1d | 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 | 47be73b | 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 | |
| 55 | // TODO(fbarchard): Remove this. std::max should be used everywhere in the code. |
| 56 | // NOMINMAX must be defined where we include <windows.h>. |
| 57 | #define stdmax(x, y) std::max(x, y) |
| 58 | #else |
| 59 | #define stdmax(x, y) rtc::_max(x, y) |
| 60 | #endif |
| 61 | |
| 62 | #define ARRAY_SIZE(x) (static_cast<int>(sizeof(x) / sizeof(x[0]))) |
| 63 | |
| 64 | ///////////////////////////////////////////////////////////////////////////// |
| 65 | // Assertions |
| 66 | ///////////////////////////////////////////////////////////////////////////// |
| 67 | |
| 68 | #ifndef ENABLE_DEBUG |
| 69 | #define ENABLE_DEBUG _DEBUG |
| 70 | #endif // !defined(ENABLE_DEBUG) |
| 71 | |
| 72 | // Even for release builds, allow for the override of LogAssert. Though no |
| 73 | // macro is provided, this can still be used for explicit runtime asserts |
| 74 | // and allow applications to override the assert behavior. |
| 75 | |
| 76 | namespace rtc { |
| 77 | |
| 78 | |
| 79 | // If a debugger is attached, triggers a debugger breakpoint. If a debugger is |
| 80 | // not attached, forces program termination. |
| 81 | void Break(); |
| 82 | |
| 83 | // LogAssert writes information about an assertion to the log. It's called by |
| 84 | // Assert (and from the ASSERT macro in debug mode) before any other action |
| 85 | // is taken (e.g. breaking the debugger, abort()ing, etc.). |
| 86 | void LogAssert(const char* function, const char* file, int line, |
| 87 | const char* expression); |
| 88 | |
| 89 | typedef void (*AssertLogger)(const char* function, |
| 90 | const char* file, |
| 91 | int line, |
| 92 | const char* expression); |
| 93 | |
| 94 | // Sets a custom assert logger to be used instead of the default LogAssert |
| 95 | // behavior. To clear the custom assert logger, pass NULL for |logger| and the |
| 96 | // default behavior will be restored. Only one custom assert logger can be set |
| 97 | // at a time, so this should generally be set during application startup and |
| 98 | // only by one component. |
| 99 | void SetCustomAssertLogger(AssertLogger logger); |
| 100 | |
| 101 | } // namespace rtc |
| 102 | |
| 103 | #if ENABLE_DEBUG |
| 104 | |
| 105 | namespace rtc { |
| 106 | |
| 107 | inline bool Assert(bool result, const char* function, const char* file, |
| 108 | int line, const char* expression) { |
| 109 | if (!result) { |
| 110 | LogAssert(function, file, line, expression); |
| 111 | Break(); |
| 112 | return false; |
| 113 | } |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | } // namespace rtc |
| 118 | |
| 119 | #if defined(_MSC_VER) && _MSC_VER < 1300 |
| 120 | #define __FUNCTION__ "" |
| 121 | #endif |
| 122 | |
| 123 | #ifndef ASSERT |
| 124 | #define ASSERT(x) \ |
| 125 | (void)rtc::Assert((x), __FUNCTION__, __FILE__, __LINE__, #x) |
| 126 | #endif |
| 127 | |
| 128 | #ifndef VERIFY |
| 129 | #define VERIFY(x) rtc::Assert((x), __FUNCTION__, __FILE__, __LINE__, #x) |
| 130 | #endif |
| 131 | |
| 132 | #else // !ENABLE_DEBUG |
| 133 | |
| 134 | namespace rtc { |
| 135 | |
| 136 | inline bool ImplicitCastToBool(bool result) { return result; } |
| 137 | |
| 138 | } // namespace rtc |
| 139 | |
| 140 | #ifndef ASSERT |
| 141 | #define ASSERT(x) (void)0 |
| 142 | #endif |
| 143 | |
| 144 | #ifndef VERIFY |
| 145 | #define VERIFY(x) rtc::ImplicitCastToBool(x) |
| 146 | #endif |
| 147 | |
| 148 | #endif // !ENABLE_DEBUG |
| 149 | |
| 150 | #define COMPILE_TIME_ASSERT(expr) char CTA_UNIQUE_NAME[expr] |
| 151 | #define CTA_UNIQUE_NAME CTA_MAKE_NAME(__LINE__) |
| 152 | #define CTA_MAKE_NAME(line) CTA_MAKE_NAME2(line) |
| 153 | #define CTA_MAKE_NAME2(line) constraint_ ## line |
| 154 | |
| 155 | // Forces compiler to inline, even against its better judgement. Use wisely. |
| 156 | #if defined(__GNUC__) |
| 157 | #define FORCE_INLINE __attribute__((always_inline)) |
| 158 | #elif defined(WEBRTC_WIN) |
| 159 | #define FORCE_INLINE __forceinline |
| 160 | #else |
| 161 | #define FORCE_INLINE |
| 162 | #endif |
| 163 | |
| 164 | // Borrowed from Chromium's base/compiler_specific.h. |
| 165 | // Annotate a virtual method indicating it must be overriding a virtual |
| 166 | // method in the parent class. |
| 167 | // Use like: |
| 168 | // virtual void foo() OVERRIDE; |
| 169 | #if defined(WEBRTC_WIN) |
| 170 | #define OVERRIDE override |
| 171 | #elif defined(__clang__) |
| 172 | // Clang defaults to C++03 and warns about using override. Squelch that. |
| 173 | // Intentionally no push/pop here so all users of OVERRIDE ignore the warning |
| 174 | // too. This is like passing -Wno-c++11-extensions, except that GCC won't die |
| 175 | // (because it won't see this pragma). |
| 176 | #pragma clang diagnostic ignored "-Wc++11-extensions" |
| 177 | #define OVERRIDE override |
| 178 | #elif defined(__GNUC__) && __cplusplus >= 201103 && \ |
| 179 | (__GNUC__ * 10000 + __GNUC_MINOR__ * 100) >= 40700 |
| 180 | // GCC 4.7 supports explicit virtual overrides when C++11 support is enabled. |
| 181 | #define OVERRIDE override |
| 182 | #else |
| 183 | #define OVERRIDE |
| 184 | #endif |
| 185 | |
| 186 | // Annotate a function indicating the caller must examine the return value. |
| 187 | // Use like: |
| 188 | // int foo() WARN_UNUSED_RESULT; |
| 189 | // To explicitly ignore a result, see |ignore_result()| in <base/basictypes.h>. |
| 190 | // TODO(ajm): Hack to avoid multiple definitions until the base/ of webrtc and |
| 191 | // libjingle are merged. |
| 192 | #if !defined(WARN_UNUSED_RESULT) |
| 193 | #if defined(__GNUC__) |
| 194 | #define WARN_UNUSED_RESULT __attribute__((warn_unused_result)) |
| 195 | #else |
| 196 | #define WARN_UNUSED_RESULT |
| 197 | #endif |
| 198 | #endif // WARN_UNUSED_RESULT |
| 199 | |
| 200 | #endif // WEBRTC_BASE_COMMON_H_ // NOLINT |