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>
23 #ifdef BOOST_NO_STDC_NAMESPACE
32 # pragma warning( disable: 304 ) // access control not specified
37 //////////////////////////////////////////////////////////////////////////////
42 virtual ~state_base() {};
44 virtual const state_base
& react( const EvFlipBit
& toEvent
) const = 0;
50 template< class Derived
>
51 class state
: public state_base
54 static const Derived
& instance()
60 static const Derived instance_
;
63 template< class Derived
>
64 const Derived state
< Derived
>::instance_
;
67 //////////////////////////////////////////////////////////////////////////////
71 virtual ~event_base() {}
77 virtual const state_base
& send( const state_base
& toState
) const = 0;
80 template< class Derived
>
81 class event
: public event_base
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
> {};
101 //////////////////////////////////////////////////////////////////////////
102 BitMachine() : pCurrentState_( &Off::instance() ) {}
104 void process_event( const event_base
& evt
)
106 pCurrentState_
= &evt
.send( *pCurrentState_
);
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 //////////////////////////////////////////////////////////////////////////////
140 //////////////////////////////////////////////////////////////////////////////
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";
154 "You may chain commands, e.g. pe<CR> performs a test and then exits the program\n\n";
156 BitMachine bitMachine
;
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
);
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";
188 std::cout
<< "Invalid key!\n";