emlabcpp
modern opinionated embedded C++ library
controller.h
Go to the documentation of this file.
1 
24 #pragma once
25 
26 #include "../../pmr/aliases.h"
27 #include "../../result.h"
28 #include "./base.h"
30 #include "./coroutine.h"
31 
32 namespace emlabcpp::testing
33 {
34 
36 {
37  struct initializing_state
38  {
39  coroutine< void > coro{};
40  };
41 
42  struct test_running_state
43  {
44  test_result context;
45  };
46 
47  struct idle_state
48  {
49  };
50 
51  using states = std::variant< initializing_state, test_running_state, idle_state >;
52 
53 public:
55  protocol::channel_type const channel,
56  pmr::memory_resource& mem_res,
57  controller_interface& iface,
59  : channel_( channel )
60  , mem_res_( mem_res )
61  , iface_( channel_, iface, std::move( send_cb ) )
62  , tests_( mem_res )
63  {
64  auto* const i_state_ptr = std::get_if< initializing_state >( &state_ );
65  EMLABCPP_ASSERT( i_state_ptr != nullptr );
66  i_state_ptr->coro = initialize( mem_res );
67  }
68 
69  [[nodiscard]] constexpr protocol::channel_type get_channel() const
70  {
71  return channel_;
72  }
73 
74  [[nodiscard]] std::string_view suite_name() const
75  {
76  return { name_.begin(), name_.end() };
77  }
78 
79  [[nodiscard]] std::string_view suite_date() const
80  {
81  return { date_.begin(), date_.end() };
82  }
83 
84  [[nodiscard]] bool is_initializing() const
85  {
86  return std::holds_alternative< initializing_state >( state_ );
87  }
88 
89  [[nodiscard]] bool is_test_running() const
90  {
91  return std::holds_alternative< test_running_state >( state_ );
92  }
93 
94  [[nodiscard]] pmr::map< test_id, name_buffer > const& get_tests() const
95  {
96  return tests_;
97  }
98 
99  outcome on_msg( std::span< std::byte const > const data );
101 
102  void start_test( test_id const tid );
103 
104  void tick();
105 
106 private:
107  coroutine< void > initialize( pmr::memory_resource& mem_res );
108 
109  protocol::channel_type channel_;
110  states state_ = initializing_state{};
111 
112  std::reference_wrapper< pmr::memory_resource > mem_res_;
115  name_buffer name_;
116  name_buffer date_;
117  run_id rid_ = 0;
118 };
119 
120 } // namespace emlabcpp::testing
#define EMLABCPP_ASSERT(cond)
MIT License.
Definition: assert.h:38
Definition: memory_resource.h:33
Definition: controller_interface_adapter.h:33
Definition: controller_interface.h:33
Definition: controller.h:36
bool is_test_running() const
Definition: controller.h:89
std::string_view suite_name() const
Definition: controller.h:74
void start_test(test_id const tid)
pmr::map< test_id, name_buffer > const & get_tests() const
Definition: controller.h:94
bool is_initializing() const
Definition: controller.h:84
controller(protocol::channel_type const channel, pmr::memory_resource &mem_res, controller_interface &iface, controller_transmit_callback send_cb)
Definition: controller.h:54
std::string_view suite_date() const
Definition: controller.h:79
outcome on_msg(reactor_controller_variant const &)
constexpr protocol::channel_type get_channel() const
Definition: controller.h:69
outcome on_msg(std::span< std::byte const > const data)
std::map< Key, T, std::less< Key >, allocator< std::pair< Key const, T > > > map
Definition: aliases.h:62
uint16_t channel_type
Definition: multiplexer.h:35
MIT License.
Definition: base.h:37
uint16_t test_id
Definition: base.h:54
uint32_t run_id
Definition: base.h:53
typename protocol::traits_for< reactor_controller_group >::value_type reactor_controller_variant
Definition: protocol.h:202
constexpr pointer data() noexcept
Returns pointer to first item of the storage.
Definition: static_storage.h:108
Definition: static_function.h:109
outcome represents tristate resut of some operation, which can succeed, fail or produce an error.
Definition: outcome.h:49
Definition: base.h:75