File: Backmp11Deferred.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 (241 lines) | stat: -rw-r--r-- 8,445 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright 2025 Christian Granzin
// Copyright 2010 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/state_machine_def.hpp>
#include <boost/msm/front/functor_row.hpp>

#ifndef BOOST_MSM_NONSTANDALONE_TEST
#define BOOST_TEST_MODULE backmp11_deferred
#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 ManualDeferEvent {};
struct FromHandleAllToHandleNone {};
struct FromHandleNoneToHandleAll {};
struct FromDeferEvent1And2ToDeferEvent1 {};
struct FromDeferEvent1ToHandleNone {};

// Actions
struct Action
{
    template <typename Fsm, typename SourceState, typename TargetState>
    void operator()(const Event1&, Fsm& fsm, SourceState&, TargetState&)
    {
        fsm.event1_action_calls++;
    }

    template <typename Fsm, typename SourceState, typename TargetState>
    void operator()(const Event2&, Fsm& fsm, SourceState&, TargetState&)
    {
        fsm.event2_action_calls++;
    }
};

// Common states
struct StateHandleNone : state<> {};
struct StateHandleAll : state<> {};

template<typename T>
struct StateMachineBase_ : state_machine_def<T> 
{
    bool check_and_reset_event1_action_calls(size_t expected)
    {
        const bool result = (event1_action_calls == expected);
        event1_action_calls = 0;
        return result;
    }

    bool check_and_reset_event2_action_calls(size_t expected)
    {
        const bool result = (event2_action_calls == expected);
        event2_action_calls = 0;
        return result;
    }

    size_t event1_action_calls{};
    size_t event2_action_calls{};
};

namespace uml_deferred
{

struct StateDeferEvent1 : state<>
{
    using deferred_events = mp11::mp_list<Event1>;
};

struct StateDeferEvent1And2 : state<>
{
    using deferred_events = mp11::mp_list<Event1, Event2>;
};

struct StateMachine_ : StateMachineBase_<StateMachine_> 
{
    using initial_state =
        mp11::mp_list<StateHandleAll, StateDeferEvent1, StateDeferEvent1And2>;

    using transition_table = mp11::mp_list<
        Row<StateHandleNone      , FromHandleNoneToHandleAll        , StateHandleAll   , none  >,
        Row<StateHandleAll       , FromHandleAllToHandleNone        , StateHandleNone  , none  >,
        Row<StateDeferEvent1And2 , FromDeferEvent1And2ToDeferEvent1 , StateDeferEvent1 , none  >,
        Row<StateDeferEvent1     , FromDeferEvent1ToHandleNone      , StateHandleNone  , none  >,
        Row<StateHandleAll       , Event1                           , none             , Action>,
        Row<StateHandleAll       , Event2                           , none             , Action>,
        Row<StateHandleAll       , ManualDeferEvent                 , none             , Defer  >
    >;
};

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


BOOST_AUTO_TEST_CASE_TEMPLATE(uml_deferred, Fsm, Fsms)
{     
    Fsm fsm;

    fsm.start();
    
    fsm.process_event(Event1{});
    BOOST_REQUIRE(fsm.check_and_reset_event1_action_calls(0));
    BOOST_REQUIRE(fsm.check_and_reset_event2_action_calls(0));
    BOOST_REQUIRE(fsm.get_deferred_events_queue().size() == 1);
    
    fsm.process_event(Event2{});
    BOOST_REQUIRE(fsm.check_and_reset_event1_action_calls(0));
    BOOST_REQUIRE(fsm.check_and_reset_event2_action_calls(0));
    BOOST_REQUIRE(fsm.get_deferred_events_queue().size() == 2);
    
    fsm.process_event(FromDeferEvent1And2ToDeferEvent1{});
    BOOST_REQUIRE(fsm.check_and_reset_event1_action_calls(0));
    BOOST_REQUIRE(fsm.check_and_reset_event2_action_calls(1));
    BOOST_REQUIRE(fsm.get_deferred_events_queue().size() == 1);
    
    fsm.process_event(FromDeferEvent1ToHandleNone{});
    BOOST_REQUIRE(fsm.check_and_reset_event1_action_calls(1));
    BOOST_REQUIRE(fsm.check_and_reset_event2_action_calls(0));
    BOOST_REQUIRE(fsm.get_deferred_events_queue().size() == 0);

    fsm.process_event(ManualDeferEvent{});
    BOOST_REQUIRE(fsm.check_and_reset_event1_action_calls(0));
    BOOST_REQUIRE(fsm.check_and_reset_event2_action_calls(0));
    BOOST_REQUIRE(fsm.get_deferred_events_queue().size() == 1);

    // The new event gets deferred: +1
    // The deferred event gets consumed and deferred again: -1, +1
    fsm.process_event(ManualDeferEvent{});
    BOOST_REQUIRE(fsm.check_and_reset_event1_action_calls(0));
    BOOST_REQUIRE(fsm.check_and_reset_event2_action_calls(0));
    BOOST_REQUIRE(fsm.get_deferred_events_queue().size() == 2);

    fsm.stop();
}

} // namespace uml_deferred

