|
| 1 | +#pragma once |
| 2 | + |
| 3 | +namespace REX |
| 4 | +{ |
| 5 | + class FUUID |
| 6 | + { |
| 7 | + public: |
| 8 | + inline constexpr FUUID() noexcept = default; |
| 9 | + inline constexpr FUUID(FUUID&&) noexcept = default; |
| 10 | + inline constexpr FUUID(const FUUID&) noexcept = default; |
| 11 | + |
| 12 | + inline constexpr FUUID(std::uint8_t a_b1, std::uint8_t a_b2, std::uint8_t a_b3, std::uint8_t a_b4, std::uint8_t a_b5, std::uint8_t a_b6, std::uint8_t a_b7, std::uint8_t a_b8, std::uint8_t a_b9, std::uint8_t a_b10, std::uint8_t a_b11, std::uint8_t a_b12, std::uint8_t a_b13, std::uint8_t a_b14, std::uint8_t a_b15, std::uint8_t a_b16) noexcept : |
| 13 | + m_data{ a_b1, a_b2, a_b3, a_b4, a_b5, a_b6, a_b7, a_b8, a_b9, a_b10, a_b11, a_b12, a_b13, a_b14, a_b15, a_b16 } |
| 14 | + {} |
| 15 | + |
| 16 | + template <std::size_t N> |
| 17 | + requires(N == 37) |
| 18 | + inline constexpr FUUID(const char (&a_string)[N]) noexcept |
| 19 | + { |
| 20 | + ParseString(a_string); |
| 21 | + } |
| 22 | + |
| 23 | + inline constexpr FUUID& operator=(FUUID&&) noexcept = default; |
| 24 | + inline constexpr FUUID& operator=(const FUUID&) noexcept = default; |
| 25 | + |
| 26 | + inline constexpr std::uint8_t& operator[](std::size_t a_pos) noexcept |
| 27 | + { |
| 28 | + return m_data[a_pos]; |
| 29 | + } |
| 30 | + |
| 31 | + inline constexpr const std::uint8_t operator[](std::size_t a_pos) const noexcept |
| 32 | + { |
| 33 | + return m_data[a_pos]; |
| 34 | + } |
| 35 | + |
| 36 | + inline constexpr bool operator==(const FUUID& a_rhs) const noexcept |
| 37 | + { |
| 38 | + for (auto i = 0u; i < sizeof(m_data); i++) { |
| 39 | + if (m_data[i] != a_rhs.m_data[i]) { |
| 40 | + return false; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + return true; |
| 45 | + } |
| 46 | + |
| 47 | + inline constexpr explicit operator bool() const noexcept |
| 48 | + { |
| 49 | + for (auto i = 0u; i < sizeof(m_data); i++) { |
| 50 | + if (m_data[i]) { |
| 51 | + return true; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return false; |
| 56 | + } |
| 57 | + |
| 58 | + private: |
| 59 | + inline static constexpr std::uint8_t ParseStringHexChar(const char a_char) |
| 60 | + { |
| 61 | + const char c = a_char | 0x20; |
| 62 | + if (c >= '0' && c <= '9') |
| 63 | + return c - '0'; |
| 64 | + else if (c >= 'a' && c <= 'f') |
| 65 | + return c - 'a' + 10; |
| 66 | + else |
| 67 | + throw "invalid hexadecimal character in FUUID"; |
| 68 | + } |
| 69 | + |
| 70 | + inline static constexpr std::uint8_t ParseStringHex(const char* a_str, const std::size_t a_pos) |
| 71 | + { |
| 72 | + return (ParseStringHexChar(a_str[a_pos]) << 4) + ParseStringHexChar(a_str[a_pos + 1]); |
| 73 | + } |
| 74 | + |
| 75 | + inline constexpr void ParseString(const char* a_str) |
| 76 | + { |
| 77 | + //constexpr std::size_t parse_string_table_le[16]{ |
| 78 | + // 6, 4, 2, 0, 11, 9, 16, 14, 19, 21, 24, 26, 28, 30, 32, 34 |
| 79 | + //}; |
| 80 | + |
| 81 | + constexpr std::size_t parse_string_table_be[16]{ |
| 82 | + 0, 2, 4, 6, 9, 11, 14, 16, 19, 21, 24, 26, 28, 30, 32, 34 |
| 83 | + }; |
| 84 | + |
| 85 | + for (auto i = 0u; i < sizeof(m_data); i++) { |
| 86 | + m_data[i] = ParseStringHex(a_str, parse_string_table_be[i]); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private: |
| 91 | + std::uint8_t m_data[16]; |
| 92 | + }; |
| 93 | + static_assert(sizeof(FUUID) == 16); |
| 94 | + static_assert(FUUID{ "62470f2d-f92b-4189-8af6-157310dadc8b" } == FUUID{ 0x62, 0x47, 0x0f, 0x2d, 0xf9, 0x2b, 0x41, 0x89, 0x8a, 0xf6, 0x15, 0x73, 0x10, 0xda, 0xdc, 0x8b }); |
| 95 | +} |
0 commit comments