我的用例类似于这个 SSCCE。问题是,如果没有发生转换,我需要转发似乎不自然的事件,以便它们由其他正交区域处理。更重要的是,尽管在我需要过渡的情况下,我无法找到一种方法来让该事件在其他地区得到反应。我应该如何重组来解决这个问题?
如果这很重要,我正在使用 boost 1.53。
namespace sc = boost::statechart;
struct Active;
struct Keyboard : sc::state_machine< Keyboard, Active > {};
struct NumLockOff;
struct CapsLockOff;
struct ScrollLockOff;
struct Active: sc::simple_state< Active, Keyboard, boost::mpl::list< NumLockOff, CapsLockOff, ScrollLockOff > > {};
struct EvNumLockPressed : sc::event< EvNumLockPressed > {};
struct EvCapsLockPressed : sc::event< EvCapsLockPressed > {};
struct EvScrollLockPressed : sc::event< EvScrollLockPressed > {};
struct EvAllLocksOffPressed : sc::event< EvAllLocksOffPressed > {
int i_;
EvAllLocksOffPressed(int i):sc::event< EvAllLocksOffPressed >(),i_(i){}
};
struct NumLockOn : sc::simple_state< NumLockOn, Active::orthogonal< 0 > >
{
typedef boost::mpl::list<sc::transition< EvNumLockPressed, NumLockOff >, sc::custom_reaction<EvAllLocksOffPressed>> reactions;
sc::result react( const EvAllLocksOffPressed & e)
{
if(e.i_ == 42)
return transit< NumLockOff >();
return forward_event();
}
};
struct NumLockOff : sc::simple_state< NumLockOff, Active::orthogonal< 0 > >
{
typedef sc::transition< EvNumLockPressed, NumLockOn > reactions;
};
struct CapsLockOn : sc::simple_state< CapsLockOn, Active::orthogonal< 1 > >
{
typedef boost::mpl::list<sc::transition< EvCapsLockPressed, CapsLockOff >, sc::custom_reaction<EvAllLocksOffPressed>> reactions;
sc::result react( const EvAllLocksOffPressed & e)
{
if(e.i_ == 42)
return transit< CapsLockOff >();
return forward_event();
}
};
struct CapsLockOff : sc::simple_state< CapsLockOff, Active::orthogonal< 1 > >
{
typedef sc::transition< EvCapsLockPressed, CapsLockOn > reactions;
};
struct ScrollLockOn : sc::simple_state< ScrollLockOn, Active::orthogonal< 2 > >
{
typedef boost::mpl::list<sc::transition< EvScrollLockPressed, ScrollLockOff >, sc::custom_reaction<EvAllLocksOffPressed>> reactions;
sc::result react( const EvAllLocksOffPressed & e)
{
if(e.i_ == 42)
return transit< ScrollLockOff >();
return forward_event();
}
};
struct ScrollLockOff : sc::simple_state< ScrollLockOff, Active::orthogonal< 2 > >
{
typedef sc::transition< EvScrollLockPressed, ScrollLockOn > reactions;
};
int main(){
Keyboard k;
k.initiate();
k.process_event(EvNumLockPressed());
k.process_event(EvCapsLockPressed());
k.process_event(EvScrollLockPressed());
k.process_event(EvAllLocksOffPressed(1));
k.process_event(EvAllLocksOffPressed(42));
}