Giter Club home page Giter Club logo

cppfsm's Introduction

Finite State Machine for C++

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

Documentation

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.

Usage

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.

Example

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;
fsm.add_transitions({
//  from state ,to state  ,triggers        ,guard                    ,action
  { States::A  ,States::B ,Triggers::Exec  ,[&]{return count == 1;}  ,[&]{count++;} },
});
fsm.execute(Triggers::Exec);
assert(count == 2);
assert(fsm.state() == States::B);

See the tests for more examples.

Stability

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

Tests can be run with

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

Contributions

Contributions are welcome. Please use the Github issue tracker.

cppfsm's People

Contributors

eglimi avatar mjopenglsdl avatar golxzn avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.