Nameless Engine
Loading...
Searching...
No Matches
Timer.h
1#pragma once
2
3// Standard.
4#include <future>
5#include <optional>
6#include <chrono>
7#include <vector>
8
9namespace ne {
10 class Node;
11
13 class Timer {
14 // Only node and game instance can create timers because they have some additional protection
15 // code to avoid shooting yourself in the foot (like if you forget to stop the timer).
16 // Although only nodes and game instances can create timers this does not mean that the timer
17 // depends on their functionality - no, the timer is modular and you can use it outside of these
18 // classes if you remove `friend class` and move functions from the `protected` section of
19 // the timer to the `public` section.
20 friend class Node;
21 friend class GameInstance;
22
23 public:
24 Timer() = delete;
25
26 ~Timer();
27
28 Timer(const Timer&) = delete;
29 Timer& operator=(const Timer&) = delete;
30
71 long long iTimeToWaitInMs, const std::function<void()>& callback, bool bIsLooping = false);
72
81 void start();
82
94 void stop(bool bDisableTimer = false);
95
109 std::optional<long long> getElapsedTimeInMs();
110
116 std::string getName() const;
117
123 size_t getStartCount();
124
130 bool isRunning();
131
138 bool isStopped();
139
145 bool isEnabled();
146
147 protected:
154 Timer(const std::string& sTimerName);
155
183 void setCallbackValidator(const std::function<bool(size_t)>& validator);
184
190 void setEnable(bool bEnable);
191
192 private:
198 void timerThread(std::chrono::milliseconds timeToWaitInMs);
199
201 std::optional<std::future<void>> timerThreadFuture;
202
204 std::optional<std::function<void()>> callbackForTimeout;
205
210 std::optional<std::function<bool(size_t)>> callbackValidator;
211
213 std::string sTimerName;
214
219 std::pair<std::mutex, std::optional<std::chrono::steady_clock::time_point>> mtxTimeWhenStarted;
220
222 size_t iStartCount = 0;
223
226
228 std::condition_variable cvTerminateTimerThread;
229
231 std::atomic_flag bIsShuttingDown{};
232
234 std::atomic_flag bIsStopRequested{};
235
237 std::optional<long long> elapsedTimeWhenStopped;
238
240 long long iTimeToWaitInMs = 0;
241
243 bool bIsRunning = false;
244
249 bool bIsEnabled = true;
250
252 bool bIsLooping = false;
253 };
254} // namespace ne
Definition: GameInstance.h:32
Definition: Node.h:39
Definition: Timer.h:13
std::string sTimerName
Definition: Timer.h:213
void stop(bool bDisableTimer=false)
Definition: Timer.cpp:115
bool bIsRunning
Definition: Timer.h:243
void timerThread(std::chrono::milliseconds timeToWaitInMs)
Definition: Timer.cpp:196
void setEnable(bool bEnable)
Definition: Timer.cpp:23
bool isEnabled()
Definition: Timer.cpp:191
std::condition_variable cvTerminateTimerThread
Definition: Timer.h:228
bool isRunning()
Definition: Timer.cpp:181
std::optional< std::function< bool(size_t)> > callbackValidator
Definition: Timer.h:210
void setCallbackValidator(const std::function< bool(size_t)> &validator)
Definition: Timer.cpp:13
size_t iStartCount
Definition: Timer.h:222
std::optional< std::function< void()> > callbackForTimeout
Definition: Timer.h:204
std::string getName() const
Definition: Timer.cpp:174
bool bIsLooping
Definition: Timer.h:252
bool bIsEnabled
Definition: Timer.h:249
std::mutex mtxTerminateTimerThread
Definition: Timer.h:225
std::pair< std::mutex, std::optional< std::chrono::steady_clock::time_point > > mtxTimeWhenStarted
Definition: Timer.h:219
std::optional< long long > elapsedTimeWhenStopped
Definition: Timer.h:237
bool isStopped()
Definition: Timer.cpp:186
long long iTimeToWaitInMs
Definition: Timer.h:240
size_t getStartCount()
Definition: Timer.cpp:176
void start()
Definition: Timer.cpp:77
std::atomic_flag bIsShuttingDown
Definition: Timer.h:231
std::optional< std::future< void > > timerThreadFuture
Definition: Timer.h:201
void setCallbackForTimeout(long long iTimeToWaitInMs, const std::function< void()> &callback, bool bIsLooping=false)
Definition: Timer.cpp:50
std::optional< long long > getElapsedTimeInMs()
Definition: Timer.cpp:155
std::atomic_flag bIsStopRequested
Definition: Timer.h:234