44 template<
typename T>
class ChangeEndiannessOneImpl
47 static_assert(T_TRAIT::IS_INT,
"T must be an integral type");
48 static_assert(T_TRAIT::SIZE > 1,
"T must be at least 2 bytes long");
52 T operator()(
const T& value)
const
59 template<
typename T,
typename TT>
class ChangeEndiannessManyImpl
61 static_assert(
sizeof(T) >
sizeof(TT),
"T must contain at least 2 TT");
63 static_assert(TT_TRAIT::IS_INT,
"TT must be an integral type");
64 static_assert(TT_TRAIT::SIZE > 1,
"TT must be at least 2 bytes long");
68 T operator()(
const T& value)
const
70 constexpr uint8_t COUNT =
sizeof(T) /
sizeof(TT);
72 TT* ptr =
reinterpret_cast<TT*
>(&result);
73 TT* last = ptr + COUNT;
96 RES_TYPE operator()(
const ARG_TYPE& value)
const
115 RES_TYPE operator()(
UNUSED const ARG_TYPE& value)
const
128 template<
typename ARG,
typename RES>
class Cast
132 using ARG_TYPE = ARG;
133 using RES_TYPE = RES;
134 RES_TYPE operator()(
const ARG_TYPE& value)
const
136 return (RES_TYPE) value;
146 template<
typename F1,
typename F2>
class Compose
150 using ARG_TYPE =
typename F2::ARG_TYPE;
151 using RES_TYPE =
typename F1::RES_TYPE;
152 RES_TYPE operator()(
const ARG_TYPE& value)
const
156 return f1(f2(value));
175 T operator()(
const T& value)
const
184 public functor_impl::ChangeEndiannessManyImpl<T, int16_t>{};
185 template<
typename T>
class ChangeEndianness<T, uint16_t> :
186 public functor_impl::ChangeEndiannessManyImpl<T, uint16_t>{};
187 template<
typename T>
class ChangeEndianness<T, int32_t> :
188 public functor_impl::ChangeEndiannessManyImpl<T, int32_t>{};
189 template<
typename T>
class ChangeEndianness<T, uint32_t> :
190 public functor_impl::ChangeEndiannessManyImpl<T, uint32_t>{};
191 template<
typename T>
class ChangeEndianness<T, int64_t> :
192 public functor_impl::ChangeEndiannessManyImpl<T, int64_t>{};
193 template<
typename T>
class ChangeEndianness<T, uint64_t> :
194 public functor_impl::ChangeEndiannessManyImpl<T, uint64_t>{};
196 template<>
class ChangeEndianness<int16_t, int16_t> :
197 public functor_impl::ChangeEndiannessOneImpl<int16_t>{};
198 template<>
class ChangeEndianness<uint16_t, uint16_t> :
199 public functor_impl::ChangeEndiannessOneImpl<uint16_t>{};
200 template<>
class ChangeEndianness<int32_t, int32_t> :
201 public functor_impl::ChangeEndiannessOneImpl<int32_t>{};
202 template<>
class ChangeEndianness<uint32_t, uint32_t> :
203 public functor_impl::ChangeEndiannessOneImpl<uint32_t>{};
204 template<>
class ChangeEndianness<int64_t, int64_t> :
205 public functor_impl::ChangeEndiannessOneImpl<int64_t>{};
206 template<>
class ChangeEndianness<uint64_t, uint64_t> :
207 public functor_impl::ChangeEndiannessOneImpl<uint64_t>{};
221 using ARG_TYPE =
typename FUNCTOR::ARG_TYPE;
222 using RES_TYPE =
typename FUNCTOR::RES_TYPE;
223 static RES_TYPE call(
const ARG_TYPE& value)
225 return FUNCTOR{}(value);
Cast functor: returns its argument of type ARG casted to type RES.
Endianness change functor: will change from big to little or little to big endian format on integer t...
Composition functor: applies 2 functors one after each other.
Constant functor: always returns a constant value.
Identity functor: always returns its argument, unchanged.
#define UNUSED
Specific GCC attribute to declare an argument or variable unused, so that the compiler does not emit ...
This namespace defines a few useful functors.
T change_endianness(const T &value)
Change endianness of any integral type (from big to small or small to big).
void swap_bytes(uint16_t &value)
Swap 2 bytes of a 2-bytes integer.
Utility to instantiate and execute a functor from its type.
This trait allows static checks (at compile-time) of properties of various types.
Useful traits for common types.
General utilities API that have broad application in programs.