OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #ifndef BASE_BASICTYPES_H_ | 5 #ifndef BASE_BASICTYPES_H_ |
6 #define BASE_BASICTYPES_H_ | 6 #define BASE_BASICTYPES_H_ |
7 | 7 |
8 #include <limits.h> // So we can set the bounds of our types | 8 #include <limits.h> // So we can set the bounds of our types |
9 #include <stddef.h> // For size_t | 9 #include <stddef.h> // For size_t |
10 #include <string.h> // for memcpy | 10 #include <string.h> // for memcpy |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 // | 266 // |
267 // I tested this code with gcc 2.95.3, gcc 4.0.1, icc 8.1, and msvc 7.1. | 267 // I tested this code with gcc 2.95.3, gcc 4.0.1, icc 8.1, and msvc 7.1. |
268 // | 268 // |
269 // WARNING: if Dest or Source is a non-POD type, the result of the memcpy | 269 // WARNING: if Dest or Source is a non-POD type, the result of the memcpy |
270 // is likely to surprise you. | 270 // is likely to surprise you. |
271 | 271 |
272 template <class Dest, class Source> | 272 template <class Dest, class Source> |
273 inline Dest bit_cast(const Source& source) { | 273 inline Dest bit_cast(const Source& source) { |
274 // Compile time assertion: sizeof(Dest) == sizeof(Source) | 274 // Compile time assertion: sizeof(Dest) == sizeof(Source) |
275 // A compile error here means your Dest and Source have different sizes. | 275 // A compile error here means your Dest and Source have different sizes. |
276 typedef char VerifySizesAreEqual [sizeof(Dest) == sizeof(Source) ? 1 : -1]; | 276 // typedef char VerifySizesAreEqual [sizeof(Dest) == sizeof(Source) ? 1 : -1]; |
277 | 277 |
278 Dest dest; | 278 Dest dest; |
279 memcpy(&dest, &source, sizeof(dest)); | 279 memcpy(&dest, &source, sizeof(dest)); |
280 return dest; | 280 return dest; |
281 } | 281 } |
282 | 282 |
283 // The following enum should be used only as a constructor argument to indicate | 283 // The following enum should be used only as a constructor argument to indicate |
284 // that the variable has static storage class, and that the constructor should | 284 // that the variable has static storage class, and that the constructor should |
285 // do nothing to its state. It indicates to the reader that it is legal to | 285 // do nothing to its state. It indicates to the reader that it is legal to |
286 // declare a static instance of the class, provided the constructor is given | 286 // declare a static instance of the class, provided the constructor is given |
(...skipping 23 matching lines...) Expand all Loading... |
310 // If target architecture does not support unaligned loads and stores, | 310 // If target architecture does not support unaligned loads and stores, |
311 // use memcpy version of UNALIGNED_LOAD32. | 311 // use memcpy version of UNALIGNED_LOAD32. |
312 inline uint32 UnalignedLoad32(const void* p) { | 312 inline uint32 UnalignedLoad32(const void* p) { |
313 uint32 t; | 313 uint32 t; |
314 memcpy(&t, reinterpret_cast<const uint8*>(p), sizeof(t)); | 314 memcpy(&t, reinterpret_cast<const uint8*>(p), sizeof(t)); |
315 return t; | 315 return t; |
316 } | 316 } |
317 | 317 |
318 #endif | 318 #endif |
319 #endif // BASE_BASICTYPES_H_ | 319 #endif // BASE_BASICTYPES_H_ |
OLD | NEW |