在 C++ 中实现状态机
我是 C++ 新手。
如何用 C++ 实现状态机?
我只收到消息,应该知道下一个状态。
我需要使用的正确结构是什么?
I'm new to C++.
How can I implement a State Machine in C++?
I'm getting only the messages and should know the next state.
What is the proper structure I need to use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
对于简单的状态机,您可以在循环内使用 switch 语句,例如
For simple state machines you can just use a switch statement inside a loop, e.g.
编辑:
构建状态图是这样完成的(示例是可乐自动售货机):
实现默认操作,其中键只是状态,如下所示:
可以使用第二个映射来 逻辑可以在 StateDiagramForDefaults 中执行查找。
如果需要将操作添加到状态图,则映射的值应该是由操作和新状态组成的对,如下所示:
构建图的逻辑应该“新”类的实例实现 IAction,并将其放入 StateDiagram 中。
然后,执行逻辑仅通过虚拟方法(例如execute() 或() 运算符)执行IAction 实现。
EDIT:
Building up the state diagram is done like this (example is for a cola-vending machine):
Default actions can be implemented using a second map, where the key is only the State, like this:
Instead of printing "incorrect message", the logic can perform a lookup in the StateDiagramForDefaults.
If actions needs to be added to the state diagram, the value of the map should be a pair consisting of an action, and a new state, like this:
The logic that builds up the diagram should then "new" an instance of a class that implements IAction, and put that in the StateDiagram.
The executing logic then just executes the IAction implementation via a virtual method (e.g. execute() or the ()-operator).
标准状态机实现技术是:
以前的帖子显示了这方面的例子
技术)
如果您不熟悉状态机和 C 或 C++ 实现,我会推荐我的 Dr.Dobbs 文章“回归基础”,可在 http://www.ddj.com/184401737 (您需要点击顶部的打印链接才能方便地阅读文本。)
没有一种标准技术适合分层状态机(例如 UML 状态图)。如果您对现代 UML 状态机感兴趣,我会推荐我的由 3 部分组成的 Embedded.com 文章“UML 状态机速成课程”(http://www.embedded.com/design/testissue/215801043)。
The standard state machine implementation techniques are:
previous posts show examples of this
technique)
If you are new to state machines and implementation in C or C++, I would recommend my Dr.Dobbs article "Back to Basics" available at http://www.ddj.com/184401737 (you need to click on the Print link at the top to conveniently read the text.)
None of the standard techniques is suitable for the hierarchical state machines (such as UML statecharts). If you are interested in the modern UML state machines, I'd recommend my 3-part Embedded.com article "A crash course in UML state machines" (http://www.embedded.com/design/testissue/215801043).
除非您为了实现状态机而实现状态机,否则我强烈建议您使用状态机生成器。 Ragel 是一种非常好的、强大的解决方案。
https://github.com/bnoordhuis/ragel
Unless you are implementing a state machine for the sake of implementing one, I would highly recommend that you use a state machine generator instead. Ragel is one very good and robust solution.
https://github.com/bnoordhuis/ragel
一种方法是使用这样的类(前面有粗略的示例代码):
其中一个复杂的问题是状态是否应该是 静态享元,或者它们是否携带实例数据并因此应该被更新和删除。
One way is to use a class like this (rough example code ahead):
On of the complications is whether states should be static flyweights, or whether they carry instance data and should therefore be newed and deleted.
天哪,事情并不像看起来那么复杂。状态机代码非常简单和简短。
编写状态机代码几乎是微不足道的。困难的部分是设计一个在所有可能的情况下都能正确运行的状态机。
但让我们假设您有正确的设计。你如何编码?
将状态存储在属性中,可能是 myState。
每当您收到消息时,都会在 myState 属性上切换以执行该状态的代码。
每当
3 在每个状态下,打开消息以执行该状态和该消息的代码
因此您需要一个嵌套的 switch 语句
一旦您启动并运行它,添加更多状态和消息就会变得有趣且容易。
Gosh, it isn’t as complicated as it seems. State machine code is very simple and short.
Coding a state machines is just about trivial. The hard part is designing a state machine that behaves correctly in all possible cases.
But let us assume that you have the correct design. How do you code it?
Store the state in an attribute, myState perhaps.
Whenever you receive a message switch on the myState attribute to execute the code for that state.
3 In each state, switch on the message to execute the code for that state AND that message
So you need a nested switch statement
Once you have this up and running, it is fun and easy to add more states and messages.
请参阅在程序中实现有限自动机了解几种不同的方法构建有限状态机。
See Implementing Finite Automata in a Program for several different ways of building finite state machines.
查看分层状态机文章。通过示例描述了将状态建模为类。
Checkout the hierarchical state machine article. Modeling of states as classes is described with an example.
我正在使用的基于此:机器对象。
它似乎经过了很好的优化,并且使用起来比 Boost 状态图。
The one i am using is based on this one: Machine Objects.
It seems to be well optimized and is a hell of a lot less complex to use than Boost Statechart.
当然,Boost 为您准备了一些东西: Boost状态图库。您还可以在那里找到一些不错的教程。
Of course, boost has something in store for you: Boost Statechart library. You can also find some nice tutorials there.
您可以使用 Ragel 状态机编译器: http://www.complang.org/ragel/
Ragel 代码如下所示:
.. 并编译为:
You can use the Ragel state machine compiler: http://www.complang.org/ragel/
Ragel code looks like:
.. and it compiles to: