18 NBT_Endian(
void) =
delete;
20 ~NBT_Endian(
void) =
delete;
27 return std::endian::native == std::endian::little;
34 return std::endian::native == std::endian::big;
44 requires std::integral<T>
48 static_assert(
sizeof(T) % 2 == 0 ||
sizeof(T) == 1,
"The size of T is not a multiple of 2 or equal to 1");
51 if constexpr (
sizeof(T) == 1)
57 using UT = std::make_unsigned_t<T>;
58 static_assert(
sizeof(UT) ==
sizeof(T),
"Unsigned type size mismatch");
61 constexpr size_t szSize =
sizeof(T);
62 constexpr size_t szHalf =
sizeof(T) / 2;
68 [&] <
size_t... i>(std::index_sequence<i...>) ->
void
70 ((tmp |= ((UT)data & ((UT)0xFF << (8 * i))) << 8 * (szSize - (i * 2) - 1)), ...);
71 }(std::make_index_sequence<szHalf>{});
74 [&] <
size_t... i>(std::index_sequence<i...>) ->
void
76 ((tmp |= ((UT)data & ((UT)0xFF << (8 * (i + szHalf)))) >> 8 * (i * 2 + 1)), ...);
77 }(std::make_index_sequence<szHalf>{});
90#if CJF2_NBT_CPP_COMPILER_MSVC
91 return _byteswap_ushort(data);
92#elif CJF2_NBT_CPP_COMPILER_GCC || CJF2_NBT_CPP_COMPILER_CLANG
93 return __builtin_bswap16(data);
106#if CJF2_NBT_CPP_COMPILER_MSVC
107 return _byteswap_ulong(data);
108#elif CJF2_NBT_CPP_COMPILER_GCC || CJF2_NBT_CPP_COMPILER_CLANG
109 return __builtin_bswap32(data);
122#if CJF2_NBT_CPP_COMPILER_MSVC
123 return _byteswap_uint64(data);
124#elif CJF2_NBT_CPP_COMPILER_GCC || CJF2_NBT_CPP_COMPILER_CLANG
125 return __builtin_bswap64(data);
137 requires std::integral<T>
142 if constexpr (
sizeof(T) ==
sizeof(uint8_t))
146 else if constexpr (
sizeof(T) ==
sizeof(uint16_t))
150 else if constexpr (
sizeof(T) ==
sizeof(uint32_t))
154 else if constexpr (
sizeof(T) ==
sizeof(uint64_t))
172 requires std::integral<T>
190 requires std::integral<T>
208 requires std::integral<T>
226 requires std::integral<T>
static T LittleToNativeAny(T data) noexcept
从小端字节序转换到当前平台字节序,自动匹配位数
定义 NBT_Endian.hpp:227
static uint32_t ByteSwap32(uint32_t data) noexcept
颠倒字节序32位特化版
定义 NBT_Endian.hpp:103
static T NativeToBigAny(T data) noexcept
从当前平台字节序转换到大端字节序,自动匹配位数
定义 NBT_Endian.hpp:173
static constexpr T ByteSwapAny(T data) noexcept
颠倒字节序,需要整数字节数为2的倍数或字节数为1
定义 NBT_Endian.hpp:45
static constexpr T AutoByteSwap(T data) noexcept
颠倒字节序,自动匹配位数
定义 NBT_Endian.hpp:138
static uint16_t ByteSwap16(uint16_t data) noexcept
颠倒字节序16位特化版
定义 NBT_Endian.hpp:87
static uint64_t ByteSwap64(uint64_t data) noexcept
颠倒字节序32位特化版
定义 NBT_Endian.hpp:119
static constexpr bool IsBigEndian(void) noexcept
判断当前平台字节序是否是大端字节序
定义 NBT_Endian.hpp:32
static T BigToNativeAny(T data) noexcept
从大端字节序转换到当前平台字节序,自动匹配位数
定义 NBT_Endian.hpp:209
static constexpr bool IsLittleEndian(void) noexcept
判断当前平台字节序是否是小端字节序
定义 NBT_Endian.hpp:25
static T NativeToLittleAny(T data) noexcept
从当前平台字节序转换到小端字节序,自动匹配位数
定义 NBT_Endian.hpp:191