1// Copyright 2016 BogumiƂ Chojnowski
2// bogumil DOT chojnowski AT gmail DOT com
3// This is extended version of the state machine available in the boost::mpl library
4// Distributed under the same license as the original.
5// Copyright for the original version:
6// Copyright 2010 Christophe Henry
7// henry UNDERSCORE christophe AT hotmail DOT com
8// This is an extended version of the state machine available in the boost::mpl library
9// Distributed under the same license as the original.
10// Copyright for the original version:
11// Copyright 2005 David Abrahams and Aleksey Gurtovoy. Distributed
12// under the Boost Software License, Version 1.0. (See accompanying
13// file LICENSE_1_0.txt or copy at
14// http://www.boost.org/LICENSE_1_0.txt)
15
16#include <memory>
17// back-end
18#include <boost/msm/back/state_machine.hpp>
19//front-end
20#include <boost/msm/front/state_machine_def.hpp>
21#ifndef BOOST_MSM_NONSTANDALONE_TEST
22#define BOOST_TEST_MODULE MyTest
23#endif
24#include <boost/test/unit_test.hpp>
25
26namespace msm = boost::msm;
27namespace mpl = boost::mpl;
28
29
30namespace
31{
32 struct Lightbulp
33 {
34 Lightbulp(int c) : current(c) {}
35 int current;
36 };
37
38 // events
39 struct ev_toggle {};
40
41 // front-end: define the FSM structure
42 struct bistable_switch_ : public msm::front::state_machine_def<bistable_switch_>
43 {
44 bistable_switch_(std::unique_ptr<Lightbulp> bulp, int load)
45 : bulp_(std::move(bulp))
46 {
47 BOOST_CHECK_MESSAGE(bulp_->current == 3, "Wrong current value");
48 BOOST_CHECK_MESSAGE(load == 5, "Wrong load value");
49 bulp_->current = 10;
50 }
51
52 std::unique_ptr<Lightbulp> bulp_;
53
54 // The list of FSM states
55 struct Off : public msm::front::state<>
56 {
57 template <typename Event, typename FSM>
58 void on_entry(Event const&, FSM& ) { }
59 template <typename Event, typename FSM>
60 void on_exit(Event const&, FSM&) { }
61 };
62
63 struct On : public msm::front::state<>
64 {
65 template <typename Event, typename FSM>
66 void on_entry(Event const&, FSM& ) { }
67 template <typename Event, typename FSM>
68 void on_exit(Event const&, FSM&) { }
69 };
70
71 // the initial state of the player SM. Must be defined
72 typedef Off initial_state;
73
74 void turn_on(ev_toggle const&) { bulp_->current = 11; }
75 void turn_off(ev_toggle const&) { bulp_->current = 9; }
76
77 typedef bistable_switch_ bs_; // makes transition table cleaner
78
79 // Transition table for player
80 struct transition_table : mpl::vector<
81 // Start Event Next Action Guard
82 // +---------+-------------+---------+---------------------+----------------------+
83 a_row < Off , ev_toggle , On , &bs_::turn_on >,
84 a_row < On , ev_toggle , Off , &bs_::turn_off >
85 // +---------+-------------+---------+---------------------+----------------------+
86 > {};
87 // Replaces the default no-transition response.
88
89 template <typename Event, typename FSM>
90 void no_transition(Event const&, FSM&, int)
91 {
92 BOOST_FAIL("no_transition called!");
93 }
94 };
95
96 // Pick a back-end
97 typedef msm::back::state_machine<bistable_switch_> bistable_switch;
98
99 BOOST_AUTO_TEST_CASE(my_test)
100 {
101 auto bulp = std::make_unique<Lightbulp>(args: 3);
102
103 bistable_switch bs(std::move(bulp), 5);
104 BOOST_CHECK_MESSAGE(bs.bulp_->current == 10, "Wrong returned current value");
105
106 bs.start();
107
108 bs.process_event(evt: ev_toggle());
109 BOOST_CHECK_MESSAGE(bs.bulp_->current == 11, "Wrong returned current value");
110
111 bs.process_event(evt: ev_toggle());
112 BOOST_CHECK_MESSAGE(bs.bulp_->current == 9, "Wrong returned current value");
113
114 bs.stop();
115 }
116}
117
118

source code of boost/libs/msm/test/TestConstructorMovableOnlyTypes.cpp