A lightweight, header‑only finite state machine (FSM) library written in modern C++20.
- Runtime, table‑driven FSM core (
fsm::runtime). - Guard predicates and entry/exit actions.
- Header‑only
INTERFACECMake target – easy to consume. - Dot graph (GraphViz) generation via
to_dot.
# Clone the repo
git clone https://github.com/aajll/fsm.git
cd fsm
# Configure & build (Release)
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build
# Run the tests
ctest --test-dir build --output-on-failureFor a detailed implementation and usage guide, see the Implementation Guide.
#include <fsm/runtime.hpp>
enum class Light { Red, Green, Yellow };
enum class Event { Timer };
struct Context { int counter = 0; };
int main() {
fsm::runtime<Light, Event, Context> sm(Light::Red);
sm.add_transition({ Light::Red, Event::Timer, Light::Green, nullptr,
[](Context& ctx){ ++ctx.counter; } });
sm.add_transition({ Light::Green, Event::Timer, Light::Yellow, nullptr,
[](Context& ctx){ ++ctx.counter; } });
sm.add_transition({ Light::Yellow, Event::Timer, Light::Red, nullptr,
[](Context& ctx){ ++ctx.counter; } });
Context ctx;
sm.dispatch(Event::Timer, ctx); // Red→Green
// ...
std::cout << sm.to_dot(); // visualise with GraphViz
}If no context is needed, you can omit the third template parameter:
fsm::runtime<Light, Event> sm(Light::Red);
sm.add_transition({ Light::Red, Event::Timer, Light::Green, nullptr, nullptr });
sm.dispatch(Event::Timer); // No context object requiredThe library is documented with Doxygen. After building, run:
cmake --build build --target doc # generates HTML in build/doc/html