TestConstructorMovableOnlyTypes.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #include <iostream>
  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. namespace msm = boost::msm;
  26. namespace mpl = boost::mpl;
  27. namespace
  28. {
  29. struct Lightbulp
  30. {
  31. Lightbulp(int c) : current(c) {}
  32. int current;
  33. };
  34. // events
  35. struct ev_toggle {};
  36. // front-end: define the FSM structure
  37. struct bistable_switch_ : public msm::front::state_machine_def<bistable_switch_>
  38. {
  39. bistable_switch_(std::unique_ptr<Lightbulp> bulp, int load)
  40. : bulp_(std::move(bulp))
  41. {
  42. BOOST_CHECK_MESSAGE(bulp_->current == 3, "Wrong current value");
  43. BOOST_CHECK_MESSAGE(load == 5, "Wrong load value");
  44. bulp_->current = 10;
  45. }
  46. std::unique_ptr<Lightbulp> bulp_;
  47. // The list of FSM states
  48. struct Off : public msm::front::state<>
  49. {
  50. template <typename Event, typename FSM>
  51. void on_entry(Event const&, FSM& ) { }
  52. template <typename Event, typename FSM>
  53. void on_exit(Event const&, FSM&) { }
  54. };
  55. struct On : 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. // the initial state of the player SM. Must be defined
  63. typedef Off initial_state;
  64. void turn_on(ev_toggle const&) { bulp_->current = 11; }
  65. void turn_off(ev_toggle const&) { bulp_->current = 9; }
  66. typedef bistable_switch_ bs_; // makes transition table cleaner
  67. // Transition table for player
  68. struct transition_table : mpl::vector<
  69. // Start Event Next Action Guard
  70. // +---------+-------------+---------+---------------------+----------------------+
  71. a_row < Off , ev_toggle , On , &bs_::turn_on >,
  72. a_row < On , ev_toggle , Off , &bs_::turn_off >
  73. // +---------+-------------+---------+---------------------+----------------------+
  74. > {};
  75. // Replaces the default no-transition response.
  76. template <typename Event, typename FSM>
  77. void no_transition(Event const&, FSM&, int)
  78. {
  79. BOOST_FAIL("no_transition called!");
  80. }
  81. };
  82. // Pick a back-end
  83. typedef msm::back::state_machine<bistable_switch_> bistable_switch;
  84. BOOST_AUTO_TEST_CASE(my_test)
  85. {
  86. auto bulp = std::make_unique<Lightbulp>(3);
  87. bistable_switch bs(std::move(bulp), 5);
  88. BOOST_CHECK_MESSAGE(bs.bulp_->current == 10, "Wrong returned current value");
  89. bs.start();
  90. bs.process_event(ev_toggle());
  91. BOOST_CHECK_MESSAGE(bs.bulp_->current == 11, "Wrong returned current value");
  92. bs.process_event(ev_toggle());
  93. BOOST_CHECK_MESSAGE(bs.bulp_->current == 9, "Wrong returned current value");
  94. bs.stop();
  95. }
  96. }