Improve vacpp support.
[boost.git] / boost / libs / statechart / example / Handcrafted / Handcrafted.cpp
blob1690e9cdabe4f31e6102a6c8fda948172ca40034
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 //////////////////////////////////////////////////////////////////////////////
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 //////////////////////////////////////////////////////////////////////////////
17 #include <boost/config.hpp>
19 #include <iostream>
20 #include <iomanip>
21 #include <ctime>
23 #ifdef BOOST_NO_STDC_NAMESPACE
24 namespace std
26 using ::clock_t;
27 using ::clock;
29 #endif
31 #ifdef BOOST_INTEL
32 # pragma warning( disable: 304 ) // access control not specified
33 #endif
37 //////////////////////////////////////////////////////////////////////////////
38 class EvFlipBit;
39 class state_base
41 public:
42 virtual ~state_base() {};
44 virtual const state_base & react( const EvFlipBit & toEvent ) const = 0;
46 protected:
47 state_base() {}
50 template< class Derived >
51 class state : public state_base
53 public:
54 static const Derived & instance()
56 return instance_;
59 private:
60 static const Derived instance_;
63 template< class Derived >
64 const Derived state< Derived >::instance_;
67 //////////////////////////////////////////////////////////////////////////////
68 class event_base
70 public:
71 virtual ~event_base() {}
73 protected:
74 event_base() {}
76 public:
77 virtual const state_base & send( const state_base & toState ) const = 0;
80 template< class Derived >
81 class event : public event_base
83 protected:
84 event() {}
86 private:
87 virtual const state_base & send( const state_base & toState ) const
89 return toState.react( *static_cast< const Derived * >( this ) );
94 //////////////////////////////////////////////////////////////////////////////
95 class EvFlipBit : public event< EvFlipBit > {};
96 const EvFlipBit flip;
98 class BitMachine
100 public:
101 //////////////////////////////////////////////////////////////////////////
102 BitMachine() : pCurrentState_( &Off::instance() ) {}
104 void process_event( const event_base & evt )
106 pCurrentState_ = &evt.send( *pCurrentState_ );
109 private:
110 //////////////////////////////////////////////////////////////////////////
111 struct On : state< On >
113 virtual const state_base & react( const EvFlipBit & ) const
115 return Off::instance();
119 struct Off : state< Off >
121 virtual const state_base & react( const EvFlipBit & ) const
123 return On::instance();
127 const state_base * pCurrentState_;
131 //////////////////////////////////////////////////////////////////////////////
132 char GetKey()
134 char key;
135 std::cin >> key;
136 return key;
140 //////////////////////////////////////////////////////////////////////////////
141 int main()
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;
147 std::cout << "Boost.Statechart Handcrafted example\n";
148 std::cout << "Machine configuration: " << 2 <<
149 " states interconnected with " << 2 << " transitions.\n\n";
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";
156 BitMachine bitMachine;
158 char key = GetKey();
160 while ( key != 'e' )
162 switch ( key )
164 case 'p':
166 std::cout << "\nSending " << noOfEvents <<
167 " events. Please wait...\n";
169 const unsigned long startEvents2 = eventsSentTotal;
170 const std::clock_t startTime2 = std::clock();
172 for ( unsigned int eventNo = 0; eventNo < noOfEvents; ++eventNo )
174 bitMachine.process_event( flip );
175 ++eventsSentTotal;
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";
184 break;
186 default:
188 std::cout << "Invalid key!\n";
192 key = GetKey();
195 return 0;