Mercurial > minori
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/core/bit_cast.h Tue Jul 16 21:15:59 2024 -0400 @@ -0,0 +1,26 @@ +#ifndef MINORI_CORE_BIT_CAST_H_ +#define MINORI_CORE_BIT_CAST_H_ + +/* XXX need to move more "core" stuff into the minori namespace */ + +#include <type_traits> +#include <memory> +#include <cstring> + +namespace minori { + +/* C++17 doesn't have this unfortunately */ +template<typename To, class From> +To BitCast(From from) { + static_assert(sizeof(From) == sizeof(To), "Types must match sizes"); + static_assert(std::is_pod<From>::value, "Requires POD input"); + static_assert(std::is_pod<To>::value, "Requires POD output"); + + To to; + std::memcpy(std::addressof(to), std::addressof(from), sizeof(from)); + return to; +} + +} + +#endif /* MINORI_CORE_BIT_CAST_H_ */