StopWatch.h
Go to the documentation of this file.
1#pragma once
2
3
4#include <functional>
5
6#include <IceUtil/Time.h>
7
13
14namespace armarx::core::time
15{
16 /**
17 * @brief Measures the passed time between the construction or calling `reset()` and `stop()`.
18 *
19 * The `StopWatch` uses the system time by default, but it may use the virtual time provided
20 * by the time server as well. Also has a static method `measure()`, which takes a lambda, and
21 * returns the time it took to execute that lambda.
22 *
23 * Code examples:
24 *
25 * @code
26 * // By supplied methods.
27 * StopWatch sw;
28 * long_operation();
29 * Duration duration = sw.stop();
30 * std::cout << "Operation took " << duration << ".";
31 * @endcode
32 *
33 * @code
34 * // By executing a lambda.
35 * Duration duration = StopWatch::measure([&]() {
36 * long_operation();
37 * });
38 * std::cout << "Operation took " << duration << ".";
39 * @endcode
40 */
42 {
43
44 public:
45 /**
46 * @brief Constructs a `StopWatch` and starts it immediately.
47 * @param clockType Clock type.
48 */
50
51 /**
52 * @brief Destructs the `StopWatch`.
53 */
54 virtual ~StopWatch();
55
56 /**
57 * @brief Measures the duration needed to execute the given lambda and returns it.
58 * @param subjectToMeasure Lambda to be measured
59 * @param clockType Clock type.
60 * @return Time it took to execute the given lambda.
61 */
62 static Duration measure(std::function<void(void)> subjectToMeasure,
63 ClockType clockType = ClockType::Virtual);
64
65 /**
66 * @brief Stops the timer and returns the measured duration.
67 * @return Duration elapsed since construction or last call of `reset()`.
68 */
69 Duration stop();
70
71 /**
72 * @brief Resets the timer.
73 */
74 void reset();
75
76 /**
77 * @brief Returns whether the timer is stopped or is actively measuring time.
78 * @return True of timer is stopped, false otherwise.
79 */
80 bool isStopped() const;
81
82 /**
83 * @brief Returns the date/time at starting the timer.
84 * @return Date/time at starting the timer.
85 */
86 DateTime startingTime() const;
87
88 /**
89 * @brief Returns the date/time at stopping the timer.
90 * @return Date/time at stopping the timer.
91 *
92 * @throw std::logic_error When the timer was not stopped yet.
93 */
94 DateTime stoppingTime() const;
95
96 /**
97 * @brief Stops and resets the timer. It returns the date/time at stopping the timer.
98 * @return Date/time at stopping the timer.
99 *
100 * @throw std::logic_error When the timer was not stopped yet.
101 */
103
104 private:
105 Clock _clock;
106
107 DateTime _startingTime;
108
109 Duration _time;
110 };
111
112} // namespace armarx::core::time
113
114namespace armarx
115{
116
117
118 class [[deprecated("Use armarx::core::time::StopWatch instead")]] StopWatch
119 {
120
121 public:
123 virtual ~StopWatch();
124 static IceUtil::Time measure(std::function<void(void)> subjectToMeasure,
126 IceUtil::Time stop();
127 void reset();
128 bool isStopped() const;
129 IceUtil::Time startingTime() const;
130 IceUtil::Time stoppingTime() const;
131
132 private:
133 armarx::TimeMode m_time_mode;
134 IceUtil::Time m_starting_time;
135 IceUtil::Time m_time;
136 };
137} // namespace armarx
IceUtil::Time stop()
StopWatch(armarx::TimeMode timeMode=armarx::TimeMode::SystemTime)
Definition StopWatch.cpp:89
static IceUtil::Time measure(std::function< void(void)> subjectToMeasure, armarx::TimeMode timeMode=armarx::TimeMode::SystemTime)
IceUtil::Time startingTime() const
IceUtil::Time stoppingTime() const
bool isStopped() const
Clock to get date/time from a specific clock type or wait for certain durations or until certain date...
Definition Clock.h:26
Represents a point in time.
Definition DateTime.h:25
Represents a duration.
Definition Duration.h:17
Duration stop()
Stops the timer and returns the measured duration.
Definition StopWatch.cpp:39
Duration stopAndReset()
Stops and resets the timer.
Definition StopWatch.cpp:76
virtual ~StopWatch()
Destructs the StopWatch.
Definition StopWatch.cpp:25
static Duration measure(std::function< void(void)> subjectToMeasure, ClockType clockType=ClockType::Virtual)
Measures the duration needed to execute the given lambda and returns it.
Definition StopWatch.cpp:31
void reset()
Resets the timer.
Definition StopWatch.cpp:46
bool isStopped() const
Returns whether the timer is stopped or is actively measuring time.
Definition StopWatch.cpp:53
DateTime stoppingTime() const
Returns the date/time at stopping the timer.
Definition StopWatch.cpp:65
DateTime startingTime() const
Returns the date/time at starting the timer.
Definition StopWatch.cpp:59
StopWatch(ClockType clockType=ClockType::Virtual)
Constructs a StopWatch and starts it immediately.
Definition StopWatch.cpp:19
TimeMode
Time mode to be used.
Definition TimeUtil.h:124
ClockType
Describes the type of clock.
Definition ClockType.h:10
@ Virtual
Time given by time server if configured, realtime otherwise.
Definition ClockType.h:16
This file offers overloads of toIce() and fromIce() functions for STL container types.