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