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
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))
RtcUnused(const void *)42 inline void RtcUnused(const void*) {}
43 #endif // RTC_UNUSED
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 #endif // !defined(WEBRTC_WIN)
56
57 /////////////////////////////////////////////////////////////////////////////
58 // Assertions
59 /////////////////////////////////////////////////////////////////////////////
60
61 #ifndef ENABLE_DEBUG
62 #if !defined(NDEBUG)
63 #define ENABLE_DEBUG 1
64 #else
65 #define ENABLE_DEBUG 0
66 #endif
67 #endif // !defined(ENABLE_DEBUG)
68
69 // Even for release builds, allow for the override of LogAssert. Though no
70 // macro is provided, this can still be used for explicit runtime asserts
71 // and allow applications to override the assert behavior.
72
73 namespace rtc {
74
75
76 // If a debugger is attached, triggers a debugger breakpoint. If a debugger is
77 // not attached, forces program termination.
78 void Break();
79
80 // LogAssert writes information about an assertion to the log. It's called by
81 // Assert (and from the ASSERT macro in debug mode) before any other action
82 // is taken (e.g. breaking the debugger, abort()ing, etc.).
83 void LogAssert(const char* function, const char* file, int line,
84 const char* expression);
85
86 typedef void (*AssertLogger)(const char* function,
87 const char* file,
88 int line,
89 const char* expression);
90
91 // Sets a custom assert logger to be used instead of the default LogAssert
92 // behavior. To clear the custom assert logger, pass NULL for |logger| and the
93 // default behavior will be restored. Only one custom assert logger can be set
94 // at a time, so this should generally be set during application startup and
95 // only by one component.
96 void SetCustomAssertLogger(AssertLogger logger);
97
98 bool IsOdd(int n);
99
100 bool IsEven(int n);
101
102 } // namespace rtc
103
104 #if ENABLE_DEBUG
105
106 namespace rtc {
107
Assert(bool result,const char * function,const char * file,int line,const char * expression)108 inline bool Assert(bool result, const char* function, const char* file,
109 int line, const char* expression) {
110 if (!result) {
111 LogAssert(function, file, line, expression);
112 Break();
113 }
114 return result;
115 }
116
117 // Same as Assert above, but does not call Break(). Used in assert macros
118 // that implement their own breaking.
AssertNoBreak(bool result,const char * function,const char * file,int line,const char * expression)119 inline bool AssertNoBreak(bool result, const char* function, const char* file,
120 int line, const char* expression) {
121 if (!result)
122 LogAssert(function, file, line, expression);
123 return result;
124 }
125
126 } // namespace rtc
127
128 #if defined(_MSC_VER) && _MSC_VER < 1300
129 #define __FUNCTION__ ""
130 #endif
131
132 #ifndef ASSERT
133 #if defined(WIN32)
134 // Using debugbreak() inline on Windows directly in the ASSERT macro, has the
135 // benefit of breaking exactly where the failing expression is and not two
136 // calls up the stack.
137 #define ASSERT(x) \
138 (rtc::AssertNoBreak((x), __FUNCTION__, __FILE__, __LINE__, #x) ? \
139 (void)(1) : __debugbreak())
140 #else
141 #define ASSERT(x) \
142 (void)rtc::Assert((x), __FUNCTION__, __FILE__, __LINE__, #x)
143 #endif
144 #endif
145
146 #ifndef VERIFY
147 #if defined(WIN32)
148 #define VERIFY(x) \
149 (rtc::AssertNoBreak((x), __FUNCTION__, __FILE__, __LINE__, #x) ? \
150 true : (__debugbreak(), false))
151 #else
152 #define VERIFY(x) rtc::Assert((x), __FUNCTION__, __FILE__, __LINE__, #x)
153 #endif
154 #endif
155
156 #else // !ENABLE_DEBUG
157
158 namespace rtc {
159
ImplicitCastToBool(bool result)160 inline bool ImplicitCastToBool(bool result) { return result; }
161
162 } // namespace rtc
163
164 #ifndef ASSERT
165 #define ASSERT(x) (void)0
166 #endif
167
168 #ifndef VERIFY
169 #define VERIFY(x) rtc::ImplicitCastToBool(x)
170 #endif
171
172 #endif // !ENABLE_DEBUG
173
174 #define COMPILE_TIME_ASSERT(expr) char CTA_UNIQUE_NAME[expr]
175 #define CTA_UNIQUE_NAME CTA_MAKE_NAME(__LINE__)
176 #define CTA_MAKE_NAME(line) CTA_MAKE_NAME2(line)
177 #define CTA_MAKE_NAME2(line) constraint_ ## line
178
179 // Forces compiler to inline, even against its better judgement. Use wisely.
180 #if defined(__GNUC__)
181 #define FORCE_INLINE __attribute__ ((__always_inline__))
182 #elif defined(WEBRTC_WIN)
183 #define FORCE_INLINE __forceinline
184 #else
185 #define FORCE_INLINE
186 #endif
187
188 // Annotate a function indicating the caller must examine the return value.
189 // Use like:
190 // int foo() WARN_UNUSED_RESULT;
191 // To explicitly ignore a result, see |ignore_result()| in <base/basictypes.h>.
192 // TODO(ajm): Hack to avoid multiple definitions until the base/ of webrtc and
193 // libjingle are merged.
194 #if !defined(WARN_UNUSED_RESULT)
195 #if defined(__GNUC__) || defined(__clang__)
196 #define WARN_UNUSED_RESULT __attribute__ ((__warn_unused_result__))
197 #else
198 #define WARN_UNUSED_RESULT
199 #endif
200 #endif // WARN_UNUSED_RESULT
201
202 #endif // WEBRTC_BASE_COMMON_H_ // NOLINT
203