需要 C++ 方面的帮助模板

发布于 2024-08-16 20:09:30 字数 872 浏览 11 评论 0原文

我相当确定这是一个模板问题,因为我似乎无法以任何其他方式解决它 - 但也欢迎非模板解决方案。

有限状态机具有多个程序状态,每个状态可以对多个事件做出反应。

所以,我想为事件、状态和 FSM 定义类。 FSM 有一个状态集合(可能是向量,如果 STL 在嵌入式系统中出现问题,则可能是链表),而状态有一个事件集合。

每个状态和事件都有一个唯一的 ID 和一个用于调试 porpoise 的名称字符串。

尴尬的是,我不希望 Id 是整数,而是枚举的元素。每个 FSM 的状态和状态都有不同的枚举。事件。

如何最好地编写这个代码?您能否举一个具有两个简单 FSM 的示例,或者一个具有两个状态(每个状态有两个事件)的 FSm?

例如,如果我有

enum myEvents {a, b, c};
enum hisEvents {d, e, f, g};

,我希望能够声明一个接受构造函数参数的 Event 类

(myEvents a,char *"event_a")
and
(hisEvents g,char* "event_g")
Note that I don't want to just overload the constructor, since that is restrictive - what if new event enums are added?

与州类似,然后让我的 FSM 每个都有一个州列表。

或者我只是在坚持使用枚举作为 eventId,而传递 int 会简单?

谢谢。


顺便说一句,我宁愿避免使用 Boost,因为它本身还不确定它在嵌入式系统中的工作效果如何。我更喜欢内部开发,以便完全控制。

I'm fairly sure this is a template question, since I can't seem to solve it any other way - but non-template solutions are also welcome.

A Finite State Machine has a number of program States and each state can react to a number of Events.

So, I want to define classes for Event, State and FSM. FSM has a collection (probably vector, might be linked list if STL gives problems in an embedded system) of States, and State has a collection of Events.

Each state and event have a unique Id and a name string for debugging porpoises.

To be awkward, I don't want the Ids to be integers, but elements of an enum. Each FSM has different enums for its states & events.

How best to code this? Can you give an example with two simple FSMs, or one FSm with two states, each with two events?

For example, if I have

enum myEvents {a, b, c};
enum hisEvents {d, e, f, g};

I want to be able to declare an Event class which accpts constructors params

(myEvents a,char *"event_a")

and

(hisEvents g,char* "event_g")

Note that I don't want to just overload the constructor, since that is restrictive - what if new event enums are added?

And similarly with states, then have my FSMs each have a list of states.

Or am I just being anel, insisting on enums for eventId, when it would be much simpler to pass an int?

thanks.


Btw, I'd rather avoid Boost as it is itself undecided on how well it works in embedded systems. I prefer in-house developed, for complete control.

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

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

发布评论

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

评论(2

初懵 2024-08-23 20:09:30

我不确定我是否正确理解了事情,但我会尝试一下:

我假设您想通过定义转换来定义状态机;例如“当处于状态‘myEvents’并且你看到‘a’执行‘event_a’时”

class State {};
template<T> RealState : State
{
    static void Add(T event, char*) { /* save stuff */ }
};

class Event {};
template<T> RealEvent : Event    {
    RealEvent(T event, char* name) {RealState<T>(event, name); }
};

你需要如何添加动作和诸如此类的东西,你会想要把它搞乱一点以获得多个状态机,但我希望这能让你开始。

I'm not shure if I'm understanding things correctly but I'll take a stab at it:

I'm assuming you want to define a state machine by defining the transitions; e.g. "when in state 'myEvents' and you see 'a' do 'event_a'"

class State {};
template<T> RealState : State
{
    static void Add(T event, char*) { /* save stuff */ }
};

class Event {};
template<T> RealEvent : Event    {
    RealEvent(T event, char* name) {RealState<T>(event, name); }
};

Some how you would need to tack in actions and whatnot and you will want to muck it up a bit to get more than one state machine, but I hope that gets you started.

夜未央樱花落 2024-08-23 20:09:30

虽然我理解为什么你想通过一般地实现状态机基础知识来避免代码冗余,但我不明白为什么你想自己实现它。

查看现有的实现,例如 Boost.Statechart 或任何最适合您的使用场景的内容。

While i understand why you want to avoid code redundancies by implementing the state machine basics generically, i don't understand why you want to implement this yourself.

Take a look at existing implementations like Boost.Statechart or whatever suits your usage scenario best.

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