2023-03-20 19:45:09 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <BAN/Traits.h>
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
namespace BAN
|
|
|
|
{
|
|
|
|
|
|
|
|
template<integral T>
|
2023-09-27 22:33:38 +03:00
|
|
|
constexpr T swap_endianness(T value)
|
|
|
|
{
|
|
|
|
if constexpr(sizeof(T) == 1)
|
|
|
|
return value;
|
|
|
|
if constexpr(sizeof(T) == 2)
|
|
|
|
return (((value >> 8) & 0xFF) << 0)
|
|
|
|
| (((value >> 0) & 0xFF) << 8);
|
|
|
|
if constexpr(sizeof(T) == 4)
|
|
|
|
return (((value >> 24) & 0xFF) << 0)
|
|
|
|
| (((value >> 16) & 0xFF) << 8)
|
|
|
|
| (((value >> 8) & 0xFF) << 16)
|
|
|
|
| (((value >> 0) & 0xFF) << 24);
|
|
|
|
if constexpr(sizeof(T) == 8)
|
|
|
|
return (((value >> 56) & 0xFF) << 0)
|
|
|
|
| (((value >> 48) & 0xFF) << 8)
|
|
|
|
| (((value >> 40) & 0xFF) << 16)
|
|
|
|
| (((value >> 32) & 0xFF) << 24)
|
|
|
|
| (((value >> 24) & 0xFF) << 32)
|
|
|
|
| (((value >> 16) & 0xFF) << 40)
|
|
|
|
| (((value >> 8) & 0xFF) << 48)
|
|
|
|
| (((value >> 0) & 0xFF) << 56);
|
|
|
|
T result { 0 };
|
|
|
|
for (size_t i = 0; i < sizeof(T); i++)
|
|
|
|
result |= ((value >> (i * 8)) & 0xFF) << ((sizeof(T) - i - 1) * 8);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<integral T>
|
|
|
|
constexpr T host_to_little_endian(T value)
|
2023-03-20 19:45:09 +02:00
|
|
|
{
|
|
|
|
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
2023-09-27 22:33:38 +03:00
|
|
|
return value;
|
|
|
|
#else
|
|
|
|
return swap_endianness(value);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2024-04-09 01:13:28 +03:00
|
|
|
template<integral T>
|
|
|
|
constexpr T little_endian_to_host(T value)
|
|
|
|
{
|
|
|
|
return host_to_little_endian(value);
|
|
|
|
}
|
|
|
|
|
2023-09-27 22:33:38 +03:00
|
|
|
template<integral T>
|
|
|
|
constexpr T host_to_big_endian(T value)
|
|
|
|
{
|
|
|
|
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
|
|
|
return value;
|
2023-03-20 19:45:09 +02:00
|
|
|
#else
|
2023-09-27 22:33:38 +03:00
|
|
|
return swap_endianness(value);
|
2023-03-20 19:45:09 +02:00
|
|
|
#endif
|
2023-09-27 22:33:38 +03:00
|
|
|
}
|
|
|
|
|
2024-04-09 01:13:28 +03:00
|
|
|
template<integral T>
|
|
|
|
constexpr T big_endian_to_host(T value)
|
|
|
|
{
|
|
|
|
return host_to_big_endian(value);
|
|
|
|
}
|
|
|
|
|
2023-09-27 22:33:38 +03:00
|
|
|
template<integral T>
|
|
|
|
struct LittleEndian
|
|
|
|
{
|
2024-02-02 03:13:14 +02:00
|
|
|
constexpr LittleEndian(T value)
|
|
|
|
{
|
|
|
|
raw = host_to_little_endian(value);
|
|
|
|
}
|
|
|
|
|
2023-09-27 22:33:38 +03:00
|
|
|
constexpr operator T() const
|
|
|
|
{
|
|
|
|
return host_to_little_endian(raw);
|
2023-03-20 19:45:09 +02:00
|
|
|
}
|
2023-09-27 22:33:38 +03:00
|
|
|
private:
|
2023-03-20 19:45:09 +02:00
|
|
|
T raw;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<integral T>
|
|
|
|
struct BigEndian
|
|
|
|
{
|
2024-02-02 03:13:14 +02:00
|
|
|
constexpr BigEndian(T value)
|
|
|
|
{
|
|
|
|
raw = host_to_big_endian(value);
|
|
|
|
}
|
|
|
|
|
2023-03-20 19:45:09 +02:00
|
|
|
constexpr operator T() const
|
|
|
|
{
|
2023-09-27 22:33:38 +03:00
|
|
|
return host_to_big_endian(raw);
|
2023-03-20 19:45:09 +02:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
T raw;
|
|
|
|
};
|
|
|
|
|
2024-02-02 03:13:14 +02:00
|
|
|
template<integral T>
|
|
|
|
using NetworkEndian = BigEndian<T>;
|
|
|
|
|
2024-02-05 17:29:24 +02:00
|
|
|
template<integral T>
|
|
|
|
constexpr T host_to_network_endian(T value)
|
|
|
|
{
|
|
|
|
return host_to_big_endian(value);
|
|
|
|
}
|
|
|
|
|
2024-01-24 14:43:46 +02:00
|
|
|
}
|