Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame^] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #ifndef ART_SRC_CASTS_H_ |
| 4 | #define ART_SRC_CASTS_H_ |
| 5 | |
| 6 | #include <string.h> |
| 7 | #include "src/macros.h" |
| 8 | |
| 9 | namespace android { |
| 10 | namespace runtime { |
| 11 | |
| 12 | template <class Dest, class Source> |
| 13 | inline Dest bit_cast(const Source& source) { |
| 14 | // Compile time assertion: sizeof(Dest) == sizeof(Source) |
| 15 | // A compile error here means your Dest and Source have different sizes. |
| 16 | COMPILE_ASSERT(sizeof(Dest) == sizeof(Source), verify_sizes_are_equal); |
| 17 | Dest dest; |
| 18 | memcpy(&dest, &source, sizeof(dest)); |
| 19 | return dest; |
| 20 | } |
| 21 | |
| 22 | } } // namespace android::runtime |
| 23 | |
| 24 | #endif // ART_SRC_CASTS_H_ |