Base module

Timer

Asynchronous event based timer.

Timer

#include <icy/timer.h>

Inherits: Runner

Asynchronous event based timer.

Public Attributes

ReturnNameDescription
NullSignalTimeoutSignal that gets triggered on timeout.

Timeout

NullSignal Timeout

Signal that gets triggered on timeout.

Public Methods

ReturnNameDescription
TimerCreate a timer without a timeout or interval; values must be set before [start()](#start-12).
TimerCreate a one-shot timer that fires after timeout milliseconds.
TimerCreate a repeating timer with an initial delay and a repeat period.
~Timer virtualDestructor.
voidstartStart the timer using the previously configured timeout and interval.
voidstart virtualConnect func to the [Timeout](icy-Timeout.html#timeout-1) signal and start the timer.
voidstopStop the timer. Resets the fire count to zero.
voidrestartRestart the timer.
voidagainReset the countdown without stopping and restarting the timer.
voidsetTimeoutSet the initial delay before the first timeout event.
voidsetIntervalSet the repeat interval (calls uv_timer_set_repeat).
boolactive const#### Returns
std::int64_ttimeout const#### Returns
std::int64_tinterval const#### Returns
std::int64_tcount#### Returns
uv::Handle< uv_timer_t > &handle#### Returns
boolasync virtual const#### Returns

Timer

Timer(uv::Loop * loop)

Create a timer without a timeout or interval; values must be set before [start()](#start-12).

Parameters

  • loop Event loop to associate with. Defaults to the process-wide default loop.

Timer

Timer(std::int64_t timeout, uv::Loop * loop, std::function< void()> func)

Create a one-shot timer that fires after timeout milliseconds.

If func is provided the timer starts immediately; otherwise call [start()](#start-12) manually.

Parameters

  • timeout Delay in milliseconds before the first (and only) fire.

  • loop Event loop to associate with.

  • func Optional callback connected to the [Timeout](icy-Timeout.html#timeout-1) signal and used to start the timer immediately.


Timer

Timer(std::int64_t timeout, std::int64_t interval, uv::Loop * loop, std::function< void()> func)

Create a repeating timer with an initial delay and a repeat period.

Fires once after timeout milliseconds, then repeatedly every interval milliseconds. If func is provided the timer starts immediately; otherwise call [start()](#start-12) manually.

Parameters

  • timeout Initial delay in milliseconds.

  • interval Repeat period in milliseconds.

  • loop Event loop to associate with.

  • func Optional callback connected to the [Timeout](icy-Timeout.html#timeout-1) signal and used to start the timer immediately.


~Timer

virtual

virtual ~Timer()

Destructor.


start

void start()

Start the timer using the previously configured timeout and interval.

Throws std::logic_error if neither a timeout nor an interval has been set, or if the handle has not been allocated. Has no effect if already active.


start

virtual

virtual void start(std::function< void()> func)

Connect func to the [Timeout](icy-Timeout.html#timeout-1) signal and start the timer.

Parameters

  • func Callback invoked on each timeout event.

stop

void stop()

Stop the timer. Resets the fire count to zero.

Has no effect if the timer is not active.


restart

void restart()

Restart the timer.

If the timer is not currently active, behaves like [start()](#start-12). If it is active, calls [again()](#again) to reset the countdown using the repeat value. A timeout or interval must be set beforehand, otherwise [start()](#start-12) will throw std::logic_error.


again

void again()

Reset the countdown without stopping and restarting the timer.

For repeating timers this restarts the repeat interval from now. For non-repeating timers this has the same effect as stopping the timer. Throws on error if uv_timer_again fails (e.g. the timer was never started). Resets the fire count to zero.


setTimeout

void setTimeout(std::int64_t timeout)

Set the initial delay before the first timeout event.

Has no effect if the timer is currently active.

Parameters

  • timeout Delay in milliseconds.

setInterval

void setInterval(std::int64_t interval)

Set the repeat interval (calls uv_timer_set_repeat).

Takes effect from the next timeout event. If set from within a timer callback on a repeating timer, the new value is used from the following iteration; on a non-repeating timer the timer will have already stopped.

Parameters

  • interval Repeat period in milliseconds; 0 disables repeating.

active

const

bool active() const

Returns

true if the timer is currently counting down.


timeout

const

std::int64_t timeout() const

Returns

The configured initial timeout in milliseconds.


interval

const

std::int64_t interval() const

Returns

The effective repeat interval reported by libuv, in milliseconds.


count

std::int64_t count()

Returns

The number of times the timer has fired since the last start or again().


handle

uv::Handle< uv_timer_t > & handle()

Returns

Reference to the underlying uv_timer_t handle wrapper.


async

virtual const

virtual bool async() const

Returns

false; the timer is event-loop-driven, not thread-based.

Protected Attributes

ReturnNameDescription
uv::Handle< uv_timer_t >_handle
std::int64_t_timeout
std::int64_t_interval
std::int64_t_count

_handle

uv::Handle< uv_timer_t > _handle

_timeout

std::int64_t _timeout

_interval

std::int64_t _interval

_count

std::int64_t _count

Protected Methods

ReturnNameDescription
TimerDeleted constructor.
TimerDeleted constructor.
voidinit

Timer

Timer(const Timer &) = delete

Deleted constructor.


Timer

Timer(Timer &&) = delete

Deleted constructor.


init

void init()