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 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 9 | namespace art { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 10 | |
| 11 | template <class Dest, class Source> |
| 12 | inline Dest bit_cast(const Source& source) { |
| 13 | // Compile time assertion: sizeof(Dest) == sizeof(Source) |
| 14 | // A compile error here means your Dest and Source have different sizes. |
| 15 | COMPILE_ASSERT(sizeof(Dest) == sizeof(Source), verify_sizes_are_equal); |
| 16 | Dest dest; |
| 17 | memcpy(&dest, &source, sizeof(dest)); |
| 18 | return dest; |
| 19 | } |
| 20 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 21 | } // namespace art |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 22 | |
| 23 | #endif // ART_SRC_CASTS_H_ |