TriggeringEventTest.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //////////////////////////////////////////////////////////////////////////////
  2. // Copyright 2009 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/state.hpp>
  8. #include <boost/statechart/exception_translator.hpp>
  9. #include <boost/statechart/event.hpp>
  10. #include <boost/statechart/in_state_reaction.hpp>
  11. #include <boost/statechart/transition.hpp>
  12. #include <boost/mpl/list.hpp>
  13. #include <boost/test/test_tools.hpp>
  14. #include <memory> // std::allocator
  15. namespace sc = boost::statechart;
  16. namespace mpl = boost::mpl;
  17. struct EvGoToB : sc::event< EvGoToB > {};
  18. struct EvDoIt : sc::event< EvDoIt > {};
  19. struct A;
  20. struct TriggringEventTest : sc::state_machine<
  21. TriggringEventTest, A,
  22. std::allocator< sc::none >, sc::exception_translator<> >
  23. {
  24. void Transit(const EvGoToB &)
  25. {
  26. BOOST_REQUIRE(dynamic_cast<const EvGoToB *>(triggering_event()) != 0);
  27. }
  28. };
  29. struct B : sc::state< B, TriggringEventTest >
  30. {
  31. B( my_context ctx ) : my_base( ctx )
  32. {
  33. BOOST_REQUIRE(dynamic_cast<const EvGoToB *>(triggering_event()) != 0);
  34. }
  35. ~B()
  36. {
  37. BOOST_REQUIRE(triggering_event() == 0);
  38. }
  39. void DoIt( const EvDoIt & )
  40. {
  41. BOOST_REQUIRE(dynamic_cast<const EvDoIt *>(triggering_event()) != 0);
  42. throw std::exception();
  43. }
  44. void HandleException( const sc::exception_thrown & )
  45. {
  46. BOOST_REQUIRE(dynamic_cast<const sc::exception_thrown *>(triggering_event()) != 0);
  47. }
  48. typedef mpl::list<
  49. sc::in_state_reaction< EvDoIt, B, &B::DoIt >,
  50. sc::in_state_reaction< sc::exception_thrown, B, &B::HandleException >
  51. > reactions;
  52. };
  53. struct A : sc::state< A, TriggringEventTest >
  54. {
  55. typedef sc::transition<
  56. EvGoToB, B, TriggringEventTest, &TriggringEventTest::Transit
  57. > reactions;
  58. A( my_context ctx ) : my_base( ctx )
  59. {
  60. BOOST_REQUIRE(triggering_event() == 0);
  61. }
  62. ~A()
  63. {
  64. BOOST_REQUIRE(dynamic_cast<const EvGoToB *>(triggering_event()) != 0);
  65. }
  66. };
  67. int test_main( int, char* [] )
  68. {
  69. TriggringEventTest machine;
  70. machine.initiate();
  71. machine.process_event(EvGoToB());
  72. machine.process_event(EvDoIt());
  73. machine.terminate();
  74. return 0;
  75. }