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

Helper that models a monotonic deadline for timeout management. More...

#include <DeadlineTimer.hpp>

Public Types

using clock = std::chrono::steady_clock
 
using duration = clock::duration
 
using time_point = clock::time_point
 

Public Member Functions

 DeadlineTimer () noexcept=default
 Constructs an inactive timer.
 
 DeadlineTimer (time_point deadline) noexcept
 Constructs a timer with the specified absolute deadline.
 
template<class Rep , class Period >
 DeadlineTimer (std::chrono::duration< Rep, Period > timeout) noexcept
 Constructs a timer that expires after the given timeout.
 
 DeadlineTimer (ts_ms_t timeout_ms) noexcept
 Constructs a timer that expires after the given number of milliseconds.
 
void start (time_point deadline) noexcept
 Sets the absolute deadline and marks the timer as active.
 
template<class Rep , class Period >
void start (std::chrono::duration< Rep, Period > timeout) noexcept
 Starts the timer so it expires after the specified timeout.
 
void start_sec (ts_t timeout_sec) noexcept
 Starts the timer so it expires after the specified number of seconds.
 
void start_ms (ts_ms_t timeout_ms) noexcept
 Starts the timer so it expires after the specified number of milliseconds.
 
void stop () noexcept
 Stops the timer and invalidates the stored deadline.
 
void set_forever () noexcept
 Marks the timer as running forever (no timeout).
 
TIME_SHIELD_NODISCARD bool is_running () const noexcept
 Checks whether the timer tracks a deadline.
 
TIME_SHIELD_NODISCARD bool is_forever () const noexcept
 Checks whether the timer is configured for an infinite timeout.
 
TIME_SHIELD_NODISCARD time_point deadline () const noexcept
 Returns stored deadline.
 
TIME_SHIELD_NODISCARD ts_ms_t deadline_ms () const noexcept
 Returns stored deadline as milliseconds since the steady epoch.
 
TIME_SHIELD_NODISCARD ts_t deadline_sec () const noexcept
 Returns stored deadline as seconds since the steady epoch.
 
TIME_SHIELD_NODISCARD bool has_expired () const noexcept
 Checks if the deadline has already expired.
 
TIME_SHIELD_NODISCARD bool has_expired_ms (ts_ms_t now_ms) const noexcept
 Checks if the deadline has expired relative to the provided millisecond timestamp.
 
TIME_SHIELD_NODISCARD bool has_expired_sec (ts_t now_sec) const noexcept
 Checks if the deadline has expired relative to the provided second timestamp.
 
TIME_SHIELD_NODISCARD bool has_expired (time_point now) const noexcept
 Checks if the deadline has expired relative to the provided time point.
 
TIME_SHIELD_NODISCARD duration remaining_time () const noexcept
 Returns time remaining until the deadline.
 
TIME_SHIELD_NODISCARD ts_ms_t remaining_time_ms () const noexcept
 Returns remaining time in milliseconds until the deadline.
 
TIME_SHIELD_NODISCARD ts_t remaining_time_sec () const noexcept
 Returns remaining time in seconds until the deadline.
 
TIME_SHIELD_NODISCARD duration remaining_time (time_point now) const noexcept
 Returns remaining time relative to the provided time point.
 
void add (duration extend_by) noexcept
 Extends deadline by the specified duration while preventing overflow.
 
void add_sec (ts_t extend_by_sec) noexcept
 Extends deadline by the specified number of seconds while preventing overflow.
 
void add_ms (ts_ms_t extend_by_ms) noexcept
 Extends deadline by the specified number of milliseconds while preventing overflow.
 

Static Public Member Functions

static DeadlineTimer from_timeout (duration timeout) noexcept
 Creates a timer that expires after the specified timeout.
 
template<class Rep , class Period >
static DeadlineTimer from_timeout (std::chrono::duration< Rep, Period > timeout) noexcept
 Creates a timer that expires after the specified timeout.
 
static DeadlineTimer from_timeout_sec (ts_t timeout_sec) noexcept
 Creates a timer that expires after the specified number of seconds.
 
static DeadlineTimer from_timeout_ms (ts_ms_t timeout_ms) noexcept
 Creates a timer that expires after the specified number of milliseconds.
 

Private Attributes

time_point m_deadline {}
 
bool m_is_running {false}
 

Detailed Description

Helper that models a monotonic deadline for timeout management.

DeadlineTimer invariants:

  • Not thread-safe. Access must stay within a single thread.
  • start(timeout <= 0) marks the deadline as already due.
  • Use set_forever() to represent "no timeout" semantics.

