1 // Copyright 2010 Christophe Henry
2 // henry UNDERSCORE christophe AT hotmail DOT com
3 // This is an extended version of the state machine available in the boost::mpl library
4 // Distributed under the same license as the original.
5 // Copyright for the original version:
6 // Copyright 2005 David Abrahams and Aleksey Gurtovoy. Distributed
7 // under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
13 #include <boost/msm/back/state_machine.hpp>
14 #include <boost/msm/front/state_machine_def.hpp>
15 #include <boost/msm/front/euml/euml.hpp>
18 namespace msm
= boost::msm
;
19 namespace mpl
= boost::mpl
;
20 using namespace boost::msm::front::euml
;
23 namespace // Concrete FSM implementation
26 BOOST_MSM_EUML_EVENT(connect
)
27 BOOST_MSM_EUML_EVENT(disconnect
)
30 BOOST_MSM_EUML_FLAG(is_connected
)
32 BOOST_MSM_EUML_ACTION(SignalConnect
)
34 template <class FSM
,class EVT
,class SourceState
,class TargetState
>
35 void operator()(EVT
const& ,FSM
& fsm
,SourceState
& ,TargetState
& )
37 // by default, this would be wrong (shows false)
38 cout
<< "SignalConnect. Connected? "
40 << fsm
.template is_flag_active
<BOOST_MSM_EUML_FLAG_NAME(is_connected
)>() << endl
;
43 BOOST_MSM_EUML_ACTION(SignalDisconnect
)
45 template <class FSM
,class EVT
,class SourceState
,class TargetState
>
46 void operator()(EVT
const& ,FSM
& fsm
,SourceState
& ,TargetState
& )
48 // by default, this would be wrong (shows true)
49 cout
<< "SignalDisconnect. Connected? "
51 << fsm
.template is_flag_active
<BOOST_MSM_EUML_FLAG_NAME(is_connected
)>()
56 // The list of FSM states
57 BOOST_MSM_EUML_ACTION(Connected_Entry
)
59 template <class Event
,class FSM
,class STATE
>
60 void operator()(Event
const&,FSM
&,STATE
& )
62 std::cout
<< "entering: Connected" << std::endl
;
65 BOOST_MSM_EUML_ACTION(Connected_Exit
)
67 template <class Event
,class FSM
,class STATE
>
68 void operator()(Event
const&,FSM
&,STATE
& )
70 std::cout
<< "leaving: Connected" << std::endl
;
73 BOOST_MSM_EUML_STATE(( Connected_Entry
,Connected_Exit
,
74 attributes_
<< no_attributes_
,
75 configure_
<< is_connected
),Connected
)
77 BOOST_MSM_EUML_ACTION(Disconnected_Entry
)
79 template <class Event
,class FSM
,class STATE
>
80 void operator()(Event
const&,FSM
&,STATE
& )
82 std::cout
<< "entering: Disconnected" << std::endl
;
85 BOOST_MSM_EUML_ACTION(Disconnected_Exit
)
87 template <class Event
,class FSM
,class STATE
>
88 void operator()(Event
const&,FSM
&,STATE
& )
90 std::cout
<< "leaving: Disconnected" << std::endl
;
93 BOOST_MSM_EUML_STATE(( Disconnected_Entry
,Disconnected_Exit
),Disconnected
)
95 // replaces the old transition table
96 BOOST_MSM_EUML_TRANSITION_TABLE((
97 Disconnected
== Connected
+ disconnect
/ SignalDisconnect
,
98 Connected
== Disconnected
+ connect
/ SignalConnect
99 // +------------------------------------------------------------------------------+
102 BOOST_MSM_EUML_ACTION(Log_No_Transition
)
104 template <class FSM
,class Event
>
105 void operator()(Event
const& e
,FSM
&,int state
)
107 std::cout
<< "no transition from state " << state
108 << " on event " << typeid(e
).name() << std::endl
;
112 // create a state machine "on the fly"
113 BOOST_MSM_EUML_DECLARE_STATE_MACHINE(( transition_table
, //STT
114 init_
<< Disconnected
, // Init State
117 attributes_
<< no_attributes_
, // Attributes
118 configure_
<< switch_active_before_transition
, // configuration
119 Log_No_Transition
// no_transition handler
121 Connection_
) //fsm name
123 typedef msm::back::state_machine
<Connection_
> Connection
;
127 Connection connection
;
128 // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
130 // signal a connection
131 connection
.process_event(connect
);
132 // signal a disconnection
133 connection
.process_event(disconnect
);