Mercurial > minori
comparison include/core/bit_cast.h @ 364:99c961c91809
core: refactor out byte stream into its own file
easy dubs
| author | Paper <paper@paper.us.eu.org> |
|---|---|
| date | Tue, 16 Jul 2024 21:15:59 -0400 |
| parents | |
| children | 47c9f8502269 |
comparison
equal
deleted
inserted
replaced
| 363:f10507d8f686 | 364:99c961c91809 |
|---|---|
| 1 #ifndef MINORI_CORE_BIT_CAST_H_ | |
| 2 #define MINORI_CORE_BIT_CAST_H_ | |
| 3 | |
| 4 /* XXX need to move more "core" stuff into the minori namespace */ | |
| 5 | |
| 6 #include <type_traits> | |
| 7 #include <memory> | |
| 8 #include <cstring> | |
| 9 | |
| 10 namespace minori { | |
| 11 | |
| 12 /* C++17 doesn't have this unfortunately */ | |
| 13 template<typename To, class From> | |
| 14 To BitCast(From from) { | |
| 15 static_assert(sizeof(From) == sizeof(To), "Types must match sizes"); | |
| 16 static_assert(std::is_pod<From>::value, "Requires POD input"); | |
| 17 static_assert(std::is_pod<To>::value, "Requires POD output"); | |
| 18 | |
| 19 To to; | |
| 20 std::memcpy(std::addressof(to), std::addressof(from), sizeof(from)); | |
| 21 return to; | |
| 22 } | |
| 23 | |
| 24 } | |
| 25 | |
| 26 #endif /* MINORI_CORE_BIT_CAST_H_ */ |
