File: Backmp11ManyDeferTransitions.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (196 lines) | stat: -rw-r--r-- 5,871 bytes parent folder | download | duplicates (3)
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
// Copyright 2024 Christophe Henry
// henry UNDERSCORE christophe AT hotmail DOT com
// This is an extended version of the state machine available in the boost::mpl library
// Distributed under the same license as the original.
// Copyright for the original version:
// Copyright 2005 David Abrahams and Aleksey Gurtovoy. Distributed
// under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

// back-end
#include "BackCommon.hpp"
//front-end
#include <boost/msm/front/functor_row.hpp>
#include <boost/msm/front/state_machine_def.hpp>
#ifndef BOOST_MSM_NONSTANDALONE_TEST
#define BOOST_TEST_MODULE backmp11_many_defer_transitions
#endif
#include <boost/test/unit_test.hpp>

using namespace boost::msm::front;
using namespace boost::msm::backmp11;
namespace mp11 = boost::mp11;

// Events
struct Event1 {};
struct Event2 {};
struct Event3 {};
struct Event4 {};

namespace uml_defer_transitions
{

// States
struct State1 : state<>
{
    using deferred_events = mp11::mp_list<Event1, Event2, Event3>;
};
struct State2 : state<>
{
    using deferred_events = mp11::mp_list<Event1, Event3>;
};
struct State3 : state<>
{
};
struct State4 : state<>
{
};
struct State5 : state<>
{
};

// ----- State machine
struct Sm1_ : state_machine_def<Sm1_>
{
    // Set initial state
    typedef State1 initial_state;
    // Enable deferred capability
    typedef int activate_deferred_events;
    // Transition table
    using transition_table = mp11::mp_list<
        //    Start   Event   Next    Action
        Row < State1, Event4, State2, none >,

        Row < State2, Event2, State3, none >,

        Row < State3, Event1, State4, none >,
        Row < State3, Event3, State5, none >,
        Row < State4, Event3, State5, none >,
        Row < State5, Event1, State4, none >
    >;
};

// Pick a back-end
using TestMachines = mp11::mp_list<
#ifndef BOOST_MSM_TEST_SKIP_BACKMP11
    state_machine<Sm1_>,
    state_machine<Sm1_, favor_compile_time_config>
#endif // BOOST_MSM_TEST_SKIP_BACKMP11
    >;

BOOST_AUTO_TEST_CASE_TEMPLATE(uml_defer_transitions, TestMachine, TestMachines)
{
    TestMachine state_machine;
    state_machine.start();
    
    state_machine.process_event(Event1());
    // Queue: Event1
    BOOST_REQUIRE(state_machine.get_deferred_events_queue().size() == 1);
    
    state_machine.process_event(Event2());
    // Queue: Event1, Event2
    BOOST_REQUIRE(state_machine.get_deferred_events_queue().size() == 2);
    
    state_machine.process_event(Event3());
    // Queue: Event1, Event2, Event3
    BOOST_REQUIRE(state_machine.get_deferred_events_queue().size() == 3);
    
    state_machine.process_event(Event4());
    // Event4 gets consumed, new state is State2.
    // Event1 stays deferred (queue: Event1, Event2, Event3)
    // Event2 gets consumed, new state is State3 (queue: Event1, Event3)
    // Event1 gets consumed, new state is State4 (queue: Event3)
    // Event3 gets consumed, new state is State5
    BOOST_REQUIRE(state_machine.get_deferred_events_queue().size() == 0);
    BOOST_REQUIRE(state_machine.template is_state_active<State5>());
}

} // namespace uml_defer_transitions

// Test case for manual deferral by using transitions with Defer actions.
// Not specified in UML and thus no clear semantics how it should behave.
// Currently a Defer action consumes the event (it gets removed from the queue)
// and then defers it (it gets pushed back to the queue), the Defer action
// returns HANDLED_DEFERRED as processing result).
namespace action_defer_transitions
{

// States
struct State1 : state<>
{
};
struct State2 : state<>
{
};
struct State3 : state<>
{
};
struct State4 : state<>
{
};
struct State5 : state<>
{
};

// ----- State machine
struct Sm1_ : state_machine_def<Sm1_>
{
    // Set initial state
    typedef State1 initial_state;
    // Enable deferred capability
    typedef int activate_deferred_events;
    // Transition table
    using transition_table = mp11::mp_list<
        //    Start   Event   Next    Action
        Row < State1, Event1, none  , Defer>,
        Row < State1, Event2, none  , Defer>,
        Row < State1, Event3, none  , Defer>,
        Row < State1, Event4, State2, none >,

        Row < State2, Event3, none  , Defer>,
        Row < State2, Event1, none  , Defer>,
        Row < State2, Event2, State3, none >,

        Row < State3, Event1, State4, none >,
        Row < State3, Event3, State5, none >,
        Row < State4, Event3, State5, none >,
        Row < State5, Event1, State4, none >
    >;
};

// Pick a back-end
using TestMachines = mp11::mp_list<
#ifndef BOOST_MSM_TEST_SKIP_BACKMP11
    state_machine<Sm1_>,
    state_machine<Sm1_, favor_compile_time_config>
#endif // BOOST_MSM_TEST_SKIP_BACKMP11
    >;

BOOST_AUTO_TEST_CASE_TEMPLATE(action_defer_transitions, TestMachine, TestMachines)
{
    TestMachine state_machine;
    state_machine.start();
    
    state_machine.process_event(Event1());
    // Queue: Event1
    BOOST_REQUIRE(state_machine.get_deferred_events_queue().size() == 1);
    
    state_machine.process_event(Event2());
    // Queue: Event2, Event1
    BOOST_REQUIRE(state_machine.get_deferred_events_queue().size() == 2);
    
    state_machine.process_event(Event3());
    // Queue: Event3, Event2, Event1
    BOOST_REQUIRE(state_machine.get_deferred_events_queue().size() == 3);
    
    state_machine.process_event(Event4());
    // Event4 gets consumed, new state is State2.
    // Event3 gets deferred (queue: Event2, Event1, Event3)
    // Event2 gets consumed, new state is State3 (queue: Event1, Event3)
    // Event1 gets consumed, new state is State4 (queue: Event3)
    BOOST_REQUIRE(state_machine.get_deferred_events_queue().size() == 1);
    BOOST_REQUIRE(state_machine.template is_state_active<State4>());
}

} // namespace action_defer_transitions