// 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_deferred
{

struct StateDeferEvent1 : state<>
{
};

struct StateDeferEvent1And2 : state<>
{
};

struct StateMachine_ : StateMachineBase_<StateMachine_> 
{
    using activate_deferred_events = int;
    using initial_state =
        mp11::mp_list<StateHandleAll, StateDeferEvent1And2>;

    using transition_table = mp11::mp_list<
        Row<StateHandleNone      , FromHandleNoneToHandleAll        , StateHandleAll   , none  >,
        Row<StateHandleAll       , FromHandleAllToHandleNone        , StateHandleNone  , none  >,
        Row<StateDeferEvent1And2 , FromDeferEvent1And2ToDeferEvent1 , StateDeferEvent1 , none  >,
        Row<StateDeferEvent1     , FromDeferEvent1ToHandleNone      , StateHandleNone  , none  >,
        Row<StateDeferEvent1     , Event1                           , none             , Defer >,
        Row<StateDeferEvent1And2 , Event1                           , none             , Defer >,
        Row<StateDeferEvent1And2 , Event2                           , none             , Defer >,
        Row<StateHandleAll       , Event1                           , none             , Action>,
        Row<StateHandleAll       , Event2                           , none             , Action>
    >;
};

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


BOOST_AUTO_TEST_CASE_TEMPLATE(action_deferred, Fsm, Fsms)
{     
    Fsm fsm;

    fsm.start();
    
    fsm.process_event(Event1{});
    // Processed by StateHandleAll, deferred by StateDeferEvent1And2.
    // Queue: Event1
    BOOST_REQUIRE(fsm.check_and_reset_event1_action_calls(1));
    BOOST_REQUIRE(fsm.get_deferred_events_queue().size() == 1);
    
    fsm.process_event(Event2{});
    // Processed by StateHandleAll, deferred by StateDeferEvent1And2.
    // StateHandleAll also processes Event1 again.
    // Queue: Event2, Event1
    BOOST_REQUIRE(fsm.check_and_reset_event1_action_calls(1));
    BOOST_REQUIRE(fsm.check_and_reset_event2_action_calls(1));
    BOOST_REQUIRE(fsm.get_deferred_events_queue().size() == 2);
    
    fsm.process_event(FromDeferEvent1And2ToDeferEvent1{});
    // Event2 is no more deferred.
    // StateHandleAll processes Event2 & Event1,
    // StateDeferEvent1 defers Event1.
    // Queue: Event1
    BOOST_REQUIRE(fsm.check_and_reset_event1_action_calls(1));
    BOOST_REQUIRE(fsm.check_and_reset_event2_action_calls(1));
    BOOST_REQUIRE(fsm.get_deferred_events_queue().size() == 1);
    
    fsm.process_event(FromDeferEvent1ToHandleNone{});
    // Event1 is no more deferred.
    // StateHandleAll processes Event1.
    BOOST_REQUIRE(fsm.check_and_reset_event1_action_calls(1));
    BOOST_REQUIRE(fsm.get_deferred_events_queue().size() == 0);

    fsm.stop();
}

} // namespace action_deferred