annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
364
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
1 #ifndef MINORI_CORE_BIT_CAST_H_
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
2 #define MINORI_CORE_BIT_CAST_H_
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
3
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
4 /* XXX need to move more "core" stuff into the minori namespace */
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
5
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
6 #include <type_traits>
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
7 #include <memory>
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
8 #include <cstring>
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
9
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
10 namespace minori {
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
11
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
12 /* C++17 doesn't have this unfortunately */
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
13 template<typename To, class From>
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
14 To BitCast(From from) {
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
15 static_assert(sizeof(From) == sizeof(To), "Types must match sizes");
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
16 static_assert(std::is_pod<From>::value, "Requires POD input");
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
17 static_assert(std::is_pod<To>::value, "Requires POD output");
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
18
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
19 To to;
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
20 std::memcpy(std::addressof(to), std::addressof(from), sizeof(from));
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
21 return to;
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
22 }
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
23
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
24 }
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
25
99c961c91809 core: refactor out byte stream into its own file
Paper <paper@paper.us.eu.org>
parents:
diff changeset
26 #endif /* MINORI_CORE_BIT_CAST_H_ */