⏱️ golxzn::os::chrono ⏱️ 1.2.4
Chrono for golxzn's projects.
Loading...
Searching...
No Matches
clock.hpp
Go to the documentation of this file.
1/**
2 * @file golxzn/os/chrono/clock.hpp
3 * @author Ruslan Golovinskii (golxzn@gmail.com)
4 * @brief Clock classes to measure elapsed time
5 * @date 2023-10-24
6 *
7 * @copyright Copyright (c) 2023
8 */
9
10#pragma once
11
14
15namespace golxzn::os::chrono {
16
17/**
18 * @brief Class that represents fast clock, which store only one time point.
19 * @ingroup Chrono clocks
20 * This class allows to measure time since last call of elapsed() or since construction.
21 * It's fast because it doesn't store any information but last time point
22 * This class provides measurement of elapsed time since last call of elapsed() or since construction.
23 * > It doesn't store any information but last time point. When you call elapsed(), it stores new time point,
24 * > and returns difference of old and new time points.
25 * @tparam BaseClock clock that will be used for measurement. It has to be monotonic and STL compatible.
26 *
27 * Usage:
28 * @code{.cpp}
29 * golxzn::os::chrono::fast_clock<> clock;
30 * while (game_running) {
31 * const auto elapsed{ clock.elapsed() };
32 * Game::Update(elapsed.seconds<double>());
33 * ...
34 * }
35 * @endcode
36 * @see golxzn::os::chrono::clock
37 */
38template<class BaseClock = utils::default_base_clock>
40 static_assert(BaseClock::is_steady,
41 "[golxzn::os::chrono::fast_clock] BaseClock is not a monotonic clock");
42 static_assert(utils::enough_resolution_v<BaseClock>,
43 "[golxzn::os::chrono::fast_clock] BaseClock's resolution is less than microseconds!");
44
45public:
46 using base_clock = BaseClock; ///< Base clock type
47 using time_point = typename base_clock::time_point; ///< Time point type from base clock
48 static constexpr time_point zero{}; ///< Zero time point
49
50 /**
51 * @brief Returns elapsed time since last call of `elapsed()` or since construction.
52 * @return time
53 */
54 [[nodiscard]] time elapsed() noexcept;
55
56private:
57 time_point m_last_time{ base_clock::now() };
58};
59
60/**
61 * @brief Class that represents clock.
62 * @ingroup Chrono clocks
63 * In comparison with @ref golxzn::os::chrono::fast_clock, it provides more functionality but it's slower.
64 * @tparam BaseClock clock that will be used for measurement. It has to be monotonic and STL compatible.
65 * @see golxzn::os::chrono::fast_clock
66 */
67template<class BaseClock = utils::default_base_clock>
68class clock {
69 static_assert(BaseClock::is_steady,
70 "[golxzn::os::chrono::clock] BaseClock is not a monotonic clock");
71 static_assert(utils::enough_resolution_v<BaseClock>,
72 "[golxzn::os::chrono::clock] BaseClock's resolution is less than microseconds!");
73
74public:
75 using base_clock = BaseClock;
76 using time_point = typename base_clock::time_point;
77 static constexpr time_point zero{};
78
79 /**
80 * @brief Returns true if clock is running.
81 * @return clock running state
82 * @see time restart() noexcept;
83 * @see time reset() const noexcept;
84 * @see time elapsed() const noexcept;
85 */
86 [[nodiscard]] bool running() const noexcept;
87
88 /**
89 * @brief Returns elapsed time since last reset or construction.
90 * @return time
91 * @see time restart() noexcept;
92 * @see time reset() const noexcept;
93 * @see bool running() const noexcept
94 */
95 [[nodiscard]] time elapsed() const noexcept;
96
97 /**
98 * @brief Restarts clock
99 * @return elapsed time since last reset or construction.
100 * @see time reset() const noexcept;
101 * @see time elapsed() const noexcept;
102 * @see bool running() const noexcept
103 * @see void start() noexcept
104 * @see void stop() noexcept
105 */
106 [[nodiscard]] time restart() noexcept;
107
108 /**
109 * @brief Resets clock
110 * @return elapsed time since last reset or construction.
111 * @see time restart() const noexcept;
112 * @see time elapsed() const noexcept;
113 * @see bool running() const noexcept
114 * @see void start() noexcept
115 * @see void stop() noexcept
116 */
117 [[nodiscard]] time reset() noexcept;
118
119 /**
120 * @brief Starts clock
121 * @see stop()
122 * @see bool running() const noexcept
123 */
124 void start() noexcept;
125
126 /**
127 * @brief Stops clock
128 * @see start()
129 * @see bool running() const noexcept
130 */
131 void stop() noexcept;
132
133private:
134 time_point m_last_reset_time{ base_clock::now() };
135 time_point m_stop_time{ zero };
136};
137
138#include "golxzn/os/chrono/impl/clock.inl"
139
140} // namespace golxzn::os::chrono
141
Class that represents clock.In comparison with golxzn::os::chrono::fast_clock, it provides more funct...
Definition clock.hpp:68
time elapsed() const noexcept
Returns elapsed time since last reset or construction.
time reset() noexcept
Resets clock.
void start() noexcept
Starts clock.
void stop() noexcept
Stops clock.
time restart() noexcept
Restarts clock.
bool running() const noexcept
Returns true if clock is running.
Class that represents fast clock, which store only one time point.This class allows to measure time s...
Definition clock.hpp:39
typename base_clock::time_point time_point
Time point type from base clock.
Definition clock.hpp:47
BaseClock base_clock
Base clock type.
Definition clock.hpp:46
time elapsed() noexcept
Returns elapsed time since last call of elapsed() or since construction.
static constexpr time_point zero
Zero time point.
Definition clock.hpp:48
Class that represents time.
Definition time.hpp:30
Class that represents time.
Useful functions and aliases for golxzn::os::chrono.