Time Shield Library
C++ library for working with time
Loading...
Searching...
No Matches
time_shield::Timer Class Reference

Timer that mimics the behavior of Qt timers. More...

#include <TimerScheduler.hpp>

Public Types

using Callback = detail::TimerCallback
 

Public Member Functions

 Timer (TimerScheduler &scheduler)
 
 ~Timer ()
 
 Timer (const Timer &)=delete
 
Timeroperator= (const Timer &)=delete
 
 Timer (Timer &&)=delete
 
Timeroperator= (Timer &&)=delete
 
template<class Rep, class Period>
void set_interval (std::chrono::duration< Rep, Period > interval) noexcept
 Sets the interval used by the timer.
 
std::chrono::milliseconds interval () const noexcept
 Returns the currently configured interval.
 
void start ()
 Starts the timer using the previously configured interval.
 
template<class Rep, class Period>
void start (std::chrono::duration< Rep, Period > interval)
 Starts the timer with the specified interval.
 
void stop ()
 Stops the timer.
 
void stop_and_wait ()
 Stops the timer and waits until an active callback finishes.
 
void set_single_shot (bool is_single_shot) noexcept
 Sets whether the timer should fire only once.
 
bool is_single_shot () const noexcept
 Returns true if the timer fires only once.
 
bool is_active () const noexcept
 Returns true if the timer is active.
 
bool is_running () const noexcept
 Returns true if the timer callback is being executed.
 
void set_callback (Callback callback)
 Sets the callback that should be invoked when the timer fires.
 

Static Public Member Functions

template<class Rep, class Period>
static void single_shot (TimerScheduler &scheduler, std::chrono::duration< Rep, Period > interval, Callback callback)
 Creates a single-shot timer that invokes the callback once.
 

Private Attributes

TimerSchedulerm_scheduler
 
timer_state_ptr m_state
 

Detailed Description

Timer that mimics the behavior of Qt timers.

Definition at line 176 of file TimerScheduler.hpp.

Member Typedef Documentation

◆ Callback

Constructor & Destructor Documentation

◆ Timer() [1/3]

time_shield::Timer::Timer ( TimerScheduler & scheduler)
inlineexplicit

Definition at line 518 of file TimerScheduler.hpp.

◆ ~Timer()

time_shield::Timer::~Timer ( )
inline

Definition at line 525 of file TimerScheduler.hpp.

◆ Timer() [2/3]

time_shield::Timer::Timer ( const Timer & )
delete

◆ Timer() [3/3]

time_shield::Timer::Timer ( Timer && )
delete

Member Function Documentation

◆ interval()

std::chrono::milliseconds time_shield::Timer::interval ( ) const
inlinenoexcept

Returns the currently configured interval.

Definition at line 549 of file TimerScheduler.hpp.

◆ is_active()

bool time_shield::Timer::is_active ( ) const
inlinenoexcept

Returns true if the timer is active.

Definition at line 587 of file TimerScheduler.hpp.

◆ is_running()

bool time_shield::Timer::is_running ( ) const
inlinenoexcept

Returns true if the timer callback is being executed.

Definition at line 591 of file TimerScheduler.hpp.

◆ is_single_shot()

bool time_shield::Timer::is_single_shot ( ) const
inlinenoexcept

Returns true if the timer fires only once.

Definition at line 583 of file TimerScheduler.hpp.

◆ operator=() [1/2]

Timer & time_shield::Timer::operator= ( const Timer & )
delete

◆ operator=() [2/2]

Timer & time_shield::Timer::operator= ( Timer && )
delete

◆ set_callback()

void time_shield::Timer::set_callback ( Callback callback)
inline

Sets the callback that should be invoked when the timer fires.

Definition at line 595 of file TimerScheduler.hpp.

◆ set_interval()

template<class Rep, class Period>
void time_shield::Timer::set_interval ( std::chrono::duration< Rep, Period > interval)
noexcept

Sets the interval used by the timer.

Negative durations are clamped to zero. An interval of zero means the timer is rescheduled immediately after firing.

Definition at line 541 of file TimerScheduler.hpp.

◆ set_single_shot()

void time_shield::Timer::set_single_shot ( bool is_single_shot)
inlinenoexcept

Sets whether the timer should fire only once.

Definition at line 579 of file TimerScheduler.hpp.

◆ single_shot()

template<class Rep, class Period>
void time_shield::Timer::single_shot ( TimerScheduler & scheduler,
std::chrono::duration< Rep, Period > interval,
Callback callback )
static

Creates a single-shot timer that invokes the callback once.

The helper keeps the timer alive until the callback finishes.

Definition at line 601 of file TimerScheduler.hpp.

◆ start() [1/2]

void time_shield::Timer::start ( )
inline

Starts the timer using the previously configured interval.

Definition at line 554 of file TimerScheduler.hpp.

◆ start() [2/2]

template<class Rep, class Period>
void time_shield::Timer::start ( std::chrono::duration< Rep, Period > interval)

Starts the timer with the specified interval.

Definition at line 561 of file TimerScheduler.hpp.

◆ stop()

void time_shield::Timer::stop ( )
inline

Stops the timer.

The operation is non-blocking: the method does not wait for a running callback to finish. Use stop_and_wait() to synchronously wait for completion.

Definition at line 566 of file TimerScheduler.hpp.

◆ stop_and_wait()

void time_shield::Timer::stop_and_wait ( )
inline

Stops the timer and waits until an active callback finishes.

Must not be called from inside the timer callback itself.

Definition at line 570 of file TimerScheduler.hpp.

Member Data Documentation

◆ m_scheduler

TimerScheduler& time_shield::Timer::m_scheduler
private

Definition at line 241 of file TimerScheduler.hpp.

◆ m_state

timer_state_ptr time_shield::Timer::m_state
private

Definition at line 242 of file TimerScheduler.hpp.


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