1//////////////////////////////////////////////////////////////////////////////
2// Copyright 2005-2008 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
19namespace sc = boost::statechart;
20namespace mpl = boost::mpl;
21
22
23struct E : sc::event< E > {};
24struct F : sc::event< F > {};
25struct G : sc::event< G > {};
26struct H : sc::event< H > {};
27struct I : sc::event< I > {};
28
29struct A;
30struct InStateReactionTest : sc::state_machine< InStateReactionTest, A > {};
31
32struct B;
33struct 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 sc::in_state_reaction< I >
64 > reactions;
65
66 unsigned int eventCount_;
67 };
68
69
70
71void RequireEventCounts(
72 const InStateReactionTest & machine,
73 unsigned int aCount, unsigned int bCount)
74{
75 BOOST_REQUIRE(
76 machine.state_downcast< const A & >().eventCount_ == aCount );
77 BOOST_REQUIRE(
78 machine.state_downcast< const B & >().eventCount_ == bCount );
79}
80
81int test_main( int, char* [] )
82{
83 InStateReactionTest machine;
84 machine.initiate();
85
86 RequireEventCounts(machine, aCount: 0, bCount: 0);
87 machine.process_event( evt: F() );
88 RequireEventCounts(machine, aCount: 0, bCount: 1);
89 machine.process_event( evt: E() );
90 RequireEventCounts(machine, aCount: 1, bCount: 1);
91 machine.process_event( evt: E() );
92 machine.process_event( evt: F() );
93 RequireEventCounts(machine, aCount: 2, bCount: 2);
94 machine.process_event( evt: G() );
95 RequireEventCounts(machine, aCount: 3, bCount: 2);
96 machine.process_event( evt: H() );
97 RequireEventCounts(machine, aCount: 4, bCount: 2);
98 machine.process_event( evt: I() );
99 RequireEventCounts(machine, aCount: 4, bCount: 2);
100
101 return 0;
102}
103

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