PageRenderTime 51ms CodeModel.GetById 23ms app.highlight 24ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/msm/front/functor_row.hpp

http://hadesmem.googlecode.com/
C++ Header | 338 lines | 287 code | 15 blank | 36 comment | 0 complexity | 935ac834e6a5f8e499a489d86f33f3a0 MD5 | raw file
  1// Copyright 2008 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)
 10
 11#ifndef BOOST_MSM_FRONT_FUNCTOR_ROW_H
 12#define BOOST_MSM_FRONT_FUNCTOR_ROW_H
 13
 14#include <boost/mpl/set.hpp>
 15#include <boost/mpl/for_each.hpp>
 16#include <boost/mpl/has_xxx.hpp>
 17
 18#include <boost/typeof/typeof.hpp>
 19
 20#include <boost/msm/back/common_types.hpp>
 21#include <boost/msm/row_tags.hpp>
 22#include <boost/msm/common.hpp>
 23#include <boost/msm/front/completion_event.hpp>
 24
 25#include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
 26
 27BOOST_MPL_HAS_XXX_TRAIT_DEF(deferring_action)
 28
 29namespace boost { namespace msm { namespace front
 30{
 31    template <class Func,class Enable=void>
 32    struct get_functor_return_value 
 33    {
 34        static const ::boost::msm::back::HandledEnum value = ::boost::msm::back::HANDLED_TRUE;
 35    };
 36    template <class Func>
 37    struct get_functor_return_value<Func, 
 38        typename ::boost::enable_if<
 39            typename has_deferring_action<Func>::type 
 40        >::type
 41    > 
 42    {
 43        static const ::boost::msm::back::HandledEnum value = ::boost::msm::back::HANDLED_DEFERRED;
 44    };
 45
 46    template <class SOURCE,class EVENT,class TARGET,class ACTION=none,class GUARD=none>
 47    struct Row
 48    {
 49        typedef SOURCE  Source;
 50        typedef EVENT   Evt;
 51        typedef TARGET  Target;
 52        typedef ACTION  Action;
 53        typedef GUARD   Guard;
 54        // action plus guard
 55        typedef row_tag row_type_tag;
 56        template <class EVT,class FSM,class SourceState,class TargetState,class AllStates>
 57        static ::boost::msm::back::HandledEnum action_call(FSM& fsm,EVT const& evt,SourceState& src,TargetState& tgt, AllStates&)
 58        {
 59            // create functor, call it
 60            Action()(evt,fsm,src,tgt);
 61            return get_functor_return_value<Action>::value;
 62        }
 63        template <class EVT,class FSM,class SourceState,class TargetState,class AllStates>
 64        static bool guard_call(FSM& fsm,EVT const& evt,SourceState& src,TargetState& tgt,AllStates&)
 65        {
 66            // create functor, call it
 67            return Guard()(evt,fsm,src,tgt);
 68        }
 69    };
 70
 71    template<class SOURCE,class EVENT,class TARGET>
 72    struct Row<SOURCE,EVENT,TARGET,none,none>
 73    {
 74        typedef SOURCE  Source;
 75        typedef EVENT   Evt;
 76        typedef TARGET  Target;
 77        typedef none    Action;
 78        typedef none    Guard;
 79        // no action, no guard
 80        typedef _row_tag row_type_tag;
 81    };
 82    template<class SOURCE,class EVENT,class TARGET,class ACTION>
 83    struct Row<SOURCE,EVENT,TARGET,ACTION,none>
 84    {
 85        typedef SOURCE  Source;
 86        typedef EVENT   Evt;
 87        typedef TARGET  Target;
 88        typedef ACTION  Action;
 89        typedef none    Guard;
 90        // no guard
 91        typedef a_row_tag row_type_tag;
 92        template <class EVT,class FSM,class SourceState,class TargetState,class AllStates>
 93        static ::boost::msm::back::HandledEnum action_call(FSM& fsm,EVT const& evt,SourceState& src,TargetState& tgt, AllStates&)
 94        {
 95            // create functor, call it
 96            Action()(evt,fsm,src,tgt);
 97            return get_functor_return_value<Action>::value;
 98        }
 99    };
100    template<class SOURCE,class EVENT,class TARGET,class GUARD>
101    struct Row<SOURCE,EVENT,TARGET,none,GUARD>
102    {
103        typedef SOURCE  Source;
104        typedef EVENT   Evt;
105        typedef TARGET  Target;
106        typedef none    Action;
107        typedef GUARD   Guard;
108        // no action
109        typedef g_row_tag row_type_tag;
110        template <class EVT,class FSM,class SourceState,class TargetState,class AllStates>
111        static bool guard_call(FSM& fsm,EVT const& evt,SourceState& src,TargetState& tgt, AllStates&)
112        {
113            // create functor, call it
114            return Guard()(evt,fsm,src,tgt);
115        }
116    };
117    // internal transitions
118    template<class SOURCE,class EVENT,class ACTION>
119    struct Row<SOURCE,EVENT,none,ACTION,none>
120    {
121        typedef SOURCE  Source;
122        typedef EVENT   Evt;
123        typedef Source  Target;
124        typedef ACTION  Action;
125        typedef none    Guard;
126        // no guard
127        typedef a_irow_tag row_type_tag;
128        template <class EVT,class FSM,class SourceState,class TargetState,class AllStates>
129        static ::boost::msm::back::HandledEnum action_call(FSM& fsm,EVT const& evt,SourceState& src,TargetState& tgt, AllStates&)
130        {
131            // create functor, call it
132            Action()(evt,fsm,src,tgt);
133            return get_functor_return_value<Action>::value;
134        }
135    };
136    template<class SOURCE,class EVENT,class GUARD>
137    struct Row<SOURCE,EVENT,none,none,GUARD>
138    {
139        typedef SOURCE  Source;
140        typedef EVENT   Evt;
141        typedef Source  Target;
142        typedef none    Action;
143        typedef GUARD   Guard;
144        // no action
145        typedef g_irow_tag row_type_tag;
146        template <class EVT,class FSM,class SourceState,class TargetState,class AllStates>
147        static bool guard_call(FSM& fsm,EVT const& evt,SourceState& src,TargetState& tgt, AllStates&)
148        {
149            // create functor, call it
150            return Guard()(evt,fsm,src,tgt);
151        }
152    };
153    template<class SOURCE,class EVENT,class ACTION,class GUARD>
154    struct Row<SOURCE,EVENT,none,ACTION,GUARD>
155    {
156        typedef SOURCE  Source;
157        typedef EVENT   Evt;
158        typedef Source  Target;
159        typedef ACTION  Action;
160        typedef GUARD   Guard;
161        // action + guard
162        typedef irow_tag row_type_tag;
163        template <class EVT,class FSM,class SourceState,class TargetState,class AllStates>
164        static ::boost::msm::back::HandledEnum action_call(FSM& fsm,EVT const& evt,SourceState& src,TargetState& tgt, AllStates&)
165        {
166            // create functor, call it
167            Action()(evt,fsm,src,tgt);
168            return get_functor_return_value<Action>::value;
169        }
170        template <class EVT,class FSM,class SourceState,class TargetState,class AllStates>
171        static bool guard_call(FSM& fsm,EVT const& evt,SourceState& src,TargetState& tgt, AllStates&)
172        {
173            // create functor, call it
174            return Guard()(evt,fsm,src,tgt);
175        }
176    };
177    template<class SOURCE,class EVENT>
178    struct Row<SOURCE,EVENT,none,none,none>
179    {
180        typedef SOURCE  Source;
181        typedef EVENT   Evt;
182        typedef Source  Target;
183        typedef none    Action;
184        typedef none    Guard;
185        // no action, no guard
186        typedef _irow_tag row_type_tag;
187    };
188    template<class TGT>
189    struct get_row_target
190    {
191        typedef typename TGT::Target type;
192    };
193
194    template <class EVENT,class ACTION=none,class GUARD=none>
195    struct Internal
196    {
197        typedef EVENT   Evt;
198        typedef ACTION  Action;
199        typedef GUARD   Guard;
200        // action plus guard
201        typedef sm_i_row_tag row_type_tag;
202        template <class EVT,class FSM,class SourceState,class TargetState,class AllStates>
203        static ::boost::msm::back::HandledEnum action_call(FSM& fsm,EVT const& evt,SourceState& src,TargetState& tgt, AllStates&)
204        {
205            // create functor, call it
206            Action()(evt,fsm,src,tgt);
207            return get_functor_return_value<Action>::value;
208        }
209        template <class EVT,class FSM,class SourceState,class TargetState,class AllStates>
210        static bool guard_call(FSM& fsm,EVT const& evt,SourceState& src,TargetState& tgt, AllStates&)
211        {
212            // create functor, call it
213            return Guard()(evt,fsm,src,tgt);
214        }
215    };
216
217    template<class EVENT,class ACTION>
218    struct Internal<EVENT,ACTION,none>
219    {
220        typedef EVENT   Evt;
221        typedef ACTION  Action;
222        typedef none    Guard;
223        // no guard
224        typedef sm_a_i_row_tag row_type_tag;
225        template <class EVT,class FSM,class SourceState,class TargetState,class AllStates>
226        static ::boost::msm::back::HandledEnum action_call(FSM& fsm,EVT const& evt,SourceState& src,TargetState& tgt, AllStates&)
227        {
228            // create functor, call it
229            Action()(evt,fsm,src,tgt);
230            return get_functor_return_value<Action>::value;
231        }
232    };
233    template<class EVENT,class GUARD>
234    struct Internal<EVENT,none,GUARD>
235    {
236        typedef EVENT   Evt;
237        typedef none    Action;
238        typedef GUARD   Guard;
239        // no action
240        typedef sm_g_i_row_tag row_type_tag;
241        template <class EVT,class FSM,class SourceState,class TargetState,class AllStates>
242        static bool guard_call(FSM& fsm,EVT const& evt,SourceState& src,TargetState& tgt, AllStates&)
243        {
244            // create functor, call it
245            return Guard()(evt,fsm,src,tgt);
246        }
247    };
248    template<class EVENT>
249    struct Internal<EVENT,none,none>
250    {
251        typedef EVENT   Evt;
252        typedef none    Action;
253        typedef none    Guard;
254        // no action, no guard
255        typedef sm__i_row_tag row_type_tag;
256    };
257    struct event_tag{};
258    struct action_tag{};
259    struct state_action_tag{};
260    struct flag_tag{};
261    struct config_tag{};
262    struct not_euml_tag{};
263
264    template <class Sequence>
265    struct ActionSequence_
266    {
267        typedef Sequence sequence;
268        template <class Event,class FSM,class STATE >
269        struct state_action_result 
270        {
271            typedef void type;
272        };
273        template <class EVT,class FSM,class STATE>
274        struct Call
275        {
276            Call(EVT const& evt,FSM& fsm,STATE& state):
277        evt_(evt),fsm_(fsm),state_(state){}
278        template <class FCT>
279        void operator()(::boost::msm::wrap<FCT> const& )
280        {
281            FCT()(evt_,fsm_,state_);
282        }
283        private:
284            EVT const&  evt_;
285            FSM&        fsm_;
286            STATE&      state_;
287        };
288        template <class EVT,class FSM,class SourceState,class TargetState>
289        struct transition_action_result 
290        {
291            typedef void type;
292        };
293        template <class EVT,class FSM,class SourceState,class TargetState>
294        struct Call2
295        {
296            Call2(EVT const& evt,FSM& fsm,SourceState& src,TargetState& tgt):
297        evt_(evt),fsm_(fsm),src_(src),tgt_(tgt){}
298        template <class FCT>
299        void operator()(::boost::msm::wrap<FCT> const& )
300        {
301            FCT()(evt_,fsm_,src_,tgt_);
302        }
303        private:
304            EVT const & evt_;
305            FSM& fsm_;
306            SourceState& src_;
307            TargetState& tgt_;
308        };
309
310        typedef ::boost::mpl::set<state_action_tag,action_tag> tag_type;
311
312        template <class EVT,class FSM,class STATE>
313        void operator()(EVT const& evt,FSM& fsm,STATE& state)
314        {
315            mpl::for_each<Sequence,boost::msm::wrap< ::boost::mpl::placeholders::_1> >
316                (Call<EVT,FSM,STATE>(evt,fsm,state));
317        }
318        template <class EVT,class FSM,class SourceState,class TargetState>
319        void operator()(EVT const& evt,FSM& fsm,SourceState& src,TargetState& tgt)
320        {
321            mpl::for_each<Sequence,boost::msm::wrap< ::boost::mpl::placeholders::_1> >
322                (Call2<EVT,FSM,SourceState,TargetState>(evt,fsm,src,tgt));
323        }
324    };
325
326    // functor pre-defined for basic functionality
327    struct Defer 
328    {
329        // mark as deferring to avoid stack overflows in certain conditions
330        typedef int deferring_action;
331        template <class EVT,class FSM,class SourceState,class TargetState>
332        void operator()(EVT const& evt,FSM& fsm,SourceState& ,TargetState& ) const
333        {
334            fsm.defer_event(evt);
335        }
336    };
337}}}
338#endif //BOOST_MSM_FRONT_FUNCTOR_ROW_H