Time Shield Library
C++ library for working with time
|
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 currently 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} |
Helper that models a monotonic deadline for timeout management.
DeadlineTimer invariants:
start(timeout <= 0)
marks the deadline as already due.set_forever()
to represent "no timeout" semantics. Definition at line 26 of file DeadlineTimer.hpp.
using time_shield::DeadlineTimer::clock = std::chrono::steady_clock |
Definition at line 28 of file DeadlineTimer.hpp.
using time_shield::DeadlineTimer::duration = clock::duration |
Definition at line 29 of file DeadlineTimer.hpp.
using time_shield::DeadlineTimer::time_point = clock::time_point |
Definition at line 30 of file DeadlineTimer.hpp.
|
defaultnoexcept |
Constructs an inactive timer.
|
inlineexplicitnoexcept |
Constructs a timer with the specified absolute deadline.
Definition at line 36 of file DeadlineTimer.hpp.
|
inlineexplicitnoexcept |
Constructs a timer that expires after the given timeout.
Definition at line 42 of file DeadlineTimer.hpp.
|
inlineexplicitnoexcept |
Constructs a timer that expires after the given number of milliseconds.
Definition at line 47 of file DeadlineTimer.hpp.
|
inlinenoexcept |
Extends deadline by the specified duration while preventing overflow.
Definition at line 209 of file DeadlineTimer.hpp.
|
inlinenoexcept |
Extends deadline by the specified number of milliseconds while preventing overflow.
Definition at line 230 of file DeadlineTimer.hpp.
|
inlinenoexcept |
Extends deadline by the specified number of seconds while preventing overflow.
Definition at line 222 of file DeadlineTimer.hpp.
|
inlinenoexcept |
Returns stored deadline.
Definition at line 147 of file DeadlineTimer.hpp.
|
inlinenoexcept |
Returns stored deadline as milliseconds since the steady epoch.
Definition at line 152 of file DeadlineTimer.hpp.
|
inlinenoexcept |
Returns stored deadline as seconds since the steady epoch.
Definition at line 157 of file DeadlineTimer.hpp.
|
inlinestaticnoexcept |
Creates a timer that expires after the specified timeout.
Definition at line 52 of file DeadlineTimer.hpp.
|
inlinestaticnoexcept |
Creates a timer that expires after the specified timeout.
Definition at line 60 of file DeadlineTimer.hpp.
|
inlinestaticnoexcept |
Creates a timer that expires after the specified number of milliseconds.
Definition at line 74 of file DeadlineTimer.hpp.
|
inlinestaticnoexcept |
Creates a timer that expires after the specified number of seconds.
Definition at line 67 of file DeadlineTimer.hpp.
|
inlinenoexcept |
Checks if the deadline has already expired.
Definition at line 162 of file DeadlineTimer.hpp.
|
inlinenoexcept |
Checks if the deadline has expired relative to the provided time point.
Definition at line 179 of file DeadlineTimer.hpp.
|
inlinenoexcept |
Checks if the deadline has expired relative to the provided millisecond timestamp.
Definition at line 167 of file DeadlineTimer.hpp.
|
inlinenoexcept |
Checks if the deadline has expired relative to the provided second timestamp.
Definition at line 173 of file DeadlineTimer.hpp.
|
inlinenoexcept |
Checks whether the timer is configured for an infinite timeout.
Definition at line 142 of file DeadlineTimer.hpp.
|
inlinenoexcept |
Checks whether the timer currently tracks a deadline.
Definition at line 137 of file DeadlineTimer.hpp.
|
inlinenoexcept |
Returns time remaining until the deadline.
Definition at line 184 of file DeadlineTimer.hpp.
|
inlinenoexcept |
Returns remaining time relative to the provided time point.
Non-running timers and already expired timers report zero duration.
Definition at line 201 of file DeadlineTimer.hpp.
|
inlinenoexcept |
Returns remaining time in milliseconds until the deadline.
Definition at line 189 of file DeadlineTimer.hpp.
|
inlinenoexcept |
Returns remaining time in seconds until the deadline.
Definition at line 194 of file DeadlineTimer.hpp.
|
inlinenoexcept |
Marks the timer as running forever (no timeout).
Definition at line 131 of file DeadlineTimer.hpp.
|
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 92 of file DeadlineTimer.hpp.
|
inlinenoexcept |
Sets the absolute deadline and marks the timer as active.
Definition at line 81 of file DeadlineTimer.hpp.
|
inlinenoexcept |
Starts the timer so it expires after the specified number of milliseconds.
Definition at line 120 of file DeadlineTimer.hpp.
|
inlinenoexcept |
Starts the timer so it expires after the specified number of seconds.
Definition at line 115 of file DeadlineTimer.hpp.
|
inlinenoexcept |
Stops the timer and invalidates the stored deadline.
Definition at line 125 of file DeadlineTimer.hpp.
|
private |
Definition at line 238 of file DeadlineTimer.hpp.
|
private |
Definition at line 239 of file DeadlineTimer.hpp.