Execute a callback after an interval/delay. More...
#include <Timer.h>
Public Types | |
enum | Status { Status::Running, Status::Paused, Status::Stopped } |
States the timer can be in. More... | |
template<typename... Args> | |
using | Callback = std::function< void(Args...)> |
Event listener. More... | |
Public Member Functions | |
Timer () | |
Constructor. More... | |
void | setInterval (Time interval) |
Set the countdown starting point. More... | |
Time | getInterval () const |
Get the countdown starting point. More... | |
Time | getRemainingDuration () const |
Get time remaining before the timer reaches zero. More... | |
void | setRepeat (int repeatCount) |
Set whether or not the timer restarts after reaching zero. More... | |
int | getRepeatCount () const |
Get the number of times the timer restarts before coming to a stop. More... | |
bool | isRepeating () const |
Check whether or not the timer restarts after reaching zero. More... | |
void | setTimeoutCallback (Callback<> callback) |
Set the function that is executed when the timer reaches zero. More... | |
void | setTimeoutCallback (Callback< Timer & > callback) |
Set the function that is executed when the timer reaches zero. More... | |
void | start () |
Start the countdown/timer. More... | |
void | stop () |
Stop the timer. More... | |
void | pause () |
Stop the timer without resetting the remaining duration. More... | |
void | restart () |
Restart the countdown. More... | |
Status | getStatus () const |
Get the current status of the timer (running, paused, stopped) More... | |
void | update (Time deltaTime) |
Update the time. More... | |
int | getDispatchCount () const |
Get the number of times the callback has been invoked. More... | |
bool | isDispatched () const |
Check whether or not the callback is invoked. More... | |
Static Public Member Functions | |
static Timer | create (Time interval, Callback<> callback, int repeatCounter=0) |
Create a timer. More... | |
static Timer | create (Time interval, Callback< Timer & > callback, int repeatCounter=0) |
Create a timer. More... | |
using ime::Timer::Callback = std::function<void(Args...)> |
|
strong |
ime::Timer::Timer | ( | ) |
Constructor.
|
static |
Create a timer.
interval | Countdown starting point |
callback | Function to execute when the timer reaches zero |
repeatCounter | The number of timer the timer should repeat |
The timer is not repeated be default (repeatCounter = 0), this means that the timer will stop after invoking the callback
|
static |
Create a timer.
interval | Countdown starting point |
callback | Function to execute when the timer reaches zero |
repeatCounter | The number of timer the timer should repeat |
The timer is not repeated be default (repeatCounter = 0), this means that the timer will stop after invoking the callback
int ime::Timer::getDispatchCount | ( | ) | const |
Get the number of times the callback has been invoked.
This function will return 0 if the callback is not yet invoked, and 1 if the callback is invoked but not repeating
Time ime::Timer::getInterval | ( | ) | const |
Get the countdown starting point.
Time ime::Timer::getRemainingDuration | ( | ) | const |
Get time remaining before the timer reaches zero.
int ime::Timer::getRepeatCount | ( | ) | const |
Get the number of times the timer restarts before coming to a stop.
-1 = The timer repeats forever 0 = The timer does not repeat after invoking the callback (default) x = The timer repeats x times before stopping
Status ime::Timer::getStatus | ( | ) | const |
Get the current status of the timer (running, paused, stopped)
By default, the timer is stopped
bool ime::Timer::isDispatched | ( | ) | const |
Check whether or not the callback is invoked.
bool ime::Timer::isRepeating | ( | ) | const |
Check whether or not the timer restarts after reaching zero.
void ime::Timer::pause | ( | ) |
Stop the timer without resetting the remaining duration.
When the timer is paused it can be resumed by calling the start function. The timer will begin the countdown from the remaining duration instead of restarting from the interval
void ime::Timer::restart | ( | ) |
Restart the countdown.
Unlike stop, this function stops the timer and immediately starts it. The countdown will start from the interval. The remaining duration will also be reset to the value of the interval
void ime::Timer::setInterval | ( | Time | interval | ) |
Set the countdown starting point.
interval | Countdown starting point |
if the interval is set while the timer is running and the interval is not zero, then the timer will restart. If the interval is less than zero, it will be set to zero and the timer will stop immediately without executing the callback if it was running
void ime::Timer::setRepeat | ( | int | repeatCount | ) |
Set whether or not the timer restarts after reaching zero.
repeatCount | The number of times the timer repeats |
Pass -1 to repeat the timer indefinitely or 0 to stop the repetition if the timer is currently repeating
By default, the repeat counter is 0, this means that the timer stops after invoking the callback for the first time
Set the function that is executed when the timer reaches zero.
callback | Function to be executed |
Note that setting a new callback resets the dispatch count
The callback is passed the Timer on invocation
void ime::Timer::setTimeoutCallback | ( | Callback<> | callback | ) |
Set the function that is executed when the timer reaches zero.
callback | Function to execute |
Note that setting a new callback resets the dispatch count
void ime::Timer::start | ( | ) |
Start the countdown/timer.
The callback function will be called when the countdown reaches zero
void ime::Timer::stop | ( | ) |
Stop the timer.
This function resets the remaining duration to the value of the interval and stops the timer without executing the callback. This behavior is similar to the restart function except the timer is not immediately started after it has been stooped. The function start must be called to restart the timer after it has been stopped
void ime::Timer::update | ( | Time | deltaTime | ) |
Update the time.
deltaTime | Time passed since last update |