PureFSM
PureFSM

C++ Finite State Machine, written without dynamic memory allocation and RTTI, only with template metaprogramming. Uses C++17.

Actually this project is a fork of this project, which is inspired by this lecture.

Finite-state machine theory

The key feature of the project is that transitions (edges between the vertices of a graph) are stored in a table. This gives much more readability than the other finite state machine implementations. Table transition representation is close to a State/Event table representation.

Transition consists of a source state, which is a current state in a given discrete moment of time, target state and an event, which will lead to state change from source to target. Transition has an action, that is performed when state is changed. Actions allows to easily implement Miley automatons.

Also transition contains a guard, which is an additional condition to change a state. It is seems as a logical AND between an event and a guard: if Event AND Guard then perform a transition. PureFSM allows to specify a sets of guards using any_of and a negated sets using none_of. Negated set of guards means that the transition will be performed, if the current guard is not appeared in this set.

Suppose that we have the following code snippet:

State Machine.
Definition: fsm.hpp:156
Determines the behaviour of an FSM.
Definition: fsm.hpp:91
Represents a transition between two states.
Definition: fsm.hpp:31

This is a state machine with the only one transition: from StateA to StateB with event Event and guard Guard. This transition will be performed only if the current state of a machine is StateA, current guard is Guard and machine got an event Event. When the transition will be performed, Action will be called. You can also pass arguments to an action. If an action is a functor and it is can be called with the given arguments, it will be called:

machine.event<Event>(args...);
void event(Args &&... args)
Pass an event to a State Machine.
Definition: fsm.hpp:231

Not only transitions can have an action, but the states too. So you can implement Moore automatons with PureFSM. For to call a state action, you should use method pure::state_machine::action:

machine.action(args...);
void action(Args &&... args)
Calls a state action.
Definition: fsm.hpp:257

If the current state type is callable and it can be called with arguments args..., it will be called.

none guard matches with any guard.

Cloning and Building

git clone --recurse-submodules https://github.com/edKotinsky/purefsm.git
cd purefsm

Tests

cmake -S . -B build/ -D PUREFSM_TESTING=ON
cmake --build build/

Documentation

cmake -S . -B build/ -D PUREFSM_DOC=ON
cmake --build build/

License

MIT