1 #ifndef BOOST_STATECHART_STATE_MACHINE_HPP_INCLUDED
2 #define BOOST_STATECHART_STATE_MACHINE_HPP_INCLUDED
12 #include <boost/statechart/null_exception_translator.hpp>
13 #include <boost/statechart/result.hpp>
15 #include <boost/statechart/detail/rtti_policy.hpp>
16 #include <boost/statechart/detail/state_base.hpp>
17 #include <boost/statechart/detail/leaf_state.hpp>
18 #include <boost/statechart/detail/node_state.hpp>
19 #include <boost/statechart/detail/constructor.hpp>
20 #include <boost/statechart/detail/avoid_unused_warning.hpp>
22 #include <boost/mpl/list.hpp>
23 #include <boost/mpl/clear.hpp>
24 #include <boost/mpl/if.hpp>
25 #include <boost/mpl/at.hpp>
26 #include <boost/mpl/integral_c.hpp>
27 #include <boost/mpl/minus.hpp>
28 #include <boost/mpl/equal_to.hpp>
30 #include <boost/intrusive_ptr.hpp>
31 #include <boost/type_traits/is_pointer.hpp>
32 #include <boost/type_traits/remove_reference.hpp>
33 #include <boost/noncopyable.hpp>
34 #include <boost/assert.hpp>
35 #include <boost/static_assert.hpp>
36 #include <boost/polymorphic_cast.hpp>
38 #include <boost/config.hpp>
40 #include <boost/detail/allocator_utilities.hpp>
43 # pragma warning( push )
44 # pragma warning( disable: 4702 ) // unreachable code (in release mode only)
50 # pragma warning( pop )
70 template<
class StateBaseType,
class EventBaseType,
class IdType >
76 StateBaseType & toState,
77 const EventBaseType & evt,
80 toState_( toState ), evt_( evt ), eventType_( eventType )
86 return detail::result_utility::make_result(
87 toState_.react_impl( evt_, eventType_ ) );
95 StateBaseType & toState_;
96 const EventBaseType & evt_;
106 template<
class StateBaseType >
108 const StateBaseType * pState )
113 template<
class Target,
class IdType >
117 return type_id_impl< IdType >( p );
120 static bool found(
const void * pFound )
125 template<
class Target >
133 template<
class IdType,
class Type >
134 static IdType type_id_impl(
const Type * )
136 return Type::static_type();
142 template<
class StateBaseType >
144 const StateBaseType * pState )
149 template<
class Target,
class IdType >
152 return remove_reference< Target >::type::static_type();
155 template<
class Dummy >
161 template<
class Target >
164 throw std::bad_cast();
168 template<
class Target >
170 is_pointer< Target >,
171 state_cast_impl_pointer_target,
172 state_cast_impl_reference_target
177 template<
class RttiPolicy >
182 template<
class HistorizedState >
186 HistorizedState::context_type::static_type(),
187 HistorizedState::orthogonal_position::value );
192 return historyContextType_;
199 std::less< typename RttiPolicy::id_type >()(
200 left.historyContextType_, right.historyContextType_ ) ||
201 ( ( left.historyContextType_ == right.historyContextType_ ) &&
202 ( left.historizedOrthogonalRegion_ <
203 right.historizedOrthogonalRegion_ ) );
209 typename RttiPolicy::id_type historyContextType,
210 orthogonal_position_type historizedOrthogonalRegion
212 historyContextType_( historyContextType ),
213 historizedOrthogonalRegion_( historizedOrthogonalRegion )
218 history_key & operator=(
const history_key & );
220 const typename RttiPolicy::id_type historyContextType_;
221 const orthogonal_position_type historizedOrthogonalRegion_;
231 template<
class MostDerived,
233 class Allocator = std::allocator< none >,
250 detail::result_utility::get_result( translator_(
256 process_queued_events();
262 detail::result_utility::get_result( translator_(
270 return pOutermostState_ == 0;
275 if ( send_event( evt ) == detail::do_defer_event )
277 deferredEventQueue_.push_back( evt.intrusive_from_this() );
280 process_queued_events();
283 template<
class Target >
288 for (
typename state_list_type::const_iterator pCurrentLeafState =
289 currentStates_.begin();
290 pCurrentLeafState != currentStatesEnd_;
291 ++pCurrentLeafState )
294 get_pointer( *pCurrentLeafState ) );
296 while ( pCurrentState != 0 )
300 #ifndef BOOST_NO_EXCEPTIONS
304 Target result =
dynamic_cast< Target
>(
305 impl::deref_if_necessary( pCurrentState ) );
307 if ( impl::found( result ) )
312 #ifndef BOOST_NO_EXCEPTIONS
315 catch (
const std::bad_cast & ) {}
318 pCurrentState = pCurrentState->outer_state_ptr();
322 return impl::template not_found< Target >();
325 template<
class Target >
330 typename rtti_policy_type::id_type targetType =
331 impl::template type_id< Target, rtti_policy_type::id_type >();
333 for (
typename state_list_type::const_iterator pCurrentLeafState =
334 currentStates_.begin();
335 pCurrentLeafState != currentStatesEnd_;
336 ++pCurrentLeafState )
339 get_pointer( *pCurrentLeafState ) );
341 while ( pCurrentState != 0 )
343 if ( pCurrentState->dynamic_type() == targetType )
345 return static_cast< Target
>(
346 impl::deref_if_necessary( pCurrentState ) );
349 pCurrentState = pCurrentState->outer_state_ptr();
353 return impl::template not_found< Target >();
356 typedef detail::state_base< allocator_type, rtti_policy_type >
360 std::forward_iterator_tag,
361 state_base_type, std::ptrdiff_t
362 #ifndef BOOST_MSVC_STD_ITERATOR
363 , const state_base_type *, const state_base_type &
370 typename state_base_type::state_list_type::const_iterator
372 ) : baseIterator_( baseIterator ) {}
377 return &**baseIterator_;
388 return baseIterator_ == right.baseIterator_;
392 return !( *
this == right );
396 typename state_base_type::state_list_type::const_iterator
402 return state_iterator( currentStates_.begin() );
407 return state_iterator( currentStatesEnd_ );
415 currentStatesEnd_( currentStates_.end() ),
416 pOutermostState_( 0 ),
417 isInnermostCommonOuter_( false ),
418 performFullExit_( true ),
419 pTriggeringEvent_( 0 )
427 terminate_impl(
false );
446 class HistoryContext,
447 detail::orthogonal_position_type orthogonalPosition >
456 BOOST_STATIC_ASSERT( HistoryContext::shallow_history::value );
458 typedef typename mpl::at_c<
459 typename HistoryContext::inner_initial_list,
460 orthogonalPosition >
::type historized_state;
464 history_key_type::make_history_key< historized_state >(),
469 class HistoryContext,
470 detail::orthogonal_position_type orthogonalPosition >
479 BOOST_STATIC_ASSERT( HistoryContext::deep_history::value );
481 typedef typename mpl::at_c<
482 typename HistoryContext::inner_initial_list,
483 orthogonalPosition >
::type historized_state;
487 history_key_type::make_history_key< historized_state >(),
493 return pTriggeringEvent_;
502 typedef mpl::integral_c< detail::orthogonal_position_type, 0 >
504 typedef mpl::integral_c< detail::orthogonal_position_type, 1 >
510 typedef typename state_base_type::node_state_base_ptr_type
523 BOOST_ASSERT( get_pointer( pEvent ) != 0 );
524 eventQueue_.push_back( pEvent );
534 typename rtti_policy_type::id_type )
536 return detail::do_forward_event;
541 typename state_base_type::node_state_base_ptr_type &,
545 typename state_base_type::node_state_base_ptr_type &
546 pOutermostUnstableState )
548 pOutermostUnstableState = 0;
554 template<
class Context >
559 return *polymorphic_downcast< MostDerived * >(
this );
562 template<
class Context >
567 return *polymorphic_downcast< const MostDerived * >(
this );
572 return *polymorphic_downcast< MostDerived * >(
this );
577 return *polymorphic_downcast< const MostDerived * >(
this );
592 terminate_impl( theState, performFullExit_ );
593 pOutermostUnstableState_ = 0;
598 terminate_impl( theState, performFullExit_ );
599 isInnermostCommonOuter_ =
true;
604 terminate_impl( *pOutermostState_, performFullExit_ );
605 isInnermostCommonOuter_ =
true;
609 template<
class State >
610 void add(
const intrusive_ptr< State > & pState )
615 add_impl( pState, *pState );
617 if ( isInnermostCommonOuter_ ||
618 ( is_in_highest_orthogonal_region< State >() &&
619 ( get_pointer( pOutermostUnstableState_ ) ==
620 pState->State::outer_state_ptr() ) ) )
622 isInnermostCommonOuter_ =
false;
623 pOutermostUnstableState_ = pNewOutermostUnstableStateCandidate;
629 detail::orthogonal_position_type position,
632 BOOST_ASSERT( position == 0 );
633 detail::avoid_unused_warning( position );
634 pOutermostState_ = pOutermostState;
639 BOOST_ASSERT( position == 0 );
640 detail::avoid_unused_warning( position );
641 pOutermostState_ = 0;
647 eventQueue_.splice( eventQueue_.begin(), deferredEventQueue_ );
651 template<
class HistorizedState >
659 history_key_type::make_history_key< HistorizedState >(),
660 reinterpret_cast< void (*)()
>( &HistorizedState::deep_construct ) );
663 template<
class DefaultState >
665 const typename DefaultState::context_ptr_type & pContext )
667 construct_with_history_impl< DefaultState >(
668 shallowHistoryMap_, pContext );
672 template<
class HistorizedState,
class LeafState >
675 typedef typename detail::make_context_list<
676 typename HistorizedState::context_type,
677 LeafState >
::type history_context_list;
678 typedef detail::constructor<
685 history_key_type::make_history_key< HistorizedState >(),
686 reinterpret_cast< void (*)()
>( &constructor_type::construct ) );
689 template<
class DefaultState >
691 const typename DefaultState::context_ptr_type & pContext )
693 construct_with_history_impl< DefaultState >(
694 deepHistoryMap_, pContext );
699 void initial_construct()
701 InitialState::initial_deep_construct(
702 *polymorphic_downcast< MostDerived * >(
this ) );
716 machine_.initial_construct();
717 return detail::result_utility::make_result(
718 detail::do_discard_event );
739 machine_.terminate_impl(
true );
740 return detail::result_utility::make_result(
741 detail::do_discard_event );
753 template<
class ExceptionEvent >
754 detail::reaction_result handle_exception_event(
755 const ExceptionEvent & exceptionEvent,
772 get_pointer( pOutermostUnstableState_ );
773 state_base_type *
const pHandlingState = pOutermostUnstableState == 0 ?
774 pCurrentState : pOutermostUnstableState;
776 BOOST_ASSERT( pHandlingState != 0 );
787 performFullExit_ =
false;
788 const detail::reaction_result reactionResult = pHandlingState->react_impl(
789 exceptionEvent, exceptionEvent.dynamic_type() );
793 performFullExit_ =
true;
795 if ( ( reactionResult != detail::do_discard_event ) ||
796 ( get_pointer( pOutermostUnstableState_ ) != 0 ) )
801 return detail::do_discard_event;
813 pCurrentState_( pCurrentState )
817 template<
class ExceptionEvent >
819 const ExceptionEvent & exceptionEvent )
821 return detail::result_utility::make_result(
822 machine_.handle_exception_event(
823 exceptionEvent, pCurrentState_ ) );
842 state_machine & machine,
const event_base * pNewTriggeringEvent ) :
844 pOldTriggeringEvent_(machine_.pTriggeringEvent_),
847 machine_.pTriggeringEvent_ = pNewTriggeringEvent;
852 if ( !dismissed_ ) { machine_.terminate_impl(
false ); }
853 machine_.pTriggeringEvent_ = pOldTriggeringEvent_;
856 void dismiss() { dismissed_ =
true; }
873 BOOST_ASSERT( get_pointer( pOutermostUnstableState_ ) == 0 );
874 const typename rtti_policy_type::id_type eventType = evt.dynamic_type();
875 detail::reaction_result reactionResult = detail::do_forward_event;
878 typename state_list_type::iterator pState = currentStates_.begin();
879 ( reactionResult == detail::do_forward_event ) &&
880 ( pState != currentStatesEnd_ );
885 reactionResult = detail::result_utility::get_result( translator_(
888 **pState, evt, eventType ),
894 if ( reactionResult == detail::do_forward_event )
896 polymorphic_downcast< MostDerived * >(
this )->unconsumed_event( evt );
899 return reactionResult;
903 void process_queued_events()
905 while ( !eventQueue_.empty() )
908 eventQueue_.pop_front();
910 if ( send_event( *pEvent ) == detail::do_defer_event )
912 deferredEventQueue_.push_back( pEvent );
918 void terminate_impl(
bool performFullExit )
920 performFullExit_ =
true;
924 terminate_impl( *pOutermostState_, performFullExit );
928 deferredEventQueue_.clear();
929 shallowHistoryMap_.clear();
930 deepHistoryMap_.clear();
933 void terminate_impl(
state_base_type & theState,
bool performFullExit )
935 isInnermostCommonOuter_ =
false;
940 if ( get_pointer( pOutermostUnstableState_ ) != 0 )
942 theState.remove_from_state_list(
943 currentStatesEnd_, pOutermostUnstableState_, performFullExit );
950 else if ( currentStates_.begin() == --currentStatesEnd_ )
957 pState, pOutermostUnstableState_, performFullExit );
961 BOOST_ASSERT( currentStates_.size() > 1 );
963 theState.remove_from_state_list(
964 ++currentStatesEnd_, pOutermostUnstableState_, performFullExit );
971 detail::leaf_state< allocator_type, rtti_policy_type > & )
973 if ( currentStatesEnd_ == currentStates_.end() )
975 pState->set_list_position(
976 currentStates_.insert( currentStatesEnd_, pState ) );
980 *currentStatesEnd_ = pState;
981 pState->set_list_position( currentStatesEnd_ );
995 template<
class State >
996 static bool is_in_highest_orthogonal_region()
998 return mpl::equal_to<
999 typename State::orthogonal_position,
1001 typename State::context_type::no_of_orthogonal_regions,
1002 mpl::integral_c< detail::orthogonal_position_type, 1 > >
1011 std::less< history_key_type >,
1012 typename boost::detail::allocator::rebind_to<
1017 void store_history_impl(
1018 history_map_type & historyMap,
1020 void (*pConstructFunction)() )
1022 historyMap[ historyId ] = pConstructFunction;
1025 template<
class DefaultState >
1026 void construct_with_history_impl(
1027 history_map_type & historyMap,
1028 const typename DefaultState::context_ptr_type & pContext )
1030 typename history_map_type::iterator pFoundSlot = historyMap.find(
1031 history_key_type::make_history_key< DefaultState >() );
1033 if ( ( pFoundSlot == historyMap.end() ) || ( pFoundSlot->second == 0 ) )
1036 DefaultState::deep_construct(
1037 pContext, *polymorphic_downcast< MostDerived * >(
this ) );
1041 typedef void construct_function(
1042 const typename DefaultState::context_ptr_type &,
1043 typename DefaultState::outermost_context_base_type & );
1047 construct_function *
const pConstructFunction =
1048 reinterpret_cast< construct_function *
>( pFoundSlot->second );
1049 (*pConstructFunction)(
1050 pContext, *polymorphic_downcast< MostDerived * >(
this ) );
1056 typename boost::detail::allocator::rebind_to<
1062 std::less< const state_base_type * >,
1063 typename boost::detail::allocator::rebind_to<
1065 std::pair< const state_base_type * const, event_queue_type >
1067 > deferred_map_type;
1070 event_queue_type eventQueue_;
1071 event_queue_type deferredEventQueue_;
1073 typename state_list_type::iterator currentStatesEnd_;
1075 bool isInnermostCommonOuter_;
1077 ExceptionTranslator translator_;
1078 bool performFullExit_;
1079 history_map_type shallowHistoryMap_;
1080 history_map_type deepHistoryMap_;