1.10
Public Member Functions | List of all members
PeriodicTimer Class Reference

Periodic timer. More...

#include <timer.h>

Inheritance diagram for PeriodicTimer:
Inheritance graph
[legend]

Public Member Functions

virtual void start ()
 Start the timer.
 
void start () override
 Start the timer.
 
void start (std::chrono::milliseconds duration)
 Start the timer with the specified duration.
 
 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 &&)
 
- Public Member Functions inherited from Timer
 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.
 
Timeroperator= (const Timer &)=delete
 
Timeroperator= (Timer &&)
 
void remove_handler (RegisterHandle handle)
 Remove an event handler.
 
EGT_NODISCARD bool running () const
 Returns true if the timer is currently running.
 
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.
 

Additional Inherited Members

- Public Types inherited from Timer
using RegisterHandle = uint64_t
 Handle type.
 
using TimerCallback = std::function< void()>
 Timer callback function definition.
 
- Protected Types inherited from Timer
using CallbackArray = std::vector< CallbackMeta >
 Type for array of registered callbacks.
 
- Protected Member Functions inherited from Timer
void invoke_handlers ()
 Invoke any registered handlers.
 
- Protected Attributes inherited from Timer
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.
 

Detailed Description

Periodic timer.

This is a timer that will keep firing at the duration interval until it is stopped by calling cancel().

Example

PeriodicTimer timer(std::chrono::seconds(1));
timer.on_timeout([]()
{
// called every 1 second
});
timer.start();
Periodic timer.
Definition timer.h:281
See also
Timer

Member Function Documentation

◆ start() [1/3]

virtual void start ( )
virtual

Start the timer.

It is safe to call start() on an already running timer. It will simply reset and start again.

Note
Assumes a duration is set already.

Reimplemented from Timer.

◆ start() [2/3]

void start ( )
overridevirtual

Start the timer.

It is safe to call start() on an already running timer. It will simply reset and start again.

Note
Assumes a duration is set already.

Reimplemented from Timer.

◆ start() [3/3]

void start ( std::chrono::milliseconds  duration)
inline

Start the timer with the specified duration.

This overwrites any duration specified previously.

◆ Timer() [1/4]

Timer ( )
noexcept

Construct a one-shot timer.

The duration of the timer can be specified when calling start_with_duration() when using this constructor.

◆ Timer() [2/4]

Timer ( const Timer )
delete

◆ Timer() [3/4]

Timer ( std::chrono::milliseconds  duration)
explicitnoexcept

Construct a one-shot timer with the specified duration.

◆ Timer() [4/4]

Timer ( Timer &&  )