emlabcpp
modern opinionated embedded C++ library
collect.h
Go to the documentation of this file.
1 
24 #pragma once
25 
26 #include "../../static_function.h"
27 #include "../contiguous_tree/request_adapter.h"
28 #include "../function_view.h"
29 #include "./convert.h"
30 #include "./coroutine.h"
31 #include "./protocol.h"
32 
33 #include <source_location>
34 
35 namespace emlabcpp::testing
36 {
37 
39 
41 using collect_value_type = std::variant< value_type, contiguous_container_type >;
42 
44 {
47  std::optional< collect_key_type > opt_key;
49 };
50 
52 {
54 };
55 
56 using collect_server_client_group = std::variant< collect_reply, tree_error_reply >;
60 
66 
67 class collector;
68 
69 class [[nodiscard]] collect_awaiter : public wait_interface
70 {
71 public:
76 
78 
79  [[nodiscard]] coro_state get_state() const override
80  {
81  return state;
82  }
83 
84  void tick() override
85  {
86  }
87 
88  [[nodiscard]] bool await_ready() const
89  {
90  return false;
91  }
92 
93  void await_suspend( std::coroutine_handle< coroutine< void >::promise_type > h );
94 
95  [[nodiscard]] node_id await_resume() const
96  {
97  return res;
98  }
99 };
100 
101 // TODO: make concept for "testing module" -> has get_channel/on_msg...
102 
104 {
105 public:
107 
108  collector( collector const& ) = delete;
109  collector( collector&& ) = delete;
110  collector& operator=( collector const& ) = delete;
111  collector& operator=( collector&& ) = delete;
112 
113  [[nodiscard]] constexpr protocol::channel_type get_channel() const
114  {
115  return channel_;
116  }
117 
118  outcome on_msg( std::span< std::byte const > const& msg );
120 
122  set( node_id const parent, std::string_view key, contiguous_container_type t );
123  bool set( node_id const parent, std::string_view key, value_type const& val );
124 
125  collect_awaiter set( std::string_view key, contiguous_container_type t )
126  {
127  return set( 0, key, t );
128  }
129 
130  bool set( std::string_view key, value_type const& val )
131  {
132  return set( 0, key, val );
133  }
134 
135  template < typename Arg >
136  bool set( node_id parent, std::string_view key, Arg const& arg )
137  {
138  return set( parent, key, value_type_converter< Arg >::to_value( arg ) );
139  }
140 
141  template < typename Arg >
142  bool set( std::string_view key, Arg const& arg )
143  {
144  return set( 0, key, value_type_converter< Arg >::to_value( arg ) );
145  }
146 
148  bool append( node_id const parent, value_type const& val );
149 
150  template < typename Arg >
151  bool append( node_id parent, Arg const& arg )
152  {
153  return append( parent, value_type_converter< Arg >::to_value( arg ) );
154  }
155 
157 
158 private:
159  result send( collect_request const& req );
160 
161  protocol::channel_type channel_;
162  collect_reply_callback reply_callback_;
164 };
165 
166 inline status_awaiter
167 expect( collector& c, bool expr, std::source_location loc = std::source_location::current() )
168 {
169  if ( !expr ) {
170  c.set( "sfile", loc.file_name() );
171  c.set( "sfun", loc.function_name() );
172  c.set( "sline", loc.line() );
173  }
174  return expect( expr );
175 }
176 
178 {
179 public:
182  pmr::memory_resource& mem_res,
184 
185  [[nodiscard]] protocol::channel_type get_channel() const
186  {
187  return channel_;
188  }
189 
190  outcome on_msg( std::span< std::byte const > data );
192 
193  void clear()
194  {
195  tree_.clear();
196  }
197 
198  [[nodiscard]] data_tree const& get_tree() const
199  {
200  return tree_;
201  }
202 
203 private:
204  result send( collect_server_client_group const& val );
205 
206  protocol::channel_type channel_;
207  data_tree tree_;
209 };
210 
211 } // namespace emlabcpp::testing
void clear()
Definition: tree.h:320
Definition: memory_resource.h:33
Protocol library has custom type that represents message, however this is just simple overaly over st...
Definition: message.h:40
Definition: collect.h:70
collect_awaiter(collect_request const &req, collector &coll)
collector & col
Definition: collect.h:75
bool await_ready() const
Definition: collect.h:88
coro_state get_state() const override
Definition: collect.h:79
node_id await_resume() const
Definition: collect.h:95
collect_request req
Definition: collect.h:72
void tick() override
Definition: collect.h:84
void await_suspend(std::coroutine_handle< coroutine< void >::promise_type > h)
Definition: collect.h:178
collect_server(protocol::channel_type chan, pmr::memory_resource &mem_res, collect_server_transmit_callback send_cb)
protocol::channel_type get_channel() const
Definition: collect.h:185
void clear()
Definition: collect.h:193
outcome on_msg(std::span< std::byte const > data)
data_tree const & get_tree() const
Definition: collect.h:198
outcome on_msg(collect_request const &req)
Definition: collect.h:104
bool exchange(collect_request const &req, collect_reply_callback cb)
collector(protocol::channel_type chann, collect_client_transmit_callback send_cb)
constexpr protocol::channel_type get_channel() const
Definition: collect.h:113
collector(collector const &)=delete
bool append(node_id parent, Arg const &arg)
Definition: collect.h:151
collect_awaiter set(node_id const parent, std::string_view key, contiguous_container_type t)
collector & operator=(collector const &)=delete
bool set(std::string_view key, Arg const &arg)
Definition: collect.h:142
bool set(node_id const parent, std::string_view key, value_type const &val)
collect_awaiter append(node_id const parent, contiguous_container_type t)
bool append(node_id const parent, value_type const &val)
collector & operator=(collector &&)=delete
outcome on_msg(collect_server_client_group const &var)
collect_awaiter set(std::string_view key, contiguous_container_type t)
Definition: collect.h:125
collector(collector &&)=delete
bool set(std::string_view key, value_type const &val)
Definition: collect.h:130
outcome on_msg(std::span< std::byte const > const &msg)
bool set(node_id parent, std::string_view key, Arg const &arg)
Definition: collect.h:136
Definition: coroutine.h:53
uint16_t channel_type
Definition: multiplexer.h:35
MIT License.
Definition: base.h:37
uint32_t node_id
Definition: base.h:42
coro_state
Definition: coroutine.h:36
node_id nid
Definition: collect.h:53
typename protocol::handler< collect_request >::message_type collect_client_server_message
Definition: collect.h:59
string_buffer< 32 > key_type_buffer
Definition: base.h:40
bool expects_reply
Definition: collect.h:46
status_awaiter expect(collector &c, bool expr, std::source_location loc=std::source_location::current())
Definition: collect.h:167
std::variant< int64_t, float, bool, string_buffer > value_type
Definition: base.h:51
collect_value_type value
Definition: collect.h:48
typename protocol::handler< collect_server_client_group >::message_type collect_server_client_message
Definition: collect.h:58
static constexpr protocol::channel_type collect_channel
Definition: collect.h:38
std::variant< collect_reply, tree_error_reply > collect_server_client_group
Definition: collect.h:56
std::variant< value_type, contiguous_container_type > collect_value_type
Definition: collect.h:41
node_id parent
Definition: collect.h:45
std::optional< collect_key_type > opt_key
Definition: collect.h:47
Definition: collect.h:52
Definition: collect.h:44
constexpr pointer data() noexcept
Returns pointer to first item of the storage.
Definition: static_storage.h:108
T res
Definition: algorithm.h:505
contiguous_container_type
Definition: base.h:40
physical_quantity< 0, 0, 0, 1, 0, 0, 0, 0, 0 > current
Definition: physical_quantity.h:113
Definition: static_function.h:109
outcome represents tristate resut of some operation, which can succeed, fail or produce an error.
Definition: outcome.h:49
result represents an result of some operation, as an alternative to returning just bool with true/fal...
Definition: result.h:42
Definition: coroutine.h:177
Definition: coroutine.h:45