result.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #ifndef BOOST_STATECHART_RESULT_HPP_INCLUDED
  2. #define BOOST_STATECHART_RESULT_HPP_INCLUDED
  3. //////////////////////////////////////////////////////////////////////////////
  4. // Copyright 2002-2010 Andreas Huber Doenni
  5. // Distributed under the Boost Software License, Version 1.0. (See accompany-
  6. // ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. //////////////////////////////////////////////////////////////////////////////
  8. #include <boost/assert.hpp>
  9. namespace boost
  10. {
  11. namespace statechart
  12. {
  13. namespace detail
  14. {
  15. //////////////////////////////////////////////////////////////////////////////
  16. enum reaction_result
  17. {
  18. no_reaction,
  19. do_forward_event,
  20. do_discard_event,
  21. do_defer_event,
  22. consumed
  23. };
  24. struct result_utility;
  25. //////////////////////////////////////////////////////////////////////////////
  26. class safe_reaction_result
  27. {
  28. public:
  29. //////////////////////////////////////////////////////////////////////////
  30. safe_reaction_result( const safe_reaction_result & other ) :
  31. reactionResult_( other.reactionResult_ )
  32. {
  33. // This assert fails when an attempt is made to make multiple copies of
  34. // a result value. This makes little sense, given the requirement that
  35. // an obtained result value must be returned out of the react function.
  36. BOOST_ASSERT( reactionResult_ != consumed );
  37. other.reactionResult_ = consumed;
  38. }
  39. ~safe_reaction_result()
  40. {
  41. // This assert fails when an obtained result value is not returned out
  42. // of the react() function. This can happen if the user accidentally
  43. // makes more than one call to reaction functions inside react() or
  44. // accidentally makes one or more calls to reaction functions outside
  45. // react()
  46. BOOST_ASSERT( reactionResult_ == consumed );
  47. }
  48. private:
  49. //////////////////////////////////////////////////////////////////////////
  50. safe_reaction_result( reaction_result reactionResult ) :
  51. reactionResult_( reactionResult )
  52. {
  53. }
  54. operator reaction_result() const
  55. {
  56. const reaction_result val = reactionResult_;
  57. reactionResult_ = consumed;
  58. return val;
  59. }
  60. safe_reaction_result & operator=( const safe_reaction_result & );
  61. mutable reaction_result reactionResult_;
  62. friend struct result_utility;
  63. };
  64. } // namespace detail
  65. #ifdef NDEBUG
  66. typedef detail::reaction_result result;
  67. #else
  68. typedef detail::safe_reaction_result result;
  69. #endif
  70. namespace detail
  71. {
  72. //////////////////////////////////////////////////////////////////////////////
  73. struct result_utility
  74. {
  75. static ::boost::statechart::result make_result( reaction_result value )
  76. {
  77. return value;
  78. }
  79. static reaction_result get_result( ::boost::statechart::result value )
  80. {
  81. return value;
  82. }
  83. };
  84. } // namespace detail
  85. } // namespace statechart
  86. } // namespace boost
  87. #endif