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 
9 namespace
10 {
11  /**
12  * @brief An invalid timestamp.
13  */
14  const IceUtil::Time timestamp_invalid = IceUtil::Time::milliSeconds(-1);
15 } // namespace
16 
17 namespace armarx::core::time
18 {
20  _clock{clockType}, _startingTime{_clock.now()}, _time{Duration::Seconds(0)}
21  {
22  // pass
23  }
24 
26  {
27  // pass
28  }
29 
30  Duration
31  StopWatch::measure(std::function<void(void)> subjectToMeasure, ClockType clockType)
32  {
33  StopWatch sw{clockType};
34  subjectToMeasure();
35  return sw.stop();
36  }
37 
38  Duration
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 
58  DateTime
60  {
61  return _startingTime;
62  }
63 
64  DateTime
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 
75  Duration
77  {
78  const auto duration = stop();
79  reset();
80  return duration;
81  }
82 
83 
84 } // namespace armarx::core::time
85 
86 namespace 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 
103  StopWatch::measure(std::function<void(void)> subjectToMeasure, TimeMode timeMode)
104  {
105  StopWatch sw{timeMode};
106  subjectToMeasure();
107  return sw.stop();
108  }
109 
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 
132  {
133  return m_starting_time;
134  }
135 
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
armarx::core::time::StopWatch::isStopped
bool isStopped() const
Returns whether the timer is stopped or is actively measuring time.
Definition: StopWatch.cpp:53
armarx::core::time::StopWatch::startingTime
DateTime startingTime() const
Returns the date/time at starting the timer.
Definition: StopWatch.cpp:59
StopWatch.h
armarx::StopWatch::~StopWatch
virtual ~StopWatch()
Definition: StopWatch.cpp:97
armarx::core::time
Definition: Clock.cpp:13
armarx::StopWatch::StopWatch
StopWatch(armarx::TimeMode timeMode=armarx::TimeMode::SystemTime)
Definition: StopWatch.cpp:89
armarx::StopWatch::measure
static IceUtil::Time measure(std::function< void(void)> subjectToMeasure, armarx::TimeMode timeMode=armarx::TimeMode::SystemTime)
Definition: StopWatch.cpp:103
armarx::core::time::Duration::isPositive
bool isPositive() const
Tests whether the duration is positive (value in µs > 0).
Definition: Duration.cpp:168
armarx::core::time::StopWatch::stop
Duration stop()
Stops the timer and returns the measured duration.
Definition: StopWatch.cpp:39
armarx::StopWatch::startingTime
IceUtil::Time startingTime() const
Definition: StopWatch.cpp:131
armarx::core::time::Duration::Seconds
static Duration Seconds(std::int64_t seconds)
Constructs a duration in seconds.
Definition: Duration.cpp:72
armarx::core::time::ClockType
ClockType
Describes the type of clock.
Definition: ClockType.h:9
armarx::StopWatch::isStopped
bool isStopped() const
Definition: StopWatch.cpp:125
armarx::core::time::StopWatch::stopAndReset
Duration stopAndReset()
Stops and resets the timer.
Definition: StopWatch.cpp:76
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::TimeUtil::GetTime
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
Definition: TimeUtil.cpp:42
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
armarx::TimeMode
TimeMode
Time mode to be used.
Definition: TimeUtil.h:123
armarx::TimeUtil::GetTimeSince
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
armarx::StopWatch::reset
void reset()
Definition: StopWatch.cpp:118
TimeUtil.h
armarx::core::time::StopWatch::measure
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
armarx::core::time::Duration
Represents a duration.
Definition: Duration.h:16
armarx::StopWatch::stop
IceUtil::Time stop()
Definition: StopWatch.cpp:111
armarx::core::time::StopWatch
Measures the passed time between the construction or calling reset() and stop().
Definition: StopWatch.h:41
armarx::core::time::StopWatch::~StopWatch
virtual ~StopWatch()
Destructs the StopWatch.
Definition: StopWatch.cpp:25
armarx::core::time::StopWatch::StopWatch
StopWatch(ClockType clockType=ClockType::Virtual)
Constructs a StopWatch and starts it immediately.
Definition: StopWatch.cpp:19
armarx::StopWatch::stoppingTime
IceUtil::Time stoppingTime() const
Definition: StopWatch.cpp:137
armarx::core::time::StopWatch::stoppingTime
DateTime stoppingTime() const
Returns the date/time at stopping the timer.
Definition: StopWatch.cpp:65
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::core::time::StopWatch::reset
void reset()
Resets the timer.
Definition: StopWatch.cpp:46
armarx::core::time::Clock::now
DateTime now() const
Current date/time of the clock.
Definition: Clock.cpp:22
armarx::StopWatch
Definition: StopWatch.h:118