Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / msm / test / TestConstructorMovableOnlyTypes.cpp
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 <iostream>
17 #include <memory>
18 // back-end
19 #include <boost/msm/back/state_machine.hpp>
20 //front-end
21 #include <boost/msm/front/state_machine_def.hpp>
22 #ifndef BOOST_MSM_NONSTANDALONE_TEST
23 #define BOOST_TEST_MODULE MyTest
24 #endif
25 #include <boost/test/unit_test.hpp>
26
27 namespace msm = boost::msm;
28 namespace mpl = boost::mpl;
29
30
31 namespace
32 {
33     struct Lightbulp
34     {
35         Lightbulp(int c) : current(c) {}
36         int current;
37     };
38
39     // events
40     struct ev_toggle {};
41
42     // front-end: define the FSM structure
43     struct bistable_switch_ : public msm::front::state_machine_def<bistable_switch_>
44     {
45          bistable_switch_(std::unique_ptr<Lightbulp> bulp, int load)
46             : bulp_(std::move(bulp))
47         {
48             BOOST_CHECK_MESSAGE(bulp_->current == 3, "Wrong current value");
49             BOOST_CHECK_MESSAGE(load == 5, "Wrong load value");
50             bulp_->current = 10;
51         }
52
53         std::unique_ptr<Lightbulp> bulp_;
54
55         // The list of FSM states
56         struct Off : public msm::front::state<>
57         {
58             template <typename Event, typename FSM>
59             void on_entry(Event const&, FSM& ) { }
60             template <typename Event, typename FSM>
61             void on_exit(Event const&, FSM&) { }
62         };
63
64         struct On : public msm::front::state<>
65         {
66             template <typename Event, typename FSM>
67             void on_entry(Event const&, FSM& ) { }
68             template <typename Event, typename FSM>
69             void on_exit(Event const&, FSM&) { }
70         };
71
72         // the initial state of the player SM. Must be defined
73         typedef Off initial_state;
74
75         void turn_on(ev_toggle const&) { bulp_->current = 11; }
76         void turn_off(ev_toggle const&) { bulp_->current = 9; }
77
78         typedef bistable_switch_ bs_; // makes transition table cleaner
79
80         // Transition table for player
81         struct transition_table : mpl::vector<
82             //    Start     Event         Next      Action                               Guard
83             //  +---------+-------------+---------+---------------------+----------------------+
84           a_row < Off     , ev_toggle   , On      , &bs_::turn_on                              >,
85           a_row < On      , ev_toggle   , Off     , &bs_::turn_off                             >
86             //  +---------+-------------+---------+---------------------+----------------------+
87         > {};
88         // Replaces the default no-transition response.
89
90         template <typename Event, typename FSM>
91         void no_transition(Event const&, FSM&, int)
92         {
93             BOOST_FAIL("no_transition called!");
94         }
95     };
96
97     // Pick a back-end
98     typedef msm::back::state_machine<bistable_switch_> bistable_switch;
99
100     BOOST_AUTO_TEST_CASE(my_test)
101     {
102         auto bulp = std::make_unique<Lightbulp>(3);
103
104         bistable_switch bs(std::move(bulp), 5);
105         BOOST_CHECK_MESSAGE(bs.bulp_->current == 10, "Wrong returned current value");
106
107         bs.start();
108
109         bs.process_event(ev_toggle());
110         BOOST_CHECK_MESSAGE(bs.bulp_->current == 11, "Wrong returned current value");
111
112         bs.process_event(ev_toggle());
113         BOOST_CHECK_MESSAGE(bs.bulp_->current == 9, "Wrong returned current value");
114
115         bs.stop();
116     }
117 }
118