| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // This file contains macros and macro-like constructs (e.g., templates) that | 5 // This file contains macros and macro-like constructs (e.g., templates) that |
| 6 // are commonly used throughout Chromium source. (It may also contain things | 6 // are commonly used throughout Chromium source. (It may also contain things |
| 7 // that are closely related to things that are commonly used that belong in this | 7 // that are closely related to things that are commonly used that belong in this |
| 8 // file.) | 8 // file.) |
| 9 | 9 |
| 10 #ifndef BASE_MACROS_H_ | 10 #ifndef BASE_MACROS_H_ |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 // | 146 // |
| 147 // or to make sure a struct is smaller than a certain size: | 147 // or to make sure a struct is smaller than a certain size: |
| 148 // | 148 // |
| 149 // COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large); | 149 // COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large); |
| 150 // | 150 // |
| 151 // The second argument to the macro is the name of the variable. If | 151 // The second argument to the macro is the name of the variable. If |
| 152 // the expression is false, most compilers will issue a warning/error | 152 // the expression is false, most compilers will issue a warning/error |
| 153 // containing the name of the variable. | 153 // containing the name of the variable. |
| 154 | 154 |
| 155 #undef COMPILE_ASSERT | 155 #undef COMPILE_ASSERT |
| 156 | |
| 157 #if __cplusplus >= 201103L | |
| 158 | |
| 159 // Under C++11, just use static_assert. | |
| 160 #define COMPILE_ASSERT(expr, msg) static_assert(expr, #msg) | 156 #define COMPILE_ASSERT(expr, msg) static_assert(expr, #msg) |
| 161 | 157 |
| 162 #else | |
| 163 | |
| 164 template <bool> | |
| 165 struct CompileAssert { | |
| 166 }; | |
| 167 | |
| 168 #define COMPILE_ASSERT(expr, msg) \ | |
| 169 typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] ALLOW_UNUSED | |
| 170 | |
| 171 // Implementation details of COMPILE_ASSERT: | |
| 172 // | |
| 173 // - COMPILE_ASSERT works by defining an array type that has -1 | |
| 174 // elements (and thus is invalid) when the expression is false. | |
| 175 // | |
| 176 // - The simpler definition | |
| 177 // | |
| 178 // #define COMPILE_ASSERT(expr, msg) typedef char msg[(expr) ? 1 : -1] | |
| 179 // | |
| 180 // does not work, as gcc supports variable-length arrays whose sizes | |
| 181 // are determined at run-time (this is gcc's extension and not part | |
| 182 // of the C++ standard). As a result, gcc fails to reject the | |
| 183 // following code with the simple definition: | |
| 184 // | |
| 185 // int foo; | |
| 186 // COMPILE_ASSERT(foo, msg); // not supposed to compile as foo is | |
| 187 // // not a compile-time constant. | |
| 188 // | |
| 189 // - By using the type CompileAssert<(bool(expr))>, we ensures that | |
| 190 // expr is a compile-time constant. (Template arguments must be | |
| 191 // determined at compile-time.) | |
| 192 // | |
| 193 // - The outer parentheses in CompileAssert<(bool(expr))> are necessary | |
| 194 // to work around a bug in gcc 3.4.4 and 4.0.1. If we had written | |
| 195 // | |
| 196 // CompileAssert<bool(expr)> | |
| 197 // | |
| 198 // instead, these compilers will refuse to compile | |
| 199 // | |
| 200 // COMPILE_ASSERT(5 > 0, some_message); | |
| 201 // | |
| 202 // (They seem to think the ">" in "5 > 0" marks the end of the | |
| 203 // template argument list.) | |
| 204 // | |
| 205 // - The array size is (bool(expr) ? 1 : -1), instead of simply | |
| 206 // | |
| 207 // ((expr) ? 1 : -1). | |
| 208 // | |
| 209 // This is to avoid running into a bug in MS VC 7.1, which | |
| 210 // causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1. | |
| 211 | |
| 212 #endif | |
| 213 | |
| 214 // bit_cast<Dest,Source> is a template function that implements the | 158 // bit_cast<Dest,Source> is a template function that implements the |
| 215 // equivalent of "*reinterpret_cast<Dest*>(&source)". We need this in | 159 // equivalent of "*reinterpret_cast<Dest*>(&source)". We need this in |
| 216 // very low-level functions like the protobuf library and fast math | 160 // very low-level functions like the protobuf library and fast math |
| 217 // support. | 161 // support. |
| 218 // | 162 // |
| 219 // float f = 3.14159265358979; | 163 // float f = 3.14159265358979; |
| 220 // int i = bit_cast<int32>(f); | 164 // int i = bit_cast<int32>(f); |
| 221 // // i = 0x40490fdb | 165 // // i = 0x40490fdb |
| 222 // | 166 // |
| 223 // The classical address-casting method is: | 167 // The classical address-casting method is: |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 | 248 |
| 305 // Use these to declare and define a static local variable (static T;) so that | 249 // Use these to declare and define a static local variable (static T;) so that |
| 306 // it is leaked so that its destructors are not called at exit. If you need | 250 // it is leaked so that its destructors are not called at exit. If you need |
| 307 // thread-safe initialization, use base/lazy_instance.h instead. | 251 // thread-safe initialization, use base/lazy_instance.h instead. |
| 308 #define CR_DEFINE_STATIC_LOCAL(type, name, arguments) \ | 252 #define CR_DEFINE_STATIC_LOCAL(type, name, arguments) \ |
| 309 static type& name = *new type arguments | 253 static type& name = *new type arguments |
| 310 | 254 |
| 311 } // base | 255 } // base |
| 312 | 256 |
| 313 #endif // BASE_MACROS_H_ | 257 #endif // BASE_MACROS_H_ |
| OLD | NEW |