summaryrefslogtreecommitdiff
path: root/lib/tinyfsm/doc/50-API.md
blob: 44fcf17249adae4fb355b197d9f9b955403363d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
API Reference
=============

`#include <tinyfsm.hpp>`


Class Diagram
-------------
                                           .......
    +--------------------------------------:  T  :
    | tinyfsm::FsmList                     :.....:
    +-----------------------------------------|
    | [+] set_initial_state()     <<static>>  |
    | [+] reset()                 <<static>>  |
    | [+] enter()                 <<static>>  |
    | [+] start()                 <<static>>  |
    | [+] dispatch(Event)         <<static>>  |
    +-----------------------------------------+


                                           .......
    +--------------------------------------:  T  :
    | tinyfsm::Fsm                         :.....:
    +-----------------------------------------|
    | [+] state<S>()              <<static>>  |
    | [+] set_initial_state()     <<static>>  |
    | [+] reset()                 <<static>>  |
    | [+] enter()                 <<static>>  |
    | [+] start()                 <<static>>  |
    | [+] dispatch(Event)         <<static>>  |
    | [#] transit<S>()                        |
    | [#] transit<S>(Action)                  |
    | [#] transit<S>(Action, Condition)       |
    +-----------------------------------------+
                         #
                         |
                         |
              +---------------------+
              | MyFSM               |
              +---------------------+
              | [+] entry()         |
              | [+] exit()          |
              | [+] react(EventX)   |
              | [+] react(EventY)   |
              | ...                 |
              +---------------------+
                         #
                         |
           +-------------+-------------+
           |             |             |
      +---------+   +---------+   +---------+
      | State_A |   | State_B |   | ...     |
      +---------+   +---------+   +---------+


    [#]  protected
    [+]  public
    [-]  private


template< typename F > class Fsm
--------------------------------

### State Machine Functions

 * `template< typename S > static constexpr S & state(void)`

   Returns a reference to a (implicitly instantiated) state S. Allows
   low-level access to all states;


 * `static void set_initial_state(void)`

   Function prototype, must be defined (explicit template
   specialization) for every state machine class (e.g. by using the
   `FSM_INITIAL_STATE(fsm, state`) macro). Sets current state to
   initial (start) state.


 * `static void reset(void)`

   Empty function, can be overridden by state machine class in order to
   perform custom initialization (e.g. set static state machine
   variables, or reset states using `StateList<MyStates...>::reset()`)
   or directly via the `state<MyState>()` instance).

   Note that this function is NOT called on start().

   See example: `/examples/api/resetting_switch.cpp`

 * `static void enter(void)`

   Helper function, usually not needed to be used directly:
   calls entry() function of current state.


 * `static void start()`

   Sets the initial (start) state and calls its entry() function.


 * `template< typename E > static void dispatch(E const &)`

   Dispatch an event to the current state of this state machine.


### State Transition Functions

 * `template< typename S > void transit(void)`

   Transit to a new state:

   1. Call exit() function on current state
   2. Set new current state to S
   3. Call entry() function on new state


 * `template< typename S, typename ActionFunction > void transit(ActionFunction)`

   Transit to a new state, with action function:

   1. Call exit() function on current state
   2. Call ActionFunction
   3. Set new current state to S
   4. Call entry() function on new state


 * `template< typename S, typename ActionFunction, typename ConditionFunction > void transit(ActionFunction, ConditionFunction)`

   Transit to a new state only if ConditionFunction returns true.
   Shortcut for: `if(ConditionFunction()) transit<S>(ActionFunction);`.


### Derived Classes

#### template< typename F > class MooreMachine

Moore state machines have entry actions, but no exit actions:

 * `virtual void entry(void) { }`

   Entry action, not enforcing. Can be enforced by declaring pure
   virtual: `virtual void entry(void) = 0`

 * `void exit(void) { }`

   No exit actions.

See example: `/examples/api/more_machine.cpp`

#### template< typename F > class MealyMachine

Mealy state machines do not have entry/exit actions:

 * `void entry(void) { }`

   No entry actions.

 * `void exit(void) { }`

   No exit actions.

*Input actions* are modeled in react(), conditional dependent of event
type or payload and using `transit<>(ActionFunction)`.

See example: `/examples/api/mealy_machine.cpp`


template< typename... FF > struct FsmList
-----------------------------------------

 * `static void set_initial_state(void)`

   Calls set_initial_state() on all state machines in the list.


 * `static void reset()`

   Calls reset() on all state machines in the list.


 * `static void enter()`

   Calls enter() on all state machines in the list.


 * `static void start()`

   Sets the initial (start) state for all state machines in list, then
   call all entry() functions.


 * `template< typename E > static void dispatch(E const &)`

   Dispatch an event to the current state of all the state machines in
   the list.


template< typename... SS > struct StateList
-------------------------------------------

 * `static void reset(void)`

   Re-instantiate all states in the list, using copy-constructor.

   See example: `/examples/api/resetting_switch.cpp`