Definition at line 25 of file DeadlineTimer.hpp.

Member Typedef Documentation

◆ clock

using time_shield::DeadlineTimer::clock = std::chrono::steady_clock

Definition at line 27 of file DeadlineTimer.hpp.

◆ duration

using time_shield::DeadlineTimer::duration = clock::duration

Definition at line 28 of file DeadlineTimer.hpp.

◆ time_point

using time_shield::DeadlineTimer::time_point = clock::time_point

Definition at line 29 of file DeadlineTimer.hpp.

Constructor & Destructor Documentation

◆ DeadlineTimer() [1/4]

time_shield::DeadlineTimer::DeadlineTimer ( )
defaultnoexcept

Constructs an inactive timer.

◆ DeadlineTimer() [2/4]

time_shield::DeadlineTimer::DeadlineTimer ( time_point deadline)
inlineexplicitnoexcept

Constructs a timer with the specified absolute deadline.

Definition at line 35 of file DeadlineTimer.hpp.

◆ DeadlineTimer() [3/4]

template<class Rep , class Period >
time_shield::DeadlineTimer::DeadlineTimer ( std::chrono::duration< Rep, Period > timeout)
inlineexplicitnoexcept

Constructs a timer that expires after the given timeout.

Definition at line 41 of file DeadlineTimer.hpp.

◆ DeadlineTimer() [4/4]

time_shield::DeadlineTimer::DeadlineTimer ( ts_ms_t timeout_ms)
inlineexplicitnoexcept

Constructs a timer that expires after the given number of milliseconds.

Definition at line 46 of file DeadlineTimer.hpp.

Member Function Documentation

◆ add()

void time_shield::DeadlineTimer::add ( duration extend_by)
inlinenoexcept

Extends deadline by the specified duration while preventing overflow.

Definition at line 208 of file DeadlineTimer.hpp.

◆ add_ms()

void time_shield::DeadlineTimer::add_ms ( ts_ms_t extend_by_ms)
inlinenoexcept

Extends deadline by the specified number of milliseconds while preventing overflow.

Definition at line 229 of file DeadlineTimer.hpp.

◆ add_sec()

void time_shield::DeadlineTimer::add_sec ( ts_t extend_by_sec)
inlinenoexcept

Extends deadline by the specified number of seconds while preventing overflow.

Definition at line 221 of file DeadlineTimer.hpp.

◆ deadline()

TIME_SHIELD_NODISCARD time_point time_shield::DeadlineTimer::deadline ( ) const
inlinenoexcept

Returns stored deadline.

Definition at line 146 of file DeadlineTimer.hpp.

◆ deadline_ms()

TIME_SHIELD_NODISCARD ts_ms_t time_shield::DeadlineTimer::deadline_ms ( ) const
inlinenoexcept

Returns stored deadline as milliseconds since the steady epoch.

Definition at line 151 of file DeadlineTimer.hpp.

◆ deadline_sec()

TIME_SHIELD_NODISCARD ts_t time_shield::DeadlineTimer::deadline_sec ( ) const
inlinenoexcept

Returns stored deadline as seconds since the steady epoch.

Definition at line 156 of file DeadlineTimer.hpp.

◆ from_timeout() [1/2]

static DeadlineTimer time_shield::DeadlineTimer::from_timeout ( duration timeout)
inlinestaticnoexcept

Creates a timer that expires after the specified timeout.

Definition at line 51 of file DeadlineTimer.hpp.

◆ from_timeout() [2/2]

template<class Rep , class Period >
static DeadlineTimer time_shield::DeadlineTimer::from_timeout ( std::chrono::duration< Rep, Period > timeout)
inlinestaticnoexcept

Creates a timer that expires after the specified timeout.

Definition at line 59 of file DeadlineTimer.hpp.

◆ from_timeout_ms()

static DeadlineTimer time_shield::DeadlineTimer::from_timeout_ms ( ts_ms_t timeout_ms)
inlinestaticnoexcept

Creates a timer that expires after the specified number of milliseconds.

Definition at line 73 of file DeadlineTimer.hpp.

◆ from_timeout_sec()

static DeadlineTimer time_shield::DeadlineTimer::from_timeout_sec ( ts_t timeout_sec)
inlinestaticnoexcept

Creates a timer that expires after the specified number of seconds.

Definition at line 66 of file DeadlineTimer.hpp.

◆ has_expired() [1/2]

TIME_SHIELD_NODISCARD bool time_shield::DeadlineTimer::has_expired ( ) const
inlinenoexcept

Checks if the deadline has already expired.

