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 { Lightbulp__anon3d0dfd300111::Lightbulp35 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 { bistable_switch___anon3d0dfd300111::bistable_switch_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> on_entry__anon3d0dfd300111::bistable_switch_::Off59 void on_entry(Event const&, FSM& ) { } 60 template <typename Event, typename FSM> on_exit__anon3d0dfd300111::bistable_switch_::Off61 void on_exit(Event const&, FSM&) { } 62 }; 63 64 struct On : public msm::front::state<> 65 { 66 template <typename Event, typename FSM> on_entry__anon3d0dfd300111::bistable_switch_::On67 void on_entry(Event const&, FSM& ) { } 68 template <typename Event, typename FSM> on_exit__anon3d0dfd300111::bistable_switch_::On69 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 turn_on__anon3d0dfd300111::bistable_switch_75 void turn_on(ev_toggle const&) { bulp_->current = 11; } turn_off__anon3d0dfd300111::bistable_switch_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> no_transition__anon3d0dfd300111::bistable_switch_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 BOOST_AUTO_TEST_CASE(my_test)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 119