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 | |
---|
7 | |
---|
8 | |
---|
9 | #include <boost/statechart/state_machine.hpp> |
---|
10 | #include <boost/statechart/event.hpp> |
---|
11 | #include <boost/statechart/simple_state.hpp> |
---|
12 | #include <boost/statechart/in_state_reaction.hpp> |
---|
13 | #include <boost/statechart/result.hpp> |
---|
14 | |
---|
15 | #include <boost/mpl/list.hpp> |
---|
16 | |
---|
17 | #include <boost/test/test_tools.hpp> |
---|
18 | |
---|
19 | namespace sc = boost::statechart; |
---|
20 | namespace mpl = boost::mpl; |
---|
21 | |
---|
22 | |
---|
23 | struct E : sc::event< E > {}; |
---|
24 | struct F : sc::event< F > {}; |
---|
25 | struct G : sc::event< G > {}; |
---|
26 | struct H : sc::event< H > {}; |
---|
27 | |
---|
28 | struct A; |
---|
29 | struct InStateReactionTest : |
---|
30 | sc::state_machine< InStateReactionTest, A > {}; |
---|
31 | |
---|
32 | struct B; |
---|
33 | struct A : sc::simple_state< A, InStateReactionTest, B > |
---|
34 | { |
---|
35 | A() : eventCount_( 0 ) {} |
---|
36 | |
---|
37 | // The following 3 functions could be implemented with one function |
---|
38 | // template, but this causes problems with CW and Intel 9.1. |
---|
39 | void IncrementCount( const sc::event_base & ) { ++eventCount_; } |
---|
40 | void IncrementCount( const E & ) { ++eventCount_; } |
---|
41 | void IncrementCount( const G & ) { ++eventCount_; } |
---|
42 | |
---|
43 | typedef mpl::list< |
---|
44 | sc::in_state_reaction< E, A, &A::IncrementCount >, |
---|
45 | sc::in_state_reaction< sc::event_base, A, &A::IncrementCount > |
---|
46 | > reactions; |
---|
47 | |
---|
48 | unsigned int eventCount_; |
---|
49 | }; |
---|
50 | |
---|
51 | struct B : sc::simple_state< B, A > |
---|
52 | { |
---|
53 | B() : eventCount_( 0 ) {} |
---|
54 | |
---|
55 | void IncrementCount( const F & ) |
---|
56 | { |
---|
57 | ++eventCount_; |
---|
58 | } |
---|
59 | |
---|
60 | typedef mpl::list< |
---|
61 | sc::in_state_reaction< F, B, &B::IncrementCount >, |
---|
62 | sc::in_state_reaction< G, A, &A::IncrementCount > |
---|
63 | > reactions; |
---|
64 | |
---|
65 | unsigned int eventCount_; |
---|
66 | }; |
---|
67 | |
---|
68 | |
---|
69 | |
---|
70 | int test_main( int, char* [] ) |
---|
71 | { |
---|
72 | InStateReactionTest machine; |
---|
73 | machine.initiate(); |
---|
74 | |
---|
75 | BOOST_REQUIRE( machine.state_downcast< const A & >().eventCount_ == 0 ); |
---|
76 | BOOST_REQUIRE( machine.state_downcast< const B & >().eventCount_ == 0 ); |
---|
77 | machine.process_event( F() ); |
---|
78 | BOOST_REQUIRE( machine.state_downcast< const A & >().eventCount_ == 0 ); |
---|
79 | BOOST_REQUIRE( machine.state_downcast< const B & >().eventCount_ == 1 ); |
---|
80 | machine.process_event( E() ); |
---|
81 | BOOST_REQUIRE( machine.state_downcast< const A & >().eventCount_ == 1 ); |
---|
82 | BOOST_REQUIRE( machine.state_downcast< const B & >().eventCount_ == 1 ); |
---|
83 | machine.process_event( E() ); |
---|
84 | machine.process_event( F() ); |
---|
85 | BOOST_REQUIRE( machine.state_downcast< const A & >().eventCount_ == 2 ); |
---|
86 | BOOST_REQUIRE( machine.state_downcast< const B & >().eventCount_ == 2 ); |
---|
87 | machine.process_event( G() ); |
---|
88 | BOOST_REQUIRE( machine.state_downcast< const A & >().eventCount_ == 3 ); |
---|
89 | BOOST_REQUIRE( machine.state_downcast< const B & >().eventCount_ == 2 ); |
---|
90 | machine.process_event( H() ); |
---|
91 | BOOST_REQUIRE( machine.state_downcast< const A & >().eventCount_ == 4 ); |
---|
92 | BOOST_REQUIRE( machine.state_downcast< const B & >().eventCount_ == 2 ); |
---|
93 | |
---|
94 | return 0; |
---|
95 | } |
---|