1 #ifndef BOOST_STATECHART_SIMPLE_STATE_HPP_INCLUDED
2 #define BOOST_STATECHART_SIMPLE_STATE_HPP_INCLUDED
13 #include <boost/statechart/detail/leaf_state.hpp>
14 #include <boost/statechart/detail/node_state.hpp>
15 #include <boost/statechart/detail/constructor.hpp>
16 #include <boost/statechart/detail/memory.hpp>
18 #include <boost/mpl/eval_if.hpp>
19 #include <boost/mpl/if.hpp>
20 #include <boost/mpl/identity.hpp>
21 #include <boost/mpl/is_sequence.hpp>
22 #include <boost/mpl/list.hpp>
23 #include <boost/mpl/empty.hpp>
24 #include <boost/mpl/size.hpp>
25 #include <boost/mpl/front.hpp>
26 #include <boost/mpl/at.hpp>
27 #include <boost/mpl/find.hpp>
28 #include <boost/mpl/find_if.hpp>
29 #include <boost/mpl/contains.hpp>
30 #include <boost/mpl/distance.hpp>
31 #include <boost/mpl/deref.hpp>
32 #include <boost/mpl/pop_front.hpp>
33 #include <boost/mpl/push_front.hpp>
34 #include <boost/mpl/clear.hpp>
35 #include <boost/mpl/placeholders.hpp>
36 #include <boost/mpl/bool.hpp>
37 #include <boost/mpl/integral_c.hpp>
38 #include <boost/mpl/less.hpp>
39 #include <boost/mpl/equal_to.hpp>
40 #include <boost/mpl/not.hpp>
41 #include <boost/mpl/or.hpp>
43 #include <boost/mpl/plus.hpp>
44 #include <boost/mpl/max_element.hpp>
45 #include <boost/mpl/greater.hpp>
47 #include <boost/get_pointer.hpp>
48 #include <boost/intrusive_ptr.hpp>
49 #include <boost/assert.hpp>
50 #include <boost/type_traits/is_base_of.hpp>
51 #include <boost/type_traits/is_same.hpp>
52 #include <boost/static_assert.hpp>
53 #include <boost/polymorphic_cast.hpp>
71 mpl::is_sequence< T >,
73 mpl::identity< mpl::list< T > > > {};
76 template<
class MostDerived,
class Context,
class InnerInitial >
80 typedef typename Context::outermost_context_base_type::allocator_type
82 typedef typename Context::outermost_context_base_type::rtti_policy_type
87 inner_initial_list_size;
90 typedef typename mpl::eval_if<
91 mpl::empty< inner_initial_list >,
92 mpl::identity<
typename rtti_policy_type::
93 template rtti_derived_type< MostDerived, leaf_state<
95 rtti_policy_type > > >,
96 mpl::identity<
typename rtti_policy_type::
97 template rtti_derived_type< MostDerived, node_state<
98 inner_initial_list_size,
107 template<
class CommonContext >
111 template<
class TransitionContext,
class Event >
116 void ( TransitionContext::*pTransitionAction )(
const Event & ),
119 pTransitionAction_( pTransitionAction ),
124 template<
class CommonContext >
127 ( commonContext.template context< TransitionContext >()
128 .*pTransitionAction_ )( evt_ );
135 void ( TransitionContext::*pTransitionAction_ )(
const Event & );
140 template<
bool contextHasInheritedDeepHistory,
bool contextHasDeepHistory >
143 template<
class HistorizedState,
class LeafState,
class Context >
150 template<
class HistorizedState,
class LeafState,
class Context >
153 ctx.template store_deep_history_impl< LeafState >();
160 template<
class HistorizedState,
class LeafState,
class Context >
164 HistorizedState, LeafState >();
165 ctx.template store_deep_history_impl< LeafState >();
187 template<
class MostDerived,
189 class InnerInitial = mpl::list<>,
192 typename Context::inner_context_type, InnerInitial >
::type
195 MostDerived,
typename Context::inner_context_type,
196 InnerInitial >
::type base_type;
204 template< detail::orthogonal_position_type innerOrthogonalPosition >
207 typedef mpl::integral_c<
208 detail::orthogonal_position_type,
213 typedef typename context_type::outermost_context_type
221 BOOST_ASSERT( get_pointer( pContext_ ) != 0 );
222 return pContext_->outermost_context();
230 BOOST_ASSERT( get_pointer( pContext_ ) != 0 );
231 return pContext_->outermost_context();
234 template<
class OtherContext >
237 typedef typename mpl::if_<
238 is_base_of< OtherContext, MostDerived >,
242 return impl::template context_impl< OtherContext >( *
this );
245 template<
class OtherContext >
248 typedef typename mpl::if_<
249 is_base_of< OtherContext, MostDerived >,
253 return impl::template context_impl< OtherContext >( *
this );
256 template<
class Target >
262 template<
class Target >
296 return detail::result_utility::make_result( detail::do_discard_event );
301 return detail::result_utility::make_result( detail::do_forward_event );
306 this->state_base_type::defer_event();
307 return detail::result_utility::make_result( detail::do_defer_event );
310 template<
class DestinationState >
313 return transit_impl< DestinationState, outermost_context_type >(
317 template<
class DestinationState,
class TransitionContext,
class Event >
319 void ( TransitionContext::*pTransitionAction )(
const Event & ),
322 return transit_impl< DestinationState, TransitionContext >(
324 pTransitionAction, evt ) );
330 return detail::result_utility::make_result( detail::do_discard_event );
334 class HistoryContext,
335 detail::orthogonal_position_type orthogonalPosition >
339 HistoryContext, orthogonalPosition >();
343 class HistoryContext,
344 detail::orthogonal_position_type orthogonalPosition >
348 HistoryContext, orthogonalPosition >();
364 if ( get_pointer( pContext_ ) != 0 )
366 if ( this->deferred_events() )
371 pContext_->remove_inner_state( orthogonal_position::value );
389 typename context_type::no_of_orthogonal_regions >::value ) );
392 typedef mpl::integral_c< detail::orthogonal_position_type, 0 >
398 typedef typename context_type::outermost_context_base_type
407 typedef mpl::integral_c<
408 detail::orthogonal_position_type,
410 typedef typename mpl::push_front<
411 typename context_type::context_type_list,
424 mpl::integral_c< detail::orthogonal_position_type, 2 > >,
426 typename context_type::inherited_deep_history > >::value ) );
434 typedef typename mpl::or_<
436 typename context_type::inherited_deep_history
438 typedef typename mpl::and_<
442 void *
operator new( std::size_t size )
444 return detail::allocate< MostDerived,
445 typename outermost_context_type::allocator_type >( size );
448 void operator delete(
void * pState )
450 detail::deallocate< MostDerived,
451 typename outermost_context_type::allocator_type >( pState );
459 BOOST_ASSERT( get_pointer( pContext_ ) != 0 );
460 return pContext_->outermost_context_base();
468 BOOST_ASSERT( get_pointer( pContext_ ) != 0 );
469 return pContext_->outermost_context_base();
474 typedef typename mpl::if_<
475 is_same< outermost_context_type, context_type >,
476 outer_state_ptr_impl_outermost,
479 return impl::outer_state_ptr_impl( *
this );
484 typename rtti_policy_type::id_type eventType )
487 typename MostDerived::reactions >
::type reaction_list;
488 detail::reaction_result reactionResult =
489 local_react< reaction_list >( evt, eventType );
493 if ( reactionResult == detail::do_forward_event )
498 reactionResult = pContext_->react_impl( evt, eventType );
501 return reactionResult;
505 typename base_type::direct_state_base_ptr_type & pSelf,
506 typename state_base_type::node_state_base_ptr_type &
507 pOutermostUnstableState,
508 bool performFullExit )
511 polymorphic_downcast< MostDerived * >(
this );
513 exit_impl( pMostDerivedSelf, pOutermostUnstableState, performFullExit );
518 typename state_base_type::node_state_base_ptr_type &
519 pOutermostUnstableState,
520 bool performFullExit )
522 switch ( this->ref_count() )
525 if ( get_pointer( pOutermostUnstableState ) ==
528 pContext_->set_outermost_unstable_state(
529 pOutermostUnstableState );
538 if ( get_pointer( pOutermostUnstableState ) == 0 )
540 pContext_->set_outermost_unstable_state(
541 pOutermostUnstableState );
544 if ( performFullExit )
547 check_store_shallow_history< stores_shallow_history >();
548 check_store_deep_history< stores_deep_history >();
554 pContext, pOutermostUnstableState, performFullExit );
563 typename state_base_type::node_state_base_ptr_type &
564 pOutermostUnstableState )
566 pOutermostUnstableState =
this;
569 template<
class OtherContext >
570 const typename OtherContext::inner_context_ptr_type &
context_ptr()
const
572 typedef typename mpl::if_<
573 is_same< OtherContext, context_type >,
578 return impl::template context_ptr_impl< OtherContext >( *
this );
593 deep_construct_inner< inner_initial_list >(
594 pInnerContext, outermostContextBase );
602 pInnerContext->set_context( pContext );
603 outermostContextBase.add( pInnerContext );
604 return pInnerContext;
609 BOOST_ASSERT( get_pointer( pContext ) != 0 );
610 pContext_ = pContext;
611 base_type::set_context(
612 orthogonal_position::value, get_pointer( pContext ) );
615 template<
class InnerList >
620 typedef typename mpl::if_<
621 mpl::empty< InnerList >,
622 deep_construct_inner_impl_empty,
623 deep_construct_inner_impl_non_empty
625 impl::template deep_construct_inner_impl< InnerList >(
626 pInnerContext, outermostContextBase );
629 template<
class LeafState >
633 context_type::inherited_deep_history::value,
634 context_type::deep_history::value
635 >::template store_deep_history< MostDerived, LeafState >(
643 template<
class OtherContext,
class State >
644 static const typename OtherContext::inner_context_ptr_type &
645 context_ptr_impl(
const State & stt )
651 BOOST_ASSERT( get_pointer( stt.pContext_ ) != 0 );
652 return stt.pContext_->template context_ptr< OtherContext >();
659 template<
class OtherContext,
class State >
660 static const typename OtherContext::inner_context_ptr_type &
661 context_ptr_impl(
const State & stt )
667 BOOST_ASSERT( get_pointer( stt.pContext_ ) != 0 );
668 return stt.pContext_;
675 template<
class OtherContext,
class State >
676 static OtherContext & context_impl( State & stt )
682 BOOST_ASSERT( get_pointer( stt.pContext_ ) != 0 );
683 return stt.pContext_->template context< OtherContext >();
690 template<
class OtherContext,
class State >
691 static OtherContext & context_impl( State & stt )
693 return *polymorphic_downcast< MostDerived * >( &stt );
698 template<
class DestinationState,
699 class TransitionContext,
700 class TransitionAction >
701 result transit_impl(
const TransitionAction & transitionAction )
703 typedef typename mpl::find_if<
706 typename DestinationState::context_type_list,
707 mpl::placeholders::_ > >
::type common_context_iter;
710 typedef typename mpl::distance<
712 common_context_iter >
::type termination_state_position;
714 possible_transition_contexts;
715 typedef typename mpl::at<
716 possible_transition_contexts,
717 termination_state_position >
::type termination_state_type;
719 termination_state_type & terminationState(
720 context< termination_state_type >() );
722 common_context_type::inner_context_ptr_type pCommonContext(
723 terminationState.template context_ptr< common_context_type >() );
725 pCommonContext->outermost_context_base() );
727 #ifdef BOOST_STATECHART_RELAX_TRANSITION_CONTEXT
728 typedef typename mpl::distance<
731 possible_transition_contexts, TransitionContext >
::type
732 >
::type proposed_transition_context_position;
734 typedef typename mpl::plus<
735 termination_state_position,
737 >
::type uml_transition_context_position;
739 typedef typename mpl::deref<
typename mpl::max_element<
741 proposed_transition_context_position,
742 uml_transition_context_position >,
743 mpl::greater< mpl::placeholders::_, mpl::placeholders::_ >
746 typedef typename mpl::at<
747 possible_transition_contexts,
748 real_transition_context_position >
::type real_transition_context_type;
751 # pragma warning( push )
752 # pragma warning( disable: 4127 ) // conditional expression is constant
754 if ( ( proposed_transition_context_position::value == 0 ) &&
755 ( inner_initial_list_size::value == 0 ) )
757 transitionAction( *polymorphic_downcast< MostDerived * >(
this ) );
758 outermostContextBase.terminate_as_part_of_transit( terminationState );
760 else if ( proposed_transition_context_position::value >=
761 uml_transition_context_position::value )
763 real_transition_context_type & transitionContext =
764 context< real_transition_context_type >();
765 outermostContextBase.terminate_as_part_of_transit( terminationState );
766 transitionAction( transitionContext );
770 typename real_transition_context_type::inner_context_ptr_type
771 pTransitionContext = context_ptr< real_transition_context_type >();
772 outermostContextBase.terminate_as_part_of_transit(
773 *pTransitionContext );
774 transitionAction( *pTransitionContext );
775 pTransitionContext = 0;
776 outermostContextBase.terminate_as_part_of_transit( terminationState );
779 # pragma warning( pop )
782 outermostContextBase.terminate_as_part_of_transit( terminationState );
783 transitionAction( *pCommonContext );
786 typedef typename detail::make_context_list<
787 common_context_type, DestinationState >
::type context_list_type;
794 typename termination_state_type::orthogonal_position,
795 typename mpl::front< context_list_type >::type::orthogonal_position
800 pCommonContext, outermostContextBase );
802 return detail::result_utility::make_result( detail::do_discard_event );
807 template<
class ReactionList,
class State >
808 static detail::reaction_result local_react_impl(
811 typename rtti_policy_type::id_type eventType )
813 detail::reaction_result reactionResult =
814 mpl::front< ReactionList >::type::react(
815 *polymorphic_downcast< MostDerived * >( &stt ),
818 if ( reactionResult == detail::no_reaction )
820 reactionResult = stt.template local_react<
825 return reactionResult;
830 struct local_react_impl_empty
832 template<
class ReactionList,
class State >
833 static detail::reaction_result local_react_impl(
836 return detail::do_forward_event;
840 template<
class ReactionList >
841 detail::reaction_result local_react(
843 typename rtti_policy_type::id_type eventType )
845 typedef typename mpl::if_<
846 mpl::empty< ReactionList >,
847 local_react_impl_empty,
850 return impl::template local_react_impl< ReactionList >(
851 *
this, evt, eventType );
856 template<
class State >
857 static const state_base_type * outer_state_ptr_impl(
const State & stt )
859 return get_pointer( stt.pContext_ );
864 struct outer_state_ptr_impl_outermost
866 template<
class State >
873 struct deep_construct_inner_impl_non_empty
875 template<
class InnerList >
876 static void deep_construct_inner_impl(
890 typename current_inner::context_type::inner_initial_list,
891 typename current_inner::orthogonal_position >::
type >::value ) );
893 current_inner::deep_construct( pInnerContext, outermostContextBase );
895 pInnerContext, outermostContextBase );
899 struct deep_construct_inner_impl_empty
901 template<
class InnerList >
902 static void deep_construct_inner_impl(
906 struct check_store_shallow_history_impl_no
908 template<
class State >
909 static void check_store_shallow_history_impl( State & ) {}
914 template<
class State >
915 static void check_store_shallow_history_impl( State & stt )
917 stt.outermost_context_base().template store_shallow_history<
923 template<
class StoreShallowHistory >
924 void check_store_shallow_history()
926 typedef typename mpl::if_<
929 check_store_shallow_history_impl_no
931 impl::check_store_shallow_history_impl( *
this );
934 struct check_store_deep_history_impl_no
936 template<
class State >
937 static void check_store_deep_history_impl( State & ) {}
942 template<
class State >
943 static void check_store_deep_history_impl( State & stt )
945 stt.template store_deep_history_impl< MostDerived >();
950 template<
class StoreDeepHistory >
951 void check_store_deep_history()
953 typedef typename mpl::if_<
956 check_store_deep_history_impl_no
958 impl::check_store_deep_history_impl( *
this );
967 #ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
973 template<
class MostDerived,
class Context,
976 MostDerived, Context, InnerInitial, historyMode > * pBase )
978 if ( pBase->release() )
982 delete polymorphic_downcast< const MostDerived * >( pBase );
988 #ifndef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP