Public Types | Public Member Functions | Static Public Member Functions | List of all members
ime::Timer Class Reference

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...
 

Detailed Description

Execute a callback after an interval/delay.

Definition at line 36 of file Timer.h.

Member Typedef Documentation

◆ Callback

template<typename... Args>
using ime::Timer::Callback = std::function<void(Args...)>

Event listener.

Definition at line 39 of file Timer.h.

Member Enumeration Documentation

◆ Status

enum ime::Timer::Status
strong

States the timer can be in.

Enumerator
Running 

Timer is running.

Paused 

Timer is paused.

Stopped 

Timer is stopped.

Definition at line 44 of file Timer.h.

Constructor & Destructor Documentation

◆ Timer()

ime::Timer::Timer ( )

Constructor.

Member Function Documentation

◆ create() [1/2]

static Timer ime::Timer::create ( Time  interval,
Callback< Timer & >  callback,
int  repeatCounter = 0 
)
static

Create a timer.

Parameters
intervalCountdown starting point
callbackFunction to execute when the timer reaches zero
repeatCounterThe number of timer the timer should repeat
Returns
The new timer

The timer is not repeated be default (repeatCounter = 0), this means that the timer will stop after invoking the callback

Note
The timer is not started after creation, start function must be called on the returned timer when it is ready to be started
See also
start and setRepeat

◆ create() [2/2]

static Timer ime::Timer::create ( Time  interval,
Callback<>  callback,
int  repeatCounter = 0 
)
static

Create a timer.

Parameters
intervalCountdown starting point
callbackFunction to execute when the timer reaches zero
repeatCounterThe number of timer the timer should repeat
Returns
The new timer

The timer is not repeated be default (repeatCounter = 0), this means that the timer will stop after invoking the callback

Note
The timer is not started after creation, start function must be called on the returned timer when it is ready to be started
See also
start and setRepeat

◆ getDispatchCount()

int ime::Timer::getDispatchCount ( ) const

Get the number of times the callback has been invoked.

Returns
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

◆ getInterval()

Time ime::Timer::getInterval ( ) const

Get the countdown starting point.

Returns
The countdown starting point

◆ getRemainingDuration()

Time ime::Timer::getRemainingDuration ( ) const

Get time remaining before the timer reaches zero.

Returns
The time remaining before the timer reaches zero

◆ getRepeatCount()

int ime::Timer::getRepeatCount ( ) const

Get the number of times the timer restarts before coming to a stop.

Returns
The number of times the timer is restarts after counting down

-1 = The timer repeats forever 0 = The timer does not repeat after invoking the callback (default) x = The timer repeats x times before stopping

◆ getStatus()

Status ime::Timer::getStatus ( ) const

Get the current status of the timer (running, paused, stopped)

Returns
The current status of the timer

By default, the timer is stopped

◆ isDispatched()

bool ime::Timer::isDispatched ( ) const

Check whether or not the callback is invoked.

Returns
True if the callback was called, otherwise false

◆ isRepeating()

bool ime::Timer::isRepeating ( ) const

Check whether or not the timer restarts after reaching zero.

Returns
True if the timer restarts, otherwise false
See also
setRepeat

◆ pause()

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

See also
start and setInterval

◆ restart()

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

See also
setInterval

◆ setInterval()

void ime::Timer::setInterval ( Time  interval)

Set the countdown starting point.

Parameters
intervalCountdown 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

See also
restart

◆ setRepeat()

void ime::Timer::setRepeat ( int  repeatCount)

Set whether or not the timer restarts after reaching zero.

Parameters
repeatCountThe 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

Note
If the repetition is cancelled while the timer is running, the timer will continue execution and stop immediately after executing the callback
See also
stop and setInterval

◆ setTimeoutCallback() [1/2]

void ime::Timer::setTimeoutCallback ( Callback< Timer & >  callback)

Set the function that is executed when the timer reaches zero.

Parameters
callbackFunction to be executed
Note
The timer will not start the countdown if the start function is called and there is no callback set for when the timer reaches zero.

Note that setting a new callback resets the dispatch count

The callback is passed the Timer on invocation

See also
start

◆ setTimeoutCallback() [2/2]

void ime::Timer::setTimeoutCallback ( Callback<>  callback)

Set the function that is executed when the timer reaches zero.

Parameters
callbackFunction to execute
Note
The timer will not start the countdown if the start function is called and there is no callback set for when the timer reaches zero.

Note that setting a new callback resets the dispatch count

See also
start

◆ start()

void ime::Timer::start ( )

Start the countdown/timer.

The callback function will be called when the countdown reaches zero

Note
This function will start the countdown if it was not started or resume it if it was paused. If called while the timer is running then, the timer will restart
Warning
This function must be called only if the interval is greater than zero and the timeout callback is set, otherwise undefined behavior
See also
setInterval, setTimeoutCallback, restart and pause

◆ stop()

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

See also
start and getRemainingDuration

◆ update()

void ime::Timer::update ( Time  deltaTime)

Update the time.

Parameters
deltaTimeTime passed since last update

The documentation for this class was generated from the following file: