1.10 |
Basic one shot timer. More...
#include <timer.h>
Public Types | |
using | RegisterHandle = uint64_t |
Handle type. | |
using | TimerCallback = std::function< void()> |
Timer callback function definition. | |
Public Member Functions | |
Timer () noexcept | |
Construct a one-shot timer. | |
Timer (const Timer &)=delete | |
Timer (std::chrono::milliseconds duration) noexcept | |
Construct a one-shot timer with the specified duration. | |
Timer (Timer &&) | |
virtual | ~Timer () noexcept |
void | cancel () |
Cancel, or stop, the timer. | |
void | change_duration (std::chrono::milliseconds duration) |
Change the duration of the timer. | |
void | clear_handlers () |
Clear all handlers. | |
EGT_NODISCARD std::chrono::milliseconds | duration () const |
Return the current duration of the timer. | |
EGT_NODISCARD const std::string & | name () const |
Get the name of the Timer. | |
void | name (const std::string &name) |
Set the name of the Timer. | |
RegisterHandle | on_timeout (TimerCallback callback) |
Add a handler callback to be called with the timer times out. | |
Timer & | operator= (const Timer &)=delete |
Timer & | operator= (Timer &&) |
void | remove_handler (RegisterHandle handle) |
Remove an event handler. | |
EGT_NODISCARD bool | running () const |
Returns true if the timer is currently running. | |
virtual void | start () |
Start the timer. | |
void | start (std::chrono::milliseconds duration) |
Start the timer with the specified duration. | |
EGT_DEPRECATED void | start_with_duration (std::chrono::milliseconds duration) |
Start the timer with the specified duration. | |
void | stop () |
Alias for cancel(). | |
void | timeout () |
Called when the timer times out. | |
Protected Types | |
using | CallbackArray = std::vector< CallbackMeta > |
Type for array of registered callbacks. | |
Protected Member Functions | |
void | invoke_handlers () |
Invoke any registered handlers. | |
Protected Attributes | |
CallbackArray | m_callbacks |
Array of registered callbacks. | |
std::chrono::milliseconds | m_duration {} |
The duration of the timer. | |
RegisterHandle | m_handle_counter {0} |
Counter used to generate unique handles for each callback registration. | |
std::unique_ptr< TimerImpl > | m_impl |
std::string | m_name |
A user defined name for the Timer. | |
bool | m_running {false} |
When true, currently running. | |
asio::steady_timer | m_timer |
Asio timer object. | |
Basic one shot timer.
This is a timer that will fire once after the specified duration. To handle the timeout, call on_timeout with a callback.
There are two main use cases for this class. One, is you can derive from this class and overload the Timer::timeout() member function.
The other way is to create an instance of this class and connect a handler by calling Timer::on_timeout().
|
protected |
Type for array of registered callbacks.
using RegisterHandle = uint64_t |
Handle type.
using TimerCallback = std::function<void()> |
Timer callback function definition.
|
noexcept |
Construct a one-shot timer.
The duration of the timer can be specified when calling start_with_duration() when using this constructor.
|
explicitnoexcept |
Construct a one-shot timer with the specified duration.
|
virtualnoexcept |
void cancel | ( | ) |
Cancel, or stop, the timer.
void change_duration | ( | std::chrono::milliseconds | duration | ) |
Change the duration of the timer.
This will stop the timer, change the duration, and restart the timer with the new duration if the timer was already running.
void clear_handlers | ( | ) |
Clear all handlers.
|
inline |
Return the current duration of the timer.
|
protected |
Invoke any registered handlers.
|
inline |
Get the name of the Timer.
|
inline |
RegisterHandle on_timeout | ( | TimerCallback | callback | ) |
Add a handler callback to be called with the timer times out.
This function can be called any number of times to add handlers.
callback | The callback to invoke on event. |
void remove_handler | ( | RegisterHandle | handle | ) |
Remove an event handler.
handle | The handle returned from on_timeout(). |
|
inline |
Returns true if the timer is currently running.
|
virtual |
Start the timer.
It is safe to call start() on an already running timer. It will simply reset and start again.
Reimplemented in PeriodicTimer, and PeriodicTimer.
|
inline |
Start the timer with the specified duration.
This overwrites any duration specified previously.
EGT_DEPRECATED void start_with_duration | ( | std::chrono::milliseconds | duration | ) |
Start the timer with the specified duration.
This overwrites any duration specified previously.
|
inline |
Alias for cancel().
void timeout | ( | ) |
Called when the timer times out.
This will invoke any callback registered with add_handler(). If you override this you must make sure to call Timer::timeout() or equivalent if you want callbacks to still be called.
|
protected |
Array of registered callbacks.
|
protected |
The duration of the timer.
|
protected |
Counter used to generate unique handles for each callback registration.
|
protected |
|
protected |
A user defined name for the Timer.
|
protected |
When true, currently running.
|
protected |
Asio timer object.