cae  0.0.0
Cross-API graphics engine
Loading...
Searching...
No Matches
Clock.hpp
Go to the documentation of this file.
1///
2/// @file Clock.hpp
3/// @brief This file contains the Clock class
4/// @namespace utl
5///
6
7#pragma once
8
9#include <chrono>
10
11namespace utl
12{
13
14 ///
15 /// @class Clock
16 /// @brief Class for clock
17 /// @namespace utl
18 ///
19 class Clock
20 {
21
22 public:
23 using TimePoint = std::chrono::time_point<std::chrono::high_resolution_clock>;
24
25 explicit Clock(const bool startNow = true) : m_start{startNow ? now() : TimePoint()}, m_pausedDuration{0} {}
26 ~Clock() = default;
27
28 Clock(const Clock &) = delete;
29 Clock &operator=(const Clock &) = delete;
30 Clock(Clock &&) = delete;
31 Clock &operator=(Clock &&) = delete;
32
33 friend std::ostream &operator<<(std::ostream &os, const Clock &clock)
34 {
35 os << "Elapsed time: " << clock.getDeltaSeconds() << " seconds";
36 return os;
37 }
38
39 ///
40 /// @return Current time point
41 /// @brief Get the current time point
42 ///
43 static TimePoint now() { return std::chrono::high_resolution_clock::now(); }
44
45 ///
46 /// @brief Restart the clock
47 ///
48 void restart()
49 {
50 m_start = now();
52 m_isPaused = false;
53 }
54 ///
55 /// @brief Pause the clock
56 ///
57 void pause()
58 {
59 if (!m_isPaused)
60 {
61 m_pausedTime = now();
62 m_isPaused = true;
63 }
64 }
65
66 ///
67 /// @brief Resume the clock
68 ///
69 void resume()
70 {
71 if (m_isPaused)
72 {
74 m_isPaused = false;
75 }
76 }
77
78 ///
79 /// @return Elapsed time in seconds
80 /// @brief Get the elapsed time in seconds
81 ///
82 [[nodiscard]] float getDeltaSeconds() const
83 {
84 if (m_isPaused)
85 {
86 return std::chrono::duration<float>(m_pausedTime - m_start - m_pausedDuration).count();
87 }
88 return std::chrono::duration<float>(now() - m_start - m_pausedDuration).count();
89 }
90
91 ///
92 /// @tparam Duration Type of duration to return (default: seconds)
93 /// @return Elapsed time in specified duration
94 /// @brief Get the elapsed time in specified duration
95 ///
96 template <typename Duration = std::chrono::seconds> [[nodiscard]] auto getElapsed() const
97 {
98 return std::chrono::duration_cast<Duration>(now() - m_start - m_pausedDuration);
99 }
100
101 private:
102 using Duration = std::chrono::high_resolution_clock::duration;
103
107 bool m_isPaused{false};
108
109 }; // class Clock
110
111} // namespace utl
Class for clock.
Definition Clock.hpp:20
Duration m_pausedDuration
Definition Clock.hpp:106
TimePoint m_start
Definition Clock.hpp:104
Clock & operator=(Clock &&)=delete
Clock(const bool startNow=true)
Definition Clock.hpp:25
std::chrono::time_point< std::chrono::high_resolution_clock > TimePoint
Definition Clock.hpp:23
void restart()
Restart the clock.
Definition Clock.hpp:48
bool m_isPaused
Definition Clock.hpp:107
float getDeltaSeconds() const
Get the elapsed time in seconds.
Definition Clock.hpp:82
std::chrono::high_resolution_clock::duration Duration
Definition Clock.hpp:102
void resume()
Resume the clock.
Definition Clock.hpp:69
Clock(Clock &&)=delete
Clock(const Clock &)=delete
TimePoint m_pausedTime
Definition Clock.hpp:105
void pause()
Pause the clock.
Definition Clock.hpp:57
friend std::ostream & operator<<(std::ostream &os, const Clock &clock)
Definition Clock.hpp:33
auto getElapsed() const
Get the elapsed time in specified duration.
Definition Clock.hpp:96
static TimePoint now()
Get the current time point.
Definition Clock.hpp:43
Clock & operator=(const Clock &)=delete
~Clock()=default