1 | ////////////////////////////////////////////////////////////////////////////// |
---|
2 | // Copyright 2002-2006 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 | |
---|
7 | |
---|
8 | |
---|
9 | ////////////////////////////////////////////////////////////////////////////// |
---|
10 | // This is a quick-and-dirty handcrafted state machine with two states and two |
---|
11 | // transitions employing GOF-visitation (two virtual calls per event). |
---|
12 | // It is used to make speed comparisons with Boost.Statechart machines. |
---|
13 | ////////////////////////////////////////////////////////////////////////////// |
---|
14 | |
---|
15 | |
---|
16 | |
---|
17 | #include <boost/config.hpp> |
---|
18 | |
---|
19 | #include <iostream> |
---|
20 | #include <iomanip> |
---|
21 | #include <ctime> |
---|
22 | |
---|
23 | #ifdef BOOST_NO_STDC_NAMESPACE |
---|
24 | namespace std |
---|
25 | { |
---|
26 | using ::clock_t; |
---|
27 | using ::clock; |
---|
28 | } |
---|
29 | #endif |
---|
30 | |
---|
31 | #ifdef BOOST_INTEL |
---|
32 | # pragma warning( disable: 304 ) // access control not specified |
---|
33 | #endif |
---|
34 | |
---|
35 | |
---|
36 | |
---|
37 | ////////////////////////////////////////////////////////////////////////////// |
---|
38 | class EvFlipBit; |
---|
39 | class state_base |
---|
40 | { |
---|
41 | public: |
---|
42 | virtual ~state_base() {}; |
---|
43 | |
---|
44 | virtual const state_base & react( const EvFlipBit & toEvent ) const = 0; |
---|
45 | |
---|
46 | protected: |
---|
47 | state_base() {} |
---|
48 | }; |
---|
49 | |
---|
50 | template< class Derived > |
---|
51 | class state : public state_base |
---|
52 | { |
---|
53 | public: |
---|
54 | static const Derived & instance() |
---|
55 | { |
---|
56 | return instance_; |
---|
57 | } |
---|
58 | |
---|
59 | private: |
---|
60 | static const Derived instance_; |
---|
61 | }; |
---|
62 | |
---|
63 | template< class Derived > |
---|
64 | const Derived state< Derived >::instance_; |
---|
65 | |
---|
66 | |
---|
67 | ////////////////////////////////////////////////////////////////////////////// |
---|
68 | class event_base |
---|
69 | { |
---|
70 | public: |
---|
71 | virtual ~event_base() {} |
---|
72 | |
---|
73 | protected: |
---|
74 | event_base() {} |
---|
75 | |
---|
76 | public: |
---|
77 | virtual const state_base & send( const state_base & toState ) const = 0; |
---|
78 | }; |
---|
79 | |
---|
80 | template< class Derived > |
---|
81 | class event : public event_base |
---|
82 | { |
---|
83 | protected: |
---|
84 | event() {} |
---|
85 | |
---|
86 | private: |
---|
87 | virtual const state_base & send( const state_base & toState ) const |
---|
88 | { |
---|
89 | return toState.react( *static_cast< const Derived * >( this ) ); |
---|
90 | } |
---|
91 | }; |
---|
92 | |
---|
93 | |
---|
94 | ////////////////////////////////////////////////////////////////////////////// |
---|
95 | class EvFlipBit : public event< EvFlipBit > {}; |
---|
96 | const EvFlipBit flip; |
---|
97 | |
---|
98 | class BitMachine |
---|
99 | { |
---|
100 | public: |
---|
101 | ////////////////////////////////////////////////////////////////////////// |
---|
102 | BitMachine() : pCurrentState_( &Off::instance() ) {} |
---|
103 | |
---|
104 | void process_event( const event_base & evt ) |
---|
105 | { |
---|
106 | pCurrentState_ = &evt.send( *pCurrentState_ ); |
---|
107 | } |
---|
108 | |
---|
109 | private: |
---|
110 | ////////////////////////////////////////////////////////////////////////// |
---|
111 | struct On : state< On > |
---|
112 | { |
---|
113 | virtual const state_base & react( const EvFlipBit & ) const |
---|
114 | { |
---|
115 | return Off::instance(); |
---|
116 | } |
---|
117 | }; |
---|
118 | |
---|
119 | struct Off : state< Off > |
---|
120 | { |
---|
121 | virtual const state_base & react( const EvFlipBit & ) const |
---|
122 | { |
---|
123 | return On::instance(); |
---|
124 | } |
---|
125 | }; |
---|
126 | |
---|
127 | const state_base * pCurrentState_; |
---|
128 | }; |
---|
129 | |
---|
130 | |
---|
131 | ////////////////////////////////////////////////////////////////////////////// |
---|
132 | char GetKey() |
---|
133 | { |
---|
134 | char key; |
---|
135 | std::cin >> key; |
---|
136 | return key; |
---|
137 | } |
---|
138 | |
---|
139 | |
---|
140 | ////////////////////////////////////////////////////////////////////////////// |
---|
141 | int main() |
---|
142 | { |
---|
143 | // common prime factors of 2^n-1 for n in [1,8] |
---|
144 | const unsigned int noOfEvents = 3 * 3 * 5 * 7 * 17 * 31 * 127; |
---|
145 | unsigned long eventsSentTotal = 0; |
---|
146 | |
---|
147 | std::cout << "Boost.Statechart Handcrafted example\n"; |
---|
148 | std::cout << "Machine configuration: " << 2 << |
---|
149 | " states interconnected with " << 2 << " transitions.\n\n"; |
---|
150 | |
---|
151 | std::cout << "p<CR>: Performance test\n"; |
---|
152 | std::cout << "e<CR>: Exits the program\n\n"; |
---|
153 | std::cout << |
---|
154 | "You may chain commands, e.g. pe<CR> performs a test and then exits the program\n\n"; |
---|
155 | |
---|
156 | BitMachine bitMachine; |
---|
157 | |
---|
158 | char key = GetKey(); |
---|
159 | |
---|
160 | while ( key != 'e' ) |
---|
161 | { |
---|
162 | switch ( key ) |
---|
163 | { |
---|
164 | case 'p': |
---|
165 | { |
---|
166 | std::cout << "\nSending " << noOfEvents << |
---|
167 | " events. Please wait...\n"; |
---|
168 | |
---|
169 | const unsigned long startEvents2 = eventsSentTotal; |
---|
170 | const std::clock_t startTime2 = std::clock(); |
---|
171 | |
---|
172 | for ( unsigned int eventNo = 0; eventNo < noOfEvents; ++eventNo ) |
---|
173 | { |
---|
174 | bitMachine.process_event( flip ); |
---|
175 | ++eventsSentTotal; |
---|
176 | } |
---|
177 | |
---|
178 | const std::clock_t elapsedTime2 = std::clock() - startTime2; |
---|
179 | const unsigned int eventsSent2 = eventsSentTotal - startEvents2; |
---|
180 | std::cout << "Time to dispatch one event and\n" << |
---|
181 | "perform the resulting transition: "; |
---|
182 | std::cout << elapsedTime2 * 1000.0 / eventsSent2 << " microseconds\n\n"; |
---|
183 | } |
---|
184 | break; |
---|
185 | |
---|
186 | default: |
---|
187 | { |
---|
188 | std::cout << "Invalid key!\n"; |
---|
189 | } |
---|
190 | } |
---|
191 | |
---|
192 | key = GetKey(); |
---|
193 | } |
---|
194 | |
---|
195 | return 0; |
---|
196 | } |
---|