blob: 2a8ac335943c75846320a2d34aa603947be36160 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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.org14abcc72014-05-16 16:54:44 +000026#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))
42inline void RtcUnused(const void*) {}
43#endif // RTC_UNUSED
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000044
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.orgff689be2015-02-12 11:54:26 +000055#endif // !defined(WEBRTC_WIN)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000056
57#define ARRAY_SIZE(x) (static_cast<int>(sizeof(x) / sizeof(x[0])))
58
59/////////////////////////////////////////////////////////////////////////////
60// Assertions
61/////////////////////////////////////////////////////////////////////////////
62
63#ifndef ENABLE_DEBUG
64#define ENABLE_DEBUG _DEBUG
65#endif // !defined(ENABLE_DEBUG)
66
67// Even for release builds, allow for the override of LogAssert. Though no
68// macro is provided, this can still be used for explicit runtime asserts
69// and allow applications to override the assert behavior.
70
71namespace rtc {
72
73
74// If a debugger is attached, triggers a debugger breakpoint. If a debugger is
75// not attached, forces program termination.
76void Break();
77
78// LogAssert writes information about an assertion to the log. It's called by
79// Assert (and from the ASSERT macro in debug mode) before any other action
80// is taken (e.g. breaking the debugger, abort()ing, etc.).
81void LogAssert(const char* function, const char* file, int line,
82 const char* expression);
83
84typedef void (*AssertLogger)(const char* function,
85 const char* file,
86 int line,
87 const char* expression);
88
89// Sets a custom assert logger to be used instead of the default LogAssert
90// behavior. To clear the custom assert logger, pass NULL for |logger| and the
91// default behavior will be restored. Only one custom assert logger can be set
92// at a time, so this should generally be set during application startup and
93// only by one component.
94void SetCustomAssertLogger(AssertLogger logger);
95
henrike@webrtc.orgc00ca622014-06-23 16:15:27 +000096bool IsOdd(int n);
97
98bool IsEven(int n);
99
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000100} // namespace rtc
101
102#if ENABLE_DEBUG
103
104namespace rtc {
105
106inline bool Assert(bool result, const char* function, const char* file,
107 int line, const char* expression) {
108 if (!result) {
109 LogAssert(function, file, line, expression);
110 Break();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000111 }
henrike@webrtc.orgb614d062014-07-10 22:47:02 +0000112 return result;
113}
114
115// Same as Assert above, but does not call Break(). Used in assert macros
116// that implement their own breaking.
117inline bool AssertNoBreak(bool result, const char* function, const char* file,
118 int line, const char* expression) {
119 if (!result)
120 LogAssert(function, file, line, expression);
121 return result;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000122}
123
124} // namespace rtc
125
126#if defined(_MSC_VER) && _MSC_VER < 1300
127#define __FUNCTION__ ""
128#endif
129
130#ifndef ASSERT
henrike@webrtc.orgb614d062014-07-10 22:47:02 +0000131#if defined(WIN32)
132// Using debugbreak() inline on Windows directly in the ASSERT macro, has the
133// benefit of breaking exactly where the failing expression is and not two
134// calls up the stack.
135#define ASSERT(x) \
136 (rtc::AssertNoBreak((x), __FUNCTION__, __FILE__, __LINE__, #x) ? \
137 (void)(1) : __debugbreak())
138#else
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000139#define ASSERT(x) \
140 (void)rtc::Assert((x), __FUNCTION__, __FILE__, __LINE__, #x)
141#endif
henrike@webrtc.orgb614d062014-07-10 22:47:02 +0000142#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000143
144#ifndef VERIFY
henrike@webrtc.orgb614d062014-07-10 22:47:02 +0000145#if defined(WIN32)
146#define VERIFY(x) \
147 (rtc::AssertNoBreak((x), __FUNCTION__, __FILE__, __LINE__, #x) ? \
148 true : (__debugbreak(), false))
149#else
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000150#define VERIFY(x) rtc::Assert((x), __FUNCTION__, __FILE__, __LINE__, #x)
151#endif
henrike@webrtc.orgb614d062014-07-10 22:47:02 +0000152#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000153
154#else // !ENABLE_DEBUG
155
156namespace rtc {
157
158inline bool ImplicitCastToBool(bool result) { return result; }
159
160} // namespace rtc
161
162#ifndef ASSERT
163#define ASSERT(x) (void)0
164#endif
165
166#ifndef VERIFY
167#define VERIFY(x) rtc::ImplicitCastToBool(x)
168#endif
169
170#endif // !ENABLE_DEBUG
171
172#define COMPILE_TIME_ASSERT(expr) char CTA_UNIQUE_NAME[expr]
173#define CTA_UNIQUE_NAME CTA_MAKE_NAME(__LINE__)
174#define CTA_MAKE_NAME(line) CTA_MAKE_NAME2(line)
175#define CTA_MAKE_NAME2(line) constraint_ ## line
176
177// Forces compiler to inline, even against its better judgement. Use wisely.
178#if defined(__GNUC__)
179#define FORCE_INLINE __attribute__((always_inline))
180#elif defined(WEBRTC_WIN)
181#define FORCE_INLINE __forceinline
182#else
183#define FORCE_INLINE
184#endif
185
186// Borrowed from Chromium's base/compiler_specific.h.
187// Annotate a virtual method indicating it must be overriding a virtual
188// method in the parent class.
189// Use like:
190// virtual void foo() OVERRIDE;
191#if defined(WEBRTC_WIN)
192#define OVERRIDE override
193#elif defined(__clang__)
194// Clang defaults to C++03 and warns about using override. Squelch that.
195// Intentionally no push/pop here so all users of OVERRIDE ignore the warning
196// too. This is like passing -Wno-c++11-extensions, except that GCC won't die
197// (because it won't see this pragma).
198#pragma clang diagnostic ignored "-Wc++11-extensions"
199#define OVERRIDE override
200#elif defined(__GNUC__) && __cplusplus >= 201103 && \
201 (__GNUC__ * 10000 + __GNUC_MINOR__ * 100) >= 40700
202// GCC 4.7 supports explicit virtual overrides when C++11 support is enabled.
203#define OVERRIDE override
204#else
205#define OVERRIDE
206#endif
207
208// Annotate a function indicating the caller must examine the return value.
209// Use like:
210// int foo() WARN_UNUSED_RESULT;
211// To explicitly ignore a result, see |ignore_result()| in <base/basictypes.h>.
212// TODO(ajm): Hack to avoid multiple definitions until the base/ of webrtc and
213// libjingle are merged.
214#if !defined(WARN_UNUSED_RESULT)
215#if defined(__GNUC__)
216#define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
217#else
218#define WARN_UNUSED_RESULT
219#endif
220#endif // WARN_UNUSED_RESULT
221
222#endif // WEBRTC_BASE_COMMON_H_ // NOLINT