1 // boost/chrono/stopwatches/laps_stopwatch.hpp -----------------------------//
3 // Copyright 2011 Vicente J. Botet Escriba
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 // See http://www.boost.org/libs/libs/chrono/stopwatches for documentation.
8 #ifndef BOOST_CHRONO_STOPWATCHES_LAPS_STOPWATCH_HPP
9 #define BOOST_CHRONO_STOPWATCHES_LAPS_STOPWATCH_HPP
12 #include <boost/chrono/config.hpp>
14 #include <boost/chrono/stopwatches/stopwatch_scoped.hpp>
15 #include <boost/chrono/stopwatches/collectors/no_memory.hpp> // default laps_collector
16 #include <boost/chrono/stopwatches/dont_start.hpp>
17 #include <boost/chrono/system_clocks.hpp> // default_clock
18 #include <boost/system/error_code.hpp>
28 * A laps_stopwatch is a model of @c Stopwatch taking as template parameters the @c Clock and the @c LapsCollector.
30 * The main difference respect to a @c simple_stopwatch is that the user can stop it.
31 * Each sequence of start-stop results in a new elapsed duration sample that is provided to the LapsCollector.
33 * It is up to the LapsCollector to make whatever wants with each sample.
34 * A LapCollector must define a store(duration const&) and a clear() functions.
36 * The library provides LapsCollectors that forget the sample, store the
37 * last one, cummulates the samples in an accumulator set or store them in a container.
38 * For simplicity the default LapCollector is the one that forget the samples.
40 * Even if it is preferable to use process or thread wide clocks,
41 * the default of the Clock parameter is high_resolution_clock,
42 * as it is the single one ensured on all platforms.
44 template<typename Clock=high_resolution_clock, typename LapsCollector=no_memory<typename Clock::duration> >
48 typedef LapsCollector laps_collector;
50 typedef typename Clock::duration duration;
51 typedef typename Clock::time_point time_point;
52 typedef typename Clock::rep rep;
53 typedef typename Clock::period period;
54 BOOST_STATIC_CONSTEXPR bool is_steady = Clock::is_steady;
57 * Default constructor.
59 * Effects: Starts the stopwatch.
60 * Post-conditions: is_running().
62 explicit laps_stopwatch()
64 start_(duration::zero()),
71 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
73 * Default constructor.
75 * Effects: Assign the error code if any internal error occur while retrieving the current time.
76 * Effects: Starts the stopwatch.
77 * Post-conditions: is_running() if no error occur.
79 explicit laps_stopwatch(
80 system::error_code & ec
82 start_(duration::zero()),
90 * Not starting constructor.
92 * Effects: Don't starts the stopwatch.
93 * Post-conditions: ! is_running() if no error occur.
95 explicit laps_stopwatch(
98 start_(duration::zero()),
105 * Starting constructor from a LapsCollector instance.
107 * Effects: Copies the LapsCollector. Starts the stopwatch.
108 * Post-conditions: is_running() if no error occur.
110 * Remark: The LapsCollector is copied and owned by the stopwatch.
112 explicit laps_stopwatch(
113 laps_collector const& acc
115 start_(duration::zero()),
122 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
124 * Starting constructor from a LapsCollector instance.
126 * Effects: Assign the error code if any internal error occur while retrieving the current time.
127 * Effects: Copies the LapsCollector. Starts the stopwatch.
128 * Post-conditions: is_running() if no error occur.
130 * Remark: The LapsCollector is copied and owned by the stopwatch.
132 explicit laps_stopwatch(
133 laps_collector const& acc,
134 system::error_code & ec
136 start_(duration::zero()),
145 * Not starting constructor from a LapsCollector instance.
147 * Effects: Copies the LapsCollector. Don't starts the stopwatch.
148 * Post-conditions: ! is_running() if no error occur.
150 * Remark: The LapsCollector is copied and owned by the stopwatch.
153 laps_collector const& acc,
156 start_(duration::zero()),
165 * Effects: Do nothing.
172 * Restart the stopwatch.
174 * Effects: Assign the error code if any internal error occur while retrieving the current time.
175 * Effects: As if stop(); start() were called, but ensuring that the start time is the same as the stop time.
177 * Post-conditions: is_running() if no error occur.
181 time_point tmp = clock::now();
185 laps_collector_.store(tmp - start_);
194 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
196 * Restart the stopwatch.
198 * Effects: Assign the error code if any internal error occur while retrieving the current time.
199 * Effects: As if stop(); start() were called, but ensuring that the start time is the same as the stop time.
201 * Post-conditions: is_running() if no error occur.
204 system::error_code & ec
207 time_point tmp = clock::now(ec);
208 if (!BOOST_CHRONO_IS_THROWS(ec) && ec) return;
212 laps_collector_.store(tmp - start_);
223 * Start the stopwatch.
225 * Effects: Memorize the current time.
227 * Post-conditions: is_running().
231 start_ = clock::now();
235 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
237 * Start the stopwatch.
239 * Effects: Assign the error code if any internal error occur while retrieving the current time.
240 * Effects: Memorize the current time.
242 * Post-conditions: is_running() if no error occur.
245 system::error_code & ec
248 time_point tmp = clock::now(ec);
249 if (!BOOST_CHRONO_IS_THROWS(ec) && ec) return;
257 * Start the stopwatch.
259 * Effects: Gives the elapsed time since start time to the LapCollector.
261 * Throws: Any exception that the LapCollector can throw when .
263 * Post-conditions: !is_running() if no error occur.
269 laps_collector_.store(clock::now() - start_);
270 start_ = time_point(duration::zero());
275 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
277 * Start the stopwatch.
279 * Effects: Assign the error code if any internal error occur while retrieving the current time.
280 * Effects: Gives the elapsed time since start time to the LapCollector if no internal error occurs.
282 * Throws: Any exception that the LapCollector can Throw.
284 * Post-conditions: !is_running() if no error occur.
287 system::error_code & ec
292 time_point tmp = clock::now(ec);
293 if (!BOOST_CHRONO_IS_THROWS(ec) && ec) return;
295 laps_collector_.store(tmp - start_);
296 start_ = time_point(duration::zero());
303 * States if the Stopwatch is running.
305 bool is_running() const {
310 * Elapsed time getter for the current lap.
312 * Returns: the elapsed time since the last start if no internal error occur.
315 duration elapsed_current_lap() const
319 return clock::now() - start_;
323 return duration::zero();
327 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
329 * Elapsed time getter for the current lap.
331 * Effects: Assign the error code if any internal error occur while retrieving the current time.
333 * Returns: the elapsed time since the start if no internal error occur.
336 duration elapsed_current_lap(
337 system::error_code & ec
342 time_point tmp = clock::now(ec);
343 if (!BOOST_CHRONO_IS_THROWS(ec) && ec) return duration::zero();
348 return duration::zero();
354 * Elapsed time getter.
356 * Effects: Assign the error code if any internal error occur while retrieving the current time.
358 * Returns: the elapsed time since the start if no internal error occur.
361 duration elapsed() const
363 return laps_collector_.elapsed()+elapsed_current_lap();
366 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
368 * Elapsed time getter.
370 * Effects: Assign the error code if any internal error occur while retrieving the current time.
372 * Returns: the elapsed time since the start if no internal error occur.
376 system::error_code & ec
379 duration tmp = elapsed_current_lap(ec);
380 if (!BOOST_CHRONO_IS_THROWS(ec) && ec) return duration::zero();
381 return laps_collector_.elapsed() + tmp;
385 * Elapsed time for the last lap.
387 * Returns: the elapsed time of the last lap.
391 duration last() const
393 return laps_collector_.last();
396 * Resets the stopwatch.
398 * Effects: Resets the LapCollector.
400 * Post-conditions: !is_running() if no error occur.
406 laps_collector_.reset();
408 start_ = time_point(duration::zero());
412 * LapsCollector getter.
414 * Returns: the LapCollector instance.
417 laps_collector const& get_laps_collector() BOOST_NOEXCEPT
419 return laps_collector_;
423 * Useful typedef for scoped run
425 typedef stopwatch_runner<laps_stopwatch<Clock, LapsCollector> >
428 * Useful typedef for scoped stop
430 typedef stopwatch_stopper<laps_stopwatch<Clock, LapsCollector> >
436 laps_collector laps_collector_;
439 } // namespace chrono