📄 simple_state.hpp
字号:
#ifndef BOOST_STATECHART_SIMPLE_STATE_HPP_INCLUDED
#define BOOST_STATECHART_SIMPLE_STATE_HPP_INCLUDED
//////////////////////////////////////////////////////////////////////////////
// Copyright 2002-2006 Andreas Huber Doenni
// Distributed under the Boost Software License, Version 1.0. (See accompany-
// ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//////////////////////////////////////////////////////////////////////////////
#include <boost/statechart/event.hpp>
#include <boost/statechart/detail/leaf_state.hpp>
#include <boost/statechart/detail/node_state.hpp>
#include <boost/statechart/detail/constructor.hpp>
#include <boost/statechart/detail/memory.hpp>
#include <boost/mpl/eval_if.hpp>
#include <boost/mpl/if.hpp>
#include <boost/mpl/identity.hpp>
#include <boost/mpl/is_sequence.hpp>
#include <boost/mpl/list.hpp>
#include <boost/mpl/empty.hpp>
#include <boost/mpl/size.hpp>
#include <boost/mpl/front.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/find.hpp>
#include <boost/mpl/find_if.hpp>
#include <boost/mpl/contains.hpp>
#include <boost/mpl/distance.hpp>
#include <boost/mpl/deref.hpp>
#include <boost/mpl/pop_front.hpp>
#include <boost/mpl/push_front.hpp>
#include <boost/mpl/clear.hpp>
#include <boost/mpl/placeholders.hpp>
#include <boost/mpl/bool.hpp>
#include <boost/mpl/integral_c.hpp>
#include <boost/mpl/less.hpp>
#include <boost/mpl/equal_to.hpp>
#include <boost/mpl/not.hpp>
#include <boost/mpl/or.hpp>
#include <boost/mpl/plus.hpp>
#include <boost/mpl/max_element.hpp>
#include <boost/mpl/greater.hpp>
#include <boost/get_pointer.hpp>
#include <boost/intrusive_ptr.hpp>
#include <boost/assert.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/static_assert.hpp>
#include <boost/cast.hpp> // boost::polymorphic_downcast
#include <cstddef> // std::size_t
namespace boost
{
namespace statechart
{
namespace detail
{
//////////////////////////////////////////////////////////////////////////////
template< class T >
struct make_list : public mpl::eval_if<
mpl::is_sequence< T >,
mpl::identity< T >,
mpl::identity< mpl::list< T > > > {};
//////////////////////////////////////////////////////////////////////////////
template< class MostDerived, class Context, class InnerInitial >
struct simple_state_base_type
{
private:
typedef typename Context::outermost_context_base_type::allocator_type
allocator_type;
typedef typename Context::outermost_context_base_type::rtti_policy_type
rtti_policy_type;
typedef typename detail::make_list< InnerInitial >::type
inner_initial_list;
typedef typename mpl::size< inner_initial_list >::type
inner_initial_list_size;
public:
typedef typename mpl::eval_if<
mpl::empty< inner_initial_list >,
mpl::identity< typename rtti_policy_type::
template rtti_derived_type< MostDerived, leaf_state<
allocator_type,
rtti_policy_type > > >,
mpl::identity< typename rtti_policy_type::
template rtti_derived_type< MostDerived, node_state<
inner_initial_list_size,
allocator_type,
rtti_policy_type > > > >::type type;
};
//////////////////////////////////////////////////////////////////////////////
struct no_transition_function
{
template< class CommonContext >
void operator()( CommonContext & ) const {}
};
template< class TransitionContext, class Event >
class transition_function
{
public:
transition_function(
void ( TransitionContext::*pTransitionAction )( const Event & ),
const Event & evt
) :
pTransitionAction_( pTransitionAction ),
evt_( evt )
{
}
template< class CommonContext >
void operator()( CommonContext & commonContext ) const
{
( commonContext.template context< TransitionContext >()
.*pTransitionAction_ )( evt_ );
}
private:
void ( TransitionContext::*pTransitionAction_ )( const Event & );
const Event & evt_;
};
template< bool contextHasInheritedDeepHistory, bool contextHasDeepHistory >
struct deep_history_storer
{
template< class HistorizedState, class LeafState, class Context >
static void store_deep_history( Context & ) {}
};
template<>
struct deep_history_storer< true, false >
{
template< class HistorizedState, class LeafState, class Context >
static void store_deep_history( Context & ctx )
{
ctx.template store_deep_history_impl< LeafState >();
}
};
template<>
struct deep_history_storer< true, true >
{
template< class HistorizedState, class LeafState, class Context >
static void store_deep_history( Context & ctx )
{
ctx.outermost_context_base().template store_deep_history<
HistorizedState, LeafState >();
ctx.template store_deep_history_impl< LeafState >();
}
};
} // namespace detail
//////////////////////////////////////////////////////////////////////////////
enum history_mode
{
has_no_history,
has_shallow_history,
has_deep_history,
has_full_history // shallow & deep
};
//////////////////////////////////////////////////////////////////////////////
template< class MostDerived,
class Context,
class InnerInitial = mpl::list<>,
history_mode historyMode = has_no_history >
class simple_state : public detail::simple_state_base_type< MostDerived,
typename Context::inner_context_type, InnerInitial >::type
{
typedef typename detail::simple_state_base_type<
MostDerived, typename Context::inner_context_type,
InnerInitial >::type base_type;
public:
//////////////////////////////////////////////////////////////////////////
typedef mpl::list<> reactions;
typedef typename Context::inner_context_type context_type;
template< detail::orthogonal_position_type innerOrthogonalPosition >
struct orthogonal
{
typedef mpl::integral_c<
detail::orthogonal_position_type,
innerOrthogonalPosition > inner_orthogonal_position;
typedef MostDerived inner_context_type;
};
typedef typename context_type::outermost_context_type
outermost_context_type;
outermost_context_type & outermost_context()
{
// This assert fails when an attempt is made to access the state machine
// from a constructor of a state that is *not* a subtype of state<>.
// To correct this, derive from state<> instead of simple_state<>.
BOOST_ASSERT( get_pointer( pContext_ ) != 0 );
return pContext_->outermost_context();
}
const outermost_context_type & outermost_context() const
{
// This assert fails when an attempt is made to access the state machine
// from a constructor of a state that is *not* a subtype of state<>.
// To correct this, derive from state<> instead of simple_state<>.
BOOST_ASSERT( get_pointer( pContext_ ) != 0 );
return pContext_->outermost_context();
}
template< class OtherContext >
OtherContext & context()
{
typedef typename mpl::if_<
is_same< OtherContext, MostDerived >,
context_impl_this_context,
context_impl_other_context
>::type impl;
return impl::template context_impl< OtherContext >( *this );
}
template< class OtherContext >
const OtherContext & context() const
{
typedef typename mpl::if_<
is_same< OtherContext, MostDerived >,
context_impl_this_context,
context_impl_other_context
>::type impl;
return impl::template context_impl< OtherContext >( *this );
}
template< class Target >
Target state_cast() const
{
return outermost_context_base().template state_cast< Target >();
}
template< class Target >
Target state_downcast() const
{
return outermost_context_base().template state_downcast< Target >();
}
typedef typename context_type::state_base_type state_base_type;
typedef typename context_type::state_iterator state_iterator;
state_iterator state_begin() const
{
return outermost_context_base().state_begin();
}
state_iterator state_end() const
{
return outermost_context_base().state_end();
}
typedef typename context_type::event_base_ptr_type event_base_ptr_type;
void post_event( const event_base_ptr_type & pEvent )
{
outermost_context_base().post_event( pEvent );
}
void post_event( const event_base & evt )
{
outermost_context_base().post_event( evt );
}
result discard_event()
{
return detail::result_utility::make_result( detail::do_discard_event );
}
result forward_event()
{
return detail::result_utility::make_result( detail::do_forward_event );
}
result defer_event()
{
this->state_base_type::defer_event();
return detail::result_utility::make_result( detail::do_defer_event );
}
template< class DestinationState >
result transit()
{
return transit_impl< DestinationState, outermost_context_type >(
detail::no_transition_function() );
}
template< class DestinationState, class TransitionContext, class Event >
result transit(
void ( TransitionContext::*pTransitionAction )( const Event & ),
const Event & evt )
{
return transit_impl< DestinationState, TransitionContext >(
detail::transition_function< TransitionContext, Event >(
pTransitionAction, evt ) );
}
result terminate()
{
outermost_context_base().terminate_as_reaction( *this );
return detail::result_utility::make_result( detail::do_discard_event );
}
template<
class HistoryContext,
detail::orthogonal_position_type orthogonalPosition >
void clear_shallow_history()
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -