BitStream
enum_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 #include "../traits/integral_traits.h"
9 
10 namespace bitstream
11 {
16  template<typename T, std::underlying_type_t<T> = (std::numeric_limits<T>::min)(), std::underlying_type_t<T> = (std::numeric_limits<T>::max)()>
17  struct bounded_enum;
18 
22  template<typename T>
23  struct serialize_traits<T, typename std::enable_if_t<std::is_enum_v<T>>>
24  {
25  using value_type = std::underlying_type_t<T>;
26 
27  template<typename Stream>
29  static serialize(Stream& writer, T value, value_type min = 0, value_type max = (std::numeric_limits<value_type>::max)()) noexcept
30  {
31  value_type unsigned_value = static_cast<value_type>(value);
32 
33  return writer.template serialize<value_type>(unsigned_value, min, max);
34  }
35 
36  template<typename Stream>
38  static serialize(Stream& reader, T& value, value_type min = 0, value_type max = (std::numeric_limits<value_type>::max)()) noexcept
39  {
40  value_type unsigned_value;
41 
42  BS_ASSERT(reader.template serialize<value_type>(unsigned_value, min, max));
43 
44  value = static_cast<T>(unsigned_value);
45 
46  return true;
47  }
48  };
49 
53  template<typename T, std::underlying_type_t<T> Min, std::underlying_type_t<T> Max>
54  struct serialize_traits<bounded_enum<T, Min, Max>, typename std::enable_if_t<std::is_enum_v<T>>>
55  {
56  using value_type = std::underlying_type_t<T>;
58 
59  template<typename Stream>
61  static serialize(Stream& writer, T value) noexcept
62  {
63  value_type unsigned_value = static_cast<value_type>(value);
64 
65  return writer.template serialize<bound_type>(unsigned_value);
66  }
67 
68  template<typename Stream>
70  static serialize(Stream& reader, T& value) noexcept
71  {
72  value_type unsigned_value;
73 
74  BS_ASSERT(reader.template serialize<bound_type>(unsigned_value));
75 
76  value = static_cast<T>(unsigned_value);
77 
78  return true;
79  }
80  };
81 }
#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 compiletime known integer bounds.
Definition: enum_trait.h:17
Wrapper type for compiletime known integer bounds.
Definition: integral_traits.h:20
static utility::is_writing_t< Stream > serialize(Stream &writer, T value, value_type min=0, value_type max=(std::numeric_limits< value_type >::max)()) noexcept
Definition: enum_trait.h:29
std::underlying_type_t< T > value_type
Definition: enum_trait.h:25
static utility::is_reading_t< Stream > serialize(Stream &reader, T &value, value_type min=0, value_type max=(std::numeric_limits< value_type >::max)()) noexcept
Definition: enum_trait.h:38
static utility::is_reading_t< Stream > serialize(Stream &reader, T &value) noexcept
Definition: enum_trait.h:70
static utility::is_writing_t< Stream > serialize(Stream &writer, T value) noexcept
Definition: enum_trait.h:61
A class for specializing trait serialization functions.
Definition: serialize_traits.h:11