endian.hpp (1708B)
1 #ifndef UUID_A2E491BD_BA3E_4C92_B931_72017B89C33C 2 #define UUID_A2E491BD_BA3E_4C92_B931_72017B89C33C 3 #pragma once 4 5 #include <libshit/assert.hpp> 6 #include <libshit/lua/type_traits.hpp> 7 #include <libshit/meta.hpp> 8 #include <libshit/utils.hpp> 9 10 #include <boost/endian/conversion.hpp> 11 12 namespace Neptools 13 { 14 enum class LIBSHIT_LUAGEN() Endian 15 { 16 BIG, LITTLE, 17 }; 18 19 // not constexpr because gcc is retarded 20 inline boost::endian::order ToBoost(Endian e) noexcept 21 { 22 switch (e) 23 { 24 case Endian::BIG: return boost::endian::order::big; 25 case Endian::LITTLE: return boost::endian::order::little; 26 } 27 LIBSHIT_UNREACHABLE("Invalid Endian value"); 28 } 29 30 inline const char* ToString(Endian e) noexcept 31 { 32 switch (e) 33 { 34 case Endian::BIG: return "BIG"; 35 case Endian::LITTLE: return "LITTLE"; 36 } 37 LIBSHIT_UNREACHABLE("Invalid Endian value"); 38 } 39 40 template <typename T> 41 [[nodiscard]] inline T ToNativeCopy(T t, Endian e) noexcept 42 { 43 return boost::endian::conditional_reverse( 44 Libshit::Move(t), ToBoost(e), boost::endian::order::native); 45 } 46 47 template <typename T> 48 [[nodiscard]] inline T FromNativeCopy(T t, Endian e) noexcept 49 { 50 return boost::endian::conditional_reverse( 51 Libshit::Move(t), ToBoost(e), boost::endian::order::native); 52 } 53 54 template <typename T> 55 inline void ToNative(T& t, Endian e) noexcept 56 { 57 boost::endian::conditional_reverse_inplace( 58 t, ToBoost(e), boost::endian::order::native); 59 } 60 61 template <typename T> 62 inline void FromNative(T& t, Endian e) noexcept 63 { 64 boost::endian::conditional_reverse_inplace( 65 t, ToBoost(e), boost::endian::order::native); 66 } 67 68 } 69 70 LIBSHIT_ENUM(Neptools::Endian); 71 72 #endif