comparison include/core/bit_cast.h @ 369:47c9f8502269

*: clang-format all the things I've edited the formatting a bit. Now pointer asterisks (and reference ampersands) are on the variable instead of the type, as well as having newlines for function braces (but nothing else)
author Paper <paper@tflc.us>
date Fri, 25 Jul 2025 10:16:02 -0400
parents 99c961c91809
children
comparison
equal deleted inserted replaced
368:6d37a998cf91 369:47c9f8502269
1 #ifndef MINORI_CORE_BIT_CAST_H_ 1 #ifndef MINORI_CORE_BIT_CAST_H_
2 #define MINORI_CORE_BIT_CAST_H_ 2 #define MINORI_CORE_BIT_CAST_H_
3 3
4 /* XXX need to move more "core" stuff into the minori namespace */ 4 /* XXX need to move more "core" stuff into the minori namespace */
5 5
6 #include <cstring>
7 #include <memory>
6 #include <type_traits> 8 #include <type_traits>
7 #include <memory>
8 #include <cstring>
9 9
10 namespace minori { 10 namespace minori {
11 11
12 /* C++17 doesn't have this unfortunately */ 12 /* C++17 doesn't have this unfortunately */
13 template<typename To, class From> 13 template<typename To, class From>
14 To BitCast(From from) { 14 To BitCast(From from)
15 {
15 static_assert(sizeof(From) == sizeof(To), "Types must match sizes"); 16 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<From>::value, "Requires POD input");
17 static_assert(std::is_pod<To>::value, "Requires POD output"); 18 static_assert(std::is_pod<To>::value, "Requires POD output");
18 19
19 To to; 20 To to;
20 std::memcpy(std::addressof(to), std::addressof(from), sizeof(from)); 21 std::memcpy(std::addressof(to), std::addressof(from), sizeof(from));
21 return to; 22 return to;
22 } 23 }
23 24
24 } 25 } // namespace minori
25 26
26 #endif /* MINORI_CORE_BIT_CAST_H_ */ 27 #endif /* MINORI_CORE_BIT_CAST_H_ */