BitStream
bool_trait.h
Go to the documentation of this file.
1 #pragma once
2 #include "../utility/assert.h"
3 #include "../utility/meta.h"
4 #include "../utility/parameter.h"
5 
6 #include "../stream/serialize_traits.h"
7 
8 namespace bitstream
9 {
13  template<>
14  struct serialize_traits<bool>
15  {
16  template<typename Stream>
18  static serialize(Stream& writer, in<bool> value) noexcept
19  {
20  uint32_t unsigned_value = value;
21 
22  return writer.serialize_bits(unsigned_value, 1U);
23  }
24 
25  template<typename Stream>
27  static serialize(Stream& reader, out<bool> value) noexcept
28  {
29  uint32_t unsigned_value;
30 
31  BS_ASSERT(reader.serialize_bits(unsigned_value, 1U));
32 
33  value = unsigned_value;
34 
35  return true;
36  }
37  };
38 
42  template<size_t Size>
43  struct serialize_traits<bool[Size]>
44  {
45  template<typename Stream>
47  static serialize(Stream& writer, const bool* values) noexcept
48  {
49  uint32_t unsigned_value;
50  for (size_t i = 0; i < Size; i++)
51  {
52  unsigned_value = values[i];
53  BS_ASSERT(writer.serialize_bits(unsigned_value, 1U));
54  }
55 
56  return writer.serialize_bits(unsigned_value, 1U);
57  }
58 
59  template<typename Stream>
61  static serialize(Stream& reader, bool* values) noexcept
62  {
63  uint32_t unsigned_value;
64  for (size_t i = 0; i < Size; i++)
65  {
66  BS_ASSERT(reader.serialize_bits(unsigned_value, 1U));
67 
68  values[i] = unsigned_value;
69  }
70 
71  return true;
72  }
73  };
74 }
#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
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
static utility::is_writing_t< Stream > serialize(Stream &writer, in< bool > value) noexcept
Definition: bool_trait.h:18
static utility::is_reading_t< Stream > serialize(Stream &reader, out< bool > value) noexcept
Definition: bool_trait.h:27
static utility::is_reading_t< Stream > serialize(Stream &reader, bool *values) noexcept
Definition: bool_trait.h:61
static utility::is_writing_t< Stream > serialize(Stream &writer, const bool *values) noexcept
Definition: bool_trait.h:47
A class for specializing trait serialization functions.
Definition: serialize_traits.h:11