summaryrefslogtreecommitdiff
path: root/AK/NetworkOrdered.h
blob: 1f50e7be1fa681ccd2c0227eacd6f577b66c363e (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
#pragma once

#include <AK/Types.h>

template<typename T>
[[gnu::always_inline]] inline T convert_between_host_and_network(T value)
{
    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);
    if constexpr (sizeof(T) == 1)
        return value;
}

template<typename T>
class [[gnu::packed]] NetworkOrdered
{
public:
    NetworkOrdered() {}
    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:
    T m_network_value { 0 };
};