1/*
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
28#ifndef TALK_BASE_COMMON_H_
29#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
64#define strnicmp(x, y, n) strncasecmp(x, y, n)
65#define stricmp(x, y) strcasecmp(x, y)
66
67// TODO: Remove this. std::max should be used everywhere in the code.
68// NOMINMAX must be defined where we include <windows.h>.
69#define stdmax(x, y) std::max(x, y)
70#else
71#define stdmax(x, y) talk_base::_max(x, y)
72#endif
73
74#define ARRAY_SIZE(x) (static_cast<int>(sizeof(x) / sizeof(x[0])))
75
76/////////////////////////////////////////////////////////////////////////////
77// Assertions
78/////////////////////////////////////////////////////////////////////////////
79
80#ifndef ENABLE_DEBUG
81#define ENABLE_DEBUG _DEBUG
82#endif  // !defined(ENABLE_DEBUG)
83
84// Even for release builds, allow for the override of LogAssert. Though no
85// macro is provided, this can still be used for explicit runtime asserts
86// and allow applications to override the assert behavior.
87
88namespace talk_base {
89
90// LogAssert writes information about an assertion to the log. It's called by
91// Assert (and from the ASSERT macro in debug mode) before any other action
92// is taken (e.g. breaking the debugger, abort()ing, etc.).
93void LogAssert(const char* function, const char* file, int line,
94               const char* expression);
95
96typedef void (*AssertLogger)(const char* function,
97                             const char* file,
98                             int line,
99                             const char* expression);
100
101// Sets a custom assert logger to be used instead of the default LogAssert
102// behavior. To clear the custom assert logger, pass NULL for |logger| and the
103// default behavior will be restored. Only one custom assert logger can be set
104// at a time, so this should generally be set during application startup and
105// only by one component.
106void SetCustomAssertLogger(AssertLogger logger);
107
108}  // namespace talk_base
109
110
111#if ENABLE_DEBUG
112
113namespace talk_base {
114
115// If a debugger is attached, triggers a debugger breakpoint. If a debugger is
116// not attached, forces program termination.
117void Break();
118
119inline bool Assert(bool result, const char* function, const char* file,
120                   int line, const char* expression) {
121  if (!result) {
122    LogAssert(function, file, line, expression);
123    Break();
124    return false;
125  }
126  return true;
127}
128
129}  // namespace talk_base
130
131#if defined(_MSC_VER) && _MSC_VER < 1300
132#define __FUNCTION__ ""
133#endif
134
135#ifndef ASSERT
136#define ASSERT(x) \
137    (void)talk_base::Assert((x), __FUNCTION__, __FILE__, __LINE__, #x)
138#endif
139
140#ifndef VERIFY
141#define VERIFY(x) talk_base::Assert((x), __FUNCTION__, __FILE__, __LINE__, #x)
142#endif
143
144#else  // !ENABLE_DEBUG
145
146namespace talk_base {
147
148inline bool ImplicitCastToBool(bool result) { return result; }
149
150}  // namespace talk_base
151
152#ifndef ASSERT
153#define ASSERT(x) (void)0
154#endif
155
156#ifndef VERIFY
157#define VERIFY(x) talk_base::ImplicitCastToBool(x)
158#endif
159
160#endif  // !ENABLE_DEBUG
161
162#define COMPILE_TIME_ASSERT(expr)       char CTA_UNIQUE_NAME[expr]
163#define CTA_UNIQUE_NAME                 CTA_MAKE_NAME(__LINE__)
164#define CTA_MAKE_NAME(line)             CTA_MAKE_NAME2(line)
165#define CTA_MAKE_NAME2(line)            constraint_ ## line
166
167// Forces compiler to inline, even against its better judgement. Use wisely.
168#if defined(__GNUC__)
169#define FORCE_INLINE __attribute__((always_inline))
170#elif defined(WIN32)
171#define FORCE_INLINE __forceinline
172#else
173#define FORCE_INLINE
174#endif
175
176// Borrowed from Chromium's base/compiler_specific.h.
177// Annotate a virtual method indicating it must be overriding a virtual
178// method in the parent class.
179// Use like:
180//   virtual void foo() OVERRIDE;
181#if defined(WIN32)
182#define OVERRIDE override
183#elif defined(__clang__)
184#define OVERRIDE override
185#else
186#define OVERRIDE
187#endif
188
189#endif  // TALK_BASE_COMMON_H_
190