30#include <unordered_map>
35template<
typename state_t,
typename event_t,
typename data_t>
36using guard_t = std::function<bool(
const data_t &)>;
38template<
typename state_t,
typename event_t,
typename data_t>
39using action_t = std::function<void(
const data_t &)>;
41template<
typename state_t,
typename data_t>
44template<
typename state_t,
typename data_t>
47template<
typename state_t,
typename data_t>
48using enter_actions_t = std::unordered_map<state_t, enter_action_t<state_t, data_t>>;
50template<
typename state_t,
typename data_t>
51using leave_actions_t = std::unordered_map<state_t, leave_action_t<state_t, data_t>>;
53template<
typename state_t,
typename event_t,
typename data_t>
56template<
typename state_t,
typename event_t,
typename data_t>
59template<
typename state_t,
typename event_t,
typename data_t>
65 : m_state(state), m_transition_table(std::move(transition_table)) {}
69 return transition.first.first == m_state && transition.first.second == event;
71 if (it != m_transition_table.end()) {
72 const auto &[guard, action, state] = it->second;
74 const auto it1 = m_leave_actions.find(m_state);
75 if (it1 != m_leave_actions.end()) {
80 const auto it2 = m_enter_actions.find(m_state);
81 if (it2 != m_enter_actions.end()) {
96 m_transition_table = transition_table;
100 m_enter_actions[state] = enter_action;
104 m_leave_actions[state] = leave_action;
112 return m_transition_table;
116 return m_enter_actions;
120 return m_leave_actions;
std::vector< transition_t< state_t, event_t > > transition_table_t
Definition: StateMachine1.hpp:39
std::function< void()> action_t
Definition: StateMachine1.hpp:33
std::pair< std::pair< state_t, event_t >, std::tuple< action_t, state_t > > transition_t
Definition: StateMachine1.hpp:36
std::function< void()> enter_action_t
Definition: StateMachine2.hpp:36
std::unordered_map< state_t, leave_action_t > leave_actions_t
Definition: StateMachine2.hpp:44
std::unordered_map< state_t, enter_action_t > enter_actions_t
Definition: StateMachine2.hpp:41
std::function< void()> leave_action_t
Definition: StateMachine2.hpp:38
std::function< bool()> guard_t
Definition: StateMachine3.hpp:34
std::function< void(const data_t &)> enter_action_t
Definition: StateMachine4.hpp:42
std::unordered_map< state_t, enter_action_t< state_t, data_t > > enter_actions_t
Definition: StateMachine4.hpp:48
std::vector< transition_t< state_t, event_t, data_t > > transition_table_t
Definition: StateMachine4.hpp:57
std::function< void(const data_t &)> leave_action_t
Definition: StateMachine4.hpp:45
std::unordered_map< state_t, leave_action_t< state_t, data_t > > leave_actions_t
Definition: StateMachine4.hpp:51
std::pair< std::pair< state_t, event_t >, std::tuple< guard_t< state_t, event_t, data_t >, action_t< state_t, event_t, data_t >, state_t > > transition_t
Definition: StateMachine4.hpp:54
Definition: StateMachine1.hpp:42
state_machine_t(const state_t &state, transition_table_t< state_t, event_t, data_t > transition_table)
Definition: StateMachine4.hpp:64
leave_actions_t< state_t, data_t > get_leave_actions() const
Definition: StateMachine4.hpp:119
bool handle_event(const event_t &event, const data_t &data)
Definition: StateMachine4.hpp:67
void set_state(const state_t &state)
Definition: StateMachine4.hpp:91
void set_leave_action(const state_t &state, const leave_action_t< state_t, data_t > &leave_action)
Definition: StateMachine4.hpp:103
state_machine_t()=default
void set_enter_action(const state_t &state, const enter_action_t< state_t, data_t > &enter_action)
Definition: StateMachine4.hpp:99
void set_transition_table(const transition_table_t< state_t, event_t, data_t > &transition_table)
Definition: StateMachine4.hpp:95
enter_actions_t< state_t, data_t > get_enter_actions() const
Definition: StateMachine4.hpp:115
state_t get_state() const
Definition: StateMachine4.hpp:107
transition_table_t< state_t, event_t, data_t > get_transition_table() const
Definition: StateMachine4.hpp:111