blob: 15852cffcf5341f698cf1174aeba4fd688d08e45 [file] [log] [blame]
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -07001// 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 Shapiro6b6b5f02011-06-21 15:05:09 -07009namespace art {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070010
11template <class Dest, class Source>
12inline 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 Shapiro6b6b5f02011-06-21 15:05:09 -070021} // namespace art
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070022
23#endif // ART_SRC_CASTS_H_