Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / msm / back / favor_compile_time.hpp
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_BACK_FAVOR_COMPILE_TIME_H
12 #define BOOST_MSM_BACK_FAVOR_COMPILE_TIME_H
13
14 #include <utility>
15 #include <deque>
16
17 #include <boost/mpl/filter_view.hpp>
18 #include <boost/mpl/for_each.hpp>
19 #include <boost/mpl/bool.hpp>
20 #include <boost/any.hpp>
21
22 #include <boost/msm/common.hpp>
23 #include <boost/msm/back/metafunctions.hpp>
24 #include <boost/msm/back/common_types.hpp>
25 #include <boost/msm/back/dispatch_table.hpp>
26
27 namespace boost { namespace msm { namespace back
28 {
29
30 template <class Fsm>
31 struct process_any_event_helper
32 {
33     process_any_event_helper(msm::back::HandledEnum& res_,Fsm* self_,::boost::any any_event_):
34     res(res_),self(self_),any_event(any_event_),finished(false){}
35     template <class Event>
36     void operator()(boost::msm::wrap<Event> const&)
37     {
38         if ( ! finished && ::boost::any_cast<Event>(&any_event)!=0)
39         {
40             finished = true;
41             res = self->process_event_internal(::boost::any_cast<Event>(any_event));
42         }
43     }
44 private:
45     msm::back::HandledEnum&     res;
46     Fsm*                        self;
47     ::boost::any                any_event;
48     bool                        finished;
49 };
50
51 #define BOOST_MSM_BACK_GENERATE_PROCESS_EVENT(fsmname)                                              \
52     namespace boost { namespace msm { namespace back{                                               \
53     template<>                                                                                      \
54     ::boost::msm::back::HandledEnum fsmname::process_any_event( ::boost::any const& any_event)      \
55     {                                                                                               \
56         typedef ::boost::msm::back::recursive_get_transition_table<fsmname>::type stt;              \
57         typedef ::boost::msm::back::generate_event_set<stt>::type stt_events;                       \
58         typedef ::boost::msm::back::recursive_get_internal_transition_table<fsmname, ::boost::mpl::true_ >::type istt;    \
59         typedef ::boost::msm::back::generate_event_set<create_real_stt<fsmname,istt>::type >::type istt_events;  \
60         typedef ::boost::msm::back::set_insert_range<stt_events,istt_events>::type all_events;      \
61         ::boost::msm::back::HandledEnum res= ::boost::msm::back::HANDLED_FALSE;                     \
62         ::boost::mpl::for_each<all_events, ::boost::msm::wrap< ::boost::mpl::placeholders::_1> >    \
63         (::boost::msm::back::process_any_event_helper<fsmname>(res,this,any_event));                \
64         return res;                                                                                 \
65     }                                                                                               \
66     }}}
67
68 struct favor_compile_time
69 {
70     typedef int compile_policy;
71     typedef ::boost::mpl::false_ add_forwarding_rows;
72 };
73
74 // Generates a singleton runtime lookup table that maps current state
75 // to a function that makes the SM take its transition on the given
76 // Event type.
77 template <class Fsm,class Stt, class Event>
78 struct dispatch_table < Fsm, Stt, Event, ::boost::msm::back::favor_compile_time>
79 {
80  private:
81     // This is a table of these function pointers.
82     typedef HandledEnum (*cell)(Fsm&, int,int,Event const&);
83     typedef bool (*guard)(Fsm&, Event const&);
84
85     // Compute the maximum state value in the sm so we know how big
86     // to make the table
87     typedef typename generate_state_set<Stt>::type state_list;
88     BOOST_STATIC_CONSTANT(int, max_state = ( ::boost::mpl::size<state_list>::value));
89
90     struct chain_row
91     {
92         HandledEnum operator()(Fsm& fsm, int region,int state,Event const& evt) const
93         {
94             HandledEnum res = HANDLED_FALSE;
95             typename std::deque<cell>::const_iterator it = one_state.begin();
96             while (it != one_state.end() && (res != HANDLED_TRUE && res != HANDLED_DEFERRED ))
97             {
98                 HandledEnum handled = (*it)(fsm,region,state,evt);
99                 // reject is considered as erasing an error (HANDLED_FALSE)
100                 if ((HANDLED_FALSE==handled) && (HANDLED_GUARD_REJECT==res) )
101                     res = HANDLED_GUARD_REJECT;
102                 else
103                     res = handled;
104                 ++it;
105             }
106             return res;
107         }
108         std::deque<cell> one_state;
109     };
110     template <class TransitionState>
111     static HandledEnum call_submachine(Fsm& fsm, int , int , Event const& evt)
112     {
113         return (fsm.template get_state<TransitionState&>()).process_any_event( ::boost::any(evt));
114     }
115     // A function object for use with mpl::for_each that stuffs
116     // transitions into cells.
117     struct init_cell
118     {
119         init_cell(dispatch_table* self_)
120           : self(self_)
121         {}
122         // version for transition event not base of our event
123         template <class Transition>
124         typename ::boost::disable_if<
125             typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
126         ,void>::type
127         init_event_base_case(Transition const&, ::boost::mpl::true_ const &) const
128         {
129             typedef typename create_stt<Fsm>::type stt;
130             BOOST_STATIC_CONSTANT(int, state_id =
131                 (get_state_id<stt,typename Transition::current_state_type>::value));
132             self->entries[state_id+1].one_state.push_front(reinterpret_cast<cell>(&Transition::execute));
133         }
134         template <class Transition>
135         typename ::boost::enable_if<
136             typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
137         ,void>::type
138         init_event_base_case(Transition const&, ::boost::mpl::true_ const &) const
139         {
140             self->entries[0].one_state.push_front(reinterpret_cast<cell>(&Transition::execute));
141         }
142
143         // version for transition event base of our event
144         template <class Transition>
145         typename ::boost::disable_if<
146             typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
147         ,void>::type
148         init_event_base_case(Transition const&, ::boost::mpl::false_ const &) const
149         {
150             typedef typename create_stt<Fsm>::type stt;
151             BOOST_STATIC_CONSTANT(int, state_id =
152                 (get_state_id<stt,typename Transition::current_state_type>::value));
153             self->entries[state_id+1].one_state.push_front(&Transition::execute);
154         }
155         template <class Transition>
156         typename ::boost::enable_if<
157             typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
158         ,void>::type
159         init_event_base_case(Transition const&, ::boost::mpl::false_ const &) const
160         {
161             self->entries[0].one_state.push_front(&Transition::execute);
162         }
163         // Cell initializer function object, used with mpl::for_each
164         template <class Transition>
165         typename ::boost::enable_if<typename has_not_real_row_tag<Transition>::type,void >::type
166             operator()(Transition const&,boost::msm::back::dummy<0> = 0) const
167         {
168             // version for not real rows. No problem because irrelevant for process_event
169         }
170         template <class Transition>
171         typename ::boost::disable_if<typename has_not_real_row_tag<Transition>::type,void >::type
172         operator()(Transition const& tr,boost::msm::back::dummy<1> = 0) const
173         {
174             //only if the transition event is a base of our event is the reinterpret_case safe
175             init_event_base_case(tr,
176                 ::boost::mpl::bool_<
177                     ::boost::is_base_of<typename Transition::transition_event,Event>::type::value>() );
178         }
179
180         dispatch_table* self;
181     };
182
183     // Cell default-initializer function object, used with mpl::for_each
184     // initializes with call_no_transition, defer_transition or default_eventless_transition
185     // variant for non-anonymous transitions
186     template <class EventType,class Enable=void>
187     struct default_init_cell
188     {
189         default_init_cell(dispatch_table* self_,chain_row* tofill_entries_)
190             : self(self_),tofill_entries(tofill_entries_)
191         {}
192         template <bool deferred,bool composite, int some_dummy=0>
193         struct helper
194         {};
195         template <int some_dummy> struct helper<true,false,some_dummy>
196         {
197             template <class State>
198             static void execute(boost::msm::wrap<State> const&,chain_row* tofill)
199             {
200                 typedef typename create_stt<Fsm>::type stt;
201                 BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,State>::value));
202                 cell call_no_transition = &Fsm::defer_transition;
203                 tofill[state_id+1].one_state.push_back(call_no_transition);
204             }
205         };
206         template <int some_dummy> struct helper<true,true,some_dummy>
207         {
208             template <class State>
209             static void execute(boost::msm::wrap<State> const&,chain_row* tofill)
210             {
211                 typedef typename create_stt<Fsm>::type stt;
212                 BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,State>::value));
213                 cell call_no_transition = &Fsm::defer_transition;
214                 tofill[state_id+1].one_state.push_back(call_no_transition);
215             }
216         };
217         template <int some_dummy> struct helper<false,true,some_dummy>
218         {
219             template <class State>
220             static
221             typename ::boost::enable_if<
222                 typename ::boost::is_same<State,Fsm>::type
223             ,void>::type
224             execute(boost::msm::wrap<State> const&,chain_row* tofill,boost::msm::back::dummy<0> = 0)
225             {
226                 // for internal tables
227                 cell call_no_transition_internal = &Fsm::call_no_transition;
228                 tofill[0].one_state.push_front(call_no_transition_internal);
229             }
230             template <class State>
231             static
232             typename ::boost::disable_if<
233                 typename ::boost::is_same<State,Fsm>::type
234             ,void>::type
235             execute(boost::msm::wrap<State> const&,chain_row* tofill,boost::msm::back::dummy<1> = 0)
236             {
237                 typedef typename create_stt<Fsm>::type stt;
238                 BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,State>::value));
239                 cell call_no_transition = &call_submachine< State >;
240                 tofill[state_id+1].one_state.push_front(call_no_transition);
241             }
242         };
243         template <int some_dummy> struct helper<false,false,some_dummy>
244         {
245             template <class State>
246             static void execute(boost::msm::wrap<State> const&,chain_row* tofill)
247             {
248                 typedef typename create_stt<Fsm>::type stt;
249                 BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,State>::value));
250                 cell call_no_transition = &Fsm::call_no_transition;
251                 tofill[state_id+1].one_state.push_back(call_no_transition);
252             }
253         };
254         template <class State>
255         void operator()(boost::msm::wrap<State> const& s)
256         {
257             helper<has_state_delayed_event<State,Event>::type::value,
258                    is_composite_state<State>::type::value>::execute(s,tofill_entries);
259         }
260         dispatch_table* self;
261         chain_row* tofill_entries;
262     };
263
264     // variant for anonymous transitions
265     template <class EventType>
266     struct default_init_cell<EventType,
267                              typename ::boost::enable_if<
268                                 typename is_completion_event<EventType>::type>::type>
269     {
270         default_init_cell(dispatch_table* self_,chain_row* tofill_entries_)
271             : self(self_),tofill_entries(tofill_entries_)
272         {}
273
274         // this event is a compound one (not a real one, just one for use in event-less transitions)
275         // Note this event cannot be used as deferred!
276         template <class State>
277         void operator()(boost::msm::wrap<State> const&)
278         {
279             typedef typename create_stt<Fsm>::type stt;
280             BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,State>::value));
281             cell call_no_transition = &Fsm::default_eventless_transition;
282             tofill_entries[state_id+1].one_state.push_back(call_no_transition);
283         }
284
285         dispatch_table* self;
286         chain_row* tofill_entries;
287     };
288
289  public:
290     // initialize the dispatch table for a given Event and Fsm
291     dispatch_table()
292     {
293         // Initialize cells for no transition
294         ::boost::mpl::for_each<
295             ::boost::mpl::filter_view<
296                     Stt, ::boost::is_base_of<transition_event< ::boost::mpl::placeholders::_>, Event> > >
297         (init_cell(this));
298
299         ::boost::mpl::for_each<
300             typename generate_state_set<Stt>::type,
301             boost::msm::wrap< ::boost::mpl::placeholders::_1> >
302          (default_init_cell<Event>(this,entries));
303
304     }
305
306     // The singleton instance.
307     static const dispatch_table instance;
308
309  public: // data members
310      chain_row entries[max_state+1];
311 };
312
313 template <class Fsm,class Stt, class Event>
314 const boost::msm::back::dispatch_table<Fsm,Stt, Event,favor_compile_time>
315 dispatch_table<Fsm,Stt, Event,favor_compile_time>::instance;
316
317 }}} // boost::msm::back
318
319 #endif //BOOST_MSM_BACK_FAVOR_COMPILE_TIME_H