#include #include #include #include #include #include #include namespace sc = boost::statechart; namespace mpl = boost::mpl; struct Active; struct Proc : public sc::state_machine { Proc() { std::cout << "Proc()" << std::endl; } ~Proc() { std::cout << "~Proc()" << std::endl; } std::string name() const { return "Proc"; } void unconsumed_event(const sc::event_base& ev) { std::cout << "Proc::unconsumed_event(ev=" << ev.custom_dynamic_type_ptr() << ") -> active states:" << std::endl; for (state_iterator cur_state = state_begin(); cur_state != state_end(); ++cur_state) { std::cout << " state=" << cur_state->custom_dynamic_type_ptr() << std::endl; } } }; struct Inner1; struct Active: public sc::simple_state { Active() { std::cout << "Active()" << std::endl; } ~Active() { std::cout << "~Active()" << std::endl; } }; struct EvDataReceived: public sc::event {}; struct Inner2; struct Inner1: public sc::simple_state { typedef sc::transition< EvDataReceived, Inner2 > reactions; Inner1() { std::cout << "Inner1()" << std::endl; } ~Inner1() { std::cout << "~Inner1()" << std::endl; } }; template struct Inner3; struct Inner2: public sc::simple_state { typedef mpl::list< sc::custom_reaction > reactions; sc::result react(const EvDataReceived& ev) { std::cout << "Inner2::react::EvDataReceived" << std::endl; return transit >(); } Inner2() { std::cout << "Inner2()" << std::endl; } ~Inner2() { std::cout << "~Inner2()" << std::endl; } }; template struct Inner3: public sc::state, Active> { Inner3(my_context ctx): my_base(ctx) { std::cout << "Inner3(): my outermost_context() is '" << outermost_context().name() << "'" << std::endl; } ~Inner3() { std::cout << "~Inner3()"<< std::endl; } }; int main(int argc, char* argv[]) { Active::custom_static_type_ptr( "Active" ); Inner1::custom_static_type_ptr( "Inner1" ); Inner2::custom_static_type_ptr( "Inner2" ); Inner3::custom_static_type_ptr( "Inner3" ); EvDataReceived::custom_static_type_ptr( "EvDataReceived" ); Proc fsm; std::cout << "FSM initiate()" << std::endl; fsm.initiate(); std::cout << "FSM initiate() ... OK" << std::endl; fsm.process_event(EvDataReceived()); fsm.process_event(EvDataReceived()); fsm.process_event(EvDataReceived()); std::cout << "FSM terminate()" << std::endl; fsm.terminate(); std::cout << "FSM terminate() ... OK" << std::endl; std::cout << std::endl; std::cout << "FSM initiate()" << std::endl; fsm.initiate(); std::cout << "FSM initiate() ... OK" << std::endl; std::cout << "FSM terminate()" << std::endl; fsm.terminate(); std::cout << "FSM terminate() ... OK" << std::endl; }