blob: b5dc404103ce14f4203ac5d2f46b81602428bc36 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
#pragma once
#include <AK/Types.h>
template<typename T>
T convert_between_host_and_network(T host_value)
{
if constexpr (sizeof(T) == 4) {
auto* s = (byte*)&host_value;
return (dword)(s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3]);
}
if constexpr (sizeof(T) == 2) {
auto* s = (byte*)&host_value;
return (word)(s[0] << 8 | s[1]);
}
if constexpr (sizeof(T) == 1)
return host_value;
}
template<typename T>
class [[gnu::packed]] NetworkOrdered {
public:
NetworkOrdered()
: m_network_value(0)
{
}
NetworkOrdered(const T& host_value)
: m_network_value(convert_between_host_and_network(host_value))
{
}
NetworkOrdered(const NetworkOrdered& other)
: m_network_value(other.m_network_value)
{
}
NetworkOrdered& operator=(const NetworkOrdered& other)
{
m_network_value = other.m_network_value;
return *this;
}
operator T() const { return convert_between_host_and_network(m_network_value); }
private:
T m_network_value { 0 };
};
|