/state-machine

C++14 header-only library for implementing Finite-State Machines.

Primary LanguageC++BSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

state-machine License

C++14 header-only library for implementing Finite-State Machines.

Interface

The library provides the following three interfaces for building a state machine: State, Event and the StateMachine itself.

  • States represent the possible configurations of a StateMachine.

  • Events represent the possible transitions between States.

  • Each Event defines the destination State that the StateMachine reaches if the Event itself is triggered.

  • Events can be Controllable and NonControllable:

    • Controllable events are executed by the user;

    • NonControllable events are events that are not under the direct control of the user (for instance failures or timeouts).

  • Each State declares the ControllableEvents that are available using the State::attach<ControllableEvent>() method, and defines the logic for triggering NonControllableEvents in the State::update() method.

Build

Use a modern C++ compiler with support of the C++14 standard. Code was developed using g++ 6.3.0 20170406.

Run in the project folder:

mkdir -p build && (cd build && cmake .. && make -j$(nproc))

Examples

In order to validate and demonstrate the usage of the library, three examples have been developed: a simple On/Off switch, an On/Off switch with an automatic reset timer, and a Moka coffee brewer.

Unit tests for these examples are executed automatically at the end of the build.

Controllable events and states are showed in black, while NonControllable events and states are shown in red.

OnOffSwitch (implementation, tests)

OnOffSwitch

TimedSwitch (implementation, tests)

TimedSwitch

Moka