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"
17 template<
typename T,
size_t I>
20 #pragma region const char*
34 template<
typename Stream>
36 static serialize(Stream& writer,
const char* value, uint32_t max_size) noexcept
38 uint32_t length =
static_cast<uint32_t
>(std::char_traits<char>::length(value));
47 BS_ASSERT(writer.serialize_bits(length, num_bits));
49 return writer.serialize_bytes(
reinterpret_cast<const uint8_t*
>(value), length * 8);
59 template<
typename Stream>
61 static serialize(Stream& reader,
char* value, uint32_t max_size) noexcept
66 BS_ASSERT(reader.serialize_bits(length, num_bits));
76 BS_ASSERT(reader.serialize_bytes(
reinterpret_cast<uint8_t*
>(value), length * 8));
88 template<
size_t MaxSize>
97 template<
typename Stream>
99 static serialize(Stream& writer,
const char* value) noexcept
101 uint32_t length =
static_cast<uint32_t
>(std::char_traits<char>::length(value));
110 BS_ASSERT(writer.serialize_bits(length, num_bits));
112 return writer.serialize_bytes(
reinterpret_cast<const uint8_t*
>(value), length * 8);
121 template<
typename Stream>
128 BS_ASSERT(reader.serialize_bits(length, num_bits));
138 BS_ASSERT(reader.serialize_bytes(
reinterpret_cast<uint8_t*
>(value), length * 8));
140 value[length] =
'\0';
152 struct serialize_traits<const char8_t*>
161 template<
typename Stream>
162 typename utility::is_writing_t<Stream>
163 static serialize(Stream& writer,
const char8_t* value, uint32_t max_size) noexcept
165 uint32_t length =
static_cast<uint32_t
>(std::char_traits<char8_t>::length(value));
174 BS_ASSERT(writer.serialize_bits(length, num_bits));
176 return writer.serialize_bytes(
reinterpret_cast<const uint8_t*
>(value), length * 8);
186 template<
typename Stream>
187 typename utility::is_reading_t<Stream>
188 static serialize(Stream& reader, char8_t* value, uint32_t max_size) noexcept
193 BS_ASSERT(reader.serialize_bits(length, num_bits));
203 BS_ASSERT(reader.serialize_bytes(
reinterpret_cast<uint8_t*
>(value), length * 8));
205 value[length] =
'\0';
212 #pragma region std::basic_string
219 template<
typename T,
typename Traits,
typename Alloc>
229 template<
typename Stream>
231 static serialize(Stream& writer,
in<std::basic_string<T, Traits, Alloc>> value, uint32_t max_size) noexcept
233 uint32_t length =
static_cast<uint32_t
>(value.size());
239 BS_ASSERT(writer.serialize_bits(length, num_bits));
244 return writer.serialize_bytes(
reinterpret_cast<const uint8_t*
>(value.c_str()), length *
sizeof(T) * 8);
254 template<
typename Stream>
256 static serialize(Stream& reader,
out<std::basic_string<T, Traits, Alloc>> value, uint32_t max_size)
261 BS_ASSERT(reader.serialize_bits(length, num_bits));
271 value->resize(length);
273 BS_ASSERT(reader.serialize_bytes(
reinterpret_cast<uint8_t*
>(value->data()), length *
sizeof(T) * 8));
286 template<
typename T,
typename Traits,
typename Alloc,
size_t MaxSize>
295 template<
typename Stream>
297 static serialize(Stream& writer,
in<std::basic_string<T, Traits, Alloc>> value) noexcept
299 uint32_t length =
static_cast<uint32_t
>(value.size());
305 BS_ASSERT(writer.serialize_bits(length, num_bits));
310 return writer.serialize_bytes(
reinterpret_cast<const uint8_t*
>(value.c_str()), length *
sizeof(T) * 8);
319 template<
typename Stream>
321 static serialize(Stream& reader,
out<std::basic_string<T, Traits, Alloc>> value)
326 BS_ASSERT(reader.serialize_bits(length, num_bits));
336 value->resize(length);
338 BS_ASSERT(reader.serialize_bytes(
reinterpret_cast<uint8_t*
>(value->data()), length *
sizeof(T) * 8));
#define BS_ASSERT(x)
Definition: assert.h:15
Definition: parameter.h:64
std::enable_if_t< T::writing, R > is_writing_t
Definition: meta.h:25
constexpr uint32_t bits_to_represent(uintmax_t n)
Definition: bits.h:8
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 string max_size.
Definition: string_traits.h:18
static utility::is_writing_t< Stream > serialize(Stream &writer, const char *value) noexcept
Writes a c-style string into the writer.
Definition: string_traits.h:99
static utility::is_reading_t< Stream > serialize(Stream &reader, char *value) noexcept
Read a c-style string from the reader into value.
Definition: string_traits.h:123
static utility::is_writing_t< Stream > serialize(Stream &writer, in< std::basic_string< T, Traits, Alloc >> value) noexcept
Writes a string into the writer.
Definition: string_traits.h:297
static utility::is_reading_t< Stream > serialize(Stream &reader, out< std::basic_string< T, Traits, Alloc >> value)
Reads a string from the reader into value.
Definition: string_traits.h:321
static utility::is_reading_t< Stream > serialize(Stream &reader, char *value, uint32_t max_size) noexcept
Read a c-style string from the reader into value.
Definition: string_traits.h:61
static utility::is_writing_t< Stream > serialize(Stream &writer, const char *value, uint32_t max_size) noexcept
Writes a c-style string into the writer.
Definition: string_traits.h:36
static utility::is_reading_t< Stream > serialize(Stream &reader, out< std::basic_string< T, Traits, Alloc >> value, uint32_t max_size)
Reads a string from the reader into value.
Definition: string_traits.h:256
static utility::is_writing_t< Stream > serialize(Stream &writer, in< std::basic_string< T, Traits, Alloc >> value, uint32_t max_size) noexcept
Writes a string into the writer.
Definition: string_traits.h:231
A class for specializing trait serialization functions.
Definition: serialize_traits.h:11