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 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 // memcpy(d,s,4) compiles to one load and one store, and memcpy(d,s,8) | 122 // memcpy(d,s,4) compiles to one load and one store, and memcpy(d,s,8) |
123 // compiles to two loads and two stores. | 123 // compiles to two loads and two stores. |
124 // | 124 // |
125 // I tested this code with gcc 2.95.3, gcc 4.0.1, icc 8.1, and msvc 7.1. | 125 // I tested this code with gcc 2.95.3, gcc 4.0.1, icc 8.1, and msvc 7.1. |
126 // | 126 // |
127 // WARNING: if Dest or Source is a non-POD type, the result of the memcpy | 127 // WARNING: if Dest or Source is a non-POD type, the result of the memcpy |
128 // is likely to surprise you. | 128 // is likely to surprise you. |
129 | 129 |
130 template <class Dest, class Source> | 130 template <class Dest, class Source> |
131 inline Dest bit_cast(const Source& source) { | 131 inline Dest bit_cast(const Source& source) { |
132 COMPILE_ASSERT(sizeof(Dest) == sizeof(Source), VerifySizesAreEqual); | 132 static_assert(sizeof(Dest) == sizeof(Source), |
| 133 "bit_cast requires source and destination to be the same size"); |
133 | 134 |
134 Dest dest; | 135 Dest dest; |
135 memcpy(&dest, &source, sizeof(dest)); | 136 memcpy(&dest, &source, sizeof(dest)); |
136 return dest; | 137 return dest; |
137 } | 138 } |
138 | 139 |
139 // Used to explicitly mark the return value of a function as unused. If you are | 140 // Used to explicitly mark the return value of a function as unused. If you are |
140 // really sure you don't want to do anything with the return value of a function | 141 // really sure you don't want to do anything with the return value of a function |
141 // that has been marked WARN_UNUSED_RESULT, wrap it with this. Example: | 142 // that has been marked WARN_UNUSED_RESULT, wrap it with this. Example: |
142 // | 143 // |
(...skipping 23 matching lines...) Expand all Loading... |
166 | 167 |
167 // Use these to declare and define a static local variable (static T;) so that | 168 // Use these to declare and define a static local variable (static T;) so that |
168 // it is leaked so that its destructors are not called at exit. If you need | 169 // it is leaked so that its destructors are not called at exit. If you need |
169 // thread-safe initialization, use base/lazy_instance.h instead. | 170 // thread-safe initialization, use base/lazy_instance.h instead. |
170 #define CR_DEFINE_STATIC_LOCAL(type, name, arguments) \ | 171 #define CR_DEFINE_STATIC_LOCAL(type, name, arguments) \ |
171 static type& name = *new type arguments | 172 static type& name = *new type arguments |
172 | 173 |
173 } // base | 174 } // base |
174 | 175 |
175 #endif // BASE_MACROS_H_ | 176 #endif // BASE_MACROS_H_ |
OLD | NEW |