1 | // |
2 | // Stopwatch.h |
3 | // |
4 | // Library: Foundation |
5 | // Package: DateTime |
6 | // Module: Stopwatch |
7 | // |
8 | // Definition of the Stopwatch class. |
9 | // |
10 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
11 | // and Contributors. |
12 | // |
13 | // SPDX-License-Identifier: BSL-1.0 |
14 | // |
15 | |
16 | |
17 | #ifndef Foundation_Stopwatch_INCLUDED |
18 | #define Foundation_Stopwatch_INCLUDED |
19 | |
20 | |
21 | #include "Poco/Foundation.h" |
22 | #include "Poco/Clock.h" |
23 | |
24 | |
25 | namespace Poco { |
26 | |
27 | |
28 | class Foundation_API Stopwatch |
29 | /// A simple facility to measure time intervals |
30 | /// with microsecond resolution. |
31 | { |
32 | public: |
33 | Stopwatch(); |
34 | ~Stopwatch(); |
35 | |
36 | void start(); |
37 | /// Starts (or restarts) the stopwatch. |
38 | |
39 | void stop(); |
40 | /// Stops or pauses the stopwatch. |
41 | |
42 | void reset(); |
43 | /// Resets the stopwatch. |
44 | |
45 | void restart(); |
46 | /// Resets and starts the stopwatch. |
47 | |
48 | Clock::ClockDiff elapsed() const; |
49 | /// Returns the elapsed time in microseconds |
50 | /// since the stopwatch started. |
51 | |
52 | int elapsedSeconds() const; |
53 | /// Returns the number of seconds elapsed |
54 | /// since the stopwatch started. |
55 | |
56 | static Clock::ClockVal resolution(); |
57 | /// Returns the resolution of the stopwatch. |
58 | |
59 | private: |
60 | Stopwatch(const Stopwatch&); |
61 | Stopwatch& operator = (const Stopwatch&); |
62 | |
63 | Clock _start; |
64 | Clock::ClockDiff _elapsed; |
65 | bool _running; |
66 | }; |
67 | |
68 | |
69 | // |
70 | // inlines |
71 | // |
72 | inline void Stopwatch::start() |
73 | { |
74 | if (!_running) |
75 | { |
76 | _start.update(); |
77 | _running = true; |
78 | } |
79 | } |
80 | |
81 | |
82 | inline void Stopwatch::stop() |
83 | { |
84 | if (_running) |
85 | { |
86 | Clock current; |
87 | _elapsed += current - _start; |
88 | _running = false; |
89 | } |
90 | } |
91 | |
92 | |
93 | inline int Stopwatch::elapsedSeconds() const |
94 | { |
95 | return int(elapsed()/resolution()); |
96 | } |
97 | |
98 | |
99 | inline Clock::ClockVal Stopwatch::resolution() |
100 | { |
101 | return Clock::resolution(); |
102 | } |
103 | |
104 | |
105 | } // namespace Poco |
106 | |
107 | |
108 | #endif // Foundation_Stopwatch_INCLUDED |
109 | |