具有函数指针的有限状态机

发布于 2024-11-25 03:07:12 字数 964 浏览 5 评论 0原文

我正在学习使用有限状态机来完成某些任务,但在导航状态表和执行函数以使其成为有用的系统时遇到问题。

考虑我的状态机:

状态机
(来源:wikimedia.org

说明:
* = 将字符打印到标准输出
N = '\n'
S = ' '
A = aA-zZ

我从维基百科上基于自动机的编程开始使用的代码可以工作对于这样一个简单的机器,但我想修改它,以便我可以拥有更强大的状态转换表并根据这些状态调用函数。

我已经在 Pastebin 上发布了工作基本代码以及我想使用的转换表样式

我之前没有使用过函数指针,所以我不确定如何根据 process_event 接收到的数据编写转换函数。最终我想要一个模板,允许我有状态输入/输出和状态输入/输出。转换输入/输出函数,这样我就可以更有效地编写复杂的用户菜单,甚至编程算法。

I am learning to use finite-state machines for some tasks but I am having problems navigating my state table and executing the functions to make it a useful system.

Consider my state machine:

state machine
(source: wikimedia.org)

Explanation:
* = Print char to stdout
N = '\n'
S = ' '
A = aA-zZ

The code I started with from Automata-based programming on Wikipedia works for such a simple machine, but I want to modify it so that I can have a more robust state transition table and call functions based off those states.

I've posted working basic code at Pastebin, along with the transtion table style I want to use.

I have not used pointers to functions before so I am not sure how to write the transition functions based off the data received by process_event. Eventually I would like to have a template that allows me to have state in/out & transition in/out functions so I can write complex user menus and even programming algorithms much more efficiently.

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

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

发布评论

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

评论(3

懵少女 2024-12-02 03:07:12

使用函数作为状态非常强大,但与使用递归函数(返回函数的函数状态)相比,使用转换表非常容易出错且痛苦。一个值得您考虑的出色实现是量子分层状态机。虽然它只有大约 1000 行代码作为基础,但它有一本随附的书 解释您对其工作原理可能存在的任何疑问。非常强大,速度非常快。

Using functions as states is very powerful, but using transition tables is very error prone and painful compared to using recursive functions (function states that return functions). A fantastic implementation for you to consider is the quantum hierarchical statem machine. Although it is only about 1000 lines of code as a base, it has an accompanying book to explain any question you might have about how it works. Very powerful, very fast.

佞臣 2024-12-02 03:07:12

你检查了吗
Boost.msm - 非常高-用于富有表现力的 UML2 有限状态机的性能库。

阅读文档,因为它都是关于管理状态机的复杂性的。

您可能更喜欢 boost 中的其他状态机实现,因为它编译速度更快,因为它不是为超高速而设计的(这并不意味着它不够快)Boost.Statechart - 可以用易于阅读和维护的 C++ 代码实现任意复杂的有限状态机。

正如 Brent Arias 提到的,你应该阅读 http://www.state-machine.com/ 的书psicc2/index.php 它是状态机圣经。

Did you check
Boost.msm - A very high-performance library for expressive UML2 finite state machines.

Read documentation, because it is all about managing complexity about statemachines.

There is also other state machine implementation in boost that you might prefer because it compiles faster since it is not designed for super speed (which does not means that it is not fast enough) Boost.Statechart - Arbitrarily complex finite state machines can be implemented in easily readable and maintainable C++ code.

As Brent Arias mentioned you should read book from http://www.state-machine.com/psicc2/index.php It is state machines bible.

兮颜 2024-12-02 03:07:12

您的编译问题表明它无法从 int 转换为 void (*)(int) 来自分支结构:

struct branch
{
    int event_type:3;
    enum states state_new:2;
    int do_func:1;
};

do_func 已定义作为整数而不是 void (*do_func)(int)

Your compilation issue saying that it could not convert from int to void (*)(int) was from the branch struct:

struct branch
{
    int event_type:3;
    enum states state_new:2;
    int do_func:1;
};

The do_func is defined as an integer and not void (*do_func)(int);

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