1 #pragma once 2 3 #include <sdeventplus/event.hpp> 4 #include <sdeventplus/types.hpp> 5 6 #include <chrono> 7 #include <cstdint> 8 #include <ctime> 9 #include <type_traits> 10 11 namespace sdeventplus 12 { 13 14 /** @brief Specifies the underlying clock implementation 15 */ 16 enum class ClockId : clockid_t 17 { 18 RealTime = CLOCK_REALTIME, 19 Monotonic = CLOCK_MONOTONIC, 20 BootTime = CLOCK_BOOTTIME, 21 RealTimeAlarm = CLOCK_REALTIME_ALARM, 22 BootTimeAlarm = CLOCK_BOOTTIME_ALARM, 23 }; 24 25 /** @class Clock<Id> 26 * @brief Implements a Clock to be used with std::time_point 27 * Based on the underlying sd_event time functions 28 */ 29 template <ClockId Id> 30 class Clock 31 { 32 public: 33 /** @brief Types needed by chrono functions to store time data 34 */ 35 using rep = SdEventDuration::rep; 36 using period = SdEventDuration::period; 37 using duration = SdEventDuration; 38 using time_point = std::chrono::time_point<Clock>; 39 static constexpr bool is_steady = Id == ClockId::Monotonic; 40 41 /** @brief Constructs a new Clock with time data from the Event 42 * 43 * @param[in] event - The event used as the basis for the clock 44 */ 45 Clock(const Event& event); 46 Clock(Event&& event); 47 48 /** @brief Gets the current time of the clock 49 * 50 * @throws SdEventError for underlying sd_event errors 51 * @return The std::chrono::time_point representing the current time 52 */ 53 time_point now() const; 54 55 private: 56 Event event; 57 }; 58 59 } // namespace sdeventplus 60