]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // boost/chrono/stopwatches/stopwatch.hpp -----------------------------// |
2 | ||
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. | |
7 | ||
8 | #ifndef BOOST_CHRONO_STOPWATCHES_STOPWATCH_HPP | |
9 | #define BOOST_CHRONO_STOPWATCHES_STOPWATCH_HPP | |
10 | ||
11 | ||
12 | #include <boost/chrono/config.hpp> | |
13 | ||
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/chrono/detail/system.hpp> | |
19 | #include <utility> | |
20 | ||
21 | namespace boost | |
22 | { | |
23 | namespace chrono | |
24 | { | |
25 | ||
26 | ||
27 | /** | |
28 | * A stopwatch is a model of @c Stopwatch taking as parameters the @c Clock and the @c LapsCollector. | |
29 | * | |
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. | |
32 | * | |
33 | * It is up to the LapsCollector to do whatever it wants with each sample. | |
34 | * A LapCollector must define a store(duration const&) and a clear() functions. | |
35 | * | |
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. | |
39 | * | |
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. | |
43 | */ | |
44 | template<typename Clock=high_resolution_clock, typename LapsCollector=no_memory<typename Clock::duration> > | |
45 | class stopwatch | |
46 | { | |
47 | public: | |
48 | typedef LapsCollector laps_collector; | |
49 | typedef Clock clock; | |
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; | |
55 | ||
56 | /** | |
57 | * Default constructor. | |
58 | * | |
59 | * Effects: Starts the stopwatch. | |
60 | * Post-conditions: is_running(). | |
61 | */ | |
62 | stopwatch() | |
63 | : | |
64 | start_(duration::zero()), | |
65 | running_(false), | |
66 | laps_collector_() | |
67 | { | |
68 | start(); | |
69 | } | |
70 | ||
71 | #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING | |
72 | /** | |
73 | * Default constructor. | |
74 | * | |
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. | |
78 | */ | |
79 | explicit stopwatch( | |
80 | system::error_code & ec | |
81 | ) : | |
82 | start_(duration::zero()), | |
83 | running_(false), | |
84 | laps_collector_() | |
85 | { | |
86 | start(ec); | |
87 | } | |
88 | #endif | |
89 | /** | |
90 | * Not starting constructor. | |
91 | * | |
92 | * Effects: Don't starts the stopwatch. | |
93 | * Post-conditions: ! is_running() if no error occur. | |
94 | */ | |
95 | explicit stopwatch( | |
96 | const dont_start_t& | |
97 | ) : | |
98 | start_(duration::zero()), | |
99 | running_(false), | |
100 | laps_collector_() | |
101 | { | |
102 | } | |
103 | ||
104 | /** | |
105 | * Starting constructor from a LapsCollector instance. | |
106 | * | |
107 | * Effects: Copies the LapsCollector. Starts the stopwatch. | |
108 | * Post-conditions: is_running() if no error occur. | |
109 | * | |
110 | * Remark: The LapsCollector is copied and owned by the stopwatch. | |
111 | */ | |
112 | explicit stopwatch( | |
113 | laps_collector const& acc | |
114 | ) : | |
115 | start_(duration::zero()), | |
116 | running_(false), | |
117 | laps_collector_(acc) | |
118 | { | |
119 | start(); | |
120 | } | |
121 | ||
122 | #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING | |
123 | /** | |
124 | * Starting constructor from a LapsCollector instance. | |
125 | * | |
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. | |
129 | * | |
130 | * Remark: The LapsCollector is copied and owned by the stopwatch. | |
131 | */ | |
132 | explicit stopwatch( | |
133 | laps_collector const& acc, | |
134 | system::error_code & ec | |
135 | ) : | |
136 | start_(duration::zero()), | |
137 | running_(false), | |
138 | laps_collector_(acc) | |
139 | { | |
140 | start(ec); | |
141 | } | |
142 | #endif | |
143 | ||
144 | /** | |
145 | * Not starting constructor from a LapsCollector instance. | |
146 | * | |
147 | * Effects: Copies the LapsCollector. Don't starts the stopwatch. | |
148 | * Post-conditions: ! is_running() if no error occur. | |
149 | * | |
150 | * Remark: The LapsCollector is copied and owned by the stopwatch. | |
151 | */ | |
152 | stopwatch( | |
153 | laps_collector const& acc, | |
154 | const dont_start_t& | |
155 | ) : | |
156 | start_(duration::zero()), | |
157 | running_(false), | |
158 | laps_collector_(acc) | |
159 | { | |
160 | } | |
161 | ||
162 | /** | |
163 | * Destructor. | |
164 | * | |
165 | * Effects: Do nothing. | |
166 | */ | |
167 | ~stopwatch() BOOST_NOEXCEPT | |
168 | { | |
169 | } | |
170 | ||
171 | /** | |
172 | * Restart the stopwatch. | |
173 | * | |
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. | |
176 | * | |
177 | * Post-conditions: is_running() if no error occur. | |
178 | */ | |
179 | void restart() | |
180 | { | |
181 | time_point tmp = clock::now(); | |
182 | ||
183 | if (is_running()) | |
184 | { | |
185 | laps_collector_.store(tmp - start_); | |
186 | } | |
187 | else | |
188 | { | |
189 | running_ = true; | |
190 | } | |
191 | start_ = tmp; | |
192 | } | |
193 | ||
194 | #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING | |
195 | /** | |
196 | * Restart the stopwatch. | |
197 | * | |
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. | |
200 | * | |
201 | * Post-conditions: is_running() if no error occur. | |
202 | */ | |
203 | void restart( | |
204 | system::error_code & ec | |
205 | ) | |
206 | { | |
207 | time_point tmp = clock::now(ec); | |
208 | if (!BOOST_CHRONO_IS_THROWS(ec) && ec) return; | |
209 | ||
210 | if (is_running()) | |
211 | { | |
212 | laps_collector_.store(tmp - start_); | |
213 | } | |
214 | else | |
215 | { | |
216 | running_ = true; | |
217 | } | |
218 | start_ = tmp; | |
219 | } | |
220 | #endif | |
221 | ||
222 | /** | |
223 | * Start the stopwatch. | |
224 | * | |
225 | * Effects: Memorize the current time. | |
226 | * | |
227 | * Post-conditions: is_running(). | |
228 | */ | |
229 | void start() | |
230 | { | |
231 | start_ = clock::now(); | |
232 | running_ = true; | |
233 | } | |
234 | ||
235 | #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING | |
236 | /** | |
237 | * Start the stopwatch. | |
238 | * | |
239 | * Effects: Assign the error code if any internal error occur while retrieving the current time. | |
240 | * Effects: Memorize the current time. | |
241 | * | |
242 | * Post-conditions: @c is_running() if no error occur. | |
243 | */ | |
244 | void start( | |
245 | system::error_code & ec | |
246 | ) | |
247 | { | |
248 | time_point tmp = clock::now(ec); | |
249 | if (!BOOST_CHRONO_IS_THROWS(ec) && ec) return; | |
250 | ||
251 | start_ = tmp; | |
252 | running_ = true; | |
253 | } | |
254 | #endif | |
255 | ||
256 | /** | |
257 | * Start the stopwatch. | |
258 | * | |
259 | * Requires: is_running(). | |
260 | * Effects: Stores the elapsed time since start time into the LapCollector. | |
261 | * | |
262 | * Throws: Any exception that the LapCollector can throw. | |
263 | * | |
264 | * Post-conditions: !is_running() if no error occur. | |
265 | */ | |
266 | void stop() | |
267 | { | |
268 | if (is_running()) | |
269 | { | |
270 | laps_collector_.store(clock::now() - start_); | |
271 | start_ = time_point(duration::zero()); | |
272 | running_ = false; | |
273 | } | |
274 | } | |
275 | ||
276 | #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING | |
277 | /** | |
278 | * Start the stopwatch. | |
279 | * | |
280 | * Requires: is_running(). | |
281 | * Effects: Assign the error code if any internal error occur while retrieving the current time. | |
282 | * Effects: Stores the elapsed time since start time into the LapCollector if no internal error occurs. | |
283 | * | |
284 | * Throws: Any exception that the LapCollector can Throw. | |
285 | * | |
286 | * Post-conditions: !is_running() if no error occur. | |
287 | */ | |
288 | void stop( | |
289 | system::error_code & ec | |
290 | ) | |
291 | { | |
292 | if (is_running()) | |
293 | { | |
294 | time_point tmp = clock::now(ec); | |
295 | if (!BOOST_CHRONO_IS_THROWS(ec) && ec) return; | |
296 | ||
297 | laps_collector_.store(tmp - start_); | |
298 | start_ = time_point(duration::zero()); | |
299 | running_ = false; | |
300 | } | |
301 | } | |
302 | #endif | |
303 | ||
304 | /** | |
305 | * States if the Stopwatch is running. | |
306 | */ | |
307 | bool is_running() const { | |
308 | return running_; | |
309 | } | |
310 | ||
311 | /** | |
312 | * Elapsed time getter for the current lap. | |
313 | * | |
314 | * Returns: the elapsed time since the last start if no internal error occur. | |
315 | * | |
316 | */ | |
317 | duration elapsed_current_lap() const | |
318 | { | |
319 | if (is_running()) | |
320 | { | |
321 | return clock::now() - start_; | |
322 | } | |
323 | else | |
324 | { | |
325 | return duration::zero(); | |
326 | } | |
327 | } | |
328 | ||
329 | #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING | |
330 | /** | |
331 | * Elapsed time getter for the current lap. | |
332 | * | |
333 | * Effects: Assign the error code if any internal error occur while retrieving the current time. | |
334 | * | |
335 | * Returns: the elapsed time since the start if no internal error occur. | |
336 | * | |
337 | */ | |
338 | duration elapsed_current_lap( | |
339 | system::error_code & ec | |
340 | ) const | |
341 | { | |
342 | if (is_running()) | |
343 | { | |
344 | time_point tmp = clock::now(ec); | |
345 | if (!BOOST_CHRONO_IS_THROWS(ec) && ec) return duration::zero(); | |
346 | ||
347 | return tmp - start_; | |
348 | } else | |
349 | { | |
350 | return duration::zero(); | |
351 | } | |
352 | } | |
353 | #endif | |
354 | ||
355 | /** | |
356 | * Elapsed time getter. | |
357 | * | |
358 | * Effects: Assign the error code if any internal error occur while retrieving the current time. | |
359 | * | |
360 | * Returns: the elapsed time since the start if no internal error occur. | |
361 | * | |
362 | */ | |
363 | duration elapsed() const | |
364 | { | |
365 | return laps_collector_.elapsed()+elapsed_current_lap(); | |
366 | } | |
367 | ||
368 | #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING | |
369 | /** | |
370 | * Elapsed time getter. | |
371 | * | |
372 | * Effects: Assign the error code if any internal error occur while retrieving the current time. | |
373 | * | |
374 | * Returns: the elapsed time since the start if no internal error occur. | |
375 | * | |
376 | */ | |
377 | duration elapsed( | |
378 | system::error_code & ec | |
379 | ) const | |
380 | { | |
381 | duration tmp = elapsed_current_lap(ec); | |
382 | if (!BOOST_CHRONO_IS_THROWS(ec) && ec) return duration::zero(); | |
383 | return laps_collector_.elapsed() + tmp; | |
384 | } | |
385 | #endif | |
386 | /** | |
387 | * Elapsed time for the last lap. | |
388 | * | |
389 | * Returns: the elapsed time of the last lap. | |
390 | * | |
391 | */ | |
392 | ||
393 | duration last() const | |
394 | { | |
395 | return laps_collector_.last(); | |
396 | } | |
397 | /** | |
398 | * Resets the stopwatch. | |
399 | * | |
400 | * Effects: Resets the LapCollector. | |
401 | * | |
402 | * Post-conditions: !is_running() if no error occur. | |
403 | * | |
404 | */ | |
405 | void reset() | |
406 | { | |
407 | ||
408 | laps_collector_.reset(); | |
409 | running_ = false; | |
410 | start_ = time_point(duration::zero()); | |
411 | } | |
412 | ||
413 | /** | |
414 | * LapsCollector getter. | |
415 | * | |
416 | * Returns: the LapCollector instance. | |
417 | * | |
418 | */ | |
419 | laps_collector const& get_laps_collector() BOOST_NOEXCEPT | |
420 | { | |
421 | return laps_collector_; | |
422 | } | |
423 | ||
424 | /** | |
425 | * Useful typedef for scoped run | |
426 | */ | |
427 | typedef stopwatch_runner<stopwatch<Clock, LapsCollector> > | |
428 | scoped_run; | |
429 | /** | |
430 | * Useful typedef for scoped stop | |
431 | */ | |
432 | typedef stopwatch_stopper<stopwatch<Clock, LapsCollector> > | |
433 | scoped_stop; | |
434 | ||
435 | private: | |
436 | time_point start_; | |
437 | bool running_; | |
438 | laps_collector laps_collector_; | |
439 | }; | |
440 | ||
441 | } // namespace chrono | |
442 | } // namespace boost | |
443 | ||
444 | #endif // header |