Periodic Timer
Loading...
Searching...
No Matches
chops::periodic_timer< Clock > Class Template Reference

Public Types

using duration = typename Clock::duration
 
using time_point = typename Clock::time_point
 

Public Member Functions

 periodic_timer (asio::io_context &ioc) noexcept
 
 periodic_timer (const periodic_timer &)=delete
 
periodic_timeroperator= (const periodic_timer &)=delete
 
 periodic_timer (periodic_timer &&)=default
 
periodic_timeroperator= (periodic_timer &&rhs)
 
template<typename F >
void start_duration_timer (const duration &dur, F &&func)
 
template<typename F >
void start_duration_timer (const duration &dur, const time_point &when, F &&func)
 
template<typename F >
void start_timepoint_timer (const duration &dur, F &&func)
 
template<typename F >
void start_timepoint_timer (const duration &dur, const time_point &when, F &&func)
 
void cancel ()
 

Constructor & Destructor Documentation

◆ periodic_timer()

template<typename Clock = std::chrono::steady_clock>
chops::periodic_timer< Clock >::periodic_timer ( asio::io_context & ioc)
inlineexplicitnoexcept

Construct a periodic_timer with an io_context. Other information such as duration will be supplied when start is called.

Constructing a periodic_timer does not start the actual timer. Calling one of the start methods starts the timer.

The application supplied function object for any of the start methods requires the following signature:

bool (std::error_code, duration);

The duration parameter provides an elapsed time from the previous callback.

The clock for the asynchronous timer defaults to std::chrono::steady_clock. Other clock types can be used if desired (e.g. std::chrono::high_resolution_clock or std::chrono::system_clock). Note that some clocks allow time to be externally adjusted, which may influence the interval between the callback invocation.

Move semantics are allowed for this type, but not copy semantics. When a move construction or move assignment completes, all timers are cancelled with appropriate notification, and start will need to be called.

Parameters
iocio_context for asynchronous processing.

Member Function Documentation

◆ cancel()

template<typename Clock = std::chrono::steady_clock>
void chops::periodic_timer< Clock >::cancel ( )
inline

Cancel the timer. The application function object will be called with an "operation aborted" error code.

A cancel may implicitly be called if the periodic_timer object is move copy constructed or move assigned.

◆ start_duration_timer() [1/2]

template<typename Clock = std::chrono::steady_clock>
template<typename F >
void chops::periodic_timer< Clock >::start_duration_timer ( const duration & dur,
const time_point & when,
F && func )
inline

Start the timer, and the application supplied function object will be invoked first at a specified time point, then afterwards as specified by the duration parameter.

The function object will continue to be invoked as long as it returns true.

Parameters
durInterval to be used between callback invocations.
whenTime point when the first timer callback will be invoked.
funcFunction object to be invoked.

◆ start_duration_timer() [2/2]

template<typename Clock = std::chrono::steady_clock>
template<typename F >
void chops::periodic_timer< Clock >::start_duration_timer ( const duration & dur,
F && func )
inline

Start the timer, and the application supplied function object will be invoked after an amount of time specified by the duration parameter.

The function object will continue to be invoked as long as it returns true.

Parameters
durInterval to be used between callback invocations.
funcFunction object to be invoked.

◆ start_timepoint_timer() [1/2]

template<typename Clock = std::chrono::steady_clock>
template<typename F >
void chops::periodic_timer< Clock >::start_timepoint_timer ( const duration & dur,
const time_point & when,
F && func )
inline

Start the timer on the specified timepoint, and the application supplied function object will be invoked on timepoints with an interval specified by the duration.

The function object will continue to be invoked as long as it returns true.

Parameters
durInterval to be used between callback invocations.
whenTime point when the first timer callback will be invoked.
funcFunction object to be invoked.
Note
The elapsed time for the first callback invocation is artificially set to the duration interval.

◆ start_timepoint_timer() [2/2]

template<typename Clock = std::chrono::steady_clock>
template<typename F >
void chops::periodic_timer< Clock >::start_timepoint_timer ( const duration & dur,
F && func )
inline

Start the timer, and the application supplied function object will be invoked on timepoints with an interval specified by the duration.

The function object will continue to be invoked as long as it returns true.

Parameters
durInterval to be used between callback invocations.
funcFunction object to be invoked.

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