]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | ||
9 | ||
10 | ||
11 | #include <boost/assert.hpp> | |
12 | ||
13 | ||
14 | ||
15 | namespace boost | |
16 | { | |
17 | namespace statechart | |
18 | { | |
19 | namespace detail | |
20 | { | |
21 | ||
22 | ||
23 | ||
24 | ////////////////////////////////////////////////////////////////////////////// | |
25 | enum reaction_result | |
26 | { | |
27 | no_reaction, | |
28 | do_forward_event, | |
29 | do_discard_event, | |
30 | do_defer_event, | |
31 | consumed | |
32 | }; | |
33 | ||
34 | struct result_utility; | |
35 | ||
36 | ////////////////////////////////////////////////////////////////////////////// | |
37 | class safe_reaction_result | |
38 | { | |
39 | public: | |
40 | ////////////////////////////////////////////////////////////////////////// | |
41 | safe_reaction_result( const safe_reaction_result & other ) : | |
42 | reactionResult_( other.reactionResult_ ) | |
43 | { | |
44 | // This assert fails when an attempt is made to make multiple copies of | |
45 | // a result value. This makes little sense, given the requirement that | |
46 | // an obtained result value must be returned out of the react function. | |
47 | BOOST_ASSERT( reactionResult_ != consumed ); | |
48 | other.reactionResult_ = consumed; | |
49 | } | |
50 | ||
51 | ~safe_reaction_result() | |
52 | { | |
53 | // This assert fails when an obtained result value is not returned out | |
54 | // of the react() function. This can happen if the user accidentally | |
55 | // makes more than one call to reaction functions inside react() or | |
56 | // accidentally makes one or more calls to reaction functions outside | |
57 | // react() | |
58 | BOOST_ASSERT( reactionResult_ == consumed ); | |
59 | } | |
60 | ||
61 | private: | |
62 | ////////////////////////////////////////////////////////////////////////// | |
63 | safe_reaction_result( reaction_result reactionResult ) : | |
64 | reactionResult_( reactionResult ) | |
65 | { | |
66 | } | |
67 | ||
68 | operator reaction_result() const | |
69 | { | |
70 | const reaction_result val = reactionResult_; | |
71 | reactionResult_ = consumed; | |
72 | return val; | |
73 | } | |
74 | ||
75 | safe_reaction_result & operator=( const safe_reaction_result & ); | |
76 | ||
77 | mutable reaction_result reactionResult_; | |
78 | ||
79 | friend struct result_utility; | |
80 | }; | |
81 | ||
82 | ||
83 | ||
84 | } // namespace detail | |
85 | ||
86 | ||
87 | ||
88 | #ifdef NDEBUG | |
89 | typedef detail::reaction_result result; | |
90 | #else | |
91 | typedef detail::safe_reaction_result result; | |
92 | #endif | |
93 | ||
94 | ||
95 | namespace detail | |
96 | { | |
97 | ||
98 | ||
99 | ||
100 | ////////////////////////////////////////////////////////////////////////////// | |
101 | struct result_utility | |
102 | { | |
103 | static ::boost::statechart::result make_result( reaction_result value ) | |
104 | { | |
105 | return value; | |
106 | } | |
107 | ||
108 | static reaction_result get_result( ::boost::statechart::result value ) | |
109 | { | |
110 | return value; | |
111 | } | |
112 | }; | |
113 | ||
114 | ||
115 | ||
116 | } // namespace detail | |
117 | } // namespace statechart | |
118 | } // namespace boost | |
119 | ||
120 | ||
121 | ||
122 | #endif |