Show HN:CXXSTATETREE - 现代的C ++库,用于分层状态机器
Show HN: CXXStateTree – A modern C++ library for hierarchical state machines

原始链接: https://github.com/ZigRazor/CXXStateTree

CXXSTATETREE是一个现代的,仅标题的C ++ 20分层状态机库,旨在速度和可扩展性。它拥有流利的构建器API和基于Lambda的DSL,可通过可选的警卫和动作定义状态和过渡。它支持事件驱动的状态过渡,并在运行时具有零堆分配。 关键功能包括:带有`send()`的基本状态机能````send)'',状态跟踪,守卫和对嵌套状态的计划支持以及对状态图的GraphViz导出。 该库包括一个全面的Google Test Suite,其中包含用于代码覆盖的CodeCov集成,并使用CMAKE进行构建管理,并自动下载Googletest。它是根据MPL2.0许可的。 该项目路线图包括Coroutine/Async支持,目的是全单位测试覆盖,基准和完整的文档。强烈鼓励通过问题,建议和公关捐款。

CXXSTATETREE是Zigrazor创建层次状态机器的现代,仅标题的C ++库。它支持深层嵌套的状态,进入/退出处理程序,带有警卫和动作的状态过渡,使用C ++ 20 Coroutines的异步过渡以及可选的运行时类型标识。它专为嵌入式系统,游戏和机器人技术等领域的复杂控制逻辑而设计,旨在在建造状态树之后进行零堆的分配。 Hacker News的反馈包括针对嵌入式系统中较小目标的I/O和DOT出口功能的建议,并考虑MISRA针对安全至关重要的应用指南,可能需要完全消除堆分配。人们对其与Boost-Ext/SML等现有图书馆的区分提出了担忧。同样,关于#pragma的使用曾经与header档案中的警卫相比,有些辩论是为了删除其非标准,并可能导致放缓,而其他人则引用了其在现代编译器中的广泛支持。
相关文章

原文

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

👋 Contributions Welcome

Issues, feature suggestions, and PRs are welcome!

联系我们 contact @ memedata.com