emlabcpp
modern opinionated embedded C++ library
serializer.h
Go to the documentation of this file.
1 
24 #pragma once
25 
26 #include "../experimental/bounded_view.h"
27 #include "../range.h"
28 #include "./base.h"
29 
30 #include <bit>
31 #include <span>
32 
33 namespace emlabcpp::protocol
34 {
35 
36 template < base_type T, std::endian Endianess >
37 struct serializer
38 {
39  static constexpr std::size_t max_size = sizeof( T );
41  static constexpr bool is_big_endian = Endianess == std::endian::big;
42 
43  static constexpr auto& bget( auto& buffer, std::size_t const i )
44  {
45  return buffer[is_big_endian ? i : max_size - 1 - i];
46  }
47 
48  static constexpr void serialize_at( std::span< std::byte, max_size > buffer, T item )
49  {
50  for ( std::size_t const i : range( max_size ) ) {
51  bget( buffer, max_size - i - 1 ) = static_cast< std::byte >( item & 0xFF );
52  item = static_cast< T >( item >> 8 );
53  }
54  }
55 
56  static constexpr T deserialize( std::span< std::byte const, max_size > const& buffer )
57  {
58  T res{};
59  for ( std::size_t const i : range( max_size ) ) {
60  res = static_cast< T >( res << 8 );
61  res = static_cast< T >( res | std::to_integer< T >( bget( buffer, i ) ) );
62  }
63  return { res };
64  }
65 };
66 
67 template < base_type T, std::endian Endianess >
68 requires( std::is_enum_v< T > )
69 struct serializer< T, Endianess >
70 {
71  using utype = std::underlying_type_t< T >;
73  static constexpr std::size_t max_size = userializer::max_size;
75 
76  static constexpr void serialize_at( std::span< std::byte, max_size > buffer, T item )
77  {
78  userializer::serialize_at( buffer, static_cast< utype >( item ) );
79  }
80 
81  static constexpr T deserialize( std::span< std::byte const, max_size > const& buffer )
82  {
83  return static_cast< T >( userializer::deserialize( buffer ) );
84  }
85 };
86 
87 template < std::endian Endianess >
88 struct serializer< float, Endianess >
89 {
91  static_assert( sizeof( float ) == sizeof( uint32_t ) );
92 
93  static constexpr std::size_t max_size = sizeof( float );
95 
96  static constexpr void
97  serialize_at( std::span< std::byte, max_size > buffer, float const item )
98  {
99  auto const v = std::bit_cast< uint32_t >( item );
100  sub_serializer::serialize_at( buffer, v );
101  }
102 
103  static constexpr float deserialize( std::span< std::byte const, max_size > const& buffer )
104  {
105  uint32_t const v = sub_serializer::deserialize( buffer );
106  return std::bit_cast< float >( v );
107  }
108 };
109 
110 template < std::endian Endianess >
111 struct serializer< bool, Endianess >
112 {
113  static constexpr std::size_t max_size = sizeof( bool );
115 
116  static constexpr void
117  serialize_at( std::span< std::byte, max_size > const buffer, bool const v )
118  {
119  buffer[0] = v ? std::byte{ 0x1 } : std::byte{ 0x0 };
120  }
121 
122  static constexpr bool deserialize( std::span< std::byte const, max_size > const& buffer )
123  {
124  return buffer[0] == std::byte{ 0x1 };
125  }
126 };
127 
128 } // namespace emlabcpp::protocol
The bounded class represents a wrapper over type T constrained between MinVal and MaxVal as compile-t...
Definition: bounded.h:44
MIT License.
Definition: multiplexer.h:33
requires(std::is_enum_v< T >) struct serializer< T
static constexpr T deserialize(std::span< std::byte const, max_size > const &buffer)
Definition: serializer.h:81
Endianess
Definition: serializer.h:70
static constexpr void serialize_at(std::span< std::byte, max_size > buffer, T item)
Definition: serializer.h:76
static constexpr std::size_t max_size
Definition: serializer.h:73
T res
Definition: algorithm.h:505
constexpr view< iterators::numeric_iterator< Numeric > > range(Numeric from, Numeric to)
Builds numeric view over interval [from, to)
Definition: range.h:34
physical_quantity< 0, 0, 0, 0, 0, 0, 0, 0, 1 > byte
Definition: physical_quantity.h:118
static constexpr void serialize_at(std::span< std::byte, max_size > const buffer, bool const v)
Definition: serializer.h:117
static constexpr bool deserialize(std::span< std::byte const, max_size > const &buffer)
Definition: serializer.h:122
static constexpr void serialize_at(std::span< std::byte, max_size > buffer, float const item)
Definition: serializer.h:97
static constexpr float deserialize(std::span< std::byte const, max_size > const &buffer)
Definition: serializer.h:103
Definition: serializer.h:38
static constexpr T deserialize(std::span< std::byte const, max_size > const &buffer)
Definition: serializer.h:56
static constexpr std::size_t max_size
Definition: serializer.h:39
static constexpr bool is_big_endian
Definition: serializer.h:41
static constexpr void serialize_at(std::span< std::byte, max_size > buffer, T item)
Definition: serializer.h:48
static constexpr auto & bget(auto &buffer, std::size_t const i)
Definition: serializer.h:43