在 C++ 中实现事件条件 状态机

发布于 2024-07-09 20:14:07 字数 256 浏览 7 评论 0原文

我正在为嵌入式 C++ 应用程序接口使用分层 FSM。 我想使用小函数来确定是否可以触发某些状态间事件,并使用它们来影响数据库中的更改:但是,为每个状态创建一个具有不同事件函数的新类也是令人畏惧的将指向它们的指针设置为回调。 (我们这里讨论的是 300 个函数。)

如果 C++ 支持 lambda 函数,我会使用它们,但我对项目架构没有太多重视,所以我不愿意使用第三方解决方案需要大量的库集成(如 boost)或包含额外的预处理器步骤。

有任何想法吗?

I'm using an hierarchical FSM for an embedded C++ application interface. I'd like to use small functions to determine whether certain inter-state events can be triggered, as well as use them to effect changes in the database: however, making a new class with different event functions for each state is daunting, as well as setting pointers to them as callbacks. (we're talking about 300 functions here.)

If C++ supported lambda functions, I'd use them, but I don't have a lot of weight on the project architecture, so I'm reluctant to use third-party solutions that require heavy library integration (like boost) or the inclusion of extra preprocessor steps.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

扎心 2024-07-16 20:14:07

实际上,如果您使用 Boost/TR1 库,则支持 Lambda 函数。 详细信息可以在 Boost 网站 上找到。

Actually, if you use the Boost/TR1 library, there is support for Lambda functions. Details can be found on the Boost web site.

污味仙女 2024-07-16 20:14:07

新的 C++0x 标准即将完成并被接受。 明年推出时,它最终应该是 C++09。 该标准将直接在语言中包含 Lambda 函数,并消除这种需求对于类似的 boost 库。 Visual Studio 2010 技术预览版中的 C++ 编译器已按照 Visual C++ 团队博客

或者,您可以为您的逻辑创建 Lua 绑定,并使用表来处理 FSM 作为 在此 wiki 中显示。 这是一个非常灵活的解决方案,允许在不重新编译 C++ 项目的情况下调整 FSM。 此外,Lambda 函数(以及 闭包)在 Lua 下是原生且稳定的。 Lua 协程 也值得研究,以简化 FSM 实现。

The new C++0x standard is coming close to finalization and acceptance. It should end up being C++09 when introduced next year. This standard will include Lambda functions directly in the language and obsolete the need for the analogous boost library. The C++ compiler in the technology preview of Visual Studio 2010 already supports Lambda functions per the Visual C++ Team Blog.

Alternatively, you could create Lua bindings for your logic and use tables to handle the FSM as shown in this wiki. This is a very flexible solution that would allow the FSM to be tweaked without recompiling your C++ project. Also, Lambda functions (as well as closures) are native and stable under Lua. Lua coroutines would also be worth investigating to simplify your FSM implementation.

许久 2024-07-16 20:14:07

你看过Qt的QStateMachine吗? 或许能提供一些启发。

Have you looked at Qt's QStateMachine? It might provide some inspiration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文