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 
7 
8 
9 #include <boost/statechart/state_machine.hpp>
10 #include <boost/statechart/state.hpp>
11 #include <boost/statechart/exception_translator.hpp>
12 #include <boost/statechart/event.hpp>
13 #include <boost/statechart/in_state_reaction.hpp>
14 #include <boost/statechart/transition.hpp>
15 
16 #include <boost/mpl/list.hpp>
17 
18 #include <boost/test/test_tools.hpp>
19 
20 #include <memory>   // std::allocator
21 
22 
23 
24 namespace sc = boost::statechart;
25 namespace mpl = boost::mpl;
26 
27 
28 
29 struct EvGoToB : sc::event< EvGoToB > {};
30 struct EvDoIt : sc::event< EvDoIt > {};
31 
32 struct A;
33 struct TriggringEventTest : sc::state_machine<
34   TriggringEventTest, A,
35   std::allocator< void >, sc::exception_translator<> >
36 {
TransitTriggringEventTest37   void Transit(const EvGoToB &)
38   {
39     BOOST_REQUIRE(dynamic_cast<const EvGoToB *>(triggering_event()) != 0);
40   }
41 };
42 
43 struct B : sc::state< B, TriggringEventTest >
44 {
BB45   B( my_context ctx ) : my_base( ctx )
46   {
47     BOOST_REQUIRE(dynamic_cast<const EvGoToB *>(triggering_event()) != 0);
48   }
49 
~BB50   ~B()
51   {
52     BOOST_REQUIRE(triggering_event() == 0);
53   }
54 
DoItB55   void DoIt( const EvDoIt & )
56   {
57     BOOST_REQUIRE(dynamic_cast<const EvDoIt *>(triggering_event()) != 0);
58     throw std::exception();
59   }
60 
HandleExceptionB61   void HandleException( const sc::exception_thrown & )
62   {
63     BOOST_REQUIRE(dynamic_cast<const sc::exception_thrown *>(triggering_event()) != 0);
64   }
65 
66   typedef mpl::list<
67     sc::in_state_reaction< EvDoIt, B, &B::DoIt >,
68     sc::in_state_reaction< sc::exception_thrown, B, &B::HandleException >
69   > reactions;
70 };
71 
72 struct A : sc::state< A, TriggringEventTest >
73 {
74   typedef sc::transition<
75     EvGoToB, B, TriggringEventTest, &TriggringEventTest::Transit
76   > reactions;
77 
AA78   A( my_context ctx ) : my_base( ctx )
79   {
80     BOOST_REQUIRE(triggering_event() == 0);
81   }
82 
~AA83   ~A()
84   {
85     BOOST_REQUIRE(dynamic_cast<const EvGoToB *>(triggering_event()) != 0);
86   }
87 };
88 
89 
test_main(int,char * [])90 int test_main( int, char* [] )
91 {
92   TriggringEventTest machine;
93   machine.initiate();
94   machine.process_event(EvGoToB());
95   machine.process_event(EvDoIt());
96   machine.terminate();
97   return 0;
98 }
99