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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 DISALLOW_COPY_AND_ASSIGN(TypeName) | 45 DISALLOW_COPY_AND_ASSIGN(TypeName) |
46 | 46 |
47 // The arraysize(arr) macro returns the # of elements in an array arr. | 47 // The arraysize(arr) macro returns the # of elements in an array arr. |
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> | 55 template <typename T, size_t N> char (&ArraySizeHelper(T (&array)[N]))[N]; |
56 char (&ArraySizeHelper(T (&array)[N]))[N]; | 56 #define arraysize(array) (sizeof(ArraySizeHelper(array))) |
57 | |
58 // That gcc wants both of these prototypes seems mysterious. VC, for | |
59 // its part, can't decide which to use (another mystery). Matching of | |
60 // template overloads: the final frontier. | |
61 #ifndef _MSC_VER | |
62 template <typename T, size_t N> | |
63 char (&ArraySizeHelper(const T (&array)[N]))[N]; | |
64 #endif | |
65 | |
66 #define arraysize(array) (sizeof(::ArraySizeHelper(array))) | |
67 | 57 |
68 | 58 |
69 // Use implicit_cast as a safe version of static_cast or const_cast | 59 // Use implicit_cast as a safe version of static_cast or const_cast |
70 // for upcasting in the type hierarchy (i.e. casting a pointer to Foo | 60 // for upcasting in the type hierarchy (i.e. casting a pointer to Foo |
71 // to a pointer to SuperclassOfFoo or casting a pointer to Foo to | 61 // to a pointer to SuperclassOfFoo or casting a pointer to Foo to |
72 // a const pointer to Foo). | 62 // a const pointer to Foo). |
73 // When you use implicit_cast, the compiler checks that the cast is safe. | 63 // When you use implicit_cast, the compiler checks that the cast is safe. |
74 // Such explicit implicit_casts are necessary in surprisingly many | 64 // Such explicit implicit_casts are necessary in surprisingly many |
75 // situations where C++ demands an exact type match instead of an | 65 // situations where C++ demands an exact type match instead of an |
76 // argument type convertible to a target type. | 66 // argument type convertible to a target type. |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 | 189 |
200 // Use these to declare and define a static local variable (static T;) so that | 190 // Use these to declare and define a static local variable (static T;) so that |
201 // it is leaked so that its destructors are not called at exit. If you need | 191 // it is leaked so that its destructors are not called at exit. If you need |
202 // thread-safe initialization, use base/lazy_instance.h instead. | 192 // thread-safe initialization, use base/lazy_instance.h instead. |
203 #define CR_DEFINE_STATIC_LOCAL(type, name, arguments) \ | 193 #define CR_DEFINE_STATIC_LOCAL(type, name, arguments) \ |
204 static type& name = *new type arguments | 194 static type& name = *new type arguments |
205 | 195 |
206 } // base | 196 } // base |
207 | 197 |
208 #endif // BASE_MACROS_H_ | 198 #endif // BASE_MACROS_H_ |
OLD | NEW |