An open API service indexing awesome lists of open source software.

https://github.com/zigrazor/cxxstatetree

A C++ Header-Only Hierarchical State Tree Library
https://github.com/zigrazor/cxxstatetree

cpp cpp-library cpp20 gaming header-only-library state-machine statetrees

Last synced: 2 months ago
JSON representation

A C++ Header-Only Hierarchical State Tree Library

Awesome Lists containing this project

README

          


CXXStateTree Logo

CXXStateTree


Modern Hierarchical State Machine for C++20

## ๐Ÿš€ Features

* ๐Ÿ”ง 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
* ๐Ÿ”ง Deployed as Shared Library and as Single Header-Only library

---

## ๐Ÿ› ๏ธ Quick Example

```cpp
#include
#include "CXXStateTree/StateTree.hpp"

using namespace CXXStateTree;

int main() {
auto machine = StateTree::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");
}
```

---

## ๐Ÿ› ๏ธ Building Shared Library

```bash
cmake -S . -B build
cmake --build build
```

After these command the Shared Library can be found in `build` directory

Please Note: in future release cmake will have the ability to install the library automatically, for now it is necessary to do it manually

---

## ๐Ÿ› ๏ธ Building Single Header-Only Library

```bash
cmake -S . -B build -DENABLE_SINGLE_HEADER=ON
cmake --build build
```

After these command the Single Header-Only Library can be found in `single_include` directory with the name CXXStateTree.hpp

Please Note: in future release cmake will have the ability to install the library automatically, for now it is necessary to do it manually

---

## ๐Ÿงช Running Tests

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

---

## ๐Ÿ“ฆ Dependencies

* C++20 compiler (GCC >= 10, Clang >= 11, MSVC >= 2019)
* [GoogleTest](https://github.com/google/googletest) (auto-downloaded via CMake)

---

## ๐Ÿ“ˆ Code Coverage

[![codecov](https://codecov.io/gh/ZigRazor/CXXStateTree/graph/badge.svg?token=A1RP2ZDGI6)](https://codecov.io/gh/ZigRazor/CXXStateTree)

---

## ๐Ÿ“‚ Directory Structure

```
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
```

---

## ๐Ÿ“‹ License

MPL2.0 License โ€” see [LICENSE](LICENSE) for details.

---

## ๐ŸŒŸ Coming Soon

* Nested (hierarchical) state support
* DOT/Graphviz state diagram export
* Transitions with context/parameters

| Completed | Milestone | Features |
| :-: | :--------- | ------------------------------------------------------------------ |
| :heavy_check_mark: | v0.1.0 | Basic state machine with `send()`, transitions, and state tracking |
| :heavy_check_mark: | v0.2.0 | Guards and actions |
| :heavy_check_mark: | v0.3.0 | Nested (hierarchical) states |
| :heavy_check_mark: | v0.4.0 | Graphviz export |
| :memo: | v0.5.0 | Coroutine/async support (optional) |
| :memo: | v1.0.0 | Full unit test coverage, benchmarks, and docs |

---

## ๐Ÿ‘‹ Contributions Welcome

Issues, feature suggestions, and PRs are welcome!