galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
Timer.hpp
Go to the documentation of this file.
1
7
8#ifndef GALAXY_ASYNC_TIMER_HPP_
9#define GALAXY_ASYNC_TIMER_HPP_
10
11#include <future>
12
13namespace galaxy
14{
15 namespace async
16 {
17
21 class Timer final
22 {
23 public:
27 using Function = std::move_only_function<void(void)>;
28
32 Timer() noexcept;
33
40 Timer(Timer::Function&& func, const std::uint32_t delay) noexcept;
41
45 ~Timer() noexcept;
46
53 void set(Timer::Function&& func, const std::uint32_t delay) noexcept;
54
60 void repeat(const bool repeat) noexcept;
61
65 void start();
66
70 void stop() noexcept;
71
77 void pause(const bool pause) noexcept;
78
84 [[nodiscard]]
85 bool stopped() const noexcept;
86
92 [[nodiscard]]
93 bool paused() const noexcept;
94
95 private:
99 Timer(const Timer&) = delete;
100
104 Timer& operator=(const Timer&) = delete;
105
106 private:
110 std::atomic_bool m_stopped;
111
115 std::atomic_bool m_paused;
116
120 std::atomic_bool m_repeat;
121
125 std::uint32_t m_delay;
126
131
135 std::future<void> m_handle;
136
141 };
142 } // namespace async
143} // namespace galaxy
144
145#endif
Asynchronous timer class.
Definition Timer.hpp:22
void set(Timer::Function &&func, const std::uint32_t delay) noexcept
Run a function on a precision timer.
Definition Timer.cpp:49
std::uint32_t m_delay
Current delay on timer.
Definition Timer.hpp:125
void stop() noexcept
Stop timer.
Definition Timer.cpp:86
void repeat(const bool repeat) noexcept
Make function repeat itself instead of running once.
Definition Timer.cpp:55
bool paused() const noexcept
Is the timer paused?
Definition Timer.cpp:106
Timer() noexcept
Constructor.
Definition Timer.cpp:21
double m_time_passed
Time passed.
Definition Timer.hpp:130
std::atomic_bool m_stopped
Is timer stopped.
Definition Timer.hpp:110
void start()
Start timer.
Definition Timer.cpp:60
Timer::Function m_callback
Callback function.
Definition Timer.hpp:140
std::atomic_bool m_paused
Is timer paused.
Definition Timer.hpp:115
std::future< void > m_handle
Thread running task.
Definition Timer.hpp:135
void pause(const bool pause) noexcept
Pause the timer.
Definition Timer.cpp:96
std::move_only_function< void(void)> Function
Timer callback type.
Definition Timer.hpp:27
std::atomic_bool m_repeat
Is function repeating?
Definition Timer.hpp:120
bool stopped() const noexcept
Is the timer finished?
Definition Timer.cpp:101
Timer.hpp galaxy.
Definition Async.hpp:17
STL namespace.