Chris@16
|
1 // Copyright 2008 Christophe Henry
|
Chris@16
|
2 // henry UNDERSCORE christophe AT hotmail DOT com
|
Chris@16
|
3 // This is an extended version of the state machine available in the boost::mpl library
|
Chris@16
|
4 // Distributed under the same license as the original.
|
Chris@16
|
5 // Copyright for the original version:
|
Chris@16
|
6 // Copyright 2005 David Abrahams and Aleksey Gurtovoy. Distributed
|
Chris@16
|
7 // under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
8 // file LICENSE_1_0.txt or copy at
|
Chris@16
|
9 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_MSM_FRONT_STATEMACHINE_DEF_H
|
Chris@16
|
12 #define BOOST_MSM_FRONT_STATEMACHINE_DEF_H
|
Chris@16
|
13
|
Chris@16
|
14 #include <exception>
|
Chris@16
|
15 #include <boost/assert.hpp>
|
Chris@16
|
16
|
Chris@16
|
17 #include <boost/mpl/vector.hpp>
|
Chris@16
|
18
|
Chris@16
|
19 #include <boost/msm/row_tags.hpp>
|
Chris@16
|
20 #include <boost/msm/back/common_types.hpp>
|
Chris@16
|
21 #include <boost/msm/front/states.hpp>
|
Chris@16
|
22 #include <boost/msm/front/completion_event.hpp>
|
Chris@16
|
23 #include <boost/msm/front/common_states.hpp>
|
Chris@16
|
24
|
Chris@16
|
25 namespace boost { namespace msm { namespace front
|
Chris@16
|
26 {
|
Chris@16
|
27
|
Chris@16
|
28 template<class Derived,class BaseState = default_base_state>
|
Chris@16
|
29 struct state_machine_def : public boost::msm::front::detail::state_base<BaseState>
|
Chris@16
|
30 {
|
Chris@16
|
31 // tags
|
Chris@16
|
32 // default: no flag
|
Chris@16
|
33 typedef ::boost::mpl::vector0<> flag_list;
|
Chris@16
|
34 typedef ::boost::mpl::vector0<> internal_flag_list;
|
Chris@16
|
35 //default: no deferred events
|
Chris@16
|
36 typedef ::boost::mpl::vector0<> deferred_events;
|
Chris@16
|
37 // customization (message queue, exceptions)
|
Chris@16
|
38 typedef ::boost::mpl::vector0<> configuration;
|
Chris@16
|
39
|
Chris@16
|
40 typedef BaseState BaseAllStates;
|
Chris@16
|
41 template<
|
Chris@16
|
42 typename T1
|
Chris@16
|
43 , class Event
|
Chris@16
|
44 , typename T2
|
Chris@16
|
45 , void (Derived::*action)(Event const&)
|
Chris@16
|
46 >
|
Chris@16
|
47 struct a_row
|
Chris@16
|
48 {
|
Chris@16
|
49 typedef a_row_tag row_type_tag;
|
Chris@16
|
50 typedef T1 Source;
|
Chris@16
|
51 typedef T2 Target;
|
Chris@16
|
52 typedef Event Evt;
|
Chris@16
|
53 template <class FSM,class SourceState,class TargetState,class AllStates>
|
Chris@16
|
54 static ::boost::msm::back::HandledEnum action_call(FSM& fsm,Event const& evt,SourceState&,TargetState&, AllStates&)
|
Chris@16
|
55 {
|
Chris@16
|
56 // in this front-end, we don't need to know source and target states
|
Chris@16
|
57 (fsm.*action)(evt);
|
Chris@16
|
58 return ::boost::msm::back::HANDLED_TRUE;
|
Chris@16
|
59 }
|
Chris@16
|
60 };
|
Chris@16
|
61
|
Chris@16
|
62 template<
|
Chris@16
|
63 typename T1
|
Chris@16
|
64 , class Event
|
Chris@16
|
65 , typename T2
|
Chris@16
|
66 >
|
Chris@16
|
67 struct _row
|
Chris@16
|
68 {
|
Chris@16
|
69 typedef _row_tag row_type_tag;
|
Chris@16
|
70 typedef T1 Source;
|
Chris@16
|
71 typedef T2 Target;
|
Chris@16
|
72 typedef Event Evt;
|
Chris@16
|
73 };
|
Chris@16
|
74
|
Chris@16
|
75 template<
|
Chris@16
|
76 typename T1
|
Chris@16
|
77 , class Event
|
Chris@16
|
78 , typename T2
|
Chris@16
|
79 , void (Derived::*action)(Event const&)
|
Chris@16
|
80 , bool (Derived::*guard)(Event const&)
|
Chris@16
|
81 >
|
Chris@16
|
82 struct row
|
Chris@16
|
83 {
|
Chris@16
|
84 typedef row_tag row_type_tag;
|
Chris@16
|
85 typedef T1 Source;
|
Chris@16
|
86 typedef T2 Target;
|
Chris@16
|
87 typedef Event Evt;
|
Chris@16
|
88 template <class FSM,class SourceState,class TargetState, class AllStates>
|
Chris@16
|
89 static ::boost::msm::back::HandledEnum action_call(FSM& fsm,Event const& evt,SourceState&,TargetState&,AllStates&)
|
Chris@16
|
90 {
|
Chris@16
|
91 // in this front-end, we don't need to know source and target states
|
Chris@16
|
92 (fsm.*action)(evt);
|
Chris@16
|
93 return ::boost::msm::back::HANDLED_TRUE;
|
Chris@16
|
94 }
|
Chris@16
|
95 template <class FSM,class SourceState,class TargetState,class AllStates>
|
Chris@16
|
96 static bool guard_call(FSM& fsm,Event const& evt,SourceState&,TargetState&,AllStates&)
|
Chris@16
|
97 {
|
Chris@16
|
98 // in this front-end, we don't need to know source and target states
|
Chris@16
|
99 return (fsm.*guard)(evt);
|
Chris@16
|
100 }
|
Chris@16
|
101 };
|
Chris@16
|
102 template<
|
Chris@16
|
103 typename T1
|
Chris@16
|
104 , class Event
|
Chris@16
|
105 , typename T2
|
Chris@16
|
106 , bool (Derived::*guard)(Event const&)
|
Chris@16
|
107 >
|
Chris@16
|
108 struct g_row
|
Chris@16
|
109 {
|
Chris@16
|
110 typedef g_row_tag row_type_tag;
|
Chris@16
|
111 typedef T1 Source;
|
Chris@16
|
112 typedef T2 Target;
|
Chris@16
|
113 typedef Event Evt;
|
Chris@16
|
114 template <class FSM,class SourceState,class TargetState,class AllStates>
|
Chris@16
|
115 static bool guard_call(FSM& fsm,Event const& evt,SourceState&,TargetState&,AllStates&)
|
Chris@16
|
116 {
|
Chris@16
|
117 // in this front-end, we don't need to know source and target states
|
Chris@16
|
118 return (fsm.*guard)(evt);
|
Chris@16
|
119 }
|
Chris@16
|
120 };
|
Chris@16
|
121 // internal transitions
|
Chris@16
|
122 template<
|
Chris@16
|
123 typename T1
|
Chris@16
|
124 , class Event
|
Chris@16
|
125 , void (Derived::*action)(Event const&)
|
Chris@16
|
126 >
|
Chris@16
|
127 struct a_irow
|
Chris@16
|
128 {
|
Chris@16
|
129 typedef a_irow_tag row_type_tag;
|
Chris@16
|
130 typedef T1 Source;
|
Chris@16
|
131 typedef T1 Target;
|
Chris@16
|
132 typedef Event Evt;
|
Chris@16
|
133 template <class FSM,class SourceState,class TargetState,class AllStates>
|
Chris@16
|
134 static ::boost::msm::back::HandledEnum action_call(FSM& fsm,Event const& evt,SourceState&,TargetState&,AllStates&)
|
Chris@16
|
135 {
|
Chris@16
|
136 // in this front-end, we don't need to know source and target states
|
Chris@16
|
137 (fsm.*action)(evt);
|
Chris@16
|
138 return ::boost::msm::back::HANDLED_TRUE;
|
Chris@16
|
139 }
|
Chris@16
|
140 };
|
Chris@16
|
141
|
Chris@16
|
142 template<
|
Chris@16
|
143 typename T1
|
Chris@16
|
144 , class Event
|
Chris@16
|
145 , void (Derived::*action)(Event const&)
|
Chris@16
|
146 , bool (Derived::*guard)(Event const&)
|
Chris@16
|
147 >
|
Chris@16
|
148 struct irow
|
Chris@16
|
149 {
|
Chris@16
|
150 typedef irow_tag row_type_tag;
|
Chris@16
|
151 typedef T1 Source;
|
Chris@16
|
152 typedef T1 Target;
|
Chris@16
|
153 typedef Event Evt;
|
Chris@16
|
154 template <class FSM,class SourceState,class TargetState,class AllStates>
|
Chris@16
|
155 static ::boost::msm::back::HandledEnum action_call(FSM& fsm,Event const& evt,SourceState&,TargetState&,AllStates&)
|
Chris@16
|
156 {
|
Chris@16
|
157 // in this front-end, we don't need to know source and target states
|
Chris@16
|
158 (fsm.*action)(evt);
|
Chris@16
|
159 return ::boost::msm::back::HANDLED_TRUE;
|
Chris@16
|
160 }
|
Chris@16
|
161 template <class FSM,class SourceState,class TargetState,class AllStates>
|
Chris@16
|
162 static bool guard_call(FSM& fsm,Event const& evt,SourceState&,TargetState&,AllStates&)
|
Chris@16
|
163 {
|
Chris@16
|
164 // in this front-end, we don't need to know source and target states
|
Chris@16
|
165 return (fsm.*guard)(evt);
|
Chris@16
|
166 }
|
Chris@16
|
167 };
|
Chris@16
|
168 template<
|
Chris@16
|
169 typename T1
|
Chris@16
|
170 , class Event
|
Chris@16
|
171 , bool (Derived::*guard)(Event const&)
|
Chris@16
|
172 >
|
Chris@16
|
173 struct g_irow
|
Chris@16
|
174 {
|
Chris@16
|
175 typedef g_irow_tag row_type_tag;
|
Chris@16
|
176 typedef T1 Source;
|
Chris@16
|
177 typedef T1 Target;
|
Chris@16
|
178 typedef Event Evt;
|
Chris@16
|
179 template <class FSM,class SourceState,class TargetState,class AllStates>
|
Chris@16
|
180 static bool guard_call(FSM& fsm,Event const& evt,SourceState&,TargetState&,AllStates&)
|
Chris@16
|
181 {
|
Chris@16
|
182 // in this front-end, we don't need to know source and target states
|
Chris@16
|
183 return (fsm.*guard)(evt);
|
Chris@16
|
184 }
|
Chris@16
|
185 };
|
Chris@16
|
186 // internal row withou action or guard. Does nothing except forcing the event to be ignored.
|
Chris@16
|
187 template<
|
Chris@16
|
188 typename T1
|
Chris@16
|
189 , class Event
|
Chris@16
|
190 >
|
Chris@16
|
191 struct _irow
|
Chris@16
|
192 {
|
Chris@16
|
193 typedef _irow_tag row_type_tag;
|
Chris@16
|
194 typedef T1 Source;
|
Chris@16
|
195 typedef T1 Target;
|
Chris@16
|
196 typedef Event Evt;
|
Chris@16
|
197 };
|
Chris@16
|
198 protected:
|
Chris@16
|
199 // Default no-transition handler. Can be replaced in the Derived SM class.
|
Chris@16
|
200 template <class FSM,class Event>
|
Chris@16
|
201 void no_transition(Event const& ,FSM&, int )
|
Chris@16
|
202 {
|
Chris@16
|
203 BOOST_ASSERT(false);
|
Chris@16
|
204 }
|
Chris@16
|
205 // default exception handler. Can be replaced in the Derived SM class.
|
Chris@16
|
206 template <class FSM,class Event>
|
Chris@16
|
207 void exception_caught (Event const&,FSM&,std::exception& )
|
Chris@16
|
208 {
|
Chris@16
|
209 BOOST_ASSERT(false);
|
Chris@16
|
210 }
|
Chris@16
|
211 };
|
Chris@16
|
212
|
Chris@16
|
213
|
Chris@16
|
214 } } }// boost::msm::front
|
Chris@16
|
215 #endif //BOOST_MSM_FRONT_STATEMACHINE_DEF_H
|
Chris@16
|
216
|