| 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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 // The expression is a compile-time constant, and therefore can be | 48 // The expression is a compile-time constant, and therefore can be |
| 49 // used in defining new arrays, for example. If you use arraysize on | 49 // used in defining new arrays, for example. If you use arraysize on |
| 50 // a pointer by mistake, you will get a compile-time error. | 50 // a pointer by mistake, you will get a compile-time error. |
| 51 | 51 |
| 52 // This template function declaration is used in defining arraysize. | 52 // This template function declaration is used in defining arraysize. |
| 53 // Note that the function doesn't need an implementation, as we only | 53 // Note that the function doesn't need an implementation, as we only |
| 54 // use its type. | 54 // use its type. |
| 55 template <typename T, size_t N> char (&ArraySizeHelper(T (&array)[N]))[N]; | 55 template <typename T, size_t N> char (&ArraySizeHelper(T (&array)[N]))[N]; |
| 56 #define arraysize(array) (sizeof(ArraySizeHelper(array))) | 56 #define arraysize(array) (sizeof(ArraySizeHelper(array))) |
| 57 | 57 |
| 58 // The COMPILE_ASSERT macro can be used to verify that a compile time | |
| 59 // expression is true. For example, you could use it to verify the | |
| 60 // size of a static array: | |
| 61 // | |
| 62 // COMPILE_ASSERT(arraysize(content_type_names) == CONTENT_NUM_TYPES, | |
| 63 // content_type_names_incorrect_size); | |
| 64 // | |
| 65 // or to make sure a struct is smaller than a certain size: | |
| 66 // | |
| 67 // COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large); | |
| 68 // | |
| 69 // The second argument to the macro is the name of the variable. If | |
| 70 // the expression is false, most compilers will issue a warning/error | |
| 71 // containing the name of the variable. | |
| 72 | |
| 73 #undef COMPILE_ASSERT | |
| 74 #define COMPILE_ASSERT(expr, msg) static_assert(expr, #msg) | |
| 75 | |
| 76 // bit_cast<Dest,Source> is a template function that implements the | 58 // bit_cast<Dest,Source> is a template function that implements the |
| 77 // equivalent of "*reinterpret_cast<Dest*>(&source)". We need this in | 59 // equivalent of "*reinterpret_cast<Dest*>(&source)". We need this in |
| 78 // very low-level functions like the protobuf library and fast math | 60 // very low-level functions like the protobuf library and fast math |
| 79 // support. | 61 // support. |
| 80 // | 62 // |
| 81 // float f = 3.14159265358979; | 63 // float f = 3.14159265358979; |
| 82 // int i = bit_cast<int32>(f); | 64 // int i = bit_cast<int32>(f); |
| 83 // // i = 0x40490fdb | 65 // // i = 0x40490fdb |
| 84 // | 66 // |
| 85 // The classical address-casting method is: | 67 // The classical address-casting method is: |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 | 149 |
| 168 // Use these to declare and define a static local variable (static T;) so that | 150 // Use these to declare and define a static local variable (static T;) so that |
| 169 // it is leaked so that its destructors are not called at exit. If you need | 151 // it is leaked so that its destructors are not called at exit. If you need |
| 170 // thread-safe initialization, use base/lazy_instance.h instead. | 152 // thread-safe initialization, use base/lazy_instance.h instead. |
| 171 #define CR_DEFINE_STATIC_LOCAL(type, name, arguments) \ | 153 #define CR_DEFINE_STATIC_LOCAL(type, name, arguments) \ |
| 172 static type& name = *new type arguments | 154 static type& name = *new type arguments |
| 173 | 155 |
| 174 } // base | 156 } // base |
| 175 | 157 |
| 176 #endif // BASE_MACROS_H_ | 158 #endif // BASE_MACROS_H_ |
| OLD | NEW |