StateMachine
Loading...
Searching...
No Matches
StateMachine1.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 <utility>
31#include <vector>
32
33using action_t = std::function<void()>;
34
35template<typename state_t, typename event_t>
36using transition_t = std::pair<std::pair<state_t, event_t>, std::tuple<action_t, state_t>>;
37
38template<typename state_t, typename event_t>
39using transition_table_t = std::vector<transition_t<state_t, event_t>>;
40
41template<typename state_t, typename event_t>
43public:
44 state_machine_t() = default;
45
46 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)) {}
47
48 bool handle_event(const event_t &event) {
49 const auto it = std::find_if(m_transition_table.begin(), m_transition_table.end(), [&](const transition_t<state_t, event_t> &transition) {
50 return transition.first.first == m_state && transition.first.second == event;
51 });
52 if (it != m_transition_table.end()) {
53 const action_t &action = std::get<0>(it->second);
54 const state_t &state = std::get<1>(it->second);
55 m_state = state;
56 action();
57 return true;
58 }
59 return false;
60 }
61
62 state_t get_state() const {
63 return m_state;
64 }
65
67 return m_transition_table;
68 }
69
70 void set_state(const state_t &state) {
71 m_state = state;
72 }
73
75 m_transition_table = transition_table;
76 }
77
78private:
79 state_t m_state;
80 transition_table_t<state_t, event_t> m_transition_table;
81};
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::vector< transition_t< state_t, event_t, data_t > > transition_table_t
Definition: StateMachine4.hpp:57
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< void(const data_t &)> action_t
Definition: StateMachine4.hpp:39
Definition: StateMachine1.hpp:42
void set_transition_table(const transition_table_t< state_t, event_t > &transition_table)
Definition: StateMachine1.hpp:74
void set_state(const state_t &state)
Definition: StateMachine1.hpp:70
transition_table_t< state_t, event_t > get_transition_table() const
Definition: StateMachine1.hpp:66
state_machine_t()=default
state_machine_t(const state_t &state, transition_table_t< state_t, event_t > transition_table)
Definition: StateMachine1.hpp:46
state_t get_state() const
Definition: StateMachine1.hpp:62
bool handle_event(const event_t &event)
Definition: StateMachine1.hpp:48