使用Boost状态图,如何无条件转换到某个状态?

发布于 2024-12-09 06:12:07 字数 742 浏览 1 评论 0原文

我有一个状态 A,一旦 A 的构造函数完成,我希望无条件转换到其下一个状态 B。这可能吗?

我尝试从构造函数发布一个事件,尽管它可以编译,但它不起作用。谢谢。

编辑:这是我到目前为止所尝试的:

struct A : sc::simple_state< A, Active >
{
    public:
        typedef sc::custom_reaction< EventDoneA > reactions;
        A()
        {
            std::cout << "Inside of A()" << std::endl;
            post_event( EventDoneA() );
        }

        sc::result react( const EventDoneA & )
        {
            return transit< B >();
        }
};

这会产生以下运行时断言失败:

Assertion failed: get_pointer( pContext_ ) != 0, file /includ
e/boost/statechart/simple_state.hpp, line 459

I have a state A that I would like to transition to its next state B unconditionally, once the constructor of A has completed. Is this possible?

I tried posting an event from the constructor, which does not work, even though it compiles. Thanks.

Edit: Here is what I've tried so far:

struct A : sc::simple_state< A, Active >
{
    public:
        typedef sc::custom_reaction< EventDoneA > reactions;
        A()
        {
            std::cout << "Inside of A()" << std::endl;
            post_event( EventDoneA() );
        }

        sc::result react( const EventDoneA & )
        {
            return transit< B >();
        }
};

This yields the following runtime assertion failure:

Assertion failed: get_pointer( pContext_ ) != 0, file /includ
e/boost/statechart/simple_state.hpp, line 459

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

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

发布评论

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

评论(1

默嘫て 2024-12-16 06:12:07

我已经用我的解决方案更新了我的OP,在这里我可以关闭问题。

问题是您无法从 simple_state 继承来实现所需的转换,您必须从 state 继承,然后需要您相应地修改构造函数。

struct A : sc::state< A, Active >
{
    public:
        typedef sc::custom_reaction< EventDoneA > reactions;
        A( my_context ctx ) : my_base( ctx )
        {
            std::cout << "Inside of A()" << std::endl;
            post_event( EventDoneA() );
        }

        sc::result react( const EventDoneA & )
        {
            return transit< B >();
        }
};

I've updated my OP with my solution, here it is so I can close the question.

The problem is that you cannot inherit from simple_state to achieve the desired transition, you must inherit from state, which then requires that you modify the constructor accordingly.

struct A : sc::state< A, Active >
{
    public:
        typedef sc::custom_reaction< EventDoneA > reactions;
        A( my_context ctx ) : my_base( ctx )
        {
            std::cout << "Inside of A()" << std::endl;
            post_event( EventDoneA() );
        }

        sc::result react( const EventDoneA & )
        {
            return transit< B >();
        }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文