c++ - 帮助 Boost.Statechart 错误

标签 c++ boost-statechart

有什么办法解决这个问题吗?

在 ubuntu 8.10 w/g++ 4.3.2 上使用 1.39_0

在下面的状态图中,短语“BUGGY”打印了三个 次。人们会期望该事件只会触发一个“BUGGY”。在 就我正在进行的项目而言,我无法返回 discard_event() 因为我需要事件达到多个状态(通常 深入一组正交状态)。如果有解决方法 可以应用而不是修改状态图,我想知道。

$猫虫.cpp

#include <boost/intrusive_ptr.hpp>
#include <boost/mpl/list.hpp>    #include <boost/statechart/custom_reaction.hpp>
#include <boost/statechart/event.hpp>
#include <boost/statechart/simple_state.hpp>
#include <boost/statechart/state.hpp>
#include <boost/statechart/state_machine.hpp>
#include <iostream>

using namespace std;
namespace sc = boost::statechart;
namespace mpl = boost::mpl;

struct evSay : sc::event<evSay>{ };
struct top;
struct c1;
struct c2;
struct c3;
struct sm : public sc::state_machine<sm,top> { };

struct top : sc::simple_state<top,sm,mpl::list<c1,c2,c3> > {
       typedef sc::custom_reaction<evSay> reactions;
       sc::result react(const evSay &) {
               cout<<"BUGGY"<<endl;
               return forward_event();
       }
};

struct c1 : sc::simple_state <c1, top::orthogonal<0> > { };

struct c2 : sc::simple_state <c2, top::orthogonal<1> > { };

struct c3 : sc::state <c3, top::orthogonal<2> > {
       c3( my_context  ctx) : my_base(ctx) {
               post_event( boost::intrusive_ptr< evSay > (
                               new evSay() ) );
       }
};

int main() {
       sm* fsm = new sm();
       fsm->initiate();
       delete fsm;
       return 0;
}

$ g++ bug.cpp && ./a.out
buggy
buggy
buggy

编辑::

这是一个示例状态机,显示了我在实际处理的更大的问题中遇到的问题。我知道top会转发evSay。请注意,c1、c2、c3 不会对 evSay 使用react。这是我需要转发的示例,以便两个状态可以对 evSay 使用react。

#include <boost/intrusive_ptr.hpp>    
#include <boost/mpl/list.hpp>    
#include <boost/statechart/custom_reaction.hpp>
#include <boost/statechart/event.hpp>
#include <boost/statechart/simple_state.hpp>
#include <boost/statechart/state.hpp>
#include <boost/statechart/state_machine.hpp>    
#include <iostream>
using namespace std;
namespace sc = boost::statechart;
namespace mpl = boost::mpl;
namespace BUG {
struct evSay : sc::event<evSay>{ };
struct top;struct c1;struct c2;struct c3;struct c2_1;

struct sm : public sc::state_machine<sm,top> { };

struct top : sc::simple_state<top,sm,mpl::list<c1,c2,c3> > {
    typedef sc::simple_state<top,sm,mpl::list<c1,c2,c3> > my_type;
    typedef sc::custom_reaction<evSay> reactions;
    sc::result react(const evSay &) {
        cout<<"BUGGY"<<endl;
        return forward_event();
    }
};

struct c1 : sc::simple_state <c1, top::orthogonal<0> > { };
struct c2 : sc::simple_state <c2, top::orthogonal<1>, c2_1 > { };
struct c3 : sc::state <c3, top::orthogonal<2> > {
    c3( my_context  ctx) : my_base(ctx) {
        post_event( boost::intrusive_ptr< evSay > (
                new evSay() ) );
    }
};

struct c2_1 : sc::simple_state<c2_1, c2 > {
    typedef sc::custom_reaction<evSay> reactions;
    sc::result react(const evSay &) {
        cout<<"CHILD REACTION"<<endl;
        return forward_event();
    }
};
}

int main()
{
    BUG::sm* fsm = new BUG::sm();
    fsm->initiate();
    delete fsm;
    return 0;
}

输出: buggy
child react
buggy
buggy

最佳答案

将您想要的 react 向下移动到 top 状态的子状态。这将它从 forward_state 事件行中取出。我没有将它实现为内部类型,但你可以。

#include <boost/intrusive_ptr.hpp>    
#include <boost/mpl/list.hpp>    
#include <boost/statechart/custom_reaction.hpp>
#include <boost/statechart/event.hpp>
#include <boost/statechart/simple_state.hpp>
#include <boost/statechart/state.hpp>
#include <boost/statechart/state_machine.hpp>    
#include <iostream>
using namespace std;
namespace sc = boost::statechart;
namespace mpl = boost::mpl;


struct evSay : sc::event<evSay>{ };

struct top;struct c1;struct c2;struct c3;struct c2_1;struct sub_1;

struct sm : public sc::state_machine<sm,top> { };

struct top : sc::simple_state<top,sm, mpl::list<c1,c2,c3, sub_1> > { };

struct sub_1 : sc::simple_state<sub_1, top::orthogonal<3> > {
    typedef sc::custom_reaction<evSay> reactions;
    sc::result react(const evSay &) {
        cout<<"PARENT REACTION"<<endl;
        return forward_event();
    }
};

struct c1 : sc::simple_state <c1, top::orthogonal<0> > { };
struct c2 : sc::simple_state <c2, top::orthogonal<1>, c2_1 > { };
struct c3 : sc::simple_state <c3, top::orthogonal<2> > { };

struct c2_1 : sc::simple_state<c2_1, c2 > {
    typedef sc::custom_reaction<evSay> reactions;
    sc::result react(const evSay &) {
        cout<<"CHILD REACTION"<<endl;
        return forward_event();
    }
};

int main()
{
    sm* fsm = new sm();
    fsm->initiate();
    fsm->process_event(  evSay()  );
    delete fsm;
    return 0;
}

~$g++ test.cpp -I Downloads/boost_1_45_0
~$./a.out
child react
家长 react

关于c++ - 帮助 Boost.Statechart 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1048904/

相关文章:

c++ - 如何将数字 QString 转换为 int 数组?

c++ - 有没有办法向该类的客户隐藏该类的私有(private)成员?

c++ - 如何反转数组中的元素?

c++ - boost.statechart 库中的转换和自定义 react

c++ - 对在Boost.statechart中完成的所有正交状态使用react

c++ - 使用 boost::statecart 进行单元测试

c++ - 如何在 boost::statechart state_machine 对象中获取当前最派生的状态?

c++ - 命名空间行为怪异

c++ - 如果在 Linux 上的特定目录中创建文件,如何获取回调

c++ - Boost.Statechart - 记录的选择点方法问题