#include // back-end #include //front-end #include #include namespace msm = boost::msm; namespace mpl = boost::mpl; using namespace std; using namespace msm::front; namespace { static bool s_deviceInited = false; struct setDeviceInited { template void operator()(Evt const& evt, Fsm& fsm, SourceState& ss, TargetState& ts) { s_deviceInited = true; std::cout << "s_deviceInited=" << s_deviceInited << std::endl; } }; struct resetDeviceInited { template void operator()(Evt const& evt, Fsm& fsm, SourceState& ss, TargetState& ts) { s_deviceInited = true; std::cout << "s_deviceInited=" << s_deviceInited << std::endl; } }; struct deviceInited { template bool operator()(Evt const& evt, Fsm& fsm, SourceState& ss, TargetState& ts) { std::cout << "s_deviceInited=" << s_deviceInited << std::endl; return s_deviceInited; } }; // events struct EvDeviceInited : msm::front::euml::euml_event{}; struct EvAlarm : msm::front::euml::euml_event{}; struct EvAlarmCleared : msm::front::euml::euml_event{}; // define some dummy instances for use in the transition table EvDeviceInited evDeviceInited; EvAlarm evAlarm; EvAlarmCleared evAlarmCleared; // The list of FSM states struct alarm : public msm::front::state<> { // every (optional) entry/exit methods get the event passed. template void on_entry(Event const&,FSM& ) {std::cout << "entering: Alarm" << std::endl;} template void on_exit(Event const&,FSM& ) {std::cout << "leaving: Alarm" << std::endl;} }; struct operate : public msm::front::state<> { template void on_entry(Event const& ,FSM&) {std::cout << "entering: Operate" << std::endl;} template void on_exit(Event const&,FSM& ) {std::cout << "leaving: Operate" << std::endl;} }; struct initialize : public msm::front::state<> { template void on_entry(Event const& ,FSM&) {std::cout << "entering: Initialize" << std::endl;} template void on_exit(Event const&,FSM& ) {std::cout << "leaving: Initialize" << std::endl;} }; // Normal Submachine front-end struct Normal_T : public msm::front::state_machine_def { template void on_entry(Event const& ,FSM&) {std::cout << "entering: Normal" << std::endl;} template void on_exit(Event const&,FSM& ) {std::cout << "leaving: Normal" << std::endl;} typedef initialize initial_state; struct transition_table : boost::mpl::vector< // Start Event Next Action Guard // +------------+----------------+------------+------------------+----------------------+ Row < initialize , none , operate , none , deviceInited >, Row < initialize , EvDeviceInited , initialize , setDeviceInited , none > // +------------+----------------+------------+------------------+----------------------+ > {}; // Playing has a transition table //BOOST_MSM_EUML_DECLARE_TRANSITION_TABLE(( //// +------------------------------------------------------------------------------+ // operate == initialize [deviceInited()], // // initialize == initialize + evDeviceInited / setDeviceInited() //// +------------------------------------------------------------------------------+ //),transition_table ) }; // Normal Submachine back-end typedef boost::msm::back::state_machine normal; // front-end: define the FSM structure struct StateMachine_T : public msm::front::state_machine_def { template void on_entry(Event const& ,FSM&) {std::cout << "entering: StateMachine" << std::endl;} template void on_exit(Event const&,FSM& ) {std::cout << "leaving: StateMachine" << std::endl;} // the initial state of the player SM. Must be defined typedef normal initial_state; struct transition_table : boost::mpl::vector< // Start Event Next Action Guard // +------------+----------------+------------+------------------+----------------------+ Row < normal , EvAlarm , alarm , resetDeviceInited, none >, Row < alarm , EvAlarmCleared , normal , none , none > // +------------+----------------+------------+------------------+----------------------+ > {}; //BOOST_MSM_EUML_DECLARE_TRANSITION_TABLE(( // normal + evAlarm / resetDeviceInited() == alarm, // alarm + evAlarmCleared == normal // ),transition_table) // Replaces the default no-transition response. template void no_transition(Event const& e, FSM&,int state) { std::cout << "no transition from state " << state << " on event " << typeid(e).name() << std::endl; } }; // Link the back-end typedef msm::back::state_machine StateMachine; void test() { StateMachine p; p.start(); p.process_event(evAlarm); p.process_event(evAlarmCleared); p.process_event(evDeviceInited); p.process_event(evAlarm); p.process_event(evAlarmCleared); p.process_event(evDeviceInited); p.process_event(evAlarm); p.process_event(evAlarmCleared); p.process_event(evDeviceInited); p.process_event(evAlarm); p.process_event(evAlarmCleared); p.stop(); } } int main() { test(); return 0; }