2 #include "../utility/assert.h"
3 #include "../utility/meta.h"
4 #include "../utility/parameter.h"
6 #include "../stream/serialize_traits.h"
8 #include "../traits/bool_trait.h"
9 #include "../traits/integral_traits.h"
19 template<
typename T,
typename = T>
27 template<
typename T,
typename Trait>
31 template<u
int32_t Min, u
int32_t Max,
typename Stream>
32 static bool serialize_difference(Stream& stream,
int& previous,
int& current, uint32_t& difference)
35 if constexpr (Stream::writing)
36 use_bits = difference <= Max;
37 BS_ASSERT(stream.template serialize<bool>(use_bits));
42 BS_ASSERT(stream.template serialize<bounded_trait>(difference));
43 if constexpr (Stream::reading)
44 current = previous + difference;
52 template<
typename Stream>
53 static bool serialize_index(Stream& stream,
int& previous,
int& current,
int max_size)
56 if constexpr (Stream::writing)
59 difference = current - previous;
65 if constexpr (Stream::writing)
66 plus_one = difference == 1;
67 BS_ASSERT(stream.template serialize<bool>(plus_one));
70 if constexpr (Stream::reading)
71 current = previous + 1;
77 if (serialize_difference<2, 5>(stream, previous, current, difference))
81 if (serialize_difference<6, 13>(stream, previous, current, difference))
85 if (serialize_difference<14, 29>(stream, previous, current, difference))
89 if (serialize_difference<30, 61>(stream, previous, current, difference))
93 if (serialize_difference<62, 125>(stream, previous, current, difference))
97 BS_ASSERT(stream.template serialize<uint32_t>(difference, 126, max_size));
98 if constexpr (Stream::reading)
99 current = previous + difference;
116 template<
typename Stream,
typename Compare,
typename... Args>
118 static serialize(Stream& writer, T* values,
int max_size, Compare compare, Args&&... args) noexcept
121 for (
int index = 0; index < max_size; index++)
123 if (!compare(values[index]))
126 BS_ASSERT(serialize_index(writer, prev_index, index, max_size));
128 BS_ASSERT(writer.template serialize<Trait>(values[index], std::forward<Args>(args)...));
131 BS_ASSERT(serialize_index(writer, prev_index, max_size, max_size));
146 template<
typename Stream,
typename... Args>
148 static serialize(Stream& reader, T* values,
int max_size, Args&&... args) noexcept
154 BS_ASSERT(serialize_index(reader, prev_index, index, max_size));
156 if (index == max_size)
159 BS_ASSERT(reader.template serialize<Trait>(values[index], std::forward<Args>(args)...));
#define BS_ASSERT(x)
Definition: assert.h:15
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
Wrapper type for subsets of arrays.
Definition: array_traits.h:20
Wrapper type for compiletime known integer bounds.
Definition: integral_traits.h:20
static utility::is_reading_t< Stream > serialize(Stream &reader, T *values, int max_size, Args &&... args) noexcept
Writes a subset of a serialized array into values.
Definition: array_traits.h:148
static utility::is_writing_t< Stream > serialize(Stream &writer, T *values, int max_size, Compare compare, Args &&... args) noexcept
Writes a subset of the array values into the writer.
Definition: array_traits.h:118
A class for specializing trait serialization functions.
Definition: serialize_traits.h:11