StateMachine
Loading...
Searching...
No Matches
StateMachine3.hpp
Go to the documentation of this file.
1/*
2 MIT License
3
4 Copyright (c) 2024 George Fotopoulos
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23*/
24
25#pragma once
26
27#include <algorithm>
28#include <functional>
29#include <tuple>
30#include <unordered_map>
31#include <utility>
32#include <vector>
33
34using guard_t = std::function<bool()>;
35
36using action_t = std::function<void()>;
37
38using enter_action_t = std::function<void()>;
39
40using leave_action_t = std::function<void()>;
41
42template<typename state_t>
43using enter_actions_t = std::unordered_map<state_t, enter_action_t>;
44
45template<typename state_t>
46using leave_actions_t = std::unordered_map<state_t, leave_action_t>;
47
48template<typename state_t, typename event_t>
49using transition_t = std::pair<std::pair<state_t, event_t>, std::tuple<guard_t, action_t, state_t>>;
50
51template<typename state_t, typename event_t>
52using transition_table_t = std::vector<transition_t<state_t, event_t>>;
53
54template<typename state_t, typename event_t>
55class state_machine_t {
56public:
57 state_machine_t() = default;
58
59 state_machine_t(const state_t &state, transition_table_t<state_t, event_t> transition_table) : m_state(state), m_transition_table(std::move(transition_table)) {}
60
61 bool handle_event(const event_t &event) {
62 const auto it = std::find_if(m_transition_table.begin(), m_transition_table.end(), [&](const transition_t<state_t, event_t> &transition) {
63 return transition.first.first == m_state && transition.first.second == event;
64 });
65 if (it != m_transition_table.end()) {
66 const guard_t &guard = std::get<0>(it->second);
67 const action_t &action = std::get<1>(it->second);
68 const state_t &state = std::get<2>(it->second);
69 if (guard()) {
70 const auto it1 = m_leave_actions.find(m_state);
71 if (it1 != m_leave_actions.end()) {
72 it1->second();
73 }
74 m_state = state;
75 action();
76 const auto it2 = m_enter_actions.find(m_state);
77 if (it2 != m_enter_actions.end()) {
78 it2->second();
79 }
80 return true;
81 }
82 return true;
83 }
84 return false;
85 }
86
87 void set_state(const state_t &state) {
88 m_state = state;
89 }
90
92 m_transition_table = transition_table;
93 }
94
95 void set_enter_action(const state_t &state, const enter_action_t &enter_action) {
96 m_enter_actions[state] = enter_action;
97 }
98
99 void set_leave_action(const state_t &state, const leave_action_t &leave_action) {
100 m_leave_actions[state] = leave_action;
101 }
102
103 state_t get_state() const {
104 return m_state;
105 }
106
108 return m_transition_table;
109 }
110
112 return m_enter_actions;
113 }
114
116 return m_leave_actions;
117 }
118
119private:
120 state_t m_state;
121 transition_table_t<state_t, event_t> m_transition_table;
122 enter_actions_t<state_t> m_enter_actions;
123 leave_actions_t<state_t> m_leave_actions;
124};
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
std::function< bool(const data_t &)> guard_t
Definition: StateMachine4.hpp:36
std::function< void(const data_t &)> action_t
Definition: StateMachine4.hpp:39
Definition: StateMachine1.hpp:42
void set_enter_action(const state_t &state, const enter_action_t &enter_action)
Definition: StateMachine3.hpp:95
void set_transition_table(const transition_table_t< state_t, event_t > &transition_table)
Definition: StateMachine3.hpp:91
void set_state(const state_t &state)
Definition: StateMachine3.hpp:87
transition_table_t< state_t, event_t > get_transition_table() const
Definition: StateMachine3.hpp:107
state_machine_t()=default
state_machine_t(const state_t &state, transition_table_t< state_t, event_t > transition_table)
Definition: StateMachine3.hpp:59
leave_actions_t< state_t > get_leave_actions() const
Definition: StateMachine3.hpp:115
void set_leave_action(const state_t &state, const leave_action_t &leave_action)
Definition: StateMachine3.hpp:99
enter_actions_t< state_t > get_enter_actions() const
Definition: StateMachine3.hpp:111
state_t get_state() const
Definition: StateMachine3.hpp:103
bool handle_event(const event_t &event)
Definition: StateMachine3.hpp:61