// Composite_Sate_explicit_entry.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "boost/msm/back/state_machine.hpp" #include "boost/msm/front/state_machine_def.hpp" namespace msm = boost::msm; namespace mpl = boost::mpl; #include namespace test_fsm // Concrete FSM implementation { // events struct event1 {}; struct event2 {}; struct event3 {}; struct event4 {}; // Concrete FSM implementation struct SM1_ : public msm::front::state_machine_def { // no need for exception handling or message queue typedef int no_exception_thrown; typedef int no_message_queue; // The list of FSM states struct Idle : public msm::front::state<> { // optional entry/exit methods template void on_entry(Event const&,FSM& ) {std::cout << "entering: Idle" << std::endl;} template void on_exit(Event const&,FSM& ) {std::cout << "leaving: Idle" << std::endl;} }; struct RunningStateMachine_ : public msm::front::state_machine_def { // no need for exception handling or message queue typedef int no_exception_thrown; typedef int no_message_queue; // optional entry/exit methods template void on_entry(Event const&,FSM& ) {std::cout << "entering: RunningStateMachine" << std::endl;} template void on_exit(Event const&,FSM& ) {std::cout << "leaving: RunningStateMachine" << std::endl;} // The list of FSM states struct InitialState : public msm::front::state<> { // optional entry/exit methods template void on_entry(Event const&,FSM& ) {std::cout << "entering: InitialState" << std::endl;} template void on_exit(Event const&,FSM& ) {std::cout << "leaving: InitialState" << std::endl;} }; struct Inner1_ : public msm::front::state_machine_def , public msm::front::explicit_entry<0> { // no need for exception handling or message queue typedef int no_exception_thrown; typedef int no_message_queue; // optional entry/exit methods template void on_entry(Event const&,FSM& ) {std::cout << "entering: Inner1" << std::endl;} template void on_exit(Event const&,FSM& ) {std::cout << "leaving: Inner1" << std::endl;} // The list of FSM states struct InnerState11 : public msm::front::state<> { // optional entry/exit methods template void on_entry(Event const&,FSM& ) {std::cout << "entering: InnerState11" << std::endl;} template void on_exit(Event const&,FSM& ) {std::cout << "leaving: InnerState11" << std::endl;} }; struct InnerState12 : public msm::front::state<> { // optional entry/exit methods template void on_entry(Event const&,FSM& ) {std::cout << "entering: InnerState12" << std::endl;} template void on_exit(Event const&,FSM& ) {std::cout << "leaving: InnerState12" << std::endl;} }; // the initial state of the player SM. Must be defined typedef InnerState11 initial_state; // transition actions void onEvent3(event3 const&) { std::cout << "Action: Inner1::onEvent3" << std::endl; } struct transition_table : mpl::vector1< // Start Event Next Action Guard // +---------+-------------+---------+---------------------+----------------------+ a_row < InnerState11 , event3 , InnerState12 , &Inner1_::onEvent3 > // +---------+-------------+---------+---------------------+----------------------+ > {}; }; typedef msm::back::state_machine Inner1; struct Inner2_ : public msm::front::state_machine_def , public msm::front::explicit_entry<0> { // no need for exception handling or message queue typedef int no_exception_thrown; typedef int no_message_queue; // optional entry/exit methods template void on_entry(Event const&,FSM& ) {std::cout << "entering: Inner2" << std::endl;} template void on_exit(Event const&,FSM& ) {std::cout << "leaving: Inner2" << std::endl;} // The list of FSM states struct InnerState21 : public msm::front::state<> { // optional entry/exit methods template void on_entry(Event const&,FSM& ) {std::cout << "entering: InnerState21" << std::endl;} template void on_exit(Event const&,FSM& ) {std::cout << "leaving: InnerState21" << std::endl;} }; struct InnerState22 : public msm::front::state<> { // optional entry/exit methods template void on_entry(Event const&,FSM& ) {std::cout << "entering: InnerState22" << std::endl;} template void on_exit(Event const&,FSM& ) {std::cout << "leaving: InnerState22" << std::endl;} }; // the initial state of the player SM. Must be defined typedef InnerState21 initial_state; // transition actions void onEvent3(event3 const&) { std::cout << "Action: Inner2::onEvent3" << std::endl; } struct transition_table : mpl::vector1< // Start Event Next Action Guard // +---------+-------------+---------+---------------------+----------------------+ a_row < InnerState21 , event3 , InnerState22 , &Inner2_::onEvent3 > // +---------+-------------+---------+---------------------+----------------------+ > {}; }; typedef msm::back::state_machine Inner2; // the initial state of the player SM. Must be defined typedef InitialState initial_state; // transition actions void onEvent4(event4 const&) { std::cout << "Action: RunningStateMachine::onEvent4" << std::endl; } struct transition_table : mpl::vector1< // Start Event Next Action Guard // +---------+-------------+---------+---------------------+----------------------+ a_row < InitialState , event4 , InitialState , &RunningStateMachine_::onEvent4 > // +---------+-------------+---------+---------------------+----------------------+ > {}; }; typedef msm::back::state_machine RunningStateMachine; // the initial state of the player SM. Must be defined typedef Idle initial_state; // transition actions void onEvent1(event1 const&) { std::cout << "Action: MS1::onEvent1" << std::endl; } // transition actions void onEvent2(event2 const&) { std::cout << "Action: MS1::onEvent1" << std::endl; } // Transition table for SubFsm2 struct transition_table : mpl::vector2< // Start Event Next Action Guard // +--------------+-------------+------------+------------------------+----------------------+ a_row < Idle , event1 , RunningStateMachine::Inner1::direct ,&SM1_::onEvent1 >, a_row < Idle , event2 , RunningStateMachine::Inner2::direct ,&SM1_::onEvent2 > // +--------------+-------------+------------+------------------------+----------------------+ > {}; }; typedef msm::back::state_machine SM1; } int _tmain(int argc, _TCHAR* argv[]) { test_fsm::SM1 sm; sm.start(); sm.process_event(test_fsm::event1()); return 0; }