Skip to content

andrew-gresyk/FFSM

Repository files navigation

Standard License: MIT Build status Build Status

FFSM: Flat Finite State Machine Framework

Header-only flat FSM framework, made as an example to illustrate twitter thread on #cpp #state_machine #software_design.

Compiler Support

  • Visual Studio 14, 15, 16
  • GCC 7, 8
  • Clang 5, 6, 7

Basic Usage

  1. Include FFSM header:
#include <ffsm/machine.hpp>
  1. Define interface class between the state machine and its host (also ok to use the host object itself):
struct Context {
    // ...
};
  1. (Optional, recommended) Typedef ffsm::Machine for convenience:
using M = ffsm::Machine<Context>;
  1. (Optional) Forward declare transition target states:
struct Second;
  1. Define states inheriting them from M::State:
struct First
    : M::State
{
    virtual void update(Control& control) override {
        control.changeTo<Second>();
    }
};

struct Second
    : M::State
{
    virtual void update(Control& control) override {
        control.changeTo<First>();
    }
};

struct Done : M::State {};
  1. Declare state machine structure:
using FSM = M::Host<First,
                    Second,
                    Done>;
  1. Write the client code to use your new state machine:
int main() {
  1. Create context and state machine instances:
    Context context;
    FSM fsm(context);
  1. Kick off periodic updates until it's done:
    while (!fsm.isActive<Done>())
        fsm.update();

    return 0;
}

Get Updates