我想定义一个从statechart::simple_state
派生的基类,它有自己调用虚拟函数(必须在派生类中实现)的“预定义”反应。我想要的是,如果某些状态派生自我的基类,某些状态会自动对某些事件做出反应。继承反应
像这样(sc
是boost::statechart
):
struct EvHeartBeat : sc::event<EvHeartBeat> {};
template< class MostDerived,
class Context,
class InnerInitial = boost::mpl::list<>,
sc::history_mode historyMode = sc::has_no_history >
class BaseState : public sc::simple_state<
MostDerived, Context, InnerInitial, historyMode >
{
public:
typedef sc::custom_reaction<EvHeartBeat> reactions;
sc::result react (const EvHeartBeat&)
{
// maybe check some conditions here ...
return react_heartbeat();
}
protected:
virtual sc::result react_heartbeat() = 0;
};
然后,在派生类:
struct MyState :
BaseState<MyState, MyChart>
{
// there are also other reactions
typedef sc::custom_reaction<OtherEvent> reactions;
sc::result react_heartbeat()
{
std::cout << "foo" << std::endl;
}
sc::result react (const OtherEvent&) { /* ... */ }
};
在派生类的typedef
将 “覆盖” 的一个基类我假设,所以也许我需要将心跳事件定义为custon_reaction
作为派生类中的列表。但是,也许这种设计不像这个图书馆的设计师认为它应该是这样的,任何能够帮助我的人呢?
编辑
在此期间,我获得了一些额外的知识。 typedef
的解决方法是将其定义在派生类中而不是基类中。但是之后会出现一个奇怪的问题:编译器不会找到react (const EvHeartBeat&)
的方法,虽然它在基类中定义,但如果我删除它的其他反应(react (const OtherEvent&)
)它的工作原理。但当然这不是我想要的,我希望能够对多个事件做出反应。