Definition at line 161 of file DeadlineTimer.hpp.

◆ has_expired() [2/2]

TIME_SHIELD_NODISCARD bool time_shield::DeadlineTimer::has_expired ( time_point now) const
inlinenoexcept

Checks if the deadline has expired relative to the provided time point.

Definition at line 178 of file DeadlineTimer.hpp.

◆ has_expired_ms()

TIME_SHIELD_NODISCARD bool time_shield::DeadlineTimer::has_expired_ms ( ts_ms_t now_ms) const
inlinenoexcept

Checks if the deadline has expired relative to the provided millisecond timestamp.

Definition at line 166 of file DeadlineTimer.hpp.

◆ has_expired_sec()

TIME_SHIELD_NODISCARD bool time_shield::DeadlineTimer::has_expired_sec ( ts_t now_sec) const
inlinenoexcept

Checks if the deadline has expired relative to the provided second timestamp.

Definition at line 172 of file DeadlineTimer.hpp.

◆ is_forever()

TIME_SHIELD_NODISCARD bool time_shield::DeadlineTimer::is_forever ( ) const
inlinenoexcept

Checks whether the timer is configured for an infinite timeout.

Definition at line 141 of file DeadlineTimer.hpp.

◆ is_running()

TIME_SHIELD_NODISCARD bool time_shield::DeadlineTimer::is_running ( ) const
inlinenoexcept

Checks whether the timer tracks a deadline.

Definition at line 136 of file DeadlineTimer.hpp.

◆ remaining_time() [1/2]

TIME_SHIELD_NODISCARD duration time_shield::DeadlineTimer::remaining_time ( ) const
inlinenoexcept

Returns time remaining until the deadline.

Definition at line 183 of file DeadlineTimer.hpp.

◆ remaining_time() [2/2]

TIME_SHIELD_NODISCARD duration time_shield::DeadlineTimer::remaining_time ( time_point now) const
inlinenoexcept

Returns remaining time relative to the provided time point.

Non-running timers and already expired timers report zero duration.

Definition at line 200 of file DeadlineTimer.hpp.

◆ remaining_time_ms()

TIME_SHIELD_NODISCARD ts_ms_t time_shield::DeadlineTimer::remaining_time_ms ( ) const
inlinenoexcept

Returns remaining time in milliseconds until the deadline.

Definition at line 188 of file DeadlineTimer.hpp.

◆ remaining_time_sec()

TIME_SHIELD_NODISCARD ts_t time_shield::DeadlineTimer::remaining_time_sec ( ) const
inlinenoexcept

Returns remaining time in seconds until the deadline.

Definition at line 193 of file DeadlineTimer.hpp.

◆ set_forever()

void time_shield::DeadlineTimer::set_forever ( )
inlinenoexcept

Marks the timer as running forever (no timeout).

Definition at line 130 of file DeadlineTimer.hpp.

◆ start() [1/2]

template<class Rep , class Period >
void time_shield::DeadlineTimer::start ( std::chrono::duration< Rep, Period > timeout)
inlinenoexcept

Starts the timer so it expires after the specified timeout.

Negative durations result in an immediate expiration. Durations that are shorter than the steady clock tick are rounded up to a single tick to preserve the monotonic nature of the timer.

Definition at line 91 of file DeadlineTimer.hpp.

◆ start() [2/2]

void time_shield::DeadlineTimer::start ( time_point deadline)
inlinenoexcept

Sets the absolute deadline and marks the timer as active.

Definition at line 80 of file DeadlineTimer.hpp.

◆ start_ms()

void time_shield::DeadlineTimer::start_ms ( ts_ms_t timeout_ms)
inlinenoexcept

Starts the timer so it expires after the specified number of milliseconds.

Definition at line 119 of file DeadlineTimer.hpp.

◆ start_sec()

void time_shield::DeadlineTimer::start_sec ( ts_t timeout_sec)
inlinenoexcept

Starts the timer so it expires after the specified number of seconds.

Definition at line 114 of file DeadlineTimer.hpp.

◆ stop()

void time_shield::DeadlineTimer::stop ( )
inlinenoexcept

Stops the timer and invalidates the stored deadline.

Definition at line 124 of file DeadlineTimer.hpp.

Member Data Documentation

◆ m_deadline

time_point time_shield::DeadlineTimer::m_deadline {}
private

Definition at line 237 of file DeadlineTimer.hpp.

◆ m_is_running

bool time_shield::DeadlineTimer::m_is_running {false}
private

Definition at line 238 of file DeadlineTimer.hpp.


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