mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-11 01:06:01 +03:00
0dc9af5f7e
Also run it across the whole tree to get everything using the One True Style. We don't yet run this in an automated fashion as it's a little slow, but there is a snippet to do so in makeall.sh.
50 lines
1.1 KiB
C++
50 lines
1.1 KiB
C++
#pragma once
|
|
|
|
#include <AK/Types.h>
|
|
|
|
template<typename T>
|
|
[[gnu::always_inline]] inline 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 };
|
|
};
|