Nameless Engine
|
#include <Timer.h>
Public Member Functions | |
Timer (const Timer &)=delete | |
Timer & | operator= (const Timer &)=delete |
void | setCallbackForTimeout (long long iTimeToWaitInMs, const std::function< void()> &callback, bool bIsLooping=false) |
void | start () |
void | stop (bool bDisableTimer=false) |
std::optional< long long > | getElapsedTimeInMs () |
std::string | getName () const |
size_t | getStartCount () |
bool | isRunning () |
bool | isStopped () |
bool | isEnabled () |
Protected Member Functions | |
Timer (const std::string &sTimerName) | |
void | setCallbackValidator (const std::function< bool(size_t)> &validator) |
void | setEnable (bool bEnable) |
Private Member Functions | |
void | timerThread (std::chrono::milliseconds timeToWaitInMs) |
Private Attributes | |
std::optional< std::future< void > > | timerThreadFuture |
std::optional< std::function< void()> > | callbackForTimeout |
std::optional< std::function< bool(size_t)> > | callbackValidator |
std::string | sTimerName |
std::pair< std::mutex, std::optional< std::chrono::steady_clock::time_point > > | mtxTimeWhenStarted |
size_t | iStartCount = 0 |
std::mutex | mtxTerminateTimerThread |
std::condition_variable | cvTerminateTimerThread |
std::atomic_flag | bIsShuttingDown {} |
std::atomic_flag | bIsStopRequested {} |
std::optional< long long > | elapsedTimeWhenStopped |
long long | iTimeToWaitInMs = 0 |
bool | bIsRunning = false |
bool | bIsEnabled = true |
bool | bIsLooping = false |
Friends | |
class | Node |
class | GameInstance |
Simple timer that can trigger a callback function on a timeout.
|
protected |
Constructor.
sTimerName | Name of this timer (used for logging). Don't add "timer" word to your timer's name as it will be appended in the logs. |
std::optional< long long > ne::Timer::getElapsedTimeInMs | ( | ) |
Returns the time that has passed since the timer was started (see start).
std::string ne::Timer::getName | ( | ) | const |
Returns timer's name (only used for logging purposes).
size_t ne::Timer::getStartCount | ( | ) |
bool ne::Timer::isEnabled | ( | ) |
bool ne::Timer::isRunning | ( | ) |
Whether this timer is running (started) or not (finished/not started).
true
if currently running, false
otherwise. bool ne::Timer::isStopped | ( | ) |
void ne::Timer::setCallbackForTimeout | ( | long long | iTimeToWaitInMs, |
const std::function< void()> & | callback, | ||
bool | bIsLooping = false |
||
) |
Sets a function to be executed when the waiting time is over (timeout event).
Example:
iTimeToWaitInMs | Time this timer should wait (in milliseconds) until the callback is called. |
callback | Function to execute on timeout. |
bIsLooping | Whether the timer should start again after a timeout or not. If specified true , after the waiting time is over (timeout) the timer will automatically restart itself and will start the waiting time again. |
|
protected |
Sets a function to be called from a deferred task before the actual callback to test if the actual callback should be started or not.
Example of typical callback validator:
validator | Validator function. The only parameter is getStartCount at the moment of timeout event. Returns true if the actual callback needs to be started and false if the actual callback should not be started. |
|
protected |
void ne::Timer::start | ( | ) |
Starts the timer.
void ne::Timer::stop | ( | bool | bDisableTimer = false | ) |
Stops the timer and timer looping (if was specified in start).
bDisableTimer | Specify true to make future start calls to be ignored, false to allow restarting the timer. |
|
private |
Timer thread that waits until a timeout or a shutdown.
timeToWaitInMs | Time this thread should wait. |
|
private |
|
private |
Whether the timer should restart itself upon a timeout or not.
|
private |
Whether the timer is currently running or not.
|
private |
Whether the destructor was called or not.
|
private |
Whether the timer was explicitly stopped or not.
|
private |
Function to call on timeout.
|
private |
Function to call from a deferred task before callbackForTimeout to test if the callback should be started or not.
|
private |
Condition variable for timer thread termination.
|
private |
getElapsedTimeInMs when stop was called.
|
private |
The number of times start was called.
|
private |
Time to wait until the callback is called.
|
private |
Mutex for read/write operations on data that the timer thread is using.
|
private |
|
private |
Name of this timer (used for logging).
|
private |
Future of the waiting thread.