Kouta
A small application framework based on Boost
kouta::io::Timer Class Reference

#include <timer.hpp>

Inheritance diagram for kouta::io::Timer:
Collaboration diagram for kouta::io::Timer:

Public Types

using OnExpired = std::function< void(Timer &)>
 Signature of the function to be called when the timer expires or is cancelled. More...
 

Public Member Functions

 Timer ()=delete
 
 Timer (Component *parent, std::chrono::milliseconds duration, OnExpired &&on_expired)
 Constructor. More...
 
 Timer (const Timer &)=delete
 
Timeroperator= (const Timer &)=delete
 
 Timer (Timer &&)=delete
 
Timeroperator= (Timer &&)=delete
 
 ~Timer () override=default
 
void start ()
 Start the timer and wait for it to complete asynchronously. More...
 
void stop ()
 Stop the timer if it was running/being waited for. More...
 
void set_duration (std::chrono::milliseconds duration)
 Set the duration of the timer in future waiting operations. More...
 
- Public Member Functions inherited from kouta::base::Component
 Component ()=delete
 
 Component (Component *parent)
 Constructor. More...
 
 Component (const Component &)=delete
 
Componentoperator= (const Component &)=delete
 
 Component (Component &&)=delete
 
Componentoperator= (Component &&)=delete
 
virtual ~Component ()
 Component destructor. More...
 
virtual boost::asio::io_context & context ()
 Obtain a reference to the underlying I/O context. More...
 
void add_child (Component *component)
 Add a child component to the list. More...
 
void remove_child (Component *component)
 Remove a child component from the list. More...
 
template<class TClass , class... TMethodArgs, class... TArgs>
void post (void(TClass::*method)(TMethodArgs...), TArgs... args)
 Post a method call to the event loop for deferred execution. More...
 
template<class... TFuncArgs, class... TArgs>
void post (const std::function< void(TFuncArgs...)> &functor, TArgs... args)
 Post a function call to the event loop for deferred execution. More...
 
template<class TFunctor >
void post (TFunctor &&functor)
 Post a functor call to the event loop for deferred execution. More...
 

Private Member Functions

void handle_expiration (const boost::system::error_code &ec)
 Handle the expiration of the internal timer. More...
 

Private Attributes

boost::asio::steady_timer m_timer
 
std::chrono::milliseconds m_duration
 
OnExpired m_on_expired
 

Member Typedef Documentation

◆ OnExpired

using kouta::io::Timer::OnExpired = std::function<void(Timer&)>

Signature of the function to be called when the timer expires or is cancelled.

Constructor & Destructor Documentation

◆ Timer() [1/4]

kouta::io::Timer::Timer ( )
delete

◆ Timer() [2/4]

kouta::io::Timer::Timer ( Component parent,
std::chrono::milliseconds  duration,
OnExpired &&  on_expired 
)
inline

Constructor.

Parameters
[in]parentParent component granting access to the event loop.
[in]durationDuration of the timer.
[in]on_expiredFunction to call when the timer expires.

◆ Timer() [3/4]

kouta::io::Timer::Timer ( const Timer )
delete

◆ Timer() [4/4]

kouta::io::Timer::Timer ( Timer &&  )
delete

◆ ~Timer()

kouta::io::Timer::~Timer ( )
overridedefault

Member Function Documentation

◆ handle_expiration()

void kouta::io::Timer::handle_expiration ( const boost::system::error_code &  ec)
inlineprivate

Handle the expiration of the internal timer.

If the timer expired normally, the internal callback will be executed to notify the external world. Note that said callback will be executed within the context of the event loop as a direct invocation.

Parameters
[in]ecError code of the asynchronous wait operation.

◆ operator=() [1/2]

Timer& kouta::io::Timer::operator= ( const Timer )
delete

◆ operator=() [2/2]

Timer& kouta::io::Timer::operator= ( Timer &&  )
delete

◆ set_duration()

void kouta::io::Timer::set_duration ( std::chrono::milliseconds  duration)
inline

Set the duration of the timer in future waiting operations.

The purpose of this method is to allow setting the timer duration without actually starting it, as opposed to start(). However, this method will not affect any running timer awaits, meaning that stop() must be called explicitly if such abehaviour is required.

Parameters
[in]durationNew duration for the timer.

◆ start()

void kouta::io::Timer::start ( )
inline

Start the timer and wait for it to complete asynchronously.

The timer will be automatically stopped if it was already running (which could be seen as restarting the timer). The duration used for awaiting the timer is the one already set inside this object.

Note
This is a one-shot waiting operation.

◆ stop()

void kouta::io::Timer::stop ( )
inline

Stop the timer if it was running/being waited for.

Member Data Documentation

◆ m_duration

std::chrono::milliseconds kouta::io::Timer::m_duration
private

◆ m_on_expired

OnExpired kouta::io::Timer::m_on_expired
private

◆ m_timer

boost::asio::steady_timer kouta::io::Timer::m_timer
private

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