]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // boost/chrono/stopwatches/laps_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_LAPS_STOPWATCH_HPP | |
9 | #define BOOST_CHRONO_STOPWATCHES_LAPS_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/system/error_code.hpp> | |
19 | #include <utility> | |
20 | ||
21 | namespace boost | |
22 | { | |
23 | namespace chrono | |
24 | { | |
25 | ||
26 | ||
27 | /** | |
b32b8144 | 28 | * A laps_stopwatch is a model of @c Stopwatch taking as template parameters the @c Clock and the @c LapsCollector. |
7c673cae FG |
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 make whatever 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 laps_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 | explicit laps_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 laps_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 laps_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 laps_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 laps_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 | laps_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 | ~laps_stopwatch() | |
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: 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 | * Effects: Gives the elapsed time since start time to the LapCollector. | |
260 | * | |
261 | * Throws: Any exception that the LapCollector can throw when . | |
262 | * | |
263 | * Post-conditions: !is_running() if no error occur. | |
264 | */ | |
265 | void stop() | |
266 | { | |
267 | if (is_running()) | |
268 | { | |
269 | laps_collector_.store(clock::now() - start_); | |
270 | start_ = time_point(duration::zero()); | |
271 | running_ = false; | |
272 | } | |
273 | } | |
274 | ||
275 | #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING | |
276 | /** | |
277 | * Start the stopwatch. | |
278 | * | |
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. | |
281 | * | |
282 | * Throws: Any exception that the LapCollector can Throw. | |
283 | * | |
284 | * Post-conditions: !is_running() if no error occur. | |
285 | */ | |
286 | void stop( | |
287 | system::error_code & ec | |
288 | ) | |
289 | { | |
290 | if (is_running()) | |
291 | { | |
292 | time_point tmp = clock::now(ec); | |
293 | if (!BOOST_CHRONO_IS_THROWS(ec) && ec) return; | |
294 | ||
295 | laps_collector_.store(tmp - start_); | |
296 | start_ = time_point(duration::zero()); | |
297 | running_ = false; | |
298 | } | |
299 | } | |
300 | #endif | |
301 | ||
302 | /** | |
303 | * States if the Stopwatch is running. | |
304 | */ | |
305 | bool is_running() const { | |
306 | return running_; | |
307 | } | |
308 | ||
309 | /** | |
310 | * Elapsed time getter for the current lap. | |
311 | * | |
312 | * Returns: the elapsed time since the last start if no internal error occur. | |
313 | * | |
314 | */ | |
315 | duration elapsed_current_lap() const | |
316 | { | |
317 | if (is_running()) | |
318 | { | |
319 | return clock::now() - start_; | |
320 | } | |
321 | else | |
322 | { | |
323 | return duration::zero(); | |
324 | } | |
325 | } | |
326 | ||
327 | #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING | |
328 | /** | |
329 | * Elapsed time getter for the current lap. | |
330 | * | |
331 | * Effects: Assign the error code if any internal error occur while retrieving the current time. | |
332 | * | |
333 | * Returns: the elapsed time since the start if no internal error occur. | |
334 | * | |
335 | */ | |
336 | duration elapsed_current_lap( | |
337 | system::error_code & ec | |
338 | ) const | |
339 | { | |
340 | if (is_running()) | |
341 | { | |
342 | time_point tmp = clock::now(ec); | |
343 | if (!BOOST_CHRONO_IS_THROWS(ec) && ec) return duration::zero(); | |
344 | ||
345 | return tmp - start_; | |
346 | } else | |
347 | { | |
348 | return duration::zero(); | |
349 | } | |
350 | } | |
351 | #endif | |
352 | ||
353 | /** | |
354 | * Elapsed time getter. | |
355 | * | |
356 | * Effects: Assign the error code if any internal error occur while retrieving the current time. | |
357 | * | |
358 | * Returns: the elapsed time since the start if no internal error occur. | |
359 | * | |
360 | */ | |
361 | duration elapsed() const | |
362 | { | |
363 | return laps_collector_.elapsed()+elapsed_current_lap(); | |
364 | } | |
365 | ||
366 | #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING | |
367 | /** | |
368 | * Elapsed time getter. | |
369 | * | |
370 | * Effects: Assign the error code if any internal error occur while retrieving the current time. | |
371 | * | |
372 | * Returns: the elapsed time since the start if no internal error occur. | |
373 | * | |
374 | */ | |
375 | duration elapsed( | |
376 | system::error_code & ec | |
377 | ) const | |
378 | { | |
379 | duration tmp = elapsed_current_lap(ec); | |
380 | if (!BOOST_CHRONO_IS_THROWS(ec) && ec) return duration::zero(); | |
381 | return laps_collector_.elapsed() + tmp; | |
382 | } | |
383 | #endif | |
384 | /** | |
385 | * Elapsed time for the last lap. | |
386 | * | |
387 | * Returns: the elapsed time of the last lap. | |
388 | * | |
389 | */ | |
390 | ||
391 | duration last() const | |
392 | { | |
393 | return laps_collector_.last(); | |
394 | } | |
395 | /** | |
396 | * Resets the stopwatch. | |
397 | * | |
398 | * Effects: Resets the LapCollector. | |
399 | * | |
400 | * Post-conditions: !is_running() if no error occur. | |
401 | * | |
402 | */ | |
403 | void reset() | |
404 | { | |
405 | ||
406 | laps_collector_.reset(); | |
407 | running_ = false; | |
408 | start_ = time_point(duration::zero()); | |
409 | } | |
410 | ||
411 | /** | |
412 | * LapsCollector getter. | |
413 | * | |
414 | * Returns: the LapCollector instance. | |
415 | * | |
416 | */ | |
417 | laps_collector const& get_laps_collector() BOOST_NOEXCEPT | |
418 | { | |
419 | return laps_collector_; | |
420 | } | |
421 | ||
422 | /** | |
423 | * Useful typedef for scoped run | |
424 | */ | |
425 | typedef stopwatch_runner<laps_stopwatch<Clock, LapsCollector> > | |
426 | scoped_run; | |
427 | /** | |
428 | * Useful typedef for scoped stop | |
429 | */ | |
430 | typedef stopwatch_stopper<laps_stopwatch<Clock, LapsCollector> > | |
431 | scoped_stop; | |
432 | ||
433 | private: | |
434 | time_point start_; | |
435 | bool running_; | |
436 | laps_collector laps_collector_; | |
437 | }; | |
438 | ||
439 | } // namespace chrono | |
440 | } // namespace boost | |
441 | ||
442 | #endif // header |