Show HN: CXXStateTree – A modern C++ library for hierarchical state machines

3 hours ago 2

CXXStateTree Logo

Modern Hierarchical State Machine for C++20

  • 🔧 Fluent builder API with lambda-based DSL
  • ⚡ Fast runtime performance with zero heap allocation
  • 🛡️ Optional guards and actions for transitions
  • 🔄 Event-based state transitions
  • 🧪 Google Test integration
  • 📈 Code coverage with Codecov
  • 🌳 Designed for extensibility: nested states, DOT export coming soon

#include <iostream> #include "CXXStateTree/StateTree.hpp" using namespace CXXStateTree; int main() { auto machine = Builder() .initial("Idle") .state("Idle", [](State& s) { s.on("Start", "Running", nullptr, []() { std::cout << "Idle -> Running" << std::endl; }); }) .state("Running", [](State& s) { s.on("Stop", "Idle", nullptr, []() { std::cout << "Running -> Idle" << std::endl; }); }) .build(); machine.send("Start"); machine.send("Stop"); }

cmake -S . -B build -DENABLE_COVERAGE=ON cmake --build build cd build && ctest

  • C++20 compiler (GCC >= 10, Clang >= 11, MSVC >= 2019)
  • GoogleTest (auto-downloaded via CMake)

codecov


CXXStateTree/ ├── include/CXXStateTree/ # Public header-only API ├── examples/ # Usage examples ├── tests/ # Google Test suite ├── CMakeLists.txt # Project build └── .github/workflows/ci.yml # GitHub Actions CI

MPL2.0 License — see LICENSE for details.


  • Nested (hierarchical) state support
  • DOT/Graphviz state diagram export
  • Transitions with context/parameters
Completed Milestone Features
✔️ v0.1.0 Basic state machine with send(), transitions, and state tracking
✔️ v0.2.0 Guards and actions
✔️ v0.3.0 Nested (hierarchical) states
✔️ v0.4.0 Graphviz export
📝 v0.5.0 Coroutine/async support (optional)
📝 v1.0.0 Full unit test coverage, benchmarks, and docs

Issues, feature suggestions, and PRs are welcome!

Read Entire Article