[29] | 1 | #ifndef BOOST_STATECHART_TRANSITION_HPP_INCLUDED |
---|
| 2 | #define BOOST_STATECHART_TRANSITION_HPP_INCLUDED |
---|
| 3 | ////////////////////////////////////////////////////////////////////////////// |
---|
| 4 | // Copyright 2002-2006 Andreas Huber Doenni |
---|
| 5 | // Distributed under the Boost Software License, Version 1.0. (See accompany- |
---|
| 6 | // ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
---|
| 7 | ////////////////////////////////////////////////////////////////////////////// |
---|
| 8 | |
---|
| 9 | |
---|
| 10 | |
---|
| 11 | #include <boost/statechart/result.hpp> |
---|
| 12 | |
---|
| 13 | #include <boost/mpl/if.hpp> |
---|
| 14 | |
---|
| 15 | #include <boost/cast.hpp> // boost::polymorphic_downcast |
---|
| 16 | #include <boost/type_traits/is_same.hpp> |
---|
| 17 | |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | namespace boost |
---|
| 21 | { |
---|
| 22 | namespace statechart |
---|
| 23 | { |
---|
| 24 | namespace detail |
---|
| 25 | { |
---|
| 26 | |
---|
| 27 | |
---|
| 28 | |
---|
| 29 | ////////////////////////////////////////////////////////////////////////////// |
---|
| 30 | template< class Event > |
---|
| 31 | struct no_context |
---|
| 32 | { |
---|
| 33 | void no_function( const Event & ); |
---|
| 34 | }; |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | |
---|
| 38 | } // namespace detail |
---|
| 39 | |
---|
| 40 | |
---|
| 41 | |
---|
| 42 | class event_base; |
---|
| 43 | |
---|
| 44 | ////////////////////////////////////////////////////////////////////////////// |
---|
| 45 | template< class Event, class Destination, |
---|
| 46 | class TransitionContext = detail::no_context< Event >, |
---|
| 47 | void ( TransitionContext::*pTransitionAction )( const Event & ) = |
---|
| 48 | &detail::no_context< Event >::no_function > |
---|
| 49 | class transition |
---|
| 50 | { |
---|
| 51 | private: |
---|
| 52 | ////////////////////////////////////////////////////////////////////////// |
---|
| 53 | struct react_without_transition_action_impl |
---|
| 54 | { |
---|
| 55 | template< class State, class EventBase > |
---|
| 56 | static detail::reaction_result react( State & stt, const EventBase & ) |
---|
| 57 | { |
---|
| 58 | return detail::result_utility::get_result( |
---|
| 59 | stt.template transit< Destination >() ); |
---|
| 60 | } |
---|
| 61 | }; |
---|
| 62 | |
---|
| 63 | struct react_base_with_transition_action_impl |
---|
| 64 | { |
---|
| 65 | template< class State, class EventBase > |
---|
| 66 | static detail::reaction_result react( |
---|
| 67 | State & stt, const EventBase & toEvent ) |
---|
| 68 | { |
---|
| 69 | return detail::result_utility::get_result( |
---|
| 70 | stt.template transit< Destination >( pTransitionAction, toEvent ) ); |
---|
| 71 | } |
---|
| 72 | }; |
---|
| 73 | |
---|
| 74 | struct react_base |
---|
| 75 | { |
---|
| 76 | template< class State, class EventBase, class IdType > |
---|
| 77 | static detail::reaction_result react( |
---|
| 78 | State & stt, const EventBase & evt, const IdType & ) |
---|
| 79 | { |
---|
| 80 | typedef typename mpl::if_< |
---|
| 81 | is_same< TransitionContext, detail::no_context< Event > >, |
---|
| 82 | react_without_transition_action_impl, |
---|
| 83 | react_base_with_transition_action_impl |
---|
| 84 | >::type impl; |
---|
| 85 | return impl::react( stt, evt ); |
---|
| 86 | } |
---|
| 87 | }; |
---|
| 88 | |
---|
| 89 | struct react_derived_with_transition_action_impl |
---|
| 90 | { |
---|
| 91 | template< class State, class EventBase > |
---|
| 92 | static detail::reaction_result react( |
---|
| 93 | State & stt, const EventBase & toEvent ) |
---|
| 94 | { |
---|
| 95 | return detail::result_utility::get_result( |
---|
| 96 | stt.template transit< Destination >( |
---|
| 97 | pTransitionAction, |
---|
| 98 | *polymorphic_downcast< const Event * >( &toEvent ) ) ); |
---|
| 99 | } |
---|
| 100 | }; |
---|
| 101 | |
---|
| 102 | struct react_derived |
---|
| 103 | { |
---|
| 104 | template< class State, class EventBase, class IdType > |
---|
| 105 | static detail::reaction_result react( |
---|
| 106 | State & stt, const EventBase & evt, const IdType & eventType ) |
---|
| 107 | { |
---|
| 108 | if ( eventType == Event::static_type() ) |
---|
| 109 | { |
---|
| 110 | typedef typename mpl::if_< |
---|
| 111 | is_same< TransitionContext, detail::no_context< Event > >, |
---|
| 112 | react_without_transition_action_impl, |
---|
| 113 | react_derived_with_transition_action_impl |
---|
| 114 | >::type impl; |
---|
| 115 | return impl::react( stt, evt ); |
---|
| 116 | } |
---|
| 117 | else |
---|
| 118 | { |
---|
| 119 | return detail::no_reaction; |
---|
| 120 | } |
---|
| 121 | } |
---|
| 122 | }; |
---|
| 123 | |
---|
| 124 | public: |
---|
| 125 | ////////////////////////////////////////////////////////////////////////// |
---|
| 126 | // The following declarations should be private. |
---|
| 127 | // They are only public because many compilers lack template friends. |
---|
| 128 | ////////////////////////////////////////////////////////////////////////// |
---|
| 129 | template< class State, class EventBase, class IdType > |
---|
| 130 | static detail::reaction_result react( |
---|
| 131 | State & stt, const EventBase & evt, const IdType & eventType ) |
---|
| 132 | { |
---|
| 133 | typedef typename mpl::if_< |
---|
| 134 | is_same< Event, event_base >, react_base, react_derived |
---|
| 135 | >::type impl; |
---|
| 136 | |
---|
| 137 | return impl::react( stt, evt, eventType ); |
---|
| 138 | } |
---|
| 139 | }; |
---|
| 140 | |
---|
| 141 | |
---|
| 142 | |
---|
| 143 | } // namespace statechart |
---|
| 144 | } // namespace boost |
---|
| 145 | |
---|
| 146 | |
---|
| 147 | |
---|
| 148 | #endif |
---|