emlabcpp
modern opinionated embedded C++ library
handler.h
Go to the documentation of this file.
1 
24 #pragma once
25 
26 #include "../types.h"
27 #include "./converter.h"
28 
29 namespace emlabcpp::protocol
30 {
31 
34 //
37 template < convertible T, std::endian E = std::endian::big >
38 struct handler
39 {
41  static constexpr std::size_t max_size = def::max_size;
42  using value_type = typename def::value_type;
44 
45  static message_type serialize( value_type const& val )
46  {
48 
49  bounded const used =
50  def::serialize_at( std::span< std::byte, max_size >{ res }, val );
51  EMLABCPP_ASSERT( *used <= max_size );
52  res.resize( *used );
53  return res;
54  };
55 
56  static std::variant< value_type, error_record >
58  {
59  value_type val;
60  conversion_result const res = def::deserialize( msg, val );
61  if ( res.has_error() ) {
62  mark const* const mark = res.get_error();
63  return error_record{ .error_mark = *mark, .offset = res.used };
64  }
65  return val;
66  }
67 };
68 
69 } // namespace emlabcpp::protocol
#define EMLABCPP_ASSERT(cond)
MIT License.
Definition: assert.h:38
The bounded class represents a wrapper over type T constrained between MinVal and MaxVal as compile-t...
Definition: bounded.h:44
Protocol library has custom type that represents message, however this is just simple overaly over st...
Definition: message.h:40
Generic class to represent view of some container.
Definition: view.h:41
MIT License.
Definition: multiplexer.h:33
static constexpr T deserialize(std::span< std::byte const, max_size > const &buffer)
Definition: serializer.h:81
mark error_mark
Definition: error.h:40
static constexpr void serialize_at(std::span< std::byte, max_size > buffer, T item)
Definition: serializer.h:76
string_buffer< mark_size > mark
Definition: error.h:36
static constexpr std::size_t max_size
Definition: serializer.h:73
decltype(converter_for_impl< D, E >()) converter_for
Definition: converter.h:54
Definition: error.h:39
std::variant< int64_t, float, bool, string_buffer > value_type
Definition: base.h:51
T res
Definition: algorithm.h:505
handler< T > should be used to execute actual serialization and deserealization of protocol definitio...
Definition: handler.h:39
typename def::value_type value_type
Definition: handler.h:42
static std::variant< value_type, error_record > extract(view< std::byte const * > const &msg)
Definition: handler.h:57
static constexpr std::size_t max_size
Definition: handler.h:41
static message_type serialize(value_type const &val)
Definition: handler.h:45
converter_for< T, E > def
Definition: handler.h:40