InStateReactionTest.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //////////////////////////////////////////////////////////////////////////////
  2. // Copyright 2005-2008 Andreas Huber Doenni
  3. // Distributed under the Boost Software License, Version 1.0. (See accompany-
  4. // ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. //////////////////////////////////////////////////////////////////////////////
  6. #include <boost/statechart/state_machine.hpp>
  7. #include <boost/statechart/event.hpp>
  8. #include <boost/statechart/simple_state.hpp>
  9. #include <boost/statechart/in_state_reaction.hpp>
  10. #include <boost/statechart/result.hpp>
  11. #include <boost/mpl/list.hpp>
  12. #include <boost/test/test_tools.hpp>
  13. namespace sc = boost::statechart;
  14. namespace mpl = boost::mpl;
  15. struct E : sc::event< E > {};
  16. struct F : sc::event< F > {};
  17. struct G : sc::event< G > {};
  18. struct H : sc::event< H > {};
  19. struct I : sc::event< I > {};
  20. struct A;
  21. struct InStateReactionTest : sc::state_machine< InStateReactionTest, A > {};
  22. struct B;
  23. struct A : sc::simple_state< A, InStateReactionTest, B >
  24. {
  25. A() : eventCount_( 0 ) {}
  26. // The following 3 functions could be implemented with one function
  27. // template, but this causes problems with CW and Intel 9.1.
  28. void IncrementCount( const sc::event_base & ) { ++eventCount_; }
  29. void IncrementCount( const E & ) { ++eventCount_; }
  30. void IncrementCount( const G & ) { ++eventCount_; }
  31. typedef mpl::list<
  32. sc::in_state_reaction< E, A, &A::IncrementCount >,
  33. sc::in_state_reaction< sc::event_base, A, &A::IncrementCount >
  34. > reactions;
  35. unsigned int eventCount_;
  36. };
  37. struct B : sc::simple_state< B, A >
  38. {
  39. B() : eventCount_( 0 ) {}
  40. void IncrementCount( const F & )
  41. {
  42. ++eventCount_;
  43. }
  44. typedef mpl::list<
  45. sc::in_state_reaction< F, B, &B::IncrementCount >,
  46. sc::in_state_reaction< G, A, &A::IncrementCount >,
  47. sc::in_state_reaction< I >
  48. > reactions;
  49. unsigned int eventCount_;
  50. };
  51. void RequireEventCounts(
  52. const InStateReactionTest & machine,
  53. unsigned int aCount, unsigned int bCount)
  54. {
  55. BOOST_REQUIRE(
  56. machine.state_downcast< const A & >().eventCount_ == aCount );
  57. BOOST_REQUIRE(
  58. machine.state_downcast< const B & >().eventCount_ == bCount );
  59. }
  60. int test_main( int, char* [] )
  61. {
  62. InStateReactionTest machine;
  63. machine.initiate();
  64. RequireEventCounts(machine, 0, 0);
  65. machine.process_event( F() );
  66. RequireEventCounts(machine, 0, 1);
  67. machine.process_event( E() );
  68. RequireEventCounts(machine, 1, 1);
  69. machine.process_event( E() );
  70. machine.process_event( F() );
  71. RequireEventCounts(machine, 2, 2);
  72. machine.process_event( G() );
  73. RequireEventCounts(machine, 3, 2);
  74. machine.process_event( H() );
  75. RequireEventCounts(machine, 4, 2);
  76. machine.process_event( I() );
  77. RequireEventCounts(machine, 4, 2);
  78. return 0;
  79. }