Vivoe Lite 0.5.0
Lightweight GVA like HMI for military displays.
Loading...
Searching...
No Matches
tinyfsm.h
1/*
2 * TinyFSM - Tiny Finite State Machine Processor
3 *
4 * Copyright (c) 2012-2018 Axel Burri
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
14 * all 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
22 * THE SOFTWARE.
23 */
24
25/* ---------------------------------------------------------------------
26 * Version: 0.3.2
27 *
28 * API documentation: see "../doc/50-API.md"
29 *
30 * The official TinyFSM website is located at:
31 * https://digint.ch/tinyfsm/
32 *
33 * Author:
34 * Axel Burri <axel@tty0.ch>
35 * ---------------------------------------------------------------------
36 */
37
38#ifndef HMICORE_TINYFSM_H_
39#define HMICORE_TINYFSM_H_
40
41#ifndef TINYFSM_NOSTDLIB
42#include <type_traits>
43#endif
44
45// #include <iostream>
46// #define DBG(str) do { std::cerr << str << std::endl; } while( false )
47// DBG("*** dbg_example *** " << __PRETTY_FUNCTION__);
48
49namespace gva::tinyfsm {
50
51// --------------------------------------------------------------------------
52
53struct Event {};
54
55// ---------------------------------SRC_-----------------------------------------
56
57#ifdef TINYFSM_NOSTDLIB
58// remove dependency on standard library (silent fail!).
59// useful in conjunction with -nostdlib option, e.g. if your compiler
60// does not provide a standard library.
61// NOTE: this silently disables all static_assert() calls below!
62template <typename F, typename S>
63struct is_same_fsm {
64 static constexpr bool value = true;
65};
66#else
67// check if both fsm and state class share same fsmtype
68template <typename F, typename S>
69struct is_same_fsm : std::is_same<typename F::fsmtype, typename S::fsmtype> {};
70#endif
71
72template <typename S>
76 using value_type = S;
80 static S value;
81};
82
83template <typename S>
85
86// --------------------------------------------------------------------------
87
88template <typename F>
89class Fsm {
90 public:
92 using fsmtype = Fsm<F>;
94 using state_ptr_t = F *;
97
99 template <typename S>
100 static constexpr S &state(void) {
101 static_assert(is_same_fsm<F, S>::value, "accessing state of different state machine");
103 }
104
112 template <typename S>
113 static constexpr bool is_in_state(void) {
114 static_assert(is_same_fsm<F, S>::value, "accessing state of different state machine");
116 }
117
119 public:
120 // explicitely specialized in FSM_INITIAL_STATE macro
121 static void set_initial_state();
122
124 static void Reset() {}
125
127 static void enter() { current_state_ptr->entry(); }
128
130 static void start() {
132 enter();
133 }
134
141 template <typename E>
142 static void dispatch(E const &event) {
143 current_state_ptr->react(event);
144 }
145
147 protected:
153 template <typename S>
154 void transit(void) {
155 static_assert(is_same_fsm<F, S>::value, "transit to different state machine");
156 current_state_ptr->exit();
158 current_state_ptr->entry();
159 }
160
168 template <typename S, typename ActionFunction>
169 void transit(ActionFunction action_function) {
170 static_assert(is_same_fsm<F, S>::value, "transit to different state machine");
171 current_state_ptr->exit();
172 // NOTE: we get into deep trouble if the action_function sends a new event.
173 // TODO(ross): implement a mechanism to check for reentrancy
174 action_function();
176 current_state_ptr->entry();
177 }
178
188 template <typename S, typename ActionFunction, typename ConditionFunction>
189 void transit(ActionFunction action_function, ConditionFunction condition_function) {
190 if (condition_function()) {
192 }
193 }
194};
195
196template <typename F>
198
199// --------------------------------------------------------------------------
200
201template <typename... FF>
202struct FsmList;
203
204template <>
206struct FsmList<> {
208 static void set_initial_state() {}
210 static void enter() {}
212 template <typename E>
213 static void dispatch(E const &) {}
214};
215
216template <typename F, typename... FF>
218struct FsmList<F, FF...> {
221
226 static void set_initial_state() {
227 fsmtype::set_initial_state();
229 }
230
235 static void Reset() {
236 F::Reset();
238 }
239
244 static void enter() {
245 fsmtype::enter();
247 }
248
253 static void start() {
254 set_initial_state();
255 enter();
256 }
257
264 template <typename E>
265 static void dispatch(E const &event) {
266 fsmtype::template dispatch<E>(event);
267 FsmList<FF...>::template dispatch<E>(event);
268 }
269};
270
271// --------------------------------------------------------------------------
272
273template <typename... SS>
275template <>
277struct StateList<> {
279 static void Reset() {}
280};
281template <typename S, typename... SS>
283struct StateList<S, SS...> {
285 static void Reset() {
288 }
289};
290
291// --------------------------------------------------------------------------
292
293template <typename F>
297 virtual void entry(void) {}
299 void exit(void) {}
300};
301
302template <typename F>
305 // input actions are modeled in react():
306 // - conditional dependent of event type or payload
307 // - transit<>(ActionFunction)VIEW_GVA_H
309 void entry(void) {}
311 void exit(void) {}
312};
313
314} // namespace gva::tinyfsm
315
316#define FSM_INITIAL_STATE(_FSM, _STATE) \
317 namespace tinyfsm { \
318 template <> \
319 void Fsm<_FSM>::set_initial_state(void) { \
320 current_state_ptr = &_state_instance<_STATE>::value; \
321 } \
322 }
323
324#endif // HMICORE_TINYFSM_H_
Definition tinyfsm.h:89
static void Reset()
Reset state machine.
Definition tinyfsm.h:124
static void enter()
Enter state.
Definition tinyfsm.h:127
static void set_initial_state()
state machine functions
static constexpr S & state(void)
public, leaving ability to access state instance (e.g. on Reset)
Definition tinyfsm.h:100
static constexpr bool is_in_state(void)
Check if in state.
Definition tinyfsm.h:113
static void start()
start state
Definition tinyfsm.h:130
void transit(void)
state transition functions
Definition tinyfsm.h:154
F * state_ptr_t
Current state pointer.
Definition tinyfsm.h:94
void transit(ActionFunction action_function, ConditionFunction condition_function)
Transit to new state.
Definition tinyfsm.h:189
static state_ptr_t current_state_ptr
Current state pointer.
Definition tinyfsm.h:96
static void dispatch(E const &event)
Dispatch a new state.
Definition tinyfsm.h:142
void transit(ActionFunction action_function)
Transition to new state.
Definition tinyfsm.h:169
Definition tinyfsm.h:53
static void Reset()
Reset.
Definition tinyfsm.h:235
static void start()
Start.
Definition tinyfsm.h:253
static void dispatch(E const &event)
Dispatch.
Definition tinyfsm.h:265
static void set_initial_state()
Set the initial state object.
Definition tinyfsm.h:226
static void enter()
Enter.
Definition tinyfsm.h:244
static void set_initial_state()
Set the inital state.
Definition tinyfsm.h:208
static void dispatch(E const &)
Dispatch FSM.
Definition tinyfsm.h:213
static void enter()
Enter state function.
Definition tinyfsm.h:210
Definition tinyfsm.h:202
Mealy machine.
Definition tinyfsm.h:304
void exit(void)
no exit actions
Definition tinyfsm.h:311
void entry(void)
no entry actions
Definition tinyfsm.h:309
Moore machine.
Definition tinyfsm.h:295
virtual void entry(void)
entry actions in some states
Definition tinyfsm.h:297
void exit(void)
no exit actions
Definition tinyfsm.h:299
static void Reset()
Reset state.
Definition tinyfsm.h:285
static void Reset()
Reset state list.
Definition tinyfsm.h:279
Definition tinyfsm.h:274
State.
Definition tinyfsm.h:74
S value_type
State instance.
Definition tinyfsm.h:76
static S value
State instance.
Definition tinyfsm.h:80
Definition tinyfsm.h:69