2019-03-12 03:11:20 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Types.h>
|
|
|
|
|
|
|
|
template<typename T>
|
2019-06-26 18:01:48 +00:00
|
|
|
[[gnu::always_inline]] inline T convert_between_host_and_network(T value)
|
2019-03-12 03:11:20 +00:00
|
|
|
{
|
2019-06-26 18:01:48 +00:00
|
|
|
if constexpr (sizeof(T) == 8)
|
|
|
|
return __builtin_bswap64(value);
|
|
|
|
if constexpr (sizeof(T) == 4)
|
|
|
|
return __builtin_bswap32(value);
|
|
|
|
if constexpr (sizeof(T) == 2)
|
|
|
|
return __builtin_bswap16(value);
|
2019-03-12 03:11:20 +00:00
|
|
|
if constexpr (sizeof(T) == 1)
|
2019-06-26 18:01:48 +00:00
|
|
|
return value;
|
2019-03-12 03:11:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2019-05-28 09:53:16 +00:00
|
|
|
class [[gnu::packed]] NetworkOrdered
|
|
|
|
{
|
2019-03-12 03:11:20 +00:00
|
|
|
public:
|
2019-06-26 14:26:59 +00:00
|
|
|
NetworkOrdered() {}
|
2019-03-12 03:11:20 +00:00
|
|
|
NetworkOrdered(const T& host_value)
|
|
|
|
: m_network_value(convert_between_host_and_network(host_value))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
operator T() const { return convert_between_host_and_network(m_network_value); }
|
|
|
|
|
|
|
|
private:
|
2019-03-13 22:14:30 +00:00
|
|
|
T m_network_value { 0 };
|
2019-03-12 03:11:20 +00:00
|
|
|
};
|