2 #include "../utility/assert.h"
3 #include "../utility/bits.h"
4 #include "../utility/meta.h"
5 #include "../utility/parameter.h"
7 #include "../stream/serialize_traits.h"
11 #include <type_traits>
19 template<
typename T, T = (std::numeric_limits<T>::min)(), T = (std::numeric_limits<T>::max)()>
22 #pragma region integral types
28 struct serialize_traits<T, typename std::enable_if_t<std::is_integral_v<T> && !std::is_const_v<T>>>
30 static_assert(
sizeof(T) <= 8,
"Integers larger than 8 bytes are currently not supported. You will have to write this functionality yourself");
40 template<
typename Stream>
42 static serialize(Stream& writer,
in<T> value, T min = (std::numeric_limits<T>::min)(), T max = (std::numeric_limits<T>::max)()) noexcept
52 if constexpr (
sizeof(T) > 4)
57 uint32_t unsigned_value =
static_cast<uint32_t
>(value - min);
58 BS_ASSERT(writer.serialize_bits(unsigned_value, 32));
60 unsigned_value =
static_cast<uint32_t
>((value - min) >> 32);
61 BS_ASSERT(writer.serialize_bits(unsigned_value, num_bits - 32));
68 uint32_t unsigned_value =
static_cast<uint32_t
>(value - min);
69 BS_ASSERT(writer.serialize_bits(unsigned_value, num_bits));
82 template<
typename Stream>
84 static serialize(Stream& reader, T& value, T min = (std::numeric_limits<T>::min)(), T max = (std::numeric_limits<T>::max)()) noexcept
92 if constexpr (
sizeof(T) > 4)
98 uint32_t unsigned_value;
100 BS_ASSERT(reader.serialize_bits(unsigned_value, 32));
101 value |=
static_cast<T
>(unsigned_value);
103 BS_ASSERT(reader.serialize_bits(unsigned_value, num_bits - 32));
104 value |=
static_cast<T
>(unsigned_value) << 32;
115 uint32_t unsigned_value;
116 BS_ASSERT(reader.serialize_bits(unsigned_value, num_bits));
118 value =
static_cast<T
>(unsigned_value) + min;
127 #pragma region const integral types
134 template<
typename T, T Min, T Max>
137 static_assert(
sizeof(T) <= 8,
"Integers larger than 8 bytes are currently not supported. You will have to write this functionality yourself");
145 template<
typename Stream>
149 static_assert(Min < Max);
155 static_assert(num_bits <=
sizeof(T) * 8);
157 if constexpr (
sizeof(T) > 4 && num_bits > 32)
160 uint32_t unsigned_value =
static_cast<uint32_t
>(value - Min);
161 BS_ASSERT(writer.serialize_bits(unsigned_value, 32));
163 unsigned_value =
static_cast<uint32_t
>((value - Min) >> 32);
164 BS_ASSERT(writer.serialize_bits(unsigned_value, num_bits - 32));
169 uint32_t unsigned_value =
static_cast<uint32_t
>(value - Min);
170 BS_ASSERT(writer.serialize_bits(unsigned_value, num_bits));
182 template<
typename Stream>
186 static_assert(Min < Max);
190 static_assert(num_bits <=
sizeof(T) * 8);
192 if constexpr (
sizeof(T) > 4 && num_bits > 32)
196 uint32_t unsigned_value;
198 BS_ASSERT(reader.serialize_bits(unsigned_value, 32));
199 value |=
static_cast<T
>(unsigned_value);
201 BS_ASSERT(reader.serialize_bits(unsigned_value, num_bits - 32));
202 value |=
static_cast<T
>(unsigned_value) << 32;
209 uint32_t unsigned_value;
210 BS_ASSERT(reader.serialize_bits(unsigned_value, num_bits));
212 value =
static_cast<T
>(unsigned_value) + Min;
#define BS_ASSERT(x)
Definition: assert.h:15
constexpr uint32_t bits_in_range(intmax_t min, intmax_t max)
Definition: bits.h:22
std::enable_if_t< T::writing, R > is_writing_t
Definition: meta.h:25
std::enable_if_t< T::reading, R > is_reading_t
Definition: meta.h:28
Definition: bounded_range.h:28
std::conditional_t<(sizeof(T)<=16 &&std::is_trivially_copy_constructible_v< T >), std::add_const_t< T >, std::add_lvalue_reference_t< std::add_const_t< T > >> in
Passes by const or const reference depending on size.
Definition: parameter.h:94
Wrapper type for compiletime known integer bounds.
Definition: integral_traits.h:20
static utility::is_reading_t< Stream > serialize(Stream &reader, T &value, T min=(std::numeric_limits< T >::min)(), T max=(std::numeric_limits< T >::max)()) noexcept
Reads an integer from the reader into value.
Definition: integral_traits.h:84
static utility::is_writing_t< Stream > serialize(Stream &writer, in< T > value, T min=(std::numeric_limits< T >::min)(), T max=(std::numeric_limits< T >::max)()) noexcept
Writes an integer into the writer.
Definition: integral_traits.h:42
static utility::is_reading_t< Stream > serialize(Stream &reader, T &value) noexcept
Reads an integer from the writer into value.
Definition: integral_traits.h:184
static utility::is_writing_t< Stream > serialize(Stream &writer, in< T > value) noexcept
Writes an integer into the writer.
Definition: integral_traits.h:147
A class for specializing trait serialization functions.
Definition: serialize_traits.h:11