1 // Copyright (c) 2014 Marshall A. Greenblatt. Portions copyright (c) 2012 2 // Google Inc. All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are 6 // met: 7 // 8 // * Redistributions of source code must retain the above copyright 9 // notice, this list of conditions and the following disclaimer. 10 // * Redistributions in binary form must reproduce the above 11 // copyright notice, this list of conditions and the following disclaimer 12 // in the documentation and/or other materials provided with the 13 // distribution. 14 // * Neither the name of Google Inc. nor the name Chromium Embedded 15 // Framework nor the names of its contributors may be used to endorse 16 // or promote products derived from this software without specific prior 17 // written permission. 18 // 19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 31 #ifndef CEF_INCLUDE_BASE_CEF_MACROS_H_ 32 #define CEF_INCLUDE_BASE_CEF_MACROS_H_ 33 #pragma once 34 35 #if defined(USING_CHROMIUM_INCLUDES) 36 // When building CEF include the Chromium header directly. 37 #include "base/macros.h" 38 39 #else // !USING_CHROMIUM_INCLUDES 40 // The following is substantially similar to the Chromium implementation. 41 // If the Chromium implementation diverges the below implementation should be 42 // updated to match. 43 44 #include <stddef.h> // For size_t. 45 #include "include/base/cef_build.h" // For COMPILER_MSVC 46 47 #if !defined(arraysize) 48 49 // The arraysize(arr) macro returns the # of elements in an array arr. 50 // The expression is a compile-time constant, and therefore can be 51 // used in defining new arrays, for example. If you use arraysize on 52 // a pointer by mistake, you will get a compile-time error. 53 // 54 // One caveat is that arraysize() doesn't accept any array of an 55 // anonymous type or a type defined inside a function. In these rare 56 // cases, you have to use the unsafe ARRAYSIZE_UNSAFE() macro below. This is 57 // due to a limitation in C++'s template system. The limitation might 58 // eventually be removed, but it hasn't happened yet. 59 60 // This template function declaration is used in defining arraysize. 61 // Note that the function doesn't need an implementation, as we only 62 // use its type. 63 template <typename T, size_t N> 64 char (&ArraySizeHelper(T (&array)[N]))[N]; 65 66 // That gcc wants both of these prototypes seems mysterious. VC, for 67 // its part, can't decide which to use (another mystery). Matching of 68 // template overloads: the final frontier. 69 #ifndef _MSC_VER 70 template <typename T, size_t N> 71 char (&ArraySizeHelper(const T (&array)[N]))[N]; 72 #endif 73 74 #define arraysize(array) (sizeof(ArraySizeHelper(array))) 75 76 #endif // !arraysize 77 78 #if !defined(DISALLOW_COPY_AND_ASSIGN) 79 80 // A macro to disallow the copy constructor and operator= functions 81 // This should be used in the private: declarations for a class 82 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ 83 TypeName(const TypeName&); \ 84 void operator=(const TypeName&) 85 86 #endif // !DISALLOW_COPY_AND_ASSIGN 87 88 #if !defined(DISALLOW_IMPLICIT_CONSTRUCTORS) 89 90 // A macro to disallow all the implicit constructors, namely the 91 // default constructor, copy constructor and operator= functions. 92 // 93 // This should be used in the private: declarations for a class 94 // that wants to prevent anyone from instantiating it. This is 95 // especially useful for classes containing only static methods. 96 #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \ 97 TypeName(); \ 98 DISALLOW_COPY_AND_ASSIGN(TypeName) 99 100 #endif // !DISALLOW_IMPLICIT_CONSTRUCTORS 101 102 #if !defined(COMPILE_ASSERT) 103 104 // The COMPILE_ASSERT macro can be used to verify that a compile time 105 // expression is true. For example, you could use it to verify the 106 // size of a static array: 107 // 108 // COMPILE_ASSERT(ARRAYSIZE_UNSAFE(content_type_names) == CONTENT_NUM_TYPES, 109 // content_type_names_incorrect_size); 110 // 111 // or to make sure a struct is smaller than a certain size: 112 // 113 // COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large); 114 // 115 // The second argument to the macro is the name of the variable. If 116 // the expression is false, most compilers will issue a warning/error 117 // containing the name of the variable. 118 119 #if __cplusplus >= 201103L 120 121 // Under C++11, just use static_assert. 122 #define COMPILE_ASSERT(expr, msg) static_assert(expr, #msg) 123 124 #else 125 126 namespace cef { 127 128 template <bool> 129 struct CompileAssert {}; 130 131 } // namespace cef 132 133 #define COMPILE_ASSERT(expr, msg) \ 134 typedef cef::CompileAssert<(bool(expr))> \ 135 msg[bool(expr) ? 1 : -1] ALLOW_UNUSED_TYPE 136 137 // Implementation details of COMPILE_ASSERT: 138 // 139 // - COMPILE_ASSERT works by defining an array type that has -1 140 // elements (and thus is invalid) when the expression is false. 141 // 142 // - The simpler definition 143 // 144 // #define COMPILE_ASSERT(expr, msg) typedef char msg[(expr) ? 1 : -1] 145 // 146 // does not work, as gcc supports variable-length arrays whose sizes 147 // are determined at run-time (this is gcc's extension and not part 148 // of the C++ standard). As a result, gcc fails to reject the 149 // following code with the simple definition: 150 // 151 // int foo; 152 // COMPILE_ASSERT(foo, msg); // not supposed to compile as foo is 153 // // not a compile-time constant. 154 // 155 // - By using the type CompileAssert<(bool(expr))>, we ensures that 156 // expr is a compile-time constant. (Template arguments must be 157 // determined at compile-time.) 158 // 159 // - The outer parentheses in CompileAssert<(bool(expr))> are necessary 160 // to work around a bug in gcc 3.4.4 and 4.0.1. If we had written 161 // 162 // CompileAssert<bool(expr)> 163 // 164 // instead, these compilers will refuse to compile 165 // 166 // COMPILE_ASSERT(5 > 0, some_message); 167 // 168 // (They seem to think the ">" in "5 > 0" marks the end of the 169 // template argument list.) 170 // 171 // - The array size is (bool(expr) ? 1 : -1), instead of simply 172 // 173 // ((expr) ? 1 : -1). 174 // 175 // This is to avoid running into a bug in MS VC 7.1, which 176 // causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1. 177 178 #endif // !(__cplusplus >= 201103L) 179 180 #endif // !defined(COMPILE_ASSERT) 181 182 #endif // !USING_CHROMIUM_INCLUDES 183 184 #if !defined(MSVC_PUSH_DISABLE_WARNING) && defined(COMPILER_MSVC) 185 186 // MSVC_PUSH_DISABLE_WARNING pushes |n| onto a stack of warnings to be disabled. 187 // The warning remains disabled until popped by MSVC_POP_WARNING. 188 #define MSVC_PUSH_DISABLE_WARNING(n) \ 189 __pragma(warning(push)) __pragma(warning(disable : n)) 190 191 // MSVC_PUSH_WARNING_LEVEL pushes |n| as the global warning level. The level 192 // remains in effect until popped by MSVC_POP_WARNING(). Use 0 to disable all 193 // warnings. 194 #define MSVC_PUSH_WARNING_LEVEL(n) __pragma(warning(push, n)) 195 196 // Pop effects of innermost MSVC_PUSH_* macro. 197 #define MSVC_POP_WARNING() __pragma(warning(pop)) 198 199 #endif // !defined(MSVC_PUSH_DISABLE_WARNING) && defined(COMPILER_MSVC) 200 201 #if !defined(ALLOW_THIS_IN_INITIALIZER_LIST) 202 #if defined(COMPILER_MSVC) 203 // Allows |this| to be passed as an argument in constructor initializer lists. 204 // This uses push/pop instead of the seemingly simpler suppress feature to avoid 205 // having the warning be disabled for more than just |code|. 206 // 207 // Example usage: 208 // Foo::Foo() : x(NULL), ALLOW_THIS_IN_INITIALIZER_LIST(y(this)), z(3) {} 209 // 210 // Compiler warning C4355: 'this': used in base member initializer list: 211 // http://msdn.microsoft.com/en-us/library/3c594ae3(VS.80).aspx 212 #define ALLOW_THIS_IN_INITIALIZER_LIST(code) \ 213 MSVC_PUSH_DISABLE_WARNING(4355) \ 214 code MSVC_POP_WARNING() 215 #else // !COMPILER_MSVC 216 #define ALLOW_THIS_IN_INITIALIZER_LIST(code) code 217 #endif // !COMPILER_MSVC 218 #endif // !ALLOW_THIS_IN_INITIALIZER_LIST 219 220 #endif // CEF_INCLUDE_BASE_CEF_MACROS_H_ 221