|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <concepts> |
| 4 | +#include <array> |
| 5 | +#include <bit> |
| 6 | +#include <type_traits> |
| 7 | +#include <cstddef> |
| 8 | + |
| 9 | +namespace ecsact::entt::detail { |
| 10 | + |
| 11 | +template<typename T> |
| 12 | + requires(std::integral<T> || std::floating_point<T>) |
| 13 | +constexpr auto bytes_sizeof() -> int { |
| 14 | + using value_type = std::remove_cvref_t<T>; |
| 15 | + |
| 16 | + if constexpr(std::is_same_v<value_type, bool>) { |
| 17 | + return 1; |
| 18 | + } else { |
| 19 | + return sizeof(value_type); |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +template<typename T> |
| 24 | +struct unsigned_bit_size_equivalent; |
| 25 | + |
| 26 | +template<typename T> |
| 27 | + requires(bytes_sizeof<T>() == 1) |
| 28 | +struct unsigned_bit_size_equivalent<T> : std::type_identity<uint8_t> {}; |
| 29 | + |
| 30 | +template<typename T> |
| 31 | + requires(bytes_sizeof<T>() == 2) |
| 32 | +struct unsigned_bit_size_equivalent<T> : std::type_identity<uint16_t> {}; |
| 33 | + |
| 34 | +template<typename T> |
| 35 | + requires(bytes_sizeof<T>() == 4) |
| 36 | +struct unsigned_bit_size_equivalent<T> : std::type_identity<uint32_t> {}; |
| 37 | + |
| 38 | +template<typename T> |
| 39 | + requires(bytes_sizeof<T>() == 8) |
| 40 | +struct unsigned_bit_size_equivalent<T> : std::type_identity<uint64_t> {}; |
| 41 | + |
| 42 | +template<typename T> |
| 43 | +using unsigned_bit_size_equivalent_t = unsigned_bit_size_equivalent<T>::type; |
| 44 | + |
| 45 | +template<typename T> |
| 46 | + requires(std::integral<T> || std::floating_point<T>) |
| 47 | +auto bytes_copy_into( // |
| 48 | + T v, |
| 49 | + auto& out_bytes, |
| 50 | + auto& out_bytes_offset = 0 |
| 51 | +) -> void { |
| 52 | + using value_type = std::remove_cvref_t<T>; |
| 53 | + constexpr auto value_size = bytes_sizeof<T>(); |
| 54 | + |
| 55 | + auto v_bits = std::bit_cast<unsigned_bit_size_equivalent_t<T>>(v); |
| 56 | + |
| 57 | + if constexpr(std::is_same_v<value_type, bool>) { |
| 58 | + out_bytes[out_bytes_offset++] = static_cast<std::byte>(v_bits ? 1 : 0); |
| 59 | + } else if constexpr(value_size == 1) { |
| 60 | + out_bytes[out_bytes_offset++] = static_cast<std::byte>(v_bits); |
| 61 | + } else if constexpr(value_size == 2) { |
| 62 | + out_bytes[out_bytes_offset++] = |
| 63 | + static_cast<std::byte>((v_bits >> 8) & 0xFF); |
| 64 | + out_bytes[out_bytes_offset++] = static_cast<std::byte>(v_bits & 0xFF); |
| 65 | + } else if constexpr(value_size == 4) { |
| 66 | + out_bytes[out_bytes_offset++] = |
| 67 | + static_cast<std::byte>((v_bits >> 24) & 0xFF); |
| 68 | + out_bytes[out_bytes_offset++] = |
| 69 | + static_cast<std::byte>((v_bits >> 16) & 0xFF); |
| 70 | + out_bytes[out_bytes_offset++] = |
| 71 | + static_cast<std::byte>((v_bits >> 8) & 0xFF); |
| 72 | + out_bytes[out_bytes_offset++] = static_cast<std::byte>(v_bits & 0xFF); |
| 73 | + } else if constexpr(value_size == 8) { |
| 74 | + out_bytes[out_bytes_offset++] = |
| 75 | + static_cast<std::byte>((v_bits >> 56) & 0xFF); |
| 76 | + out_bytes[out_bytes_offset++] = |
| 77 | + static_cast<std::byte>((v_bits >> 48) & 0xFF); |
| 78 | + out_bytes[out_bytes_offset++] = |
| 79 | + static_cast<std::byte>((v_bits >> 40) & 0xFF); |
| 80 | + out_bytes[out_bytes_offset++] = |
| 81 | + static_cast<std::byte>((v_bits >> 32) & 0xFF); |
| 82 | + out_bytes[out_bytes_offset++] = |
| 83 | + static_cast<std::byte>((v_bits >> 24) & 0xFF); |
| 84 | + out_bytes[out_bytes_offset++] = |
| 85 | + static_cast<std::byte>((v_bits >> 16) & 0xFF); |
| 86 | + out_bytes[out_bytes_offset++] = |
| 87 | + static_cast<std::byte>((v_bits >> 8) & 0xFF); |
| 88 | + out_bytes[out_bytes_offset++] = static_cast<std::byte>(v_bits & 0xFF); |
| 89 | + } |
| 90 | + |
| 91 | + static_assert(value_size <= 8); |
| 92 | +} |
| 93 | + |
| 94 | +template<typename... T> |
| 95 | +auto bytes_copy( // |
| 96 | + T... values |
| 97 | +) -> std::array<std::byte, (0 + ... + bytes_sizeof<T>())> { |
| 98 | + auto result = std::array<std::byte, (0 + ... + bytes_sizeof<T>())>{}; |
| 99 | + auto offset = 0; |
| 100 | + (bytes_copy_into(values, result, offset), ...); |
| 101 | + return result; |
| 102 | +} |
| 103 | + |
| 104 | +} // namespace ecsact::entt::detail |
0 commit comments