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