Clock.h
Go to the documentation of this file.
1 #pragma once
2 
3 
7 
8 namespace armarx::core::time
9 {
10 
11  /**
12  * @brief Clock to get date/time from a specific clock type or wait for certain durations
13  * or until certain date/times in a given clock.
14  *
15  * In most cases, using the virtual clock is the desired behaviour. The virtual clock reports
16  * the current time according to the configured time server, or (as a fallback) the operating
17  * systems real time clock. The virtual clock can be accessed through the static methods of
18  * this clock.
19  *
20  * For other cases, i.e., where the actual operating system real time clock is needed
21  * (ClockType::Realtime), or a monotonic clock is required (ClockType::Monotonic), a clock
22  * with the needed clock type as constructor parameter can be instantiated. The API is then
23  * similar to those of the virtual clock using the corresponding non-static member functions.
24  */
25  class Clock
26  {
27 
28  public:
29  /**
30  * @brief Constructs a new clock of given clock type (virtual by default).
31  * @param clockType Clock type.
32  */
33  Clock(ClockType clockType = ClockType::Virtual);
34 
35  /**
36  * @brief Current date/time of the clock.
37  * @return Current date/time.
38  */
39  DateTime now() const;
40 
41  /**
42  * @brief Wait for a certain duration.
43  * @param duration How long to wait.
44  */
45  void waitFor(const Duration& duration) const;
46 
47  /**
48  * @brief Wait and block until the given date/time is surpassed.
49  * @param dateTime Date/time to wait until.
50  * @return Waiting duration.
51  */
52  Duration waitUntil(const DateTime& dateTime) const;
53 
54  /**
55  * @brief Current time on the virtual clock.
56  * @return Current date/time.
57  */
58  static DateTime Now();
59 
60  /**
61  * @brief Wait for a certain duration on the virtual clock.
62  * @param duration How long to wait.
63  */
64  static void WaitFor(const Duration& duration);
65 
66  /**
67  * @brief Wait and block until the given date/time is surpassed on the virtual clock.
68  * @param dateTime Date/time to wait until.
69  * @return Waiting duration.
70  */
71  static Duration WaitUntil(const DateTime& dateTime);
72 
73  private:
74  /**
75  * @brief Static instance of the virtual clock for static short-hand methods.
76  */
77  static Clock _virtualClock;
78 
79  /**
80  * @brief Own clock type.
81  */
82  ClockType _clockType;
83  };
84 
85 } // namespace armarx::core::time
86 
87 namespace armarx
88 {
89  using core::time::Clock;
90 }
armarx::core::time::Clock::WaitFor
static void WaitFor(const Duration &duration)
Wait for a certain duration on the virtual clock.
Definition: Clock.cpp:99
DateTime.h
Duration.h
armarx::core::time
Definition: Clock.cpp:13
armarx::core::time::Clock::waitUntil
Duration waitUntil(const DateTime &dateTime) const
Wait and block until the given date/time is surpassed.
Definition: Clock.cpp:80
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
armarx::core::time::ClockType
ClockType
Describes the type of clock.
Definition: ClockType.h:9
armarx::core::time::Clock::Clock
Clock(ClockType clockType=ClockType::Virtual)
Constructs a new clock of given clock type (virtual by default).
Definition: Clock.cpp:16
armarx::core::time::Clock::waitFor
void waitFor(const Duration &duration) const
Wait for a certain duration.
Definition: Clock.cpp:74
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::core::time::Duration
Represents a duration.
Definition: Duration.h:16
armarx::core::time::Clock::WaitUntil
static Duration WaitUntil(const DateTime &dateTime)
Wait and block until the given date/time is surpassed on the virtual clock.
Definition: Clock.cpp:105
armarx::core::time::Clock::Now
static DateTime Now()
Current time on the virtual clock.
Definition: Clock.cpp:93
stopwatch::Clock
std::chrono::system_clock Clock
Definition: Stopwatch.h:10
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
ClockType.h
armarx::core::time::Clock::now
DateTime now() const
Current date/time of the clock.
Definition: Clock.cpp:22