| 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 | /* SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]> */ |
| 3 | |
| 4 | /** |
| 5 | * @file timestamp.hpp |
| 6 | */ |
| 7 | |
| 8 | #ifndef __LIBGPIOD_CXX_TIMESTAMP_HPP__ |
| 9 | #define __LIBGPIOD_CXX_TIMESTAMP_HPP__ |
| 10 | |
| 11 | #if !defined(__LIBGPIOD_GPIOD_CXX_INSIDE__) |
| 12 | #error "Only gpiod.hpp can be included directly." |
| 13 | #endif |
| 14 | |
| 15 | #include <chrono> |
| 16 | #include <cstdint> |
| 17 | |
| 18 | namespace gpiod { |
| 19 | |
| 20 | /** |
| 21 | * @brief Stores the edge and info event timestamps as returned by the kernel |
| 22 | * and allows to convert them to std::chrono::time_point. |
| 23 | */ |
| 24 | class timestamp final |
| 25 | { |
| 26 | public: |
| 27 | |
| 28 | /** |
| 29 | * @brief Monotonic time_point. |
| 30 | */ |
| 31 | using time_point_monotonic = ::std::chrono::time_point<::std::chrono::steady_clock>; |
| 32 | |
| 33 | /** |
| 34 | * @brief Real-time time_point. |
| 35 | */ |
| 36 | using time_point_realtime = ::std::chrono::time_point<::std::chrono::system_clock, |
| 37 | ::std::chrono::nanoseconds>; |
| 38 | |
| 39 | /** |
| 40 | * @brief Constructor with implicit conversion from `uint64_t`. |
| 41 | * @param ns Timestamp in nanoseconds. |
| 42 | */ |
| 43 | timestamp(::std::uint64_t ns) : _m_ns(ns) { } |
| 44 | |
| 45 | /** |
| 46 | * @brief Copy constructor. |
| 47 | * @param other Object to copy. |
| 48 | */ |
| 49 | timestamp(const timestamp& other) noexcept = default; |
| 50 | |
| 51 | /** |
| 52 | * @brief Move constructor. |
| 53 | * @param other Object to move. |
| 54 | */ |
| 55 | timestamp(timestamp&& other) noexcept = default; |
| 56 | |
| 57 | /** |
| 58 | * @brief Assignment operator. |
| 59 | * @param other Object to copy. |
| 60 | * @return Reference to self. |
| 61 | */ |
| 62 | timestamp& operator=(const timestamp& other) noexcept = default; |
| 63 | |
| 64 | /** |
| 65 | * @brief Move assignment operator. |
| 66 | * @param other Object to move. |
| 67 | * @return Reference to self. |
| 68 | */ |
| 69 | timestamp& operator=(timestamp&& other) noexcept = default; |
| 70 | |
| 71 | ~timestamp() = default; |
| 72 | |
| 73 | /** |
| 74 | * @brief Conversion operator to `std::uint64_t`. |
| 75 | */ |
| 76 | operator ::std::uint64_t() noexcept |
| 77 | { |
| 78 | return this->ns(); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @brief Get the timestamp in nanoseconds. |
| 83 | * @return Timestamp in nanoseconds. |
| 84 | */ |
| 85 | ::std::uint64_t ns() const noexcept |
| 86 | { |
| 87 | return this->_m_ns; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * @brief Convert the timestamp to a monotonic time_point. |
| 92 | * @return time_point associated with the steady clock. |
| 93 | */ |
| 94 | time_point_monotonic to_time_point_monotonic() const |
| 95 | { |
| 96 | return time_point_monotonic(::std::chrono::nanoseconds(this->ns())); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @brief Convert the timestamp to a real-time time_point. |
| 101 | * @return time_point associated with the system clock. |
| 102 | */ |
| 103 | time_point_realtime to_time_point_realtime() const |
| 104 | { |
| 105 | return time_point_realtime(::std::chrono::nanoseconds(this->ns())); |
| 106 | } |
| 107 | |
| 108 | private: |
| 109 | ::std::uint64_t _m_ns; |
| 110 | }; |
| 111 | |
| 112 | } /* namespace gpiod */ |
| 113 | |
| 114 | #endif /* __LIBGPIOD_CXX_TIMESTAMP_HPP__ */ |