os/ossrv/ossrv_pub/boost_apis/boost/statechart/simple_state.hpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200 (2014-06-10)
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
#ifndef BOOST_STATECHART_SIMPLE_STATE_HPP_INCLUDED
sl@0
     2
#define BOOST_STATECHART_SIMPLE_STATE_HPP_INCLUDED
sl@0
     3
//////////////////////////////////////////////////////////////////////////////
sl@0
     4
// Copyright 2002-2006 Andreas Huber Doenni
sl@0
     5
// Distributed under the Boost Software License, Version 1.0. (See accompany-
sl@0
     6
// ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
sl@0
     7
//////////////////////////////////////////////////////////////////////////////
sl@0
     8
sl@0
     9
sl@0
    10
sl@0
    11
#include <boost/statechart/event.hpp>
sl@0
    12
sl@0
    13
#include <boost/statechart/detail/leaf_state.hpp>
sl@0
    14
#include <boost/statechart/detail/node_state.hpp>
sl@0
    15
#include <boost/statechart/detail/constructor.hpp>
sl@0
    16
#include <boost/statechart/detail/memory.hpp>
sl@0
    17
sl@0
    18
#include <boost/mpl/eval_if.hpp>
sl@0
    19
#include <boost/mpl/if.hpp>
sl@0
    20
#include <boost/mpl/identity.hpp>
sl@0
    21
#include <boost/mpl/is_sequence.hpp>
sl@0
    22
#include <boost/mpl/list.hpp>
sl@0
    23
#include <boost/mpl/empty.hpp>
sl@0
    24
#include <boost/mpl/size.hpp>
sl@0
    25
#include <boost/mpl/front.hpp>
sl@0
    26
#include <boost/mpl/at.hpp>
sl@0
    27
#include <boost/mpl/find.hpp>
sl@0
    28
#include <boost/mpl/find_if.hpp>
sl@0
    29
#include <boost/mpl/contains.hpp>
sl@0
    30
#include <boost/mpl/distance.hpp>
sl@0
    31
#include <boost/mpl/deref.hpp>
sl@0
    32
#include <boost/mpl/pop_front.hpp>
sl@0
    33
#include <boost/mpl/push_front.hpp>
sl@0
    34
#include <boost/mpl/clear.hpp>
sl@0
    35
#include <boost/mpl/placeholders.hpp>
sl@0
    36
#include <boost/mpl/bool.hpp>
sl@0
    37
#include <boost/mpl/integral_c.hpp>
sl@0
    38
#include <boost/mpl/less.hpp>
sl@0
    39
#include <boost/mpl/equal_to.hpp>
sl@0
    40
#include <boost/mpl/not.hpp>
sl@0
    41
#include <boost/mpl/or.hpp>
sl@0
    42
sl@0
    43
#include <boost/mpl/plus.hpp>
sl@0
    44
#include <boost/mpl/max_element.hpp>
sl@0
    45
#include <boost/mpl/greater.hpp>
sl@0
    46
sl@0
    47
#include <boost/get_pointer.hpp>
sl@0
    48
#include <boost/intrusive_ptr.hpp>
sl@0
    49
#include <boost/assert.hpp>
sl@0
    50
#include <boost/type_traits/is_same.hpp>
sl@0
    51
#include <boost/static_assert.hpp>
sl@0
    52
#include <boost/cast.hpp> // boost::polymorphic_downcast
sl@0
    53
sl@0
    54
#include <cstddef> // std::size_t
sl@0
    55
sl@0
    56
sl@0
    57
sl@0
    58
namespace boost
sl@0
    59
{
sl@0
    60
namespace statechart
sl@0
    61
{
sl@0
    62
namespace detail
sl@0
    63
{
sl@0
    64
sl@0
    65
sl@0
    66
sl@0
    67
//////////////////////////////////////////////////////////////////////////////
sl@0
    68
template< class T >
sl@0
    69
struct make_list : public mpl::eval_if<
sl@0
    70
  mpl::is_sequence< T >,
sl@0
    71
  mpl::identity< T >,
sl@0
    72
  mpl::identity< mpl::list< T > > > {};
sl@0
    73
sl@0
    74
//////////////////////////////////////////////////////////////////////////////
sl@0
    75
template< class MostDerived, class Context, class InnerInitial >
sl@0
    76
struct simple_state_base_type
sl@0
    77
{
sl@0
    78
  private:
sl@0
    79
    typedef typename Context::outermost_context_base_type::allocator_type
sl@0
    80
      allocator_type;
sl@0
    81
    typedef typename Context::outermost_context_base_type::rtti_policy_type
sl@0
    82
      rtti_policy_type;
sl@0
    83
    typedef typename detail::make_list< InnerInitial >::type
sl@0
    84
      inner_initial_list;
sl@0
    85
    typedef typename mpl::size< inner_initial_list >::type
sl@0
    86
      inner_initial_list_size;
sl@0
    87
sl@0
    88
  public:
sl@0
    89
    typedef typename mpl::eval_if<
sl@0
    90
      mpl::empty< inner_initial_list >,
sl@0
    91
      mpl::identity< typename rtti_policy_type::
sl@0
    92
        template rtti_derived_type< MostDerived, leaf_state<
sl@0
    93
          allocator_type,
sl@0
    94
          rtti_policy_type > > >,
sl@0
    95
      mpl::identity< typename rtti_policy_type::
sl@0
    96
        template rtti_derived_type< MostDerived, node_state<
sl@0
    97
          inner_initial_list_size,
sl@0
    98
          allocator_type,
sl@0
    99
          rtti_policy_type > > > >::type type;
sl@0
   100
};
sl@0
   101
sl@0
   102
sl@0
   103
//////////////////////////////////////////////////////////////////////////////
sl@0
   104
struct no_transition_function
sl@0
   105
{
sl@0
   106
  template< class CommonContext >
sl@0
   107
  void operator()( CommonContext & ) const {}
sl@0
   108
};
sl@0
   109
sl@0
   110
template< class TransitionContext, class Event >
sl@0
   111
class transition_function
sl@0
   112
{
sl@0
   113
  public:
sl@0
   114
    transition_function(
sl@0
   115
      void ( TransitionContext::*pTransitionAction )( const Event & ),
sl@0
   116
      const Event & evt
sl@0
   117
    ) :
sl@0
   118
      pTransitionAction_( pTransitionAction ),
sl@0
   119
      evt_( evt )
sl@0
   120
    {
sl@0
   121
    }
sl@0
   122
sl@0
   123
    template< class CommonContext >
sl@0
   124
    void operator()( CommonContext & commonContext ) const
sl@0
   125
    {
sl@0
   126
      ( commonContext.template context< TransitionContext >()
sl@0
   127
        .*pTransitionAction_ )( evt_ );
sl@0
   128
    }
sl@0
   129
sl@0
   130
  private:
sl@0
   131
    void ( TransitionContext::*pTransitionAction_ )( const Event & );
sl@0
   132
    const Event & evt_;
sl@0
   133
};
sl@0
   134
sl@0
   135
sl@0
   136
template< bool contextHasInheritedDeepHistory, bool contextHasDeepHistory >
sl@0
   137
struct deep_history_storer
sl@0
   138
{
sl@0
   139
  template< class HistorizedState, class LeafState, class Context >
sl@0
   140
  static void store_deep_history( Context & ) {}
sl@0
   141
};
sl@0
   142
sl@0
   143
template<>
sl@0
   144
struct deep_history_storer< true, false >
sl@0
   145
{
sl@0
   146
  template< class HistorizedState, class LeafState, class Context >
sl@0
   147
  static void store_deep_history( Context & ctx )
sl@0
   148
  {
sl@0
   149
    ctx.template store_deep_history_impl< LeafState >();
sl@0
   150
  }
sl@0
   151
};
sl@0
   152
sl@0
   153
template<>
sl@0
   154
struct deep_history_storer< true, true >
sl@0
   155
{
sl@0
   156
  template< class HistorizedState, class LeafState, class Context >
sl@0
   157
  static void store_deep_history( Context & ctx )
sl@0
   158
  {
sl@0
   159
    ctx.outermost_context_base().template store_deep_history<
sl@0
   160
      HistorizedState, LeafState >();
sl@0
   161
    ctx.template store_deep_history_impl< LeafState >();
sl@0
   162
  }
sl@0
   163
};
sl@0
   164
sl@0
   165
sl@0
   166
sl@0
   167
} // namespace detail
sl@0
   168
sl@0
   169
sl@0
   170
sl@0
   171
//////////////////////////////////////////////////////////////////////////////
sl@0
   172
enum history_mode
sl@0
   173
{
sl@0
   174
  has_no_history,
sl@0
   175
  has_shallow_history,
sl@0
   176
  has_deep_history,
sl@0
   177
  has_full_history // shallow & deep
sl@0
   178
};
sl@0
   179
sl@0
   180
sl@0
   181
sl@0
   182
//////////////////////////////////////////////////////////////////////////////
sl@0
   183
template< class MostDerived,
sl@0
   184
          class Context,
sl@0
   185
          class InnerInitial = mpl::list<>,
sl@0
   186
          history_mode historyMode = has_no_history >
sl@0
   187
class simple_state : public detail::simple_state_base_type< MostDerived,
sl@0
   188
  typename Context::inner_context_type, InnerInitial >::type
sl@0
   189
{
sl@0
   190
  typedef typename detail::simple_state_base_type<
sl@0
   191
    MostDerived, typename Context::inner_context_type,
sl@0
   192
    InnerInitial >::type base_type;
sl@0
   193
sl@0
   194
  public:
sl@0
   195
    //////////////////////////////////////////////////////////////////////////
sl@0
   196
    typedef mpl::list<> reactions;
sl@0
   197
sl@0
   198
    typedef typename Context::inner_context_type context_type;
sl@0
   199
sl@0
   200
    template< detail::orthogonal_position_type innerOrthogonalPosition >
sl@0
   201
    struct orthogonal
sl@0
   202
    {
sl@0
   203
      typedef mpl::integral_c<
sl@0
   204
        detail::orthogonal_position_type,
sl@0
   205
        innerOrthogonalPosition > inner_orthogonal_position;
sl@0
   206
      typedef MostDerived inner_context_type;
sl@0
   207
    };
sl@0
   208
sl@0
   209
    typedef typename context_type::outermost_context_type
sl@0
   210
      outermost_context_type;
sl@0
   211
sl@0
   212
    outermost_context_type & outermost_context()
sl@0
   213
    {
sl@0
   214
      // This assert fails when an attempt is made to access the state machine
sl@0
   215
      // from a constructor of a state that is *not* a subtype of state<>.
sl@0
   216
      // To correct this, derive from state<> instead of simple_state<>.
sl@0
   217
      BOOST_ASSERT( get_pointer( pContext_ ) != 0 );
sl@0
   218
      return pContext_->outermost_context();
sl@0
   219
    }
sl@0
   220
sl@0
   221
    const outermost_context_type & outermost_context() const
sl@0
   222
    {
sl@0
   223
      // This assert fails when an attempt is made to access the state machine
sl@0
   224
      // from a constructor of a state that is *not* a subtype of state<>.
sl@0
   225
      // To correct this, derive from state<> instead of simple_state<>.
sl@0
   226
      BOOST_ASSERT( get_pointer( pContext_ ) != 0 );
sl@0
   227
      return pContext_->outermost_context();
sl@0
   228
    }
sl@0
   229
sl@0
   230
    template< class OtherContext >
sl@0
   231
    OtherContext & context()
sl@0
   232
    {
sl@0
   233
      typedef typename mpl::if_<
sl@0
   234
        is_same< OtherContext, MostDerived >,
sl@0
   235
        context_impl_this_context,
sl@0
   236
        context_impl_other_context
sl@0
   237
      >::type impl;
sl@0
   238
      return impl::template context_impl< OtherContext >( *this );
sl@0
   239
    }
sl@0
   240
sl@0
   241
    template< class OtherContext >
sl@0
   242
    const OtherContext & context() const
sl@0
   243
    {
sl@0
   244
      typedef typename mpl::if_<
sl@0
   245
        is_same< OtherContext, MostDerived >,
sl@0
   246
        context_impl_this_context,
sl@0
   247
        context_impl_other_context
sl@0
   248
      >::type impl;
sl@0
   249
      return impl::template context_impl< OtherContext >( *this );
sl@0
   250
    }
sl@0
   251
sl@0
   252
    template< class Target >
sl@0
   253
    Target state_cast() const
sl@0
   254
    {
sl@0
   255
      return outermost_context_base().template state_cast< Target >();
sl@0
   256
    }
sl@0
   257
sl@0
   258
    template< class Target >
sl@0
   259
    Target state_downcast() const
sl@0
   260
    {
sl@0
   261
      return outermost_context_base().template state_downcast< Target >();
sl@0
   262
    }
sl@0
   263
sl@0
   264
    typedef typename context_type::state_base_type state_base_type;
sl@0
   265
    typedef typename context_type::state_iterator state_iterator;
sl@0
   266
sl@0
   267
    state_iterator state_begin() const
sl@0
   268
    {
sl@0
   269
      return outermost_context_base().state_begin();
sl@0
   270
    }
sl@0
   271
sl@0
   272
    state_iterator state_end() const
sl@0
   273
    {
sl@0
   274
      return outermost_context_base().state_end();
sl@0
   275
    }
sl@0
   276
sl@0
   277
sl@0
   278
    typedef typename context_type::event_base_ptr_type event_base_ptr_type;
sl@0
   279
sl@0
   280
    void post_event( const event_base_ptr_type & pEvent )
sl@0
   281
    {
sl@0
   282
      outermost_context_base().post_event( pEvent );
sl@0
   283
    }
sl@0
   284
sl@0
   285
    void post_event( const event_base & evt )
sl@0
   286
    {
sl@0
   287
      outermost_context_base().post_event( evt );
sl@0
   288
    }
sl@0
   289
sl@0
   290
    result discard_event()
sl@0
   291
    {
sl@0
   292
      return detail::result_utility::make_result( detail::do_discard_event );
sl@0
   293
    }
sl@0
   294
sl@0
   295
    result forward_event()
sl@0
   296
    {
sl@0
   297
      return detail::result_utility::make_result( detail::do_forward_event );
sl@0
   298
    }
sl@0
   299
sl@0
   300
    result defer_event()
sl@0
   301
    {
sl@0
   302
      this->state_base_type::defer_event();
sl@0
   303
      return detail::result_utility::make_result( detail::do_defer_event );
sl@0
   304
    }
sl@0
   305
sl@0
   306
    template< class DestinationState >
sl@0
   307
    result transit()
sl@0
   308
    {
sl@0
   309
      return transit_impl< DestinationState, outermost_context_type >(
sl@0
   310
        detail::no_transition_function() );
sl@0
   311
    }
sl@0
   312
sl@0
   313
    template< class DestinationState, class TransitionContext, class Event >
sl@0
   314
    result transit(
sl@0
   315
      void ( TransitionContext::*pTransitionAction )( const Event & ),
sl@0
   316
      const Event & evt )
sl@0
   317
    {
sl@0
   318
      return transit_impl< DestinationState, TransitionContext >(
sl@0
   319
        detail::transition_function< TransitionContext, Event >(
sl@0
   320
          pTransitionAction, evt ) );
sl@0
   321
    }
sl@0
   322
sl@0
   323
    result terminate()
sl@0
   324
    {
sl@0
   325
      outermost_context_base().terminate_as_reaction( *this );
sl@0
   326
      return detail::result_utility::make_result( detail::do_discard_event );
sl@0
   327
    }
sl@0
   328
sl@0
   329
    template<
sl@0
   330
      class HistoryContext,
sl@0
   331
      detail::orthogonal_position_type orthogonalPosition >
sl@0
   332
    void clear_shallow_history()
sl@0
   333
    {
sl@0
   334
      outermost_context_base().template clear_shallow_history<
sl@0
   335
        HistoryContext, orthogonalPosition >();
sl@0
   336
    }
sl@0
   337
sl@0
   338
    template<
sl@0
   339
      class HistoryContext,
sl@0
   340
      detail::orthogonal_position_type orthogonalPosition >
sl@0
   341
    void clear_deep_history()
sl@0
   342
    {
sl@0
   343
      outermost_context_base().template clear_deep_history<
sl@0
   344
        HistoryContext, orthogonalPosition >();
sl@0
   345
    }
sl@0
   346
sl@0
   347
  protected:
sl@0
   348
    //////////////////////////////////////////////////////////////////////////
sl@0
   349
    simple_state() : pContext_( 0 ) {}
sl@0
   350
sl@0
   351
    ~simple_state()
sl@0
   352
    {
sl@0
   353
      // As a result of a throwing derived class constructor, this destructor
sl@0
   354
      // can be called before the context is set.
sl@0
   355
      if ( get_pointer( pContext_ ) != 0 )
sl@0
   356
      {
sl@0
   357
        if ( this->deferred_events() )
sl@0
   358
        {
sl@0
   359
          outermost_context_base().release_events( this );
sl@0
   360
        }
sl@0
   361
sl@0
   362
        pContext_->remove_inner_state( orthogonal_position::value );
sl@0
   363
      }
sl@0
   364
    }
sl@0
   365
sl@0
   366
  public:
sl@0
   367
    //////////////////////////////////////////////////////////////////////////
sl@0
   368
    // The following declarations should be private.
sl@0
   369
    // They are only public because many compilers lack template friends.
sl@0
   370
    //////////////////////////////////////////////////////////////////////////
sl@0
   371
    typedef typename Context::inner_orthogonal_position orthogonal_position;
sl@0
   372
sl@0
   373
    // If you receive a
sl@0
   374
    // "use of undefined type 'boost::STATIC_ASSERTION_FAILURE<x>'" or similar
sl@0
   375
    // compiler error here then either this state resides in a non-existent
sl@0
   376
    // orthogonal region of the outer state or the outer state does not have
sl@0
   377
    // inner states.
sl@0
   378
    BOOST_STATIC_ASSERT( ( mpl::less<
sl@0
   379
      orthogonal_position,
sl@0
   380
      typename context_type::no_of_orthogonal_regions >::value ) );
sl@0
   381
sl@0
   382
    typedef MostDerived inner_context_type;
sl@0
   383
    typedef mpl::integral_c< detail::orthogonal_position_type, 0 >
sl@0
   384
      inner_orthogonal_position;
sl@0
   385
sl@0
   386
    typedef typename context_type::event_base_type event_base_type;
sl@0
   387
    typedef typename context_type::rtti_policy_type rtti_policy_type;
sl@0
   388
sl@0
   389
    typedef typename context_type::outermost_context_base_type
sl@0
   390
      outermost_context_base_type;
sl@0
   391
    typedef typename context_type::inner_context_ptr_type context_ptr_type;
sl@0
   392
    typedef typename context_type::state_list_type state_list_type;
sl@0
   393
    typedef intrusive_ptr< inner_context_type > inner_context_ptr_type;
sl@0
   394
    typedef typename detail::make_list< InnerInitial >::type
sl@0
   395
      inner_initial_list;
sl@0
   396
    typedef typename mpl::size< inner_initial_list >::type
sl@0
   397
      inner_initial_list_size;
sl@0
   398
    typedef mpl::integral_c<
sl@0
   399
      detail::orthogonal_position_type,
sl@0
   400
      inner_initial_list_size::value > no_of_orthogonal_regions;
sl@0
   401
    typedef typename mpl::push_front<
sl@0
   402
      typename context_type::context_type_list,
sl@0
   403
      context_type >::type context_type_list;
sl@0
   404
sl@0
   405
    // If you receive a
sl@0
   406
    // "use of undefined type 'boost::STATIC_ASSERTION_FAILURE<x>'" or similar
sl@0
   407
    // compiler error here then the direct or indirect context of this state
sl@0
   408
    // has deep history _and_ this state has two or more orthogonal regions.
sl@0
   409
    // Boost.Statechart does not currently support deep history in a state whose
sl@0
   410
    // direct or indirect inner states have two or more orthogonal regions.
sl@0
   411
    // Please consult the documentation on how to work around this limitation.
sl@0
   412
    BOOST_STATIC_ASSERT( ( mpl::or_<
sl@0
   413
      mpl::less<
sl@0
   414
        no_of_orthogonal_regions,
sl@0
   415
        mpl::integral_c< detail::orthogonal_position_type, 2 > >,
sl@0
   416
      mpl::not_<
sl@0
   417
        typename context_type::inherited_deep_history > >::value ) );
sl@0
   418
sl@0
   419
    typedef mpl::bool_< ( historyMode & has_shallow_history ) != 0 >
sl@0
   420
      shallow_history;
sl@0
   421
    typedef typename context_type::shallow_history stores_shallow_history;
sl@0
   422
sl@0
   423
    typedef mpl::bool_< ( historyMode & has_deep_history ) != 0 >
sl@0
   424
      deep_history;
sl@0
   425
    typedef typename mpl::or_<
sl@0
   426
      deep_history, 
sl@0
   427
      typename context_type::inherited_deep_history
sl@0
   428
    >::type inherited_deep_history;
sl@0
   429
    typedef typename mpl::and_<
sl@0
   430
      inherited_deep_history,
sl@0
   431
      mpl::empty< inner_initial_list > >::type stores_deep_history;
sl@0
   432
sl@0
   433
    void * operator new( std::size_t size )
sl@0
   434
    {
sl@0
   435
      return detail::allocate< MostDerived,
sl@0
   436
        typename outermost_context_type::allocator_type >( size );
sl@0
   437
    }
sl@0
   438
sl@0
   439
    void operator delete( void * pState )
sl@0
   440
    {
sl@0
   441
      detail::deallocate< MostDerived,
sl@0
   442
        typename outermost_context_type::allocator_type >( pState );
sl@0
   443
    }
sl@0
   444
sl@0
   445
    outermost_context_base_type & outermost_context_base()
sl@0
   446
    {
sl@0
   447
      // This assert fails when an attempt is made to access the state machine
sl@0
   448
      // from a constructor of a state that is *not* a subtype of state<>.
sl@0
   449
      // To correct this, derive from state<> instead of simple_state<>.
sl@0
   450
      BOOST_ASSERT( get_pointer( pContext_ ) != 0 );
sl@0
   451
      return pContext_->outermost_context_base();
sl@0
   452
    }
sl@0
   453
sl@0
   454
    const outermost_context_base_type & outermost_context_base() const
sl@0
   455
    {
sl@0
   456
      // This assert fails when an attempt is made to access the state machine
sl@0
   457
      // from a constructor of a state that is *not* a subtype of state<>.
sl@0
   458
      // To correct this, derive from state<> instead of simple_state<>.
sl@0
   459
      BOOST_ASSERT( get_pointer( pContext_ ) != 0 );
sl@0
   460
      return pContext_->outermost_context_base();
sl@0
   461
    }
sl@0
   462
sl@0
   463
    virtual const state_base_type * outer_state_ptr() const
sl@0
   464
    {
sl@0
   465
      typedef typename mpl::if_<
sl@0
   466
        is_same< outermost_context_type, context_type >,
sl@0
   467
        outer_state_ptr_impl_outermost,
sl@0
   468
        outer_state_ptr_impl_non_outermost
sl@0
   469
      >::type impl;
sl@0
   470
      return impl::outer_state_ptr_impl( *this );
sl@0
   471
    }
sl@0
   472
sl@0
   473
    virtual detail::reaction_result react_impl(
sl@0
   474
      const event_base_type & evt,
sl@0
   475
      typename rtti_policy_type::id_type eventType )
sl@0
   476
    {
sl@0
   477
      typedef typename detail::make_list<
sl@0
   478
        typename MostDerived::reactions >::type reaction_list;
sl@0
   479
      detail::reaction_result reactionResult =
sl@0
   480
        local_react< reaction_list >( evt, eventType );
sl@0
   481
sl@0
   482
      // At this point we can only safely access pContext_ if the handler did
sl@0
   483
      // not return do_discard_event!
sl@0
   484
      switch ( reactionResult )
sl@0
   485
      {
sl@0
   486
        case detail::do_forward_event:
sl@0
   487
          // TODO: The following call to react_impl of our outer state should
sl@0
   488
          // be made with a context_type:: prefix to call directly instead of
sl@0
   489
          // virtually. For some reason the compiler complains...
sl@0
   490
          reactionResult = pContext_->react_impl( evt, eventType );
sl@0
   491
          break;
sl@0
   492
        case detail::do_defer_event:
sl@0
   493
          outermost_context_base().defer_event( evt, this );
sl@0
   494
          break;
sl@0
   495
        default:
sl@0
   496
          break;
sl@0
   497
      }
sl@0
   498
sl@0
   499
      return reactionResult;
sl@0
   500
    }
sl@0
   501
sl@0
   502
    virtual void exit_impl(
sl@0
   503
      typename base_type::direct_state_base_ptr_type & pSelf,
sl@0
   504
      typename state_base_type::node_state_base_ptr_type &
sl@0
   505
        pOutermostUnstableState,
sl@0
   506
      bool performFullExit )
sl@0
   507
    {
sl@0
   508
      inner_context_ptr_type pMostDerivedSelf =
sl@0
   509
        polymorphic_downcast< MostDerived * >( this );
sl@0
   510
      pSelf = 0;
sl@0
   511
      exit_impl( pMostDerivedSelf, pOutermostUnstableState, performFullExit );
sl@0
   512
    }
sl@0
   513
sl@0
   514
    void exit_impl(
sl@0
   515
      inner_context_ptr_type & pSelf,
sl@0
   516
      typename state_base_type::node_state_base_ptr_type &
sl@0
   517
        pOutermostUnstableState,
sl@0
   518
      bool performFullExit )
sl@0
   519
    {
sl@0
   520
      switch ( this->ref_count() )
sl@0
   521
      {
sl@0
   522
        case 2:
sl@0
   523
          if ( get_pointer( pOutermostUnstableState ) ==
sl@0
   524
            static_cast< state_base_type * >( this ) )
sl@0
   525
          {
sl@0
   526
            pContext_->set_outermost_unstable_state(
sl@0
   527
              pOutermostUnstableState );
sl@0
   528
            // fall through to next case intended
sl@0
   529
          }
sl@0
   530
          else
sl@0
   531
          {
sl@0
   532
            break;
sl@0
   533
          }
sl@0
   534
        case 1:
sl@0
   535
        {
sl@0
   536
          if ( get_pointer( pOutermostUnstableState ) == 0 )
sl@0
   537
          {
sl@0
   538
            pContext_->set_outermost_unstable_state(
sl@0
   539
              pOutermostUnstableState );
sl@0
   540
          }
sl@0
   541
sl@0
   542
          if ( performFullExit )
sl@0
   543
          {
sl@0
   544
            pSelf->exit();
sl@0
   545
            check_store_shallow_history< stores_shallow_history >();
sl@0
   546
            check_store_deep_history< stores_deep_history >();
sl@0
   547
          }
sl@0
   548
sl@0
   549
          context_ptr_type pContext = pContext_;
sl@0
   550
          pSelf = 0;
sl@0
   551
          pContext->exit_impl(
sl@0
   552
            pContext, pOutermostUnstableState, performFullExit );
sl@0
   553
          break;
sl@0
   554
        }
sl@0
   555
        default:
sl@0
   556
          break;
sl@0
   557
      }
sl@0
   558
    }
sl@0
   559
sl@0
   560
    void set_outermost_unstable_state(
sl@0
   561
      typename state_base_type::node_state_base_ptr_type &
sl@0
   562
        pOutermostUnstableState )
sl@0
   563
    {
sl@0
   564
      pOutermostUnstableState = this;
sl@0
   565
    }
sl@0
   566
sl@0
   567
    template< class OtherContext >
sl@0
   568
    const typename OtherContext::inner_context_ptr_type & context_ptr() const
sl@0
   569
    {
sl@0
   570
      typedef typename mpl::if_<
sl@0
   571
        is_same< OtherContext, context_type >,
sl@0
   572
        context_ptr_impl_my_context,
sl@0
   573
        context_ptr_impl_other_context
sl@0
   574
      >::type impl;
sl@0
   575
sl@0
   576
      return impl::template context_ptr_impl< OtherContext >( *this );
sl@0
   577
    }
sl@0
   578
sl@0
   579
    static void initial_deep_construct(
sl@0
   580
      outermost_context_base_type & outermostContextBase )
sl@0
   581
    {
sl@0
   582
      deep_construct( &outermostContextBase, outermostContextBase );
sl@0
   583
    }
sl@0
   584
sl@0
   585
    static void deep_construct(
sl@0
   586
      const context_ptr_type & pContext,
sl@0
   587
      outermost_context_base_type & outermostContextBase )
sl@0
   588
    {
sl@0
   589
      const inner_context_ptr_type pInnerContext(
sl@0
   590
        shallow_construct( pContext, outermostContextBase ) );
sl@0
   591
      deep_construct_inner< inner_initial_list >(
sl@0
   592
        pInnerContext, outermostContextBase );
sl@0
   593
    }
sl@0
   594
sl@0
   595
    static inner_context_ptr_type shallow_construct(
sl@0
   596
      const context_ptr_type & pContext,
sl@0
   597
      outermost_context_base_type & outermostContextBase )
sl@0
   598
    {
sl@0
   599
      const inner_context_ptr_type pInnerContext( new MostDerived );
sl@0
   600
      pInnerContext->set_context( pContext );
sl@0
   601
      outermostContextBase.add( pInnerContext );
sl@0
   602
      return pInnerContext;
sl@0
   603
    }
sl@0
   604
sl@0
   605
    void set_context( const context_ptr_type & pContext )
sl@0
   606
    {
sl@0
   607
      BOOST_ASSERT( get_pointer( pContext ) != 0 );
sl@0
   608
      pContext_ = pContext;
sl@0
   609
      base_type::set_context(
sl@0
   610
        orthogonal_position::value, get_pointer( pContext ) );
sl@0
   611
    }
sl@0
   612
sl@0
   613
    template< class InnerList >
sl@0
   614
    static void deep_construct_inner(
sl@0
   615
      const inner_context_ptr_type & pInnerContext,
sl@0
   616
      outermost_context_base_type & outermostContextBase )
sl@0
   617
    {
sl@0
   618
      typedef typename mpl::if_<
sl@0
   619
        mpl::empty< InnerList >,
sl@0
   620
        deep_construct_inner_impl_empty,
sl@0
   621
        deep_construct_inner_impl_non_empty
sl@0
   622
      >::type impl;
sl@0
   623
      impl::template deep_construct_inner_impl< InnerList >(
sl@0
   624
        pInnerContext, outermostContextBase );
sl@0
   625
    }
sl@0
   626
sl@0
   627
    template< class LeafState >
sl@0
   628
    void store_deep_history_impl()
sl@0
   629
    {
sl@0
   630
      detail::deep_history_storer<
sl@0
   631
        context_type::inherited_deep_history::value,
sl@0
   632
        context_type::deep_history::value
sl@0
   633
      >::template store_deep_history< MostDerived, LeafState >(
sl@0
   634
        *pContext_ );
sl@0
   635
    }
sl@0
   636
sl@0
   637
  private:
sl@0
   638
    //////////////////////////////////////////////////////////////////////////
sl@0
   639
    struct context_ptr_impl_other_context
sl@0
   640
    {
sl@0
   641
      template< class OtherContext, class State >
sl@0
   642
      static const typename OtherContext::inner_context_ptr_type &
sl@0
   643
      context_ptr_impl( const State & stt )
sl@0
   644
      {
sl@0
   645
        // This assert fails when an attempt is made to access an outer 
sl@0
   646
        // context from a constructor of a state that is *not* a subtype of
sl@0
   647
        // state<>. To correct this, derive from state<> instead of
sl@0
   648
        // simple_state<>.
sl@0
   649
        BOOST_ASSERT( get_pointer( stt.pContext_ ) != 0 );
sl@0
   650
        return stt.pContext_->template context_ptr< OtherContext >();
sl@0
   651
      }
sl@0
   652
    };
sl@0
   653
    friend struct context_ptr_impl_other_context;
sl@0
   654
sl@0
   655
    struct context_ptr_impl_my_context
sl@0
   656
    {
sl@0
   657
      template< class OtherContext, class State >
sl@0
   658
      static const typename OtherContext::inner_context_ptr_type &
sl@0
   659
      context_ptr_impl( const State & stt )
sl@0
   660
      {
sl@0
   661
        // This assert fails when an attempt is made to access an outer 
sl@0
   662
        // context from a constructor of a state that is *not* a subtype of
sl@0
   663
        // state<>. To correct this, derive from state<> instead of
sl@0
   664
        // simple_state<>.
sl@0
   665
        BOOST_ASSERT( get_pointer( stt.pContext_ ) != 0 );
sl@0
   666
        return stt.pContext_;
sl@0
   667
      }
sl@0
   668
    };
sl@0
   669
    friend struct context_ptr_impl_my_context;
sl@0
   670
sl@0
   671
    struct context_impl_other_context
sl@0
   672
    {
sl@0
   673
      template< class OtherContext, class State >
sl@0
   674
      static OtherContext & context_impl( State & stt )
sl@0
   675
      {
sl@0
   676
        // This assert fails when an attempt is made to access an outer 
sl@0
   677
        // context from a constructor of a state that is *not* a subtype of
sl@0
   678
        // state<>. To correct this, derive from state<> instead of
sl@0
   679
        // simple_state<>.
sl@0
   680
        BOOST_ASSERT( get_pointer( stt.pContext_ ) != 0 );
sl@0
   681
        return stt.pContext_->template context< OtherContext >();
sl@0
   682
      }
sl@0
   683
    };
sl@0
   684
    friend struct context_impl_other_context;
sl@0
   685
sl@0
   686
    struct context_impl_this_context
sl@0
   687
    {
sl@0
   688
      template< class OtherContext, class State >
sl@0
   689
      static OtherContext & context_impl( State & stt )
sl@0
   690
      {
sl@0
   691
        return *polymorphic_downcast< MostDerived * >( &stt );
sl@0
   692
      }
sl@0
   693
    };
sl@0
   694
    friend struct context_impl_this_context;
sl@0
   695
sl@0
   696
    template< class DestinationState,
sl@0
   697
              class TransitionContext,
sl@0
   698
              class TransitionAction >
sl@0
   699
    result transit_impl( const TransitionAction & transitionAction )
sl@0
   700
    {
sl@0
   701
      typedef typename mpl::find_if<
sl@0
   702
        context_type_list,
sl@0
   703
        mpl::contains<
sl@0
   704
          typename DestinationState::context_type_list,
sl@0
   705
          mpl::placeholders::_ > >::type common_context_iter;
sl@0
   706
      typedef typename mpl::deref< common_context_iter >::type
sl@0
   707
        common_context_type;
sl@0
   708
      typedef typename mpl::distance<
sl@0
   709
        typename mpl::begin< context_type_list >::type,
sl@0
   710
        common_context_iter >::type termination_state_position;
sl@0
   711
      typedef typename mpl::push_front< context_type_list, MostDerived >::type
sl@0
   712
        possible_transition_contexts;
sl@0
   713
      typedef typename mpl::at<
sl@0
   714
        possible_transition_contexts,
sl@0
   715
        termination_state_position >::type termination_state_type;
sl@0
   716
sl@0
   717
      termination_state_type & terminationState(
sl@0
   718
        context< termination_state_type >() );
sl@0
   719
      const typename
sl@0
   720
        common_context_type::inner_context_ptr_type pCommonContext(
sl@0
   721
          terminationState.context_ptr< common_context_type >() );
sl@0
   722
      outermost_context_base_type & outermostContextBase(
sl@0
   723
        pCommonContext->outermost_context_base() );
sl@0
   724
sl@0
   725
      #ifdef BOOST_STATECHART_RELAX_TRANSITION_CONTEXT
sl@0
   726
      typedef typename mpl::distance<
sl@0
   727
        typename mpl::begin< possible_transition_contexts >::type,
sl@0
   728
        typename mpl::find<
sl@0
   729
          possible_transition_contexts, TransitionContext >::type
sl@0
   730
      >::type proposed_transition_context_position;
sl@0
   731
sl@0
   732
      typedef typename mpl::plus<
sl@0
   733
        termination_state_position,
sl@0
   734
        mpl::long_< 1 >
sl@0
   735
      >::type uml_transition_context_position;
sl@0
   736
sl@0
   737
      typedef typename mpl::deref< typename mpl::max_element<
sl@0
   738
        mpl::list<
sl@0
   739
          proposed_transition_context_position,
sl@0
   740
          uml_transition_context_position >,
sl@0
   741
        mpl::greater< mpl::placeholders::_, mpl::placeholders::_ >
sl@0
   742
      >::type >::type real_transition_context_position;
sl@0
   743
sl@0
   744
      typedef typename mpl::at<
sl@0
   745
        possible_transition_contexts,
sl@0
   746
        real_transition_context_position >::type real_transition_context_type;
sl@0
   747
sl@0
   748
      #ifdef BOOST_MSVC
sl@0
   749
      #  pragma warning( push )
sl@0
   750
      #  pragma warning( disable: 4127 ) // conditional expression is constant
sl@0
   751
      #endif
sl@0
   752
      if ( ( proposed_transition_context_position::value == 0 ) &&
sl@0
   753
           ( inner_initial_list_size::value == 0 ) )
sl@0
   754
      {
sl@0
   755
        transitionAction( *polymorphic_downcast< MostDerived * >( this ) );
sl@0
   756
        outermostContextBase.terminate_as_part_of_transit( terminationState );
sl@0
   757
      }
sl@0
   758
      else if ( proposed_transition_context_position::value >=
sl@0
   759
                uml_transition_context_position::value )
sl@0
   760
      {
sl@0
   761
        real_transition_context_type & transitionContext =
sl@0
   762
          context< real_transition_context_type >();
sl@0
   763
        outermostContextBase.terminate_as_part_of_transit( terminationState );
sl@0
   764
        transitionAction( transitionContext );
sl@0
   765
      }
sl@0
   766
      else
sl@0
   767
      {
sl@0
   768
        typename real_transition_context_type::inner_context_ptr_type
sl@0
   769
          pTransitionContext = context_ptr< real_transition_context_type >();
sl@0
   770
        outermostContextBase.terminate_as_part_of_transit(
sl@0
   771
          *pTransitionContext );
sl@0
   772
        transitionAction( *pTransitionContext );
sl@0
   773
        pTransitionContext = 0;
sl@0
   774
        outermostContextBase.terminate_as_part_of_transit( terminationState );
sl@0
   775
      }
sl@0
   776
      #ifdef BOOST_MSVC
sl@0
   777
      #  pragma warning( pop )
sl@0
   778
      #endif
sl@0
   779
      #else
sl@0
   780
      outermostContextBase.terminate_as_part_of_transit( terminationState );
sl@0
   781
      transitionAction( *pCommonContext );
sl@0
   782
      #endif
sl@0
   783
sl@0
   784
      typedef typename detail::make_context_list<
sl@0
   785
        common_context_type, DestinationState >::type context_list_type;
sl@0
   786
sl@0
   787
      // If you receive a
sl@0
   788
      // "use of undefined type 'boost::STATIC_ASSERTION_FAILURE<x>'" or
sl@0
   789
      // similar compiler error here then you tried to make an invalid
sl@0
   790
      // transition between different orthogonal regions.
sl@0
   791
      BOOST_STATIC_ASSERT( ( mpl::equal_to<
sl@0
   792
        typename termination_state_type::orthogonal_position,
sl@0
   793
        typename mpl::front< context_list_type >::type::orthogonal_position
sl@0
   794
      >::value ) );
sl@0
   795
sl@0
   796
      detail::constructor<
sl@0
   797
        context_list_type, outermost_context_base_type >::construct(
sl@0
   798
          pCommonContext, outermostContextBase );
sl@0
   799
sl@0
   800
      return detail::result_utility::make_result( detail::do_discard_event );
sl@0
   801
    }
sl@0
   802
sl@0
   803
    struct local_react_impl_non_empty
sl@0
   804
    {
sl@0
   805
      template< class ReactionList, class State >
sl@0
   806
      static detail::reaction_result local_react_impl(
sl@0
   807
        State & stt,
sl@0
   808
        const event_base_type & evt,
sl@0
   809
        typename rtti_policy_type::id_type eventType )
sl@0
   810
      {
sl@0
   811
        detail::reaction_result reactionResult =
sl@0
   812
          mpl::front< ReactionList >::type::react(
sl@0
   813
            *polymorphic_downcast< MostDerived * >( &stt ),
sl@0
   814
            evt, eventType );
sl@0
   815
sl@0
   816
        if ( reactionResult == detail::no_reaction )
sl@0
   817
        {
sl@0
   818
          reactionResult = stt.template local_react<
sl@0
   819
            typename mpl::pop_front< ReactionList >::type >(
sl@0
   820
              evt, eventType );
sl@0
   821
        }
sl@0
   822
sl@0
   823
        return reactionResult;
sl@0
   824
      }
sl@0
   825
    };
sl@0
   826
    friend struct local_react_impl_non_empty;
sl@0
   827
sl@0
   828
    struct local_react_impl_empty
sl@0
   829
    {
sl@0
   830
      template< class ReactionList, class State >
sl@0
   831
      static detail::reaction_result local_react_impl(
sl@0
   832
        State &, const event_base_type &, typename rtti_policy_type::id_type )
sl@0
   833
      {
sl@0
   834
        return detail::do_forward_event;
sl@0
   835
      }
sl@0
   836
    };
sl@0
   837
sl@0
   838
    template< class ReactionList >
sl@0
   839
    detail::reaction_result local_react(
sl@0
   840
      const event_base_type & evt,
sl@0
   841
      typename rtti_policy_type::id_type eventType )
sl@0
   842
    {
sl@0
   843
      typedef typename mpl::if_<
sl@0
   844
        mpl::empty< ReactionList >,
sl@0
   845
        local_react_impl_empty,
sl@0
   846
        local_react_impl_non_empty
sl@0
   847
      >::type impl;
sl@0
   848
      return impl::template local_react_impl< ReactionList >(
sl@0
   849
        *this, evt, eventType );
sl@0
   850
    }
sl@0
   851
sl@0
   852
    struct outer_state_ptr_impl_non_outermost
sl@0
   853
    {
sl@0
   854
      template< class State >
sl@0
   855
      static const state_base_type * outer_state_ptr_impl( const State & stt )
sl@0
   856
      {
sl@0
   857
        return get_pointer( stt.pContext_ );
sl@0
   858
      }
sl@0
   859
    };
sl@0
   860
    friend struct outer_state_ptr_impl_non_outermost;
sl@0
   861
sl@0
   862
    struct outer_state_ptr_impl_outermost
sl@0
   863
    {
sl@0
   864
      template< class State >
sl@0
   865
      static const state_base_type * outer_state_ptr_impl( const State & )
sl@0
   866
      {
sl@0
   867
        return 0;
sl@0
   868
      }
sl@0
   869
    };
sl@0
   870
sl@0
   871
    struct deep_construct_inner_impl_non_empty
sl@0
   872
    {
sl@0
   873
      template< class InnerList >
sl@0
   874
      static void deep_construct_inner_impl(
sl@0
   875
        const inner_context_ptr_type & pInnerContext,
sl@0
   876
        outermost_context_base_type & outermostContextBase )
sl@0
   877
      {
sl@0
   878
        typedef typename mpl::front< InnerList >::type current_inner;
sl@0
   879
sl@0
   880
        // If you receive a
sl@0
   881
        // "use of undefined type 'boost::STATIC_ASSERTION_FAILURE<x>'" or
sl@0
   882
        // similar compiler error here then there is a mismatch between the
sl@0
   883
        // orthogonal position of a state and its position in the inner
sl@0
   884
        // initial list of its outer state.
sl@0
   885
        BOOST_STATIC_ASSERT( ( is_same<
sl@0
   886
          current_inner,
sl@0
   887
          typename mpl::at<
sl@0
   888
            typename current_inner::context_type::inner_initial_list,
sl@0
   889
            typename current_inner::orthogonal_position >::type >::value ) );
sl@0
   890
sl@0
   891
        current_inner::deep_construct( pInnerContext, outermostContextBase );
sl@0
   892
        deep_construct_inner< typename mpl::pop_front< InnerList >::type >(
sl@0
   893
          pInnerContext, outermostContextBase );
sl@0
   894
      }
sl@0
   895
    };
sl@0
   896
sl@0
   897
    struct deep_construct_inner_impl_empty
sl@0
   898
    {
sl@0
   899
      template< class InnerList >
sl@0
   900
      static void deep_construct_inner_impl(
sl@0
   901
        const inner_context_ptr_type &, outermost_context_base_type & ) {}
sl@0
   902
    };
sl@0
   903
sl@0
   904
    struct check_store_shallow_history_impl_no
sl@0
   905
    {
sl@0
   906
      template< class State >
sl@0
   907
      static void check_store_shallow_history_impl( State & ) {}
sl@0
   908
    };
sl@0
   909
sl@0
   910
    struct check_store_shallow_history_impl_yes
sl@0
   911
    {
sl@0
   912
      template< class State >
sl@0
   913
      static void check_store_shallow_history_impl( State & stt )
sl@0
   914
      {
sl@0
   915
        stt.outermost_context_base().template store_shallow_history<
sl@0
   916
          MostDerived >();
sl@0
   917
      }
sl@0
   918
    };
sl@0
   919
    friend struct check_store_shallow_history_impl_yes;
sl@0
   920
sl@0
   921
    template< class StoreShallowHistory >
sl@0
   922
    void check_store_shallow_history()
sl@0
   923
    {
sl@0
   924
      typedef typename mpl::if_<
sl@0
   925
        StoreShallowHistory,
sl@0
   926
        check_store_shallow_history_impl_yes,
sl@0
   927
        check_store_shallow_history_impl_no
sl@0
   928
      >::type impl;
sl@0
   929
      impl::check_store_shallow_history_impl( *this );
sl@0
   930
    }
sl@0
   931
sl@0
   932
    struct check_store_deep_history_impl_no
sl@0
   933
    {
sl@0
   934
      template< class State >
sl@0
   935
      static void check_store_deep_history_impl( State & ) {}
sl@0
   936
    };
sl@0
   937
sl@0
   938
    struct check_store_deep_history_impl_yes
sl@0
   939
    {
sl@0
   940
      template< class State >
sl@0
   941
      static void check_store_deep_history_impl( State & stt )
sl@0
   942
      {
sl@0
   943
        stt.store_deep_history_impl< MostDerived >();
sl@0
   944
      }
sl@0
   945
    };
sl@0
   946
    friend struct check_store_deep_history_impl_yes;
sl@0
   947
sl@0
   948
    template< class StoreDeepHistory >
sl@0
   949
    void check_store_deep_history()
sl@0
   950
    {
sl@0
   951
      typedef typename mpl::if_<
sl@0
   952
        StoreDeepHistory,
sl@0
   953
        check_store_deep_history_impl_yes,
sl@0
   954
        check_store_deep_history_impl_no
sl@0
   955
      >::type impl;
sl@0
   956
      impl::check_store_deep_history_impl( *this );
sl@0
   957
    }
sl@0
   958
sl@0
   959
sl@0
   960
    context_ptr_type pContext_;
sl@0
   961
};
sl@0
   962
sl@0
   963
sl@0
   964
sl@0
   965
#ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
sl@0
   966
} // namespace statechart
sl@0
   967
#endif
sl@0
   968
sl@0
   969
sl@0
   970
sl@0
   971
template< class MostDerived, class Context,
sl@0
   972
          class InnerInitial, history_mode historyMode >
sl@0
   973
inline void intrusive_ptr_release( const ::boost::statechart::simple_state<
sl@0
   974
  MostDerived, Context, InnerInitial, historyMode > * pBase )
sl@0
   975
{
sl@0
   976
  if ( pBase->release() )
sl@0
   977
  {
sl@0
   978
    // The cast is necessary because the simple_state destructor is non-
sl@0
   979
    // virtual (and inaccessible from this context)
sl@0
   980
    delete polymorphic_downcast< const MostDerived * >( pBase );
sl@0
   981
  }
sl@0
   982
}
sl@0
   983
sl@0
   984
sl@0
   985
sl@0
   986
#ifndef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
sl@0
   987
} // namespace statechart
sl@0
   988
#endif
sl@0
   989
sl@0
   990
sl@0
   991
sl@0
   992
} // namespace boost
sl@0
   993
sl@0
   994
sl@0
   995
sl@0
   996
#endif