blob: 381b6b5ac49a9baac19b612d5e4bf656c2dab3de [file] [log] [blame]
henrike@webrtc.org0e118e72013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
wu@webrtc.org5c9dd592013-10-25 21:18:33 +000028#ifndef TALK_BASE_COMMON_H_ // NOLINT
henrike@webrtc.org0e118e72013-07-10 00:45:36 +000029#define TALK_BASE_COMMON_H_
30
31#include "talk/base/basictypes.h"
32#include "talk/base/constructormagic.h"
33
34#if defined(_MSC_VER)
35// warning C4355: 'this' : used in base member initializer list
36#pragma warning(disable:4355)
37#endif
38
39//////////////////////////////////////////////////////////////////////
40// General Utilities
41//////////////////////////////////////////////////////////////////////
42
43// Note: UNUSED is also defined in basictypes.h
44#ifndef UNUSED
45#define UNUSED(x) Unused(static_cast<const void*>(&x))
46#define UNUSED2(x, y) Unused(static_cast<const void*>(&x)); \
47 Unused(static_cast<const void*>(&y))
48#define UNUSED3(x, y, z) Unused(static_cast<const void*>(&x)); \
49 Unused(static_cast<const void*>(&y)); \
50 Unused(static_cast<const void*>(&z))
51#define UNUSED4(x, y, z, a) Unused(static_cast<const void*>(&x)); \
52 Unused(static_cast<const void*>(&y)); \
53 Unused(static_cast<const void*>(&z)); \
54 Unused(static_cast<const void*>(&a))
55#define UNUSED5(x, y, z, a, b) Unused(static_cast<const void*>(&x)); \
56 Unused(static_cast<const void*>(&y)); \
57 Unused(static_cast<const void*>(&z)); \
58 Unused(static_cast<const void*>(&a)); \
59 Unused(static_cast<const void*>(&b))
60inline void Unused(const void*) {}
61#endif // UNUSED
62
63#ifndef WIN32
wu@webrtc.org2a81a382014-01-03 22:08:47 +000064
65#ifndef strnicmp
henrike@webrtc.org0e118e72013-07-10 00:45:36 +000066#define strnicmp(x, y, n) strncasecmp(x, y, n)
wu@webrtc.org2a81a382014-01-03 22:08:47 +000067#endif
68
69#ifndef stricmp
henrike@webrtc.org0e118e72013-07-10 00:45:36 +000070#define stricmp(x, y) strcasecmp(x, y)
wu@webrtc.org2a81a382014-01-03 22:08:47 +000071#endif
henrike@webrtc.org0e118e72013-07-10 00:45:36 +000072
wu@webrtc.org5c9dd592013-10-25 21:18:33 +000073// TODO(fbarchard): Remove this. std::max should be used everywhere in the code.
henrike@webrtc.org0e118e72013-07-10 00:45:36 +000074// NOMINMAX must be defined where we include <windows.h>.
75#define stdmax(x, y) std::max(x, y)
76#else
77#define stdmax(x, y) talk_base::_max(x, y)
78#endif
79
80#define ARRAY_SIZE(x) (static_cast<int>(sizeof(x) / sizeof(x[0])))
81
82/////////////////////////////////////////////////////////////////////////////
83// Assertions
84/////////////////////////////////////////////////////////////////////////////
85
86#ifndef ENABLE_DEBUG
87#define ENABLE_DEBUG _DEBUG
88#endif // !defined(ENABLE_DEBUG)
89
90// Even for release builds, allow for the override of LogAssert. Though no
91// macro is provided, this can still be used for explicit runtime asserts
92// and allow applications to override the assert behavior.
93
94namespace talk_base {
95
jiayl@webrtc.org13a42bc2014-01-29 17:45:53 +000096
97// If a debugger is attached, triggers a debugger breakpoint. If a debugger is
98// not attached, forces program termination.
99void Break();
100
henrike@webrtc.org0e118e72013-07-10 00:45:36 +0000101// LogAssert writes information about an assertion to the log. It's called by
102// Assert (and from the ASSERT macro in debug mode) before any other action
103// is taken (e.g. breaking the debugger, abort()ing, etc.).
104void LogAssert(const char* function, const char* file, int line,
105 const char* expression);
106
107typedef void (*AssertLogger)(const char* function,
108 const char* file,
109 int line,
110 const char* expression);
111
112// Sets a custom assert logger to be used instead of the default LogAssert
113// behavior. To clear the custom assert logger, pass NULL for |logger| and the
114// default behavior will be restored. Only one custom assert logger can be set
115// at a time, so this should generally be set during application startup and
116// only by one component.
117void SetCustomAssertLogger(AssertLogger logger);
118
119} // namespace talk_base
120
henrike@webrtc.org0e118e72013-07-10 00:45:36 +0000121#if ENABLE_DEBUG
122
123namespace talk_base {
124
henrike@webrtc.org0e118e72013-07-10 00:45:36 +0000125inline bool Assert(bool result, const char* function, const char* file,
126 int line, const char* expression) {
127 if (!result) {
128 LogAssert(function, file, line, expression);
129 Break();
130 return false;
131 }
132 return true;
133}
134
135} // namespace talk_base
136
137#if defined(_MSC_VER) && _MSC_VER < 1300
138#define __FUNCTION__ ""
139#endif
140
141#ifndef ASSERT
142#define ASSERT(x) \
143 (void)talk_base::Assert((x), __FUNCTION__, __FILE__, __LINE__, #x)
144#endif
145
146#ifndef VERIFY
147#define VERIFY(x) talk_base::Assert((x), __FUNCTION__, __FILE__, __LINE__, #x)
148#endif
149
150#else // !ENABLE_DEBUG
151
152namespace talk_base {
153
154inline bool ImplicitCastToBool(bool result) { return result; }
155
156} // namespace talk_base
157
158#ifndef ASSERT
159#define ASSERT(x) (void)0
160#endif
161
162#ifndef VERIFY
163#define VERIFY(x) talk_base::ImplicitCastToBool(x)
164#endif
165
166#endif // !ENABLE_DEBUG
167
168#define COMPILE_TIME_ASSERT(expr) char CTA_UNIQUE_NAME[expr]
169#define CTA_UNIQUE_NAME CTA_MAKE_NAME(__LINE__)
170#define CTA_MAKE_NAME(line) CTA_MAKE_NAME2(line)
171#define CTA_MAKE_NAME2(line) constraint_ ## line
172
173// Forces compiler to inline, even against its better judgement. Use wisely.
174#if defined(__GNUC__)
175#define FORCE_INLINE __attribute__((always_inline))
176#elif defined(WIN32)
177#define FORCE_INLINE __forceinline
178#else
179#define FORCE_INLINE
180#endif
181
182// Borrowed from Chromium's base/compiler_specific.h.
183// Annotate a virtual method indicating it must be overriding a virtual
184// method in the parent class.
185// Use like:
186// virtual void foo() OVERRIDE;
187#if defined(WIN32)
188#define OVERRIDE override
189#elif defined(__clang__)
wu@webrtc.org5c9dd592013-10-25 21:18:33 +0000190// Clang defaults to C++03 and warns about using override. Squelch that.
191// Intentionally no push/pop here so all users of OVERRIDE ignore the warning
192// too. This is like passing -Wno-c++11-extensions, except that GCC won't die
193// (because it won't see this pragma).
194#pragma clang diagnostic ignored "-Wc++11-extensions"
henrike@webrtc.org0e118e72013-07-10 00:45:36 +0000195#define OVERRIDE override
andrew@webrtc.org43f871c2013-12-11 00:07:11 +0000196#elif defined(__GNUC__) && __cplusplus >= 201103 && \
197 (__GNUC__ * 10000 + __GNUC_MINOR__ * 100) >= 40700
198// GCC 4.7 supports explicit virtual overrides when C++11 support is enabled.
199#define OVERRIDE override
henrike@webrtc.org0e118e72013-07-10 00:45:36 +0000200#else
201#define OVERRIDE
202#endif
203
wu@webrtc.org5c9dd592013-10-25 21:18:33 +0000204// Annotate a function indicating the caller must examine the return value.
205// Use like:
206// int foo() WARN_UNUSED_RESULT;
207// To explicitly ignore a result, see |ignore_result()| in <base/basictypes.h>.
208// TODO(ajm): Hack to avoid multiple definitions until the base/ of webrtc and
209// libjingle are merged.
210#if !defined(WARN_UNUSED_RESULT)
wu@webrtc.org4646ae62013-11-04 18:41:34 +0000211#if defined(__GNUC__)
wu@webrtc.org5c9dd592013-10-25 21:18:33 +0000212#define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
213#else
214#define WARN_UNUSED_RESULT
215#endif
216#endif // WARN_UNUSED_RESULT
217
218#endif // TALK_BASE_COMMON_H_ // NOLINT