StateCastTest.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //////////////////////////////////////////////////////////////////////////////
  2. // Copyright 2005-2006 Andreas Huber Doenni
  3. // Distributed under the Boost Software License, Version 1.0. (See accompany-
  4. // ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. //////////////////////////////////////////////////////////////////////////////
  6. #include <boost/statechart/state_machine.hpp>
  7. #include <boost/statechart/event.hpp>
  8. #include <boost/statechart/simple_state.hpp>
  9. #include <boost/statechart/state.hpp>
  10. #include <boost/statechart/transition.hpp>
  11. #include <boost/statechart/custom_reaction.hpp>
  12. #include <boost/mpl/list.hpp>
  13. #include <boost/intrusive_ptr.hpp>
  14. #include <boost/test/test_tools.hpp>
  15. namespace sc = boost::statechart;
  16. namespace mpl = boost::mpl;
  17. struct EvToB : sc::event< EvToB > {};
  18. struct EvToF : sc::event< EvToF > {};
  19. struct EvCheck : sc::event< EvCheck > {};
  20. struct A;
  21. struct StateCastTest : sc::state_machine< StateCastTest, A >
  22. {
  23. template< class State >
  24. void AssertInState()
  25. {
  26. BOOST_REQUIRE( state_downcast< const State * >() != 0 );
  27. BOOST_REQUIRE_NO_THROW( state_downcast< const State & >() );
  28. BOOST_REQUIRE( state_cast< const State * >() != 0 );
  29. BOOST_REQUIRE_NO_THROW( state_cast< const State & >() );
  30. }
  31. template< class State >
  32. void AssertNotInState()
  33. {
  34. BOOST_REQUIRE( state_downcast< const State * >() == 0 );
  35. BOOST_REQUIRE_THROW( state_downcast< const State & >(), std::bad_cast );
  36. BOOST_REQUIRE( state_cast< const State * >() == 0 );
  37. BOOST_REQUIRE_THROW( state_cast< const State & >(), std::bad_cast );
  38. }
  39. };
  40. template< class State, class FromState >
  41. void AssertInState( const FromState & theState )
  42. {
  43. BOOST_REQUIRE( theState.template state_downcast< const State * >() != 0 );
  44. BOOST_REQUIRE_NO_THROW( theState.template state_downcast< const State & >() );
  45. BOOST_REQUIRE( theState.template state_cast< const State * >() != 0 );
  46. BOOST_REQUIRE_NO_THROW( theState.template state_cast< const State & >() );
  47. }
  48. template< class State, class FromState >
  49. void AssertNotInState( const FromState & theState )
  50. {
  51. BOOST_REQUIRE( theState.template state_downcast< const State * >() == 0 );
  52. BOOST_REQUIRE_THROW( theState.template state_downcast< const State & >(), std::bad_cast );
  53. BOOST_REQUIRE( theState.template state_cast< const State * >() == 0 );
  54. BOOST_REQUIRE_THROW( theState.template state_cast< const State & >(), std::bad_cast );
  55. }
  56. struct B;
  57. struct C;
  58. struct D;
  59. struct A : sc::simple_state< A, StateCastTest, mpl::list< C, D > >
  60. {
  61. typedef sc::transition< EvToB, B > reactions;
  62. };
  63. struct E;
  64. struct C : sc::simple_state< C, A::orthogonal< 0 >, E > {};
  65. struct E : sc::state< E, C >
  66. {
  67. typedef sc::custom_reaction< EvCheck > reactions;
  68. E( my_context ctx ) : my_base( ctx )
  69. {
  70. post_event( boost::intrusive_ptr< EvCheck >( new EvCheck() ) );
  71. }
  72. sc::result react( const EvCheck & );
  73. };
  74. struct F : sc::state< F, C >
  75. {
  76. typedef sc::custom_reaction< EvCheck > reactions;
  77. F( my_context ctx ) : my_base( ctx )
  78. {
  79. post_event( boost::intrusive_ptr< EvCheck >( new EvCheck() ) );
  80. }
  81. sc::result react( const EvCheck & );
  82. };
  83. struct G;
  84. struct D : sc::simple_state< D, A::orthogonal< 1 >, G > {};
  85. struct G : sc::simple_state< G, D > {};
  86. struct H : sc::simple_state< H, D > {};
  87. struct B : sc::simple_state< B, StateCastTest >
  88. {
  89. typedef sc::transition< EvToF, F > reactions;
  90. };
  91. sc::result E::react( const EvCheck & )
  92. {
  93. AssertInState< A >( *this );
  94. AssertNotInState< B >( *this );
  95. AssertInState< C >( *this );
  96. AssertInState< D >( *this );
  97. AssertInState< E >( *this );
  98. AssertNotInState< F >( *this );
  99. AssertInState< G >( *this );
  100. AssertNotInState< H >( *this );
  101. return discard_event();
  102. }
  103. sc::result F::react( const EvCheck & )
  104. {
  105. AssertInState< A >( *this );
  106. AssertNotInState< B >( *this );
  107. AssertInState< C >( *this );
  108. AssertInState< D >( *this );
  109. AssertNotInState< E >( *this );
  110. AssertInState< F >( *this );
  111. AssertInState< G >( *this );
  112. AssertNotInState< H >( *this );
  113. return discard_event();
  114. }
  115. int test_main( int, char* [] )
  116. {
  117. StateCastTest machine;
  118. machine.AssertNotInState< A >();
  119. machine.AssertNotInState< B >();
  120. machine.AssertNotInState< C >();
  121. machine.AssertNotInState< D >();
  122. machine.AssertNotInState< E >();
  123. machine.AssertNotInState< F >();
  124. machine.AssertNotInState< G >();
  125. machine.AssertNotInState< H >();
  126. machine.initiate();
  127. machine.AssertInState< A >();
  128. machine.AssertNotInState< B >();
  129. machine.AssertInState< C >();
  130. machine.AssertInState< D >();
  131. machine.AssertInState< E >();
  132. machine.AssertNotInState< F >();
  133. machine.AssertInState< G >();
  134. machine.AssertNotInState< H >();
  135. machine.process_event( EvToB() );
  136. machine.AssertNotInState< A >();
  137. machine.AssertInState< B >();
  138. machine.AssertNotInState< C >();
  139. machine.AssertNotInState< D >();
  140. machine.AssertNotInState< E >();
  141. machine.AssertNotInState< F >();
  142. machine.AssertNotInState< G >();
  143. machine.AssertNotInState< H >();
  144. machine.process_event( EvToF() );
  145. machine.AssertInState< A >();
  146. machine.AssertNotInState< B >();
  147. machine.AssertInState< C >();
  148. machine.AssertInState< D >();
  149. machine.AssertNotInState< E >();
  150. machine.AssertInState< F >();
  151. machine.AssertInState< G >();
  152. machine.AssertNotInState< H >();
  153. machine.terminate();
  154. machine.AssertNotInState< A >();
  155. machine.AssertNotInState< B >();
  156. machine.AssertNotInState< C >();
  157. machine.AssertNotInState< D >();
  158. machine.AssertNotInState< E >();
  159. machine.AssertNotInState< F >();
  160. machine.AssertNotInState< G >();
  161. machine.AssertNotInState< H >();
  162. return 0;
  163. }