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 namespace 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  */
41  class StopWatch
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 
114 namespace 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
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
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:25
Clock.h
armarx::TimeMode::SystemTime
@ SystemTime
armarx::core::time::StopWatch::stop
Duration stop()
Stops the timer and returns the measured duration.
Definition: StopWatch.cpp:39
armarx::core::time::ClockType
ClockType
Describes the type of clock.
Definition: ClockType.h:9
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::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:123
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::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::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
ClockType.h
armarx::StopWatch
Definition: StopWatch.h:118