StopWatch.cpp
Go to the documentation of this file.
1#include "StopWatch.h"
2
3#include <exception>
4
5#include <IceUtil/Time.h>
6
8
9namespace
10{
11 /**
12 * @brief An invalid timestamp.
13 */
14 const IceUtil::Time timestamp_invalid = IceUtil::Time::milliSeconds(-1);
15} // namespace
16
17namespace armarx::core::time
18{
20 _clock{clockType}, _startingTime{_clock.now()}, _time{Duration::Seconds(0)}
21 {
22 // pass
23 }
24
26 {
27 // pass
28 }
29
31 StopWatch::measure(std::function<void(void)> subjectToMeasure, ClockType clockType)
32 {
33 StopWatch sw{clockType};
34 subjectToMeasure();
35 return sw.stop();
36 }
37
40 {
41 _time = _clock.now() - _startingTime;
42 return _time;
43 }
44
45 void
47 {
48 _startingTime = _clock.now();
49 _time = Duration::Seconds(0);
50 }
51
52 bool
54 {
55 return _time.isPositive();
56 }
57
60 {
61 return _startingTime;
62 }
63
66 {
67 if (not isStopped())
68 {
69 throw std::logic_error{"Timer was not stopped yet, cannot assess stopping time."};
70 }
71
72 return _startingTime + _time;
73 }
74
77 {
78 const auto duration = stop();
79 reset();
80 return duration;
81 }
82
83
84} // namespace armarx::core::time
85
86namespace armarx
87{
88
90 m_time_mode{timeMode},
91 m_starting_time{TimeUtil::GetTime(m_time_mode)},
92 m_time{::timestamp_invalid}
93 {
94 // pass
95 }
96
98 {
99 // pass
100 }
101
102 IceUtil::Time
103 StopWatch::measure(std::function<void(void)> subjectToMeasure, TimeMode timeMode)
104 {
105 StopWatch sw{timeMode};
106 subjectToMeasure();
107 return sw.stop();
108 }
109
110 IceUtil::Time
112 {
113 m_time = TimeUtil::GetTimeSince(m_starting_time, m_time_mode);
114 return m_time;
115 }
116
117 void
119 {
120 m_starting_time = TimeUtil::GetTime(m_time_mode);
121 m_time = ::timestamp_invalid;
122 }
123
124 bool
126 {
127 return m_time != ::timestamp_invalid;
128 }
129
130 IceUtil::Time
132 {
133 return m_starting_time;
134 }
135
136 IceUtil::Time
138 {
139 if (not isStopped())
140 {
141 throw std::logic_error{"Timer was not stopped yet, cannot assess stopping time."};
142 }
143
144 return m_starting_time + m_time;
145 }
146} // 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
virtual ~StopWatch()
Definition StopWatch.cpp:97
IceUtil::Time stoppingTime() const
bool isStopped() const
provides utility functions for getting the current time
Definition TimeUtil.h:135
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
Definition TimeUtil.cpp:42
static IceUtil::Time GetTimeSince(IceUtil::Time referenceTime, TimeMode timeMode=TimeMode::VirtualTime)
Get the difference between the current time and a reference time.
Definition TimeUtil.cpp:68
Represents a point in time.
Definition DateTime.h:25
Represents a duration.
Definition Duration.h:17
static Duration Seconds(std::int64_t seconds)
Constructs a duration in seconds.
Definition Duration.cpp:72
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
This file offers overloads of toIce() and fromIce() functions for STL container types.