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 
10 namespace
11 {
12  /**
13  * @brief An invalid timestamp.
14  */
15  const IceUtil::Time timestamp_invalid = IceUtil::Time::milliSeconds(-1);
16 } // namespace
17 
18 namespace armarx::core::time
19 {
21  _clock{clockType}, _startingTime{_clock.now()}, _time{Duration::Seconds(0)}
22  {
23  // pass
24  }
25 
26 
28  {
29  // pass
30  }
31 
32 
33  Duration
34  StopWatch::measure(std::function<void(void)> subjectToMeasure, ClockType clockType)
35  {
36  StopWatch sw{clockType};
37  subjectToMeasure();
38  return sw.stop();
39  }
40 
41 
42  Duration
44  {
45  _time = _clock.now() - _startingTime;
46  return _time;
47  }
48 
49 
50  void
52  {
53  _startingTime = _clock.now();
54  _time = Duration::Seconds(0);
55  }
56 
57 
58  bool
60  {
61  return _time.isPositive();
62  }
63 
64 
65  DateTime
67  {
68  return _startingTime;
69  }
70 
71 
72  DateTime
74  {
75  if (not isStopped())
76  {
77  throw std::logic_error{"Timer was not stopped yet, cannot assess stopping time."};
78  }
79 
80  return _startingTime + _time;
81  }
82 
83  Duration
85  {
86  const auto duration = stop();
87  reset();
88  return duration;
89  }
90 
91 
92 
93 } // namespace armarx::core::time
94 
95 
96 namespace armarx
97 {
98 
100  m_time_mode{timeMode},
101  m_starting_time{TimeUtil::GetTime(m_time_mode)},
102  m_time{::timestamp_invalid}
103  {
104  // pass
105  }
106 
107 
109  {
110  // pass
111  }
112 
113 
115  StopWatch::measure(std::function<void(void)> subjectToMeasure, TimeMode timeMode)
116  {
117  StopWatch sw{timeMode};
118  subjectToMeasure();
119  return sw.stop();
120  }
121 
122 
125  {
126  m_time = TimeUtil::GetTimeSince(m_starting_time, m_time_mode);
127  return m_time;
128  }
129 
130 
131  void
133  {
134  m_starting_time = TimeUtil::GetTime(m_time_mode);
135  m_time = ::timestamp_invalid;
136  }
137 
138 
139  bool
141  {
142  return m_time != ::timestamp_invalid;
143  }
144 
145 
148  {
149  return m_starting_time;
150  }
151 
152 
155  {
156  if (not isStopped())
157  {
158  throw std::logic_error{"Timer was not stopped yet, cannot assess stopping time."};
159  }
160 
161  return m_starting_time + m_time;
162  }
163 } // namespace armarx
armarx::core::time::StopWatch::isStopped
bool isStopped() const
Returns whether the timer is stopped or is actively measuring time.
Definition: StopWatch.cpp:59
armarx::core::time::StopWatch::startingTime
DateTime startingTime() const
Returns the date/time at starting the timer.
Definition: StopWatch.cpp:66
StopWatch.h
armarx::StopWatch::~StopWatch
virtual ~StopWatch()
Definition: StopWatch.cpp:108
armarx::core::time
Definition: Clock.cpp:13
armarx::StopWatch::StopWatch
StopWatch(armarx::TimeMode timeMode=armarx::TimeMode::SystemTime)
Definition: StopWatch.cpp:99
armarx::StopWatch::measure
static IceUtil::Time measure(std::function< void(void)> subjectToMeasure, armarx::TimeMode timeMode=armarx::TimeMode::SystemTime)
Definition: StopWatch.cpp:115
armarx::core::time::Duration::isPositive
bool isPositive() const
Tests whether the duration is positive (value in µs > 0).
Definition: Duration.cpp:195
armarx::core::time::StopWatch::stop
Duration stop()
Stops the timer and returns the measured duration.
Definition: StopWatch.cpp:43
armarx::StopWatch::startingTime
IceUtil::Time startingTime() const
Definition: StopWatch.cpp:147
armarx::core::time::Duration::Seconds
static Duration Seconds(std::int64_t seconds)
Constructs a duration in seconds.
Definition: Duration.cpp:83
armarx::core::time::ClockType
ClockType
Describes the type of clock.
Definition: ClockType.h:10
armarx::StopWatch::isStopped
bool isStopped() const
Definition: StopWatch.cpp:140
armarx::core::time::StopWatch::stopAndReset
Duration stopAndReset()
Stops and resets the timer.
Definition: StopWatch.cpp:84
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:119
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:66
armarx::StopWatch::reset
void reset()
Definition: StopWatch.cpp:132
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:34
armarx::core::time::Duration
Represents a duration.
Definition: Duration.h:17
armarx::StopWatch::stop
IceUtil::Time stop()
Definition: StopWatch.cpp:124
armarx::core::time::StopWatch
Measures the passed time between the construction or calling reset() and stop().
Definition: StopWatch.h:42
armarx::core::time::StopWatch::~StopWatch
virtual ~StopWatch()
Destructs the StopWatch.
Definition: StopWatch.cpp:27
armarx::core::time::StopWatch::StopWatch
StopWatch(ClockType clockType=ClockType::Virtual)
Constructs a StopWatch and starts it immediately.
Definition: StopWatch.cpp:20
armarx::StopWatch::stoppingTime
IceUtil::Time stoppingTime() const
Definition: StopWatch.cpp:154
armarx::core::time::StopWatch::stoppingTime
DateTime stoppingTime() const
Returns the date/time at stopping the timer.
Definition: StopWatch.cpp:73
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::core::time::StopWatch::reset
void reset()
Resets the timer.
Definition: StopWatch.cpp:51
armarx::core::time::Clock::now
DateTime now() const
Current date/time of the clock.
Definition: Clock.cpp:23
armarx::StopWatch
Definition: StopWatch.h:120