• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * libjingle
3  * Copyright 2004--2005, 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/constructormagic.h"
32 
33 #if defined(_MSC_VER)
34 // warning C4355: 'this' : used in base member initializer list
35 #pragma warning(disable:4355)
36 #endif
37 
38 //////////////////////////////////////////////////////////////////////
39 // General Utilities
40 //////////////////////////////////////////////////////////////////////
41 
42 #ifndef UNUSED
43 #define UNUSED(x) Unused(static_cast<const void *>(&x))
44 #define UNUSED2(x,y) Unused(static_cast<const void *>(&x)); Unused(static_cast<const void *>(&y))
45 #define UNUSED3(x,y,z) Unused(static_cast<const void *>(&x)); Unused(static_cast<const void *>(&y)); Unused(static_cast<const void *>(&z))
46 #define UNUSED4(x,y,z,a) Unused(static_cast<const void *>(&x)); Unused(static_cast<const void *>(&y)); Unused(static_cast<const void *>(&z)); Unused(static_cast<const void *>(&a))
47 #define UNUSED5(x,y,z,a,b) Unused(static_cast<const void *>(&x)); Unused(static_cast<const void *>(&y)); Unused(static_cast<const void *>(&z)); Unused(static_cast<const void *>(&a)); Unused(static_cast<const void *>(&b))
Unused(const void *)48 inline void Unused(const void *) { }
49 #endif // UNUSED
50 
51 #ifndef WIN32
52 #define strnicmp(x,y,n) strncasecmp(x,y,n)
53 #define stricmp(x,y) strcasecmp(x,y)
54 
55 // TODO: Remove this. std::max should be used everywhere in the code.
56 // NOMINMAX must be defined where we include <windows.h>.
57 #define stdmax(x,y) std::max(x,y)
58 #else
59 #define stdmax(x,y) talk_base::_max(x,y)
60 #endif
61 
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 #if ENABLE_DEBUG
74 
75 namespace talk_base {
76 
77 // Break causes the debugger to stop executing, or the program to abort
78 void Break();
79 
80 // LogAssert writes information about an assertion to the log
81 void LogAssert(const char * function, const char * file, int line,
82                const char * expression);
83 
Assert(bool result,const char * function,const char * file,int line,const char * expression)84 inline bool Assert(bool result, const char * function, const char * file,
85                    int line, const char * expression) {
86   if (!result) {
87     LogAssert(function, file, line, expression);
88     Break();
89     return false;
90   }
91   return true;
92 }
93 
94 }  // namespace talk_base
95 
96 #if defined(_MSC_VER) && _MSC_VER < 1300
97 #define __FUNCTION__ ""
98 #endif
99 
100 #ifndef ASSERT
101 #define ASSERT(x) (void)talk_base::Assert((x),__FUNCTION__,__FILE__,__LINE__,#x)
102 #endif
103 
104 #ifndef VERIFY
105 #define VERIFY(x) talk_base::Assert((x),__FUNCTION__,__FILE__,__LINE__,#x)
106 #endif
107 
108 #else // !ENABLE_DEBUG
109 
110 namespace talk_base {
111 
ImplicitCastToBool(bool result)112 inline bool ImplicitCastToBool(bool result) { return result; }
113 
114 }  // namespace talk_base
115 
116 #ifndef ASSERT
117 #define ASSERT(x) (void)0
118 #endif
119 
120 #ifndef VERIFY
121 #define VERIFY(x) talk_base::ImplicitCastToBool(x)
122 #endif
123 
124 #endif // !ENABLE_DEBUG
125 
126 #define COMPILE_TIME_ASSERT(expr)       char CTA_UNIQUE_NAME[expr]
127 #define CTA_UNIQUE_NAME                 MAKE_NAME(__LINE__)
128 #define CTA_MAKE_NAME(line)             MAKE_NAME2(line)
129 #define CTA_MAKE_NAME2(line)            constraint_ ## line
130 
131 #ifdef __GNUC__
132 // Forces compiler to inline, even against its better judgement. Use wisely.
133 #define FORCE_INLINE __attribute__((always_inline))
134 #else
135 #define FORCE_INLINE
136 #endif
137 
138 #endif // TALK_BASE_COMMON_H_
139