StateCastTest.cpp
5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//////////////////////////////////////////////////////////////////////////////
// Copyright 2005-2006 Andreas Huber Doenni
// Distributed under the Boost Software License, Version 1.0. (See accompany-
// ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//////////////////////////////////////////////////////////////////////////////
#include <boost/statechart/state_machine.hpp>
#include <boost/statechart/event.hpp>
#include <boost/statechart/simple_state.hpp>
#include <boost/statechart/state.hpp>
#include <boost/statechart/transition.hpp>
#include <boost/statechart/custom_reaction.hpp>
#include <boost/mpl/list.hpp>
#include <boost/intrusive_ptr.hpp>
#include <boost/test/test_tools.hpp>
namespace sc = boost::statechart;
namespace mpl = boost::mpl;
struct EvToB : sc::event< EvToB > {};
struct EvToF : sc::event< EvToF > {};
struct EvCheck : sc::event< EvCheck > {};
struct A;
struct StateCastTest : sc::state_machine< StateCastTest, A >
{
template< class State >
void AssertInState()
{
BOOST_REQUIRE( state_downcast< const State * >() != 0 );
BOOST_REQUIRE_NO_THROW( state_downcast< const State & >() );
BOOST_REQUIRE( state_cast< const State * >() != 0 );
BOOST_REQUIRE_NO_THROW( state_cast< const State & >() );
}
template< class State >
void AssertNotInState()
{
BOOST_REQUIRE( state_downcast< const State * >() == 0 );
BOOST_REQUIRE_THROW( state_downcast< const State & >(), std::bad_cast );
BOOST_REQUIRE( state_cast< const State * >() == 0 );
BOOST_REQUIRE_THROW( state_cast< const State & >(), std::bad_cast );
}
};
template< class State, class FromState >
void AssertInState( const FromState & theState )
{
BOOST_REQUIRE( theState.template state_downcast< const State * >() != 0 );
BOOST_REQUIRE_NO_THROW( theState.template state_downcast< const State & >() );
BOOST_REQUIRE( theState.template state_cast< const State * >() != 0 );
BOOST_REQUIRE_NO_THROW( theState.template state_cast< const State & >() );
}
template< class State, class FromState >
void AssertNotInState( const FromState & theState )
{
BOOST_REQUIRE( theState.template state_downcast< const State * >() == 0 );
BOOST_REQUIRE_THROW( theState.template state_downcast< const State & >(), std::bad_cast );
BOOST_REQUIRE( theState.template state_cast< const State * >() == 0 );
BOOST_REQUIRE_THROW( theState.template state_cast< const State & >(), std::bad_cast );
}
struct B;
struct C;
struct D;
struct A : sc::simple_state< A, StateCastTest, mpl::list< C, D > >
{
typedef sc::transition< EvToB, B > reactions;
};
struct E;
struct C : sc::simple_state< C, A::orthogonal< 0 >, E > {};
struct E : sc::state< E, C >
{
typedef sc::custom_reaction< EvCheck > reactions;
E( my_context ctx ) : my_base( ctx )
{
post_event( boost::intrusive_ptr< EvCheck >( new EvCheck() ) );
}
sc::result react( const EvCheck & );
};
struct F : sc::state< F, C >
{
typedef sc::custom_reaction< EvCheck > reactions;
F( my_context ctx ) : my_base( ctx )
{
post_event( boost::intrusive_ptr< EvCheck >( new EvCheck() ) );
}
sc::result react( const EvCheck & );
};
struct G;
struct D : sc::simple_state< D, A::orthogonal< 1 >, G > {};
struct G : sc::simple_state< G, D > {};
struct H : sc::simple_state< H, D > {};
struct B : sc::simple_state< B, StateCastTest >
{
typedef sc::transition< EvToF, F > reactions;
};
sc::result E::react( const EvCheck & )
{
AssertInState< A >( *this );
AssertNotInState< B >( *this );
AssertInState< C >( *this );
AssertInState< D >( *this );
AssertInState< E >( *this );
AssertNotInState< F >( *this );
AssertInState< G >( *this );
AssertNotInState< H >( *this );
return discard_event();
}
sc::result F::react( const EvCheck & )
{
AssertInState< A >( *this );
AssertNotInState< B >( *this );
AssertInState< C >( *this );
AssertInState< D >( *this );
AssertNotInState< E >( *this );
AssertInState< F >( *this );
AssertInState< G >( *this );
AssertNotInState< H >( *this );
return discard_event();
}
int test_main( int, char* [] )
{
StateCastTest machine;
machine.AssertNotInState< A >();
machine.AssertNotInState< B >();
machine.AssertNotInState< C >();
machine.AssertNotInState< D >();
machine.AssertNotInState< E >();
machine.AssertNotInState< F >();
machine.AssertNotInState< G >();
machine.AssertNotInState< H >();
machine.initiate();
machine.AssertInState< A >();
machine.AssertNotInState< B >();
machine.AssertInState< C >();
machine.AssertInState< D >();
machine.AssertInState< E >();
machine.AssertNotInState< F >();
machine.AssertInState< G >();
machine.AssertNotInState< H >();
machine.process_event( EvToB() );
machine.AssertNotInState< A >();
machine.AssertInState< B >();
machine.AssertNotInState< C >();
machine.AssertNotInState< D >();
machine.AssertNotInState< E >();
machine.AssertNotInState< F >();
machine.AssertNotInState< G >();
machine.AssertNotInState< H >();
machine.process_event( EvToF() );
machine.AssertInState< A >();
machine.AssertNotInState< B >();
machine.AssertInState< C >();
machine.AssertInState< D >();
machine.AssertNotInState< E >();
machine.AssertInState< F >();
machine.AssertInState< G >();
machine.AssertNotInState< H >();
machine.terminate();
machine.AssertNotInState< A >();
machine.AssertNotInState< B >();
machine.AssertNotInState< C >();
machine.AssertNotInState< D >();
machine.AssertNotInState< E >();
machine.AssertNotInState< F >();
machine.AssertNotInState< G >();
machine.AssertNotInState< H >();
return 0;
}