1#ifndef NEFORCE_CORE_MEMORY_ENDIAN_HPP__
2#define NEFORCE_CORE_MEMORY_ENDIAN_HPP__
18NEFORCE_BEGIN_NAMESPACE__
39#ifdef NEFORCE_PLATFORM_WINDOWS
41#elif defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__)
42 __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__;
43#elif defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN)
44 __BYTE_ORDER == __LITTLE_ENDIAN;
47# warning "Unsupported constexpr endian type"
60 return *
reinterpret_cast<const byte_t*
>(&test) == 0x01;
76 return ((value >> 24) & 0x000000FF) | ((value >> 8) & 0x0000FF00) | ((value << 8) & 0x00FF0000) |
77 ((value << 24) & 0xFF000000);
86 return ((value >> 56) & 0x00000000000000FF) | ((value >> 40) & 0x000000000000FF00) |
87 ((value >> 24) & 0x0000000000FF0000) | ((value >> 8) & 0x00000000FF000000) |
88 ((value << 8) & 0x000000FF00000000) | ((value << 24) & 0x0000FF0000000000) |
89 ((value << 40) & 0x00FF000000000000) | ((value << 56) & 0xFF00000000000000);
99 static constexpr enable_if_t<
sizeof(T) == 2, T> host_to_network_impl2(T value)
noexcept {
103 template <
typename T>
104 static constexpr enable_if_t<
sizeof(T) == 4, T> host_to_network_impl2(T value)
noexcept {
108 template <
typename T>
109 static constexpr enable_if_t<
sizeof(T) == 8, T> host_to_network_impl2(T value)
noexcept {
113 template <
typename T>
114 static constexpr enable_if_t<
sizeof(T) != 2 &&
sizeof(T) != 4 &&
sizeof(T) != 8, T>
115 host_to_network_impl2(T value)
noexcept {
116 static_assert(
sizeof(T) == 0,
"Unsupported type size for endian swap");
120 template <
typename T>
122 return endian::host_to_network_impl2(value);
125 template <
typename T>
130 template <
typename T>
132 return endian::host_to_network_impl2(value);
145 template <
typename T>
148 return endian::host_to_network_impl1(value);
159 template <
typename T>
173 template <
typename T>
176 return endian::host_to_network_impl3(value);
187 template <
typename T>
201 template <
typename T>
204 return endian::host_to_network_impl1(value);
215 template <
typename T>
229 template <
typename T>
232 return endian::host_to_network_impl2(value);
303 dest[0] =
static_cast<byte_t>(value & 0xFF);
304 dest[1] =
static_cast<byte_t>((value >> 8) & 0xFF);
313 for (
int i = 0; i < 4; ++i) {
314 dest[i] =
static_cast<byte_t>((value >> (i * 8)) & 0xFF);
324 for (
int i = 0; i < 8; ++i) {
325 dest[i] =
static_cast<byte_t>((value >> (i * 8)) & 0xFF);
335 dest[0] =
static_cast<byte_t>((value >> 8) & 0xFF);
336 dest[1] =
static_cast<byte_t>(value & 0xFF);
345 for (
int i = 0; i < 4; ++i) {
346 dest[i] =
static_cast<byte_t>((value >> ((3 - i) * 8)) & 0xFF);
356 for (
int i = 0; i < 8; ++i) {
357 dest[i] =
static_cast<byte_t>((value >> ((7 - i) * 8)) & 0xFF);
364NEFORCE_END_NAMESPACE__
NEFORCE_INLINE17 constexpr bool is_integral_v
is_integral的便捷变量模板
unsigned char byte_t
字节类型,定义为无符号字符
unsigned int uint32_t
32位无符号整数类型
unsigned short uint16_t
16位无符号整数类型
unsigned long long uint64_t
64位无符号整数类型
NEFORCE_NODISCARD NEFORCE_ALWAYS_INLINE constexpr decltype(auto) data(Container &cont) noexcept(noexcept(cont.data()))
获取容器的底层数据指针
typename enable_if< Test, T >::type enable_if_t
enable_if的便捷别名
static constexpr void write_le64(byte_t *dest, uint64_t value) noexcept
写入64位小端整数
static constexpr uint16_t byteswap16(uint16_t value) noexcept
16位整数字节序反转
static constexpr T le_to_host(T value) noexcept
小端序转主机序
static constexpr T host_to_network(T value) noexcept
主机序转网络序
static constexpr uint32_t read_le32(const byte_t *data) noexcept
读取32位小端整数
static constexpr T swap_endian(T value) noexcept
字节序反转(不区分端序)
static constexpr T be_to_host(T value) noexcept
大端序转主机序
static constexpr uint32_t byteswap32(uint32_t value) noexcept
32位整数字节序反转
static NEFORCE_NODISCARD NEFORCE_CONST_FUNCTION NEFORCE_ALWAYS_INLINE bool is_little_endian_runtime() noexcept
运行时检测是否为小端序
static constexpr void write_be64(byte_t *dest, uint64_t value) noexcept
写入64位大端整数
static constexpr T host_to_le(T value) noexcept
主机序转小端序
static constexpr bool is_big_endian
编译时检测是否为大端序
static constexpr void write_le16(byte_t *dest, uint16_t value) noexcept
写入16位小端整数
static constexpr T network_to_host(T value) noexcept
网络序转主机序
static constexpr uint64_t read_le64(const byte_t *data) noexcept
读取64位小端整数
static constexpr uint16_t read_be16(const byte_t *data) noexcept
读取16位大端整数
static constexpr uint64_t read_be64(const byte_t *data) noexcept
读取64位大端整数
static constexpr void write_le32(byte_t *dest, uint32_t value) noexcept
写入32位小端整数
static constexpr bool is_little_endian
编译时检测是否为小端序
static constexpr void write_be32(byte_t *dest, uint32_t value) noexcept
写入32位大端整数
static constexpr uint16_t read_le16(const byte_t *data) noexcept
读取16位小端整数
static constexpr void write_be16(byte_t *dest, uint16_t value) noexcept
写入16位大端整数
static constexpr T host_to_be(T value) noexcept
主机序转大端序
static constexpr uint32_t read_be32(const byte_t *data) noexcept
读取32位大端整数
static constexpr uint64_t byteswap64(uint64_t value) noexcept
64位整数字节序反转