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

Periodic timer. More...

Inheritance diagram for PeriodicTimer:
Inheritance graph
[legend]

Public Member Functions

virtual void start ()
 Start the timer. More...
 
void start () override
 Start the timer. More...
 
void start (std::chrono::milliseconds duration)
 Start the timer with the specified duration. More...
 
 Timer () noexcept
 Construct a one-shot timer. More...
 
 Timer (const Timer &)=delete
 
 Timer (std::chrono::milliseconds duration) noexcept
 Construct a one-shot timer with the specified duration. More...
 
 Timer (Timer &&)
 
- Public Member Functions inherited from Timer
 Timer () noexcept
 Construct a one-shot timer. More...
 
 Timer (const Timer &)=delete
 
 Timer (std::chrono::milliseconds duration) noexcept
 Construct a one-shot timer with the specified duration. More...
 
 Timer (Timer &&)
 
virtual ~Timer () noexcept
 
void cancel ()
 Cancel, or stop, the timer. More...
 
void change_duration (std::chrono::milliseconds duration)
 Change the duration of the timer. More...
 
void clear_handlers ()
 Clear all handlers. More...
 
EGT_NODISCARD std::chrono::milliseconds duration () const
 Return the current duration of the timer. More...
 
EGT_NODISCARD const std::string & name () const
 Get the name of the Timer. More...
 
void name (const std::string &name)
 Set the name of the Timer. More...
 
RegisterHandle on_timeout (TimerCallback callback)
 Add a handler callback to be called with the timer times out. More...
 
Timeroperator= (const Timer &)=delete
 
Timeroperator= (Timer &&)
 
void remove_handler (RegisterHandle handle)
 Remove an event handler. More...
 
EGT_NODISCARD bool running () const
 Returns true if the timer is currently running. More...
 
void start (std::chrono::milliseconds duration)
 Start the timer with the specified duration. More...
 
EGT_DEPRECATED void start_with_duration (std::chrono::milliseconds duration)
 Start the timer with the specified duration. More...
 
void stop ()
 Alias for cancel(). More...
 
void timeout ()
 Called when the timer times out. More...
 

Additional Inherited Members

- Public Types inherited from Timer
using RegisterHandle = uint64_t
 Handle type. More...
 
using TimerCallback = std::function< void()>
 Timer callback function definition. More...
 
- Protected Types inherited from Timer
using CallbackArray = std::vector< CallbackMeta >
 Type for array of registered callbacks. More...
 
- Protected Member Functions inherited from Timer
void invoke_handlers ()
 Invoke any registered handlers. More...
 
- Protected Attributes inherited from Timer
CallbackArray m_callbacks
 Array of registered callbacks. More...
 
std::chrono::milliseconds m_duration {}
 The duration of the timer. More...
 
RegisterHandle m_handle_counter {0}
 Counter used to generate unique handles for each callback registration. More...
 
std::unique_ptr< TimerImpl > m_impl
 
std::string m_name
 A user defined name for the Timer. More...
 
bool m_running {false}
 When true, currently running. More...
 
asio::steady_timer m_timer
 Asio timer object. More...
 

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();
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
inlinevirtual

Start the timer with the specified duration.

This overwrites any duration specified previously.

Reimplemented from Timer.

◆ 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
delete

◆ Timer() [3/4]

Timer
explicitnoexcept

Construct a one-shot timer with the specified duration.

◆ Timer() [4/4]