Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/eglimi/cppfsm
A simple, generic, header-only state machine implementation for C++.
https://github.com/eglimi/cppfsm
c-plus-plus state-machine
Last synced: 3 months ago
JSON representation
A simple, generic, header-only state machine implementation for C++.
- Host: GitHub
- URL: https://github.com/eglimi/cppfsm
- Owner: eglimi
- License: mit
- Created: 2014-12-18T09:33:47.000Z (almost 10 years ago)
- Default Branch: main
- Last Pushed: 2022-01-29T18:25:30.000Z (almost 3 years ago)
- Last Synced: 2024-05-02T20:13:53.233Z (6 months ago)
- Topics: c-plus-plus, state-machine
- Language: C++
- Size: 261 KB
- Stars: 58
- Watchers: 5
- Forks: 16
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- AwesomeCppGameDev - cppfsm - only state machine implementation for C++. (GameProgramming)
README
Finite State Machine for C++
============================A simple, generic, header-only state machine implementation for C++.
Documentation
-------------Please see the documentation in [fsm.h](./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.
[article 1]: http://wisol.ch/w/articles/2015-01-04-state-machine-intro/
[article 2]: http://wisol.ch/w/articles/2015-03-27-state-machine-impl/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 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.