| 1 | //! @file time.hpp |
| 2 | //! @author ryftchen |
| 3 | //! @brief The declarations (time) in the utility module. |
| 4 | //! @version 0.1.0 |
| 5 | //! @copyright Copyright (c) 2022-2025 ryftchen. All rights reserved. |
| 6 | |
| 7 | #pragma once |
| 8 | |
| 9 | #include <condition_variable> |
| 10 | #include <functional> |
| 11 | #include <mutex> |
| 12 | #include <thread> |
| 13 | |
| 14 | //! @brief The utility module. |
| 15 | namespace utility // NOLINT(modernize-concat-nested-namespaces) |
| 16 | { |
| 17 | //! @brief Timing-related functions in the utility module. |
| 18 | namespace time |
| 19 | { |
| 20 | //! @brief Brief function description. |
| 21 | //! @return function description (module_function) |
| 22 | inline static const char* description() noexcept |
| 23 | { |
| 24 | return "UTIL_TIME" ; |
| 25 | } |
| 26 | extern const char* version() noexcept; |
| 27 | |
| 28 | //! @brief Timer. |
| 29 | class Timer |
| 30 | { |
| 31 | public: |
| 32 | //! @brief Construct a new Timer object. |
| 33 | //! @param callback - timeout callback function |
| 34 | explicit Timer(std::function<void()> callback) : callback{std::move(callback)} {} |
| 35 | //! @brief Destroy the Timer object. |
| 36 | virtual ~Timer(); |
| 37 | //! @brief Construct a new Timer object. |
| 38 | Timer(const Timer&) = delete; |
| 39 | //! @brief Construct a new Timer object. |
| 40 | Timer(Timer&&) noexcept = delete; |
| 41 | //! @brief The operator (=) overloading of Timer class. |
| 42 | //! @return reference of the Timer object |
| 43 | Timer& operator=(const Timer&) = delete; |
| 44 | //! @brief The operator (=) overloading of Timer class. |
| 45 | //! @return reference of the Timer object |
| 46 | Timer& operator=(Timer&&) noexcept = delete; |
| 47 | |
| 48 | //! @brief Start the timer. |
| 49 | //! @param interval - time interval |
| 50 | //! @param isPeriodic - whether to repeat periodically or not |
| 51 | void start(const std::chrono::milliseconds& interval, const bool isPeriodic = false); |
| 52 | //! @brief Stop the timer. |
| 53 | void stop(); |
| 54 | //! @brief Check if the timer is running. |
| 55 | //! @return be running or not |
| 56 | bool isRunning() const; |
| 57 | |
| 58 | private: |
| 59 | //! @brief The callback function that is executed on the timer trigger. |
| 60 | const std::function<void()> callback; |
| 61 | //! @brief Mutex for controlling thread. |
| 62 | mutable std::mutex mtx; |
| 63 | //! @brief The synchronization condition for queue. Use with mtx. |
| 64 | std::condition_variable_any cond; |
| 65 | //! @brief Working thread. |
| 66 | std::jthread worker; |
| 67 | }; |
| 68 | |
| 69 | //! @brief Stopwatch. |
| 70 | class Stopwatch |
| 71 | { |
| 72 | public: |
| 73 | //! @brief Construct a new Stopwatch object. |
| 74 | Stopwatch(); |
| 75 | |
| 76 | //! @brief Reset the beginning time. |
| 77 | void reset(); |
| 78 | //! @brief Calculate the elapsed time. |
| 79 | //! @tparam Rep - type of number of ticks |
| 80 | //! @tparam Period - type of tick period |
| 81 | //! @return elapsed time |
| 82 | template <typename Rep = double, typename Period = std::milli> |
| 83 | Rep elapsedTime() const; |
| 84 | |
| 85 | private: |
| 86 | //! @brief Beginning time. |
| 87 | std::chrono::high_resolution_clock::time_point beginTime; |
| 88 | }; |
| 89 | |
| 90 | template <typename Rep, typename Period> |
| 91 | Rep Stopwatch::elapsedTime() const |
| 92 | { |
| 93 | return std::chrono::duration<Rep, Period>(std::chrono::high_resolution_clock::now() - beginTime).count(); |
| 94 | } |
| 95 | |
| 96 | extern std::string currentStandardTime(); |
| 97 | } // namespace time |
| 98 | } // namespace utility |
| 99 | |