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 | |
59 // Use implicit_cast as a safe version of static_cast or const_cast | |
60 // for upcasting in the type hierarchy (i.e. casting a pointer to Foo | |
61 // to a pointer to SuperclassOfFoo or casting a pointer to Foo to | |
62 // a const pointer to Foo). | |
63 // When you use implicit_cast, the compiler checks that the cast is safe. | |
64 // Such explicit implicit_casts are necessary in surprisingly many | |
65 // situations where C++ demands an exact type match instead of an | |
66 // argument type convertible to a target type. | |
67 // | |
68 // The From type can be inferred, so the preferred syntax for using | |
69 // implicit_cast is the same as for static_cast etc.: | |
70 // | |
71 // implicit_cast<ToType>(expr) | |
72 // | |
73 // implicit_cast would have been part of the C++ standard library, | |
74 // but the proposal was submitted too late. It will probably make | |
75 // its way into the language in the future. | |
76 template<typename To, typename From> | |
77 inline To implicit_cast(From const &f) { | |
78 return f; | |
79 } | |
80 | |
81 // The COMPILE_ASSERT macro can be used to verify that a compile time | 58 // The COMPILE_ASSERT macro can be used to verify that a compile time |
82 // expression is true. For example, you could use it to verify the | 59 // expression is true. For example, you could use it to verify the |
83 // size of a static array: | 60 // size of a static array: |
84 // | 61 // |
85 // COMPILE_ASSERT(arraysize(content_type_names) == CONTENT_NUM_TYPES, | 62 // COMPILE_ASSERT(arraysize(content_type_names) == CONTENT_NUM_TYPES, |
86 // content_type_names_incorrect_size); | 63 // content_type_names_incorrect_size); |
87 // | 64 // |
88 // or to make sure a struct is smaller than a certain size: | 65 // or to make sure a struct is smaller than a certain size: |
89 // | 66 // |
90 // COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large); | 67 // COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large); |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 | 166 |
190 // Use these to declare and define a static local variable (static T;) so that | 167 // Use these to declare and define a static local variable (static T;) so that |
191 // it is leaked so that its destructors are not called at exit. If you need | 168 // it is leaked so that its destructors are not called at exit. If you need |
192 // thread-safe initialization, use base/lazy_instance.h instead. | 169 // thread-safe initialization, use base/lazy_instance.h instead. |
193 #define CR_DEFINE_STATIC_LOCAL(type, name, arguments) \ | 170 #define CR_DEFINE_STATIC_LOCAL(type, name, arguments) \ |
194 static type& name = *new type arguments | 171 static type& name = *new type arguments |
195 | 172 |
196 } // base | 173 } // base |
197 | 174 |
198 #endif // BASE_MACROS_H_ | 175 #endif // BASE_MACROS_H_ |
OLD | NEW |