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
24namespace sc = boost::statechart;
25namespace mpl = boost::mpl;
26
27
28
29struct EvGoToB : sc::event< EvGoToB > {};
30struct EvDoIt : sc::event< EvDoIt > {};
31
32struct A;
33struct TriggringEventTest : sc::state_machine<
34 TriggringEventTest, A,
35 std::allocator< sc::none >, sc::exception_translator<> >
36{
37 void Transit(const EvGoToB &)
38 {
39 BOOST_REQUIRE(dynamic_cast<const EvGoToB *>(triggering_event()) != 0);
40 }
41};
42
43struct B : sc::state< B, TriggringEventTest >
44{
45 B( my_context ctx ) : my_base( ctx )
46 {
47 BOOST_REQUIRE(dynamic_cast<const EvGoToB *>(triggering_event()) != 0);
48 }
49
50 ~B()
51 {
52 BOOST_REQUIRE(triggering_event() == 0);
53 }
54
55 void DoIt( const EvDoIt & )
56 {
57 BOOST_REQUIRE(dynamic_cast<const EvDoIt *>(triggering_event()) != 0);
58 throw std::exception();
59 }
60
61 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
72struct A : sc::state< A, TriggringEventTest >
73{
74 typedef sc::transition<
75 EvGoToB, B, TriggringEventTest, &TriggringEventTest::Transit
76 > reactions;
77
78 A( my_context ctx ) : my_base( ctx )
79 {
80 BOOST_REQUIRE(triggering_event() == 0);
81 }
82
83 ~A()
84 {
85 BOOST_REQUIRE(dynamic_cast<const EvGoToB *>(triggering_event()) != 0);
86 }
87};
88
89
90int test_main( int, char* [] )
91{
92 TriggringEventTest machine;
93 machine.initiate();
94 machine.process_event(evt: EvGoToB());
95 machine.process_event(evt: EvDoIt());
96 machine.terminate();
97 return 0;
98}
99

source code of boost/libs/statechart/test/TriggeringEventTest.cpp