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 // The following code implements the state-machine (this is the version
11 // discussed in the tutorial):
13 // --------------------------------
18 // | ---------------------------- | |
21 // | ---------------------------- |
23 // | | EvStartStop | EvStartStop |<-----O
25 // | ---------------------------- |
28 // | ---------------------------- |
29 // --------------------------------
33 #include <boost/statechart/event.hpp>
34 #include <boost/statechart/state_machine.hpp>
35 #include <boost/statechart/simple_state.hpp>
36 #include <boost/statechart/transition.hpp>
38 #include <boost/config.hpp>
43 #ifdef BOOST_NO_STDC_NAMESPACE
53 # pragma warning( disable: 304 ) // access control not specified
54 # pragma warning( disable: 444 ) // destructor for base is not virtual
55 # pragma warning( disable: 981 ) // operands are evaluated in unspecified order
60 namespace sc = boost::statechart;
64 //////////////////////////////////////////////////////////////////////////////
65 struct EvStartStop : sc::event< EvStartStop > {};
66 struct EvReset : sc::event< EvReset > {};
70 virtual double ElapsedTime() const = 0;
74 struct StopWatch : sc::state_machine< StopWatch, Active > {};
77 struct Active : sc::simple_state< Active, StopWatch, Stopped >
80 typedef sc::transition< EvReset, Active > reactions;
82 Active() : elapsedTime_( 0.0 ) {}
84 double & ElapsedTime()
89 double ElapsedTime() const
98 struct Running : IElapsedTime, sc::simple_state< Running, Active >
101 typedef sc::transition< EvStartStop, Stopped > reactions;
103 Running() : startTime_( std::time( 0 ) ) {}
107 context< Active >().ElapsedTime() = ElapsedTime();
110 virtual double ElapsedTime() const
112 return context< Active >().ElapsedTime() +
113 std::difftime( std::time( 0 ), startTime_ );
117 std::time_t startTime_;
120 struct Stopped : IElapsedTime, sc::simple_state< Stopped, Active >
122 typedef sc::transition< EvStartStop, Running > reactions;
124 virtual double ElapsedTime() const
126 return context< Active >().ElapsedTime();
131 //////////////////////////////////////////////////////////////////////////////
140 //////////////////////////////////////////////////////////////////////////////
143 std::cout << "Boost.Statechart StopWatch example\n\n";
144 std::cout << "s<CR>: Starts/Stops stop watch\n";
145 std::cout << "r<CR>: Resets stop watch\n";
146 std::cout << "d<CR>: Displays the elapsed time in seconds\n";
147 std::cout << "e<CR>: Exits the program\n\n";
148 std::cout << "You may chain commands, e.g. rs<CR> resets and starts stop watch\n\n";
151 stopWatch.initiate();
161 stopWatch.process_event( EvReset() );
167 stopWatch.process_event( EvStartStop() );
173 std::cout << "Elapsed time: " <<
174 stopWatch.state_cast< const IElapsedTime & >().ElapsedTime() << "\n";
180 std::cout << "Invalid key!\n";