emlabcpp
modern opinionated embedded C++ library
command_group.h
Go to the documentation of this file.
1 
24 #pragma once
25 
26 #include "../algorithm.h"
27 #include "../assert.h"
28 #include "../types.h"
29 #include "./traits.h"
30 
31 namespace emlabcpp::protocol
32 {
33 
37 //
40 
44 //
46 template < auto ID, convertible... Defs >
47 struct command
48 {
49  using id_type = decltype( ID );
50  using value_type = std::tuple< tag< ID >, typename proto_traits< Defs >::value_type... >;
51  using def_type = std::tuple< tag< ID >, Defs... >;
52 
53  static constexpr id_type id = ID;
54 
55  template < typename... NewDefs >
56  using with_args = command< ID, Defs..., NewDefs... >;
57 
59  constexpr static value_type
61  {
62  return { tag< ID >{}, args... };
63  }
64 };
65 
70 //
74 template < std::endian Endianess, typename... Cmds >
76 {
77  using cmds_type = std::tuple< Cmds... >;
78 
79 private:
80  static constexpr std::size_t get_id_index( auto id )
81  {
82  return find_index< sizeof...( Cmds ) >( [id]< std::size_t i >() {
83  return std::tuple_element_t< i, cmds_type >::id == id;
84  } );
85  }
86 
87 public:
88  template < auto ID >
89  static constexpr std::size_t id_index = get_id_index( ID );
90 
91  template < auto ID >
92  using cmd_type = std::tuple_element_t< id_index< ID >, cmds_type >;
93 
94  template < auto ID >
96 
97  template < typename... SubCmds >
98  using with_commands = command_group< Endianess, Cmds..., SubCmds... >;
99 
100  static_assert(
101  are_same_v< typename Cmds::id_type... >,
102  "Each command of one group has to use same type of id" );
103 
104  using def_type = endianess_wrapper< Endianess, group< typename Cmds::def_type... > >;
106  using value_type = typename traits::value_type;
107 
108  static constexpr std::size_t max_size = proto_traits< def_type >::max_size;
109 
111 
114  template < auto id, typename... Args >
115  requires( ( id == Cmds::id ) || ... )
116  constexpr static value_type make_val( Args const&... args )
117  {
118  std::optional< value_type > res;
119 
120  for_each_index< sizeof...( Cmds ) >( [&res, &args...]< std::size_t i >() {
121  using cmd = std::tuple_element_t< i, cmds_type >;
122  if constexpr ( cmd::id == id )
123  res.emplace( cmd::make_val( args... ) );
124  } );
125 
126  EMLABCPP_ASSERT( res );
127  return *res;
128  }
129 };
130 
131 template < std::endian Endianess >
133 {
134  template < typename... Cmds >
135  using with_commands = command_group< Endianess, Cmds... >;
136 };
137 
138 } // namespace emlabcpp::protocol
#define EMLABCPP_ASSERT(cond)
MIT License.
Definition: assert.h:38
Protocol library has custom type that represents message, however this is just simple overaly over st...
Definition: message.h:40
MIT License.
Definition: multiplexer.h:33
Endianess
Definition: serializer.h:70
concept convertible
This concepts limits types to types that can be declared, that is the overload of 'traits_for' is ful...
Definition: traits.h:67
Serializes values from definitions Ds to std::variant.
Definition: base.h:89
Each definition of item provided to protocol library should have specialization of 'proto_traits' str...
Definition: traits.h:44
std::variant< int64_t, float, bool, string_buffer > value_type
Definition: base.h:51
constexpr void for_each_index(NullCallable &&f)
Executes unary callable f() with template argument of type 'std::size_t', which ranges from 0 to N.
Definition: algorithm.h:466
Args const & args
Definition: min_max.h:83
T res
Definition: algorithm.h:505
Command group should be used as a collection of commands that are selected based on the ID,...
Definition: command_group.h:76
std::tuple_element_t< id_index< ID >, cmds_type > cmd_type
Definition: command_group.h:92
requires((id==Cmds::id)||...) const expr static value_type make_val(Args const &... args)
Creates value of the command group, that is variant with value of the command 'id' that will receive ...
Definition: command_group.h:115
static constexpr std::size_t max_size
Definition: command_group.h:108
static constexpr std::size_t id_index
Definition: command_group.h:89
typename cmd_type< ID >::value_type cmd_value_type
Definition: command_group.h:95
typename traits::value_type value_type
Definition: command_group.h:106
std::tuple< Cmds... > cmds_type
Definition: command_group.h:77
Command group represents a segment in the message, that may contain multiple different variants of va...
Definition: command_group.h:48
constexpr static value_type make_val(typename proto_traits< Defs >::value_type const &... args)
Creates value of the command based on the args.
Definition: command_group.h:60
decltype(ID) id_type
Definition: command_group.h:49
std::tuple< tag< ID >, typename proto_traits< Defs >::value_type... > value_type
Definition: command_group.h:50
std::tuple< tag< ID >, Defs... > def_type
Definition: command_group.h:51
More complex constructs have custom mechanics that internally produces def_type alias used by the lib...
Definition: base.h:122
Follows a set of special data types used for definition of protocol.
Definition: base.h:79
Definition: types.h:73