A simple, generic, header-only state machine implementation for C++.
Branch: master
Clone or download
eglimi Merge pull request #2 from mjopenglsdl/master
Fix the examples in the documentation.
Latest commit bdf9e91 May 26, 2017
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore Initial commit Dec 18, 2014
fsm.h Merge pull request #2 from mjopenglsdl/master May 26, 2017


Finite State Machine for C++

A simple, generic, header-only state machine implementation for C++.


Please see the documentation in fsm.h for detailed documentation about the implemented features and semantics.

Some more information about this component can also be found on our website. See article 1 for the motivation, and article 2 for the implementation.


The implementation is contained in a single header file, fsm.h. Simply copy the file to a convenient place in your project and include it.


As an example, consider the state machine below. It starts in state A. When it receives the exec trigger, it checks that the count variable is 1, increments it, and changes to state B.

       +------+  Exec[count=1] / count++    +------+
  o--->|  A   |---------------------------->|  B   |
       +------+                             +------+

The implementation of this state machine is done in a declarative way.

int count = 1;
enum class States { A, B };
enum class Triggers { Exec };
FSM::Fsm<States, States::A, Triggers> fsm;
//  from state ,to state  ,triggers        ,guard                    ,action
  { States::A  ,States::B ,Triggers::Exec  ,[&]{return count == 1;}  ,[&]{count++;} },
assert(count == 2);
assert(fsm.state() == States::B);

See the tests for more examples.


The implementation is already in use in different commercial applications. We have not found any issues so far. Please report any problems you find.


Tests can be run with

cd tests
g++ -std=c++11 -Wall -o tests fsm_test.cpp


Contributions are welcome. Please use the Github issue tracker.