25#ifndef M5_UTILITY_TYPES_HPP
26#define M5_UTILITY_TYPES_HPP
44template <
typename T,
bool DataIsLittle>
55 template <
bool HostIsLittle = m5::endian::little>
61 template <
typename U = T,
typename std::enable_if<sizeof(U) == 2,
int>::type = 0>
62 constexpr EndianInt(
const uint8_t b0,
const uint8_t b1) :
u8{b0, b1}
66 template <
typename U = T,
typename std::enable_if<sizeof(U) == 4,
int>::type = 0>
67 constexpr EndianInt(
const uint8_t b0,
const uint8_t b1,
const uint8_t b2,
const uint8_t b3) :
u8{b0, b1, b2, b3}
71 template <
typename U = T,
typename std::enable_if<sizeof(U) == 8,
int>::type = 0>
72 constexpr EndianInt(
const uint8_t b0,
const uint8_t b1,
const uint8_t b2,
const uint8_t b3,
const uint8_t b4,
73 const uint8_t b5,
const uint8_t b6,
const uint8_t b7)
74 :
u8{b0, b1, b2, b3, b4, b5, b6, b7}
89 template <
bool HostIsLittle =
m5::
endian::little>
95 template <
typename H,
typename L,
typename U = T,
typename std::enable_if<sizeof(U) == 2,
int>::type = 0>
96 EndianInt& operator=(
const std::pair<H, L>& o)
98 static_assert(std::is_integral<H>::value && std::is_integral<L>::value,
"HIGH & LOW Must be integral");
99 u8[0] =
static_cast<uint8_t
>(o.first);
100 u8[1] =
static_cast<uint8_t
>(o.second);
108 inline explicit operator bool()
const
113 inline explicit operator const uint8_t*()
const
118 inline explicit operator uint8_t*()
123 inline explicit operator T()
const
133 template <
bool HostIsLittle = m5::endian::little>
134 inline void set(
const T v)
136 if (DataIsLittle == HostIsLittle) {
139 value = m5::stl::byteswap(v);
146 template <
bool HostIsLittle = m5::endian::little>
149 if (DataIsLittle == HostIsLittle) {
152 return m5::stl::byteswap(
value);
160 inline uint8_t
low()
const
162 return u8[
sizeof(T) - 1];
165 inline const uint8_t*
data()
const
181 uint8_t
u8[
sizeof(T)];
202#if defined(__SIZEOF_INT128__)
213template <
typename T,
bool DL1,
bool DL2>
216 return a.
get() == b.
get();
218template <
typename T,
bool DL1,
bool DL2>
219inline bool operator!=(
const EndianInt<T, DL1>& a,
const EndianInt<T, DL2>& b)
223template <
typename T,
bool DL1,
bool DL2>
224inline bool operator<(
const EndianInt<T, DL1>& a,
const EndianInt<T, DL2>& b)
226 return a.get() < b.get();
228template <
typename T,
bool DL1,
bool DL2>
229inline bool operator>(
const EndianInt<T, DL1>& a,
const EndianInt<T, DL2>& b)
233template <
typename T,
bool DL1,
bool DL2>
234inline bool operator<=(
const EndianInt<T, DL1>& a,
const EndianInt<T, DL2>& b)
238template <
typename T,
bool DL1,
bool DL2>
239inline bool operator>=(
const EndianInt<T, DL1>& a,
const EndianInt<T, DL2>& b)
std::byteswap for less than C++23
Compile-time endian identification.
Top level namespace of M5.
Definition base64.cpp:39
Definition byteswap.hpp:39
Endian-compliant integer type.
Definition types.hpp:45
const uint8_t * data() const
Gets the const pointer.
Definition types.hpp:165
uint8_t low() const
Gets the low byte (u8[sizeof(T)-1])
Definition types.hpp:160
constexpr EndianInt(EndianInt &&o) noexcept=default
default constructor
constexpr EndianInt()
default constructor
Definition types.hpp:51
size_t size() const
Gets size in uint8_t units.
Definition types.hpp:175
void set(const T v)
Set value with specified endianness.
Definition types.hpp:134
uint8_t * data()
Gets the pointer.
Definition types.hpp:170
T value
Raw value.
Definition types.hpp:180
constexpr EndianInt(const uint8_t b0, const uint8_t b1, const uint8_t b2, const uint8_t b3)
Stored in order of bytes (4-byte types)
Definition types.hpp:67
constexpr EndianInt(const uint8_t b0, const uint8_t b1)
Stored in order of bytes (2-byte types)
Definition types.hpp:62
uint8_t u8[sizeof(T)]
Raw value according to uint8_t.
Definition types.hpp:181
uint8_t high() const
Gets the high byte (u8[0])
Definition types.hpp:155
constexpr EndianInt(const EndianInt &)=default
default constructor
EndianInt(const T v)
from T
Definition types.hpp:56
constexpr EndianInt(const uint8_t b0, const uint8_t b1, const uint8_t b2, const uint8_t b3, const uint8_t b4, const uint8_t b5, const uint8_t b6, const uint8_t b7)
Stored in order of bytes (8-byte types)
Definition types.hpp:72
T get() const
Gets value with specified endianness.
Definition types.hpp:147