16 #include "../../base/type_traits.h" 18 #include "../transitions.h" 19 #include "../states.h" 20 #include "../contexts.h" 31 template<
class State,
class E>
32 void begin_event_handling () {}
33 void end_event_handling(
bool) {}
41 template<
class States,
class Context,
class Tracer = NullTracer>
45 using type_list =
typename States::type_list;
64 substates_.template create<T>(context_, tracer_);
67 emplace_state<T>(context_);
75 auto dispatch(E
const& e) {
81 result = substate_dispatch(substate, e);
85 if (result ==
false) {
86 states_.visit([
this, &e, &result](
auto &
state) {
87 tracer_.template begin_event_handling<
88 std::remove_reference_t<decltype(state)>,
89 std::remove_reference_t<decltype(e)>>();
91 result = handle(
state, e);
92 tracer_.end_event_handling(result);
101 return states_.template is_in<S>();
110 template<
class T,
class C>
111 void emplace_state(C &c) {
112 if constexpr (std::is_constructible_v<T, C&>) {
113 states_.template enter<T>(c);
115 states_.template enter<T>();
119 template<
class T,
class... C>
120 static constexpr
bool is_constructible_by_one_of() {
121 return (std::is_constructible_v<T, C&> || ...);
124 template<
class T,
class U,
class... C>
126 if constexpr (std::is_constructible_v<T, U&>) {
127 states_.template enter<T>(ctx.template get<U>());
131 template<
class T,
class... C>
134 states_.template enter<T>(ctx);
136 if constexpr(is_constructible_by_one_of<T, C...>()) {
137 (try_emplace_state<T, C, C...>(ctx), ...);
139 states_.template enter<T>();
145 if constexpr (States::count > 0) {
151 template<
class SS,
class E>
152 bool substate_dispatch(SS& substate, E
const &e) {
153 return substate.dispatch(e);
157 bool substate_dispatch(std::monostate, E
const&) {
161 template<
class S,
class E>
162 auto handle(S &
state, E
const& e) -> std::enable_if_t<detail::can_handle_event<S, E>::value,
bool> {
163 if (handle_result(state.handle(e))) {
170 template<
class S,
class E>
171 auto handle(S &state, E
const& e) -> std::enable_if_t<detail::can_handle_event_with_context<S, E, Context>::value,
bool> {
172 if (handle_result(state.handle(e, context_))) {
179 template<
class S,
class E>
180 auto handle(S&, E
const& ) -> std::enable_if_t<
190 handle_transition(t, std::make_index_sequence<
sizeof...(T)>{});
197 template<
class Transition, std::size_t... I>
198 void handle_transition(Transition trans, std::index_sequence<I...>) {
199 (handle_transition_impl<I>(trans), ...);
202 template<std::
size_t I,
class Transition>
203 void handle_transition_impl(Transition trans) {
204 if (trans.idx == I) {
205 using transition_type_list =
typename Transition::list;
207 using type_at_index = mpl::type_list_element_t<I, transition_type_list>;
210 if (mpl::type_list_contains_v<type_list, type_at_index>) {
212 enter<type_at_index>();
Get first type in provided type_list<>
Definition: type_traits.h:414
Definition: testJsonContexts.cpp:18
Manages a set of state, creates, destroys and pass events to a proper state.
Definition: state_manager.h:42
Definition: handle_result.h:14
Denotes a state.
Definition: states.h:61
bool is_transition() const
Check if this object is a transition type.
Definition: transitions.h:92
Definition: state_manager.h:30
Definition: handle_result.h:19
Event handler return type.
Definition: transitions.h:28
A set of references to different context types.
Definition: contexts.h:26
bool is_handled() const
Check if the event was handled.
Definition: transitions.h:102
Wrapper around std::variant, which is current implementation of states container. ...
Definition: state_container.h:30