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)
19 #include <boost/msm/back/state_machine.hpp>
21 #include <boost/msm/front/state_machine_def.hpp>
22 #ifndef BOOST_MSM_NONSTANDALONE_TEST
23 #define BOOST_TEST_MODULE MyTest
25 #include <boost/test/unit_test.hpp>
27 namespace msm = boost::msm;
28 namespace mpl = boost::mpl;
35 Lightbulp(int c) : current(c) {}
42 // front-end: define the FSM structure
43 struct bistable_switch_ : public msm::front::state_machine_def<bistable_switch_>
45 bistable_switch_(std::unique_ptr<Lightbulp> bulp, int load)
46 : bulp_(std::move(bulp))
48 BOOST_CHECK_MESSAGE(bulp_->current == 3, "Wrong current value");
49 BOOST_CHECK_MESSAGE(load == 5, "Wrong load value");
53 std::unique_ptr<Lightbulp> bulp_;
55 // The list of FSM states
56 struct Off : public msm::front::state<>
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&) { }
64 struct On : public msm::front::state<>
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&) { }
72 // the initial state of the player SM. Must be defined
73 typedef Off initial_state;
75 void turn_on(ev_toggle const&) { bulp_->current = 11; }
76 void turn_off(ev_toggle const&) { bulp_->current = 9; }
78 typedef bistable_switch_ bs_; // makes transition table cleaner
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 // +---------+-------------+---------+---------------------+----------------------+
88 // Replaces the default no-transition response.
90 template <typename Event, typename FSM>
91 void no_transition(Event const&, FSM&, int)
93 BOOST_FAIL("no_transition called!");
98 typedef msm::back::state_machine<bistable_switch_> bistable_switch;
100 BOOST_AUTO_TEST_CASE(my_test)
102 auto bulp = std::make_unique<Lightbulp>(3);
104 bistable_switch bs(std::move(bulp), 5);
105 BOOST_CHECK_MESSAGE(bs.bulp_->current == 10, "Wrong returned current value");
109 bs.process_event(ev_toggle());
110 BOOST_CHECK_MESSAGE(bs.bulp_->current == 11, "Wrong returned current value");
112 bs.process_event(ev_toggle());
113 BOOST_CHECK_MESSAGE(bs.bulp_->current == 9, "Wrong returned current value");