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
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 ) );
705 class initial_construct_function
716 machine_.initial_construct();
717 return detail::result_utility::make_result(
718 detail::do_discard_event );
724 initial_construct_function & operator=(
725 const initial_construct_function & );
735 terminate_function(
state_machine & machine ) : machine_( machine ) {}
739 machine_.terminate_impl(
true );
740 return detail::result_utility::make_result(
741 detail::do_discard_event );
747 terminate_function & operator=(
const terminate_function & );
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 );
777 terminator guard( *
this, &exceptionEvent );
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;
804 class exception_event_handler
808 exception_event_handler(
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_ ) );
829 exception_event_handler & operator=(
830 const exception_event_handler & );
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; }
861 terminator & operator=(
const terminator & );
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 > >
1007 typedef detail::history_key< rtti_policy_type > history_key_type;
1010 history_key_type, void (*)(),
1011 std::less< history_key_type >,
1012 typename boost::detail::allocator::rebind_to<
1017 void store_history_impl(
1018 history_map_type & historyMap,
1019 const history_key_type & historyId,
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_;