Home | History | Annotate | Download | only in base
      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_  // NOLINT
     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))
     60 inline void Unused(const void*) {}
     61 #endif  // UNUSED
     62 
     63 #ifndef WIN32
     64 
     65 #ifndef strnicmp
     66 #define strnicmp(x, y, n) strncasecmp(x, y, n)
     67 #endif
     68 
     69 #ifndef stricmp
     70 #define stricmp(x, y) strcasecmp(x, y)
     71 #endif
     72 
     73 // TODO(fbarchard): Remove this. std::max should be used everywhere in the code.
     74 // 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 
     94 namespace talk_base {
     95 
     96 
     97 // If a debugger is attached, triggers a debugger breakpoint. If a debugger is
     98 // not attached, forces program termination.
     99 void Break();
    100 
    101 // 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.).
    104 void LogAssert(const char* function, const char* file, int line,
    105                const char* expression);
    106 
    107 typedef 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.
    117 void SetCustomAssertLogger(AssertLogger logger);
    118 
    119 bool IsOdd(int n);
    120 
    121 bool IsEven(int n);
    122 
    123 }  // namespace talk_base
    124 
    125 #if ENABLE_DEBUG
    126 
    127 namespace talk_base {
    128 
    129 inline bool Assert(bool result, const char* function, const char* file,
    130                    int line, const char* expression) {
    131   if (!result) {
    132     LogAssert(function, file, line, expression);
    133     Break();
    134     return false;
    135   }
    136   return true;
    137 }
    138 
    139 }  // namespace talk_base
    140 
    141 #if defined(_MSC_VER) && _MSC_VER < 1300
    142 #define __FUNCTION__ ""
    143 #endif
    144 
    145 #ifndef ASSERT
    146 #define ASSERT(x) \
    147     (void)talk_base::Assert((x), __FUNCTION__, __FILE__, __LINE__, #x)
    148 #endif
    149 
    150 #ifndef VERIFY
    151 #define VERIFY(x) talk_base::Assert((x), __FUNCTION__, __FILE__, __LINE__, #x)
    152 #endif
    153 
    154 #else  // !ENABLE_DEBUG
    155 
    156 namespace talk_base {
    157 
    158 inline bool ImplicitCastToBool(bool result) { return result; }
    159 
    160 }  // namespace talk_base
    161 
    162 #ifndef ASSERT
    163 #define ASSERT(x) (void)0
    164 #endif
    165 
    166 #ifndef VERIFY
    167 #define VERIFY(x) talk_base::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(WIN32)
    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(WIN32)
    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  // TALK_BASE_COMMON_H_    // NOLINT
    223