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