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