| 1 | // Copyright 2008 Google Inc. All Rights Reserved. | 
|---|
| 2 | // | 
|---|
| 3 | // Various Google-specific macros. | 
|---|
| 4 | // | 
|---|
| 5 | // This code is compiled directly on many platforms, including client | 
|---|
| 6 | // platforms like Windows, Mac, and embedded systems.  Before making | 
|---|
| 7 | // any changes here, make sure that you're not breaking any platforms. | 
|---|
| 8 | // | 
|---|
| 9 |  | 
|---|
| 10 | #ifndef BASE_MACROS_H_ | 
|---|
| 11 | #define BASE_MACROS_H_ | 
|---|
| 12 |  | 
|---|
| 13 | #include <stddef.h>         // For size_t | 
|---|
| 14 |  | 
|---|
| 15 | // We use our own  local  version of type traits while we're waiting | 
|---|
| 16 | // for TR1 type traits to be standardized. Define some macros so that | 
|---|
| 17 | // most google3 code doesn't have to work with type traits directly. | 
|---|
| 18 | #include "base/type_traits.h" | 
|---|
| 19 |  | 
|---|
| 20 | // The swigged version of an abstract class must be concrete if any methods | 
|---|
| 21 | // return objects of the abstract type. We keep it abstract in C++ and | 
|---|
| 22 | // concrete for swig. | 
|---|
| 23 | #ifndef SWIG | 
|---|
| 24 | #define ABSTRACT = 0 | 
|---|
| 25 | #endif | 
|---|
| 26 |  | 
|---|
| 27 | // The COMPILE_ASSERT macro can be used to verify that a compile time | 
|---|
| 28 | // expression is true. For example, you could use it to verify the | 
|---|
| 29 | // size of a static array: | 
|---|
| 30 | // | 
|---|
| 31 | //   COMPILE_ASSERT(ARRAYSIZE(content_type_names) == CONTENT_NUM_TYPES, | 
|---|
| 32 | //                  content_type_names_incorrect_size); | 
|---|
| 33 | // | 
|---|
| 34 | // or to make sure a struct is smaller than a certain size: | 
|---|
| 35 | // | 
|---|
| 36 | //   COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large); | 
|---|
| 37 | // | 
|---|
| 38 | // The second argument to the macro is the name of the variable. If | 
|---|
| 39 | // the expression is false, most compilers will issue a warning/error | 
|---|
| 40 | // containing the name of the variable. | 
|---|
| 41 |  | 
|---|
| 42 | template <bool> | 
|---|
| 43 | struct CompileAssert { | 
|---|
| 44 | }; | 
|---|
| 45 |  | 
|---|
| 46 | #define COMPILE_ASSERT(expr, msg) \ | 
|---|
| 47 | typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] | 
|---|
| 48 |  | 
|---|
| 49 | // Implementation details of COMPILE_ASSERT: | 
|---|
| 50 | // | 
|---|
| 51 | // - COMPILE_ASSERT works by defining an array type that has -1 | 
|---|
| 52 | //   elements (and thus is invalid) when the expression is false. | 
|---|
| 53 | // | 
|---|
| 54 | // - The simpler definition | 
|---|
| 55 | // | 
|---|
| 56 | //     #define COMPILE_ASSERT(expr, msg) typedef char msg[(expr) ? 1 : -1] | 
|---|
| 57 | // | 
|---|
| 58 | //   does not work, as gcc supports variable-length arrays whose sizes | 
|---|
| 59 | //   are determined at run-time (this is gcc's extension and not part | 
|---|
| 60 | //   of the C++ standard).  As a result, gcc fails to reject the | 
|---|
| 61 | //   following code with the simple definition: | 
|---|
| 62 | // | 
|---|
| 63 | //     int foo; | 
|---|
| 64 | //     COMPILE_ASSERT(foo, msg); // not supposed to compile as foo is | 
|---|
| 65 | //                               // not a compile-time constant. | 
|---|
| 66 | // | 
|---|
| 67 | // - By using the type CompileAssert<(bool(expr))>, we ensures that | 
|---|
| 68 | //   expr is a compile-time constant.  (Template arguments must be | 
|---|
| 69 | //   determined at compile-time.) | 
|---|
| 70 | // | 
|---|
| 71 | // - The outter parentheses in CompileAssert<(bool(expr))> are necessary | 
|---|
| 72 | //   to work around a bug in gcc 3.4.4 and 4.0.1.  If we had written | 
|---|
| 73 | // | 
|---|
| 74 | //     CompileAssert<bool(expr)> | 
|---|
| 75 | // | 
|---|
| 76 | //   instead, these compilers will refuse to compile | 
|---|
| 77 | // | 
|---|
| 78 | //     COMPILE_ASSERT(5 > 0, some_message); | 
|---|
| 79 | // | 
|---|
| 80 | //   (They seem to think the ">" in "5 > 0" marks the end of the | 
|---|
| 81 | //   template argument list.) | 
|---|
| 82 | // | 
|---|
| 83 | // - The array size is (bool(expr) ? 1 : -1), instead of simply | 
|---|
| 84 | // | 
|---|
| 85 | //     ((expr) ? 1 : -1). | 
|---|
| 86 | // | 
|---|
| 87 | //   This is to avoid running into a bug in MS VC 7.1, which | 
|---|
| 88 | //   causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1. | 
|---|
| 89 |  | 
|---|
| 90 |  | 
|---|
| 91 | // A macro to disallow the copy constructor and operator= functions | 
|---|
| 92 | // This should be used in the private: declarations for a class | 
|---|
| 93 | // | 
|---|
| 94 | // For disallowing only assign or copy, write the code directly, but declare | 
|---|
| 95 | // the intend in a comment, for example: | 
|---|
| 96 | // void operator=(const TypeName&);  // DISALLOW_ASSIGN | 
|---|
| 97 | // Note, that most uses of DISALLOW_ASSIGN and DISALLOW_COPY are broken | 
|---|
| 98 | // semantically, one should either use disallow both or neither. Try to | 
|---|
| 99 | // avoid these in new code. | 
|---|
| 100 | #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ | 
|---|
| 101 | TypeName(const TypeName&);               \ | 
|---|
| 102 | void operator=(const TypeName&) | 
|---|
| 103 |  | 
|---|
| 104 | // An older, politically incorrect name for the above. | 
|---|
| 105 | // Prefer DISALLOW_COPY_AND_ASSIGN for new code. | 
|---|
| 106 | #define DISALLOW_EVIL_CONSTRUCTORS(TypeName) DISALLOW_COPY_AND_ASSIGN(TypeName) | 
|---|
| 107 |  | 
|---|
| 108 | // A macro to disallow all the implicit constructors, namely the | 
|---|
| 109 | // default constructor, copy constructor and operator= functions. | 
|---|
| 110 | // | 
|---|
| 111 | // This should be used in the private: declarations for a class | 
|---|
| 112 | // that wants to prevent anyone from instantiating it. This is | 
|---|
| 113 | // especially useful for classes containing only static methods. | 
|---|
| 114 | #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \ | 
|---|
| 115 | TypeName();                                    \ | 
|---|
| 116 | DISALLOW_COPY_AND_ASSIGN(TypeName) | 
|---|
| 117 |  | 
|---|
| 118 | // The arraysize(arr) macro returns the # of elements in an array arr. | 
|---|
| 119 | // The expression is a compile-time constant, and therefore can be | 
|---|
| 120 | // used in defining new arrays, for example.  If you use arraysize on | 
|---|
| 121 | // a pointer by mistake, you will get a compile-time error. | 
|---|
| 122 | // | 
|---|
| 123 | // One caveat is that arraysize() doesn't accept any array of an | 
|---|
| 124 | // anonymous type or a type defined inside a function.  In these rare | 
|---|
| 125 | // cases, you have to use the unsafe ARRAYSIZE() macro below.  This is | 
|---|
| 126 | // due to a limitation in C++'s template system.  The limitation might | 
|---|
| 127 | // eventually be removed, but it hasn't happened yet. | 
|---|
| 128 |  | 
|---|
| 129 | // This template function declaration is used in defining arraysize. | 
|---|
| 130 | // Note that the function doesn't need an implementation, as we only | 
|---|
| 131 | // use its type. | 
|---|
| 132 | template <typename T, size_t N> | 
|---|
| 133 | char (&ArraySizeHelper(T (&array)[N]))[N]; | 
|---|
| 134 |  | 
|---|
| 135 | // That gcc wants both of these prototypes seems mysterious. VC, for | 
|---|
| 136 | // its part, can't decide which to use (another mystery). Matching of | 
|---|
| 137 | // template overloads: the final frontier. | 
|---|
| 138 | #ifndef COMPILER_MSVC | 
|---|
| 139 | template <typename T, size_t N> | 
|---|
| 140 | char (&ArraySizeHelper(const T (&array)[N]))[N]; | 
|---|
| 141 | #endif | 
|---|
| 142 |  | 
|---|
| 143 | #define arraysize(array) (sizeof(ArraySizeHelper(array))) | 
|---|
| 144 |  | 
|---|
| 145 | // ARRAYSIZE performs essentially the same calculation as arraysize, | 
|---|
| 146 | // but can be used on anonymous types or types defined inside | 
|---|
| 147 | // functions.  It's less safe than arraysize as it accepts some | 
|---|
| 148 | // (although not all) pointers.  Therefore, you should use arraysize | 
|---|
| 149 | // whenever possible. | 
|---|
| 150 | // | 
|---|
| 151 | // The expression ARRAYSIZE(a) is a compile-time constant of type | 
|---|
| 152 | // size_t. | 
|---|
| 153 | // | 
|---|
| 154 | // ARRAYSIZE catches a few type errors.  If you see a compiler error | 
|---|
| 155 | // | 
|---|
| 156 | //   "warning: division by zero in ..." | 
|---|
| 157 | // | 
|---|
| 158 | // when using ARRAYSIZE, you are (wrongfully) giving it a pointer. | 
|---|
| 159 | // You should only use ARRAYSIZE on statically allocated arrays. | 
|---|
| 160 | // | 
|---|
| 161 | // The following comments are on the implementation details, and can | 
|---|
| 162 | // be ignored by the users. | 
|---|
| 163 | // | 
|---|
| 164 | // ARRAYSIZE(arr) works by inspecting sizeof(arr) (the # of bytes in | 
|---|
| 165 | // the array) and sizeof(*(arr)) (the # of bytes in one array | 
|---|
| 166 | // element).  If the former is divisible by the latter, perhaps arr is | 
|---|
| 167 | // indeed an array, in which case the division result is the # of | 
|---|
| 168 | // elements in the array.  Otherwise, arr cannot possibly be an array, | 
|---|
| 169 | // and we generate a compiler error to prevent the code from | 
|---|
| 170 | // compiling. | 
|---|
| 171 | // | 
|---|
| 172 | // Since the size of bool is implementation-defined, we need to cast | 
|---|
| 173 | // !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final | 
|---|
| 174 | // result has type size_t. | 
|---|
| 175 | // | 
|---|
| 176 | // This macro is not perfect as it wrongfully accepts certain | 
|---|
| 177 | // pointers, namely where the pointer size is divisible by the pointee | 
|---|
| 178 | // size.  Since all our code has to go through a 32-bit compiler, | 
|---|
| 179 | // where a pointer is 4 bytes, this means all pointers to a type whose | 
|---|
| 180 | // size is 3 or greater than 4 will be (righteously) rejected. | 
|---|
| 181 | // | 
|---|
| 182 | // Kudos to Jorg Brown for this simple and elegant implementation. | 
|---|
| 183 | // | 
|---|
| 184 | // - wan 2005-11-16 | 
|---|
| 185 | // | 
|---|
| 186 | // Starting with Visual C++ 2005, WinNT.h includes ARRAYSIZE. | 
|---|
| 187 | #if !defined(COMPILER_MSVC) || (defined(_MSC_VER) && _MSC_VER < 1400) | 
|---|
| 188 | #define ARRAYSIZE(a) \ | 
|---|
| 189 | ((sizeof(a) / sizeof(*(a))) / \ | 
|---|
| 190 | static_cast<size_t>(!(sizeof(a) % sizeof(*(a))))) | 
|---|
| 191 | #endif | 
|---|
| 192 |  | 
|---|
| 193 | // A macro to turn a symbol into a string | 
|---|
| 194 | #define AS_STRING(x)   AS_STRING_INTERNAL(x) | 
|---|
| 195 | #define AS_STRING_INTERNAL(x)   #x | 
|---|
| 196 |  | 
|---|
| 197 |  | 
|---|
| 198 | // One of the type traits, is_pod, makes it possible to query whether | 
|---|
| 199 | // a type is a POD type. It is impossible for type_traits.h to get | 
|---|
| 200 | // this right without compiler support, so it fails conservatively. It | 
|---|
| 201 | // knows that fundamental types and pointers are PODs, but it can't | 
|---|
| 202 | // tell whether user classes are PODs. The DECLARE_POD macro is used | 
|---|
| 203 | // to inform the type traits library that a user class is a POD. | 
|---|
| 204 | // | 
|---|
| 205 | // Implementation note: the typedef at the end is just to make it legal | 
|---|
| 206 | // to put a semicolon after DECLARE_POD(foo). | 
|---|
| 207 | // | 
|---|
| 208 | // The only reason this matters is that a few parts of the google3 | 
|---|
| 209 | // code base either require their template arguments to be PODs | 
|---|
| 210 | // (e.g. compact_vector) or are able to use a more efficient code path | 
|---|
| 211 | // when their template arguments are PODs (e.g. sparse_hash_map). You | 
|---|
| 212 | // should use DECLARE_POD if you have written a class that you intend | 
|---|
| 213 | // to use with one of those components, and if you know that your | 
|---|
| 214 | // class satisfies all of the conditions to be a POD type. | 
|---|
| 215 | // | 
|---|
| 216 | // So what's a POD?  The C++ standard (clause 9 paragraph 4) gives a | 
|---|
| 217 | // full definition, but a good rule of thumb is that a struct is a POD | 
|---|
| 218 | // ("plain old data") if it doesn't use any of the features that make | 
|---|
| 219 | // C++ different from C.  A POD struct can't have constructors, | 
|---|
| 220 | // destructors, assignment operators, base classes, private or | 
|---|
| 221 | // protected members, or virtual functions, and all of its member | 
|---|
| 222 | // variables must themselves be PODs. | 
|---|
| 223 |  | 
|---|
| 224 | #define DECLARE_POD(TypeName)                       \ | 
|---|
| 225 | namespace base {                                    \ | 
|---|
| 226 | template<> struct is_pod<TypeName> : true_type { }; \ | 
|---|
| 227 | }                                                   \ | 
|---|
| 228 | typedef int Dummy_Type_For_DECLARE_POD              \ | 
|---|
| 229 |  | 
|---|
| 230 | // We once needed a different technique to assert that a nested class | 
|---|
| 231 | // is a POD. This is no longer necessary, and DECLARE_NESTED_POD is | 
|---|
| 232 | // just a synonym for DECLARE_POD. We continue to provide | 
|---|
| 233 | // DECLARE_NESTED_POD only so we don't have to change client | 
|---|
| 234 | // code. Regardless of whether you use DECLARE_POD or | 
|---|
| 235 | // DECLARE_NESTED_POD: use it after the outer class. Using it within a | 
|---|
| 236 | // class definition will give a compiler error. | 
|---|
| 237 | #define DECLARE_NESTED_POD(TypeName) DECLARE_POD(TypeName) | 
|---|
| 238 |  | 
|---|
| 239 | // Declare that TemplateName<T> is a POD whenever T is | 
|---|
| 240 | #define PROPAGATE_POD_FROM_TEMPLATE_ARGUMENT(TemplateName)             \ | 
|---|
| 241 | namespace base {                                                       \ | 
|---|
| 242 | template <typename T> struct is_pod<TemplateName<T> > : is_pod<T> { }; \ | 
|---|
| 243 | }                                                                      \ | 
|---|
| 244 | typedef int Dummy_Type_For_PROPAGATE_POD_FROM_TEMPLATE_ARGUMENT | 
|---|
| 245 |  | 
|---|
| 246 | // Macro that does nothing if TypeName is a POD, and gives a compiler | 
|---|
| 247 | // error if TypeName is a non-POD.  You should put a descriptive | 
|---|
| 248 | // comment right next to the macro call so that people can tell what | 
|---|
| 249 | // the compiler error is about. | 
|---|
| 250 | // | 
|---|
| 251 | // Implementation note: this works by taking the size of a type that's | 
|---|
| 252 | // complete when TypeName is a POD and incomplete otherwise. | 
|---|
| 253 |  | 
|---|
| 254 | template <bool IsPod> struct ERROR_TYPE_MUST_BE_POD; | 
|---|
| 255 | template <> struct ERROR_TYPE_MUST_BE_POD<true> { }; | 
|---|
| 256 | #define ENFORCE_POD(TypeName)                                             \ | 
|---|
| 257 | enum { dummy_##TypeName                                                 \ | 
|---|
| 258 | = sizeof(ERROR_TYPE_MUST_BE_POD<                               \ | 
|---|
| 259 | base::is_pod<TypeName>::value>) } | 
|---|
| 260 |  | 
|---|
| 261 | #endif  // BASE_MACROS_H_ | 
|---|
| 262 |  | 
|---|