ADTF  3.18.2
Timer

Periodic timer running in a separate thread. More...

Classes

class  IntrusivePtr
 

Public Member Functions

 Timer ()
 Default CTOR.
 
 ~Timer ()
 DTOR - Calls stop()
 
 Timer (std::uint64_t period_us, void(*function)())
 CTOR for functions as callbacks. More...
 
template<typename M , typename T >
 Timer (std::uint64_t period_us, M method, T &instance)
 CTOR for methods as callbacks. More...
 
void setCallback (void(*function)())
 Set the callback for the timer. More...
 
template<typename M , typename T >
void setCallback (M method, T &instance)
 Set the callback for the timer. More...
 
void setPeriod (std::uint64_t period_us)
 Set the period used by this timer, restarting the timer if already running. More...
 
std::uint64_t getPeriod () const
 Get the currently configured period of the timer. More...
 
bool start ()
 Start the timer. More...
 
bool stop ()
 Stop the timer - blocks until the callback returns. More...
 
bool isRunning () const
 Check whether the timer is currently running. More...
 

Private Member Functions

 Timer (const Timer &)
 
Timeroperator= (const Timer &)
 
void setCallback (const a_util::experimental::NullaryDelegate< void > &cb)
 

Static Private Member Functions

template<typename T , typename... Args>
static auto makeIntrusive (Args &&... args) -> IntrusivePtr< T >
 

Private Attributes

IntrusivePtr< Implementation > _impl
 

Detailed Description

Periodic timer running in a separate thread.

  • Invokes a callback periodically
  • If an invocation takes longer than the period, any missed expirations are lost!
  • The callback is invoked from a different thread, take care about thread safety!

Definition at line 45 of file timer_decl.h.

Constructor & Destructor Documentation

◆ Timer() [1/2]

Timer ( std::uint64_t  period_us,
void(*)()  function 
)

CTOR for functions as callbacks.

To start the timer, call start

Parameters
[in]period_usDuration between calls to function (in microseconds) 0 -> One shot timer
[in]functionPointer to callback function

◆ Timer() [2/2]

Timer ( std::uint64_t  period_us,
method,
T &  instance 
)
inline

CTOR for methods as callbacks.

To start the timer, call start

Template Parameters
MMethod type to call
TClass containing the method
Parameters
[in]period_usDuration between calls to function (in microseconds). 0 -> One shot timer
[in]methodMethod used as callback
[in]instanceInstance of the class the method to invoke from

Definition at line 24 of file timer_impl.h.

References Timer::setCallback(), and Timer::setPeriod().

Member Function Documentation

◆ getPeriod()

std::uint64_t getPeriod ( ) const

Get the currently configured period of the timer.

Returns
Period of the timer in microseconds

◆ isRunning()

bool isRunning ( ) const

Check whether the timer is currently running.

Returns
true if timer is running, false otherwise

◆ setCallback() [1/2]

void setCallback ( method,
T &  instance 
)
inline

Set the callback for the timer.

Template Parameters
MMethod type to call
TClass containing the method
Parameters
[in]methodMethod used as callback
[in]instanceInstance of the class the method to invoke from

Definition at line 32 of file timer_impl.h.

References Timer::setCallback().

◆ setCallback() [2/2]

void setCallback ( void(*)()  function)

Set the callback for the timer.

Parameters
[in]functionPointer to callback function

Referenced by Timer::Timer(), and Timer::setCallback().

◆ setPeriod()

void setPeriod ( std::uint64_t  period_us)

Set the period used by this timer, restarting the timer if already running.

Parameters
[in]period_usDuration in microseconds between invocations of callback function. 0 -> One shot timer

Referenced by Timer::Timer().

◆ start()

bool start ( )

Start the timer.

Returns
true on success, false if the timer is already running

◆ stop()

bool stop ( )

Stop the timer - blocks until the callback returns.

Returns
true on success, false if the timer is already stopped