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

Helper that measures elapsed monotonic time spans. More...

#include <ElapsedTimer.hpp>

Public Types

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

Public Member Functions

 ElapsedTimer () noexcept=default
 Constructs an invalid timer.
 
 ElapsedTimer (bool start_immediately) noexcept
 Constructs a timer that starts immediately when requested.
 
void start () noexcept
 Starts the timer using the current steady clock time.
 
TIME_SHIELD_NODISCARD duration restart () noexcept
 Restarts the timer and returns the elapsed duration so far.
 
TIME_SHIELD_NODISCARD ts_ms_t restart_ms (ts_ms_t now_ms) noexcept
 Restarts the timer using a millisecond timestamp and returns elapsed milliseconds.
 
TIME_SHIELD_NODISCARD ts_t restart_sec (ts_t now_sec) noexcept
 Restarts the timer using a second timestamp and returns elapsed seconds.
 
void invalidate () noexcept
 Invalidates the timer so subsequent elapsed() calls return zero.
 
TIME_SHIELD_NODISCARD bool is_running () const noexcept
 Checks whether the timer currently measures elapsed time.
 
TIME_SHIELD_NODISCARD bool is_valid () const noexcept
 Alias for is_running() to match Qt naming conventions.
 
TIME_SHIELD_NODISCARD time_point start_time () const noexcept
 Returns start time stored by the timer.
 
TIME_SHIELD_NODISCARD duration elapsed () const noexcept
 Returns elapsed duration since the timer was started.
 
TIME_SHIELD_NODISCARD duration elapsed (time_point now) const noexcept
 Returns elapsed duration relative to the provided time point.
 
TIME_SHIELD_NODISCARD std::int64_t elapsed_ns () const noexcept
 Returns elapsed nanoseconds since the timer was started.
 
TIME_SHIELD_NODISCARD std::int64_t elapsed_ns (std::int64_t now_ns) const noexcept
 Returns elapsed nanoseconds relative to the provided timestamp in nanoseconds.
 
TIME_SHIELD_NODISCARD ts_ms_t elapsed_ms () const noexcept
 Returns elapsed milliseconds since the timer was started.
 
TIME_SHIELD_NODISCARD ts_ms_t elapsed_ms (ts_ms_t now_ms) const noexcept
 Returns elapsed milliseconds relative to the provided timestamp in milliseconds.
 
TIME_SHIELD_NODISCARD ts_t elapsed_sec () const noexcept
 Returns elapsed seconds since the timer was started.
 
TIME_SHIELD_NODISCARD ts_t elapsed_sec (ts_t now_sec) const noexcept
 Returns elapsed seconds relative to the provided timestamp in seconds.
 
template<class Duration>
TIME_SHIELD_NODISCARD Duration::rep elapsed_count () const noexcept
 Returns elapsed duration in the desired chrono duration type.
 
TIME_SHIELD_NODISCARD bool has_expired (ts_ms_t timeout_ms) const noexcept
 Checks if the given timeout in milliseconds has expired.
 
TIME_SHIELD_NODISCARD bool has_expired_sec (ts_t timeout_sec) const noexcept
 Checks if the given timeout in seconds has expired.
 
TIME_SHIELD_NODISCARD std::int64_t ms_since_reference () const noexcept
 Returns milliseconds since the internal clock reference.
 

Private Attributes

time_point m_start_time {}
 
bool m_is_running {false}
 

Detailed Description

Helper that measures elapsed monotonic time spans.

Instances are expected to be used from a single thread without additional synchronization.

Definition at line 25 of file ElapsedTimer.hpp.

Member Typedef Documentation

◆ clock

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

Definition at line 27 of file ElapsedTimer.hpp.

◆ duration

using time_shield::ElapsedTimer::duration = clock::duration

Definition at line 28 of file ElapsedTimer.hpp.

◆ time_point

using time_shield::ElapsedTimer::time_point = clock::time_point

Definition at line 29 of file ElapsedTimer.hpp.

Constructor & Destructor Documentation

◆ ElapsedTimer() [1/2]

time_shield::ElapsedTimer::ElapsedTimer ( )
defaultnoexcept

Constructs an invalid timer.

◆ ElapsedTimer() [2/2]

time_shield::ElapsedTimer::ElapsedTimer ( bool start_immediately)
inlineexplicitnoexcept

Constructs a timer that starts immediately when requested.

Definition at line 35 of file ElapsedTimer.hpp.

Member Function Documentation

◆ elapsed() [1/2]

TIME_SHIELD_NODISCARD duration time_shield::ElapsedTimer::elapsed ( ) const
inlinenoexcept

Returns elapsed duration since the timer was started.

Definition at line 97 of file ElapsedTimer.hpp.

◆ elapsed() [2/2]

TIME_SHIELD_NODISCARD duration time_shield::ElapsedTimer::elapsed ( time_point now) const
inlinenoexcept

Returns elapsed duration relative to the provided time point.

Definition at line 102 of file ElapsedTimer.hpp.

◆ elapsed_count()

template<class Duration>
TIME_SHIELD_NODISCARD Duration::rep time_shield::ElapsedTimer::elapsed_count ( ) const
inlinenoexcept

Returns elapsed duration in the desired chrono duration type.

Definition at line 147 of file ElapsedTimer.hpp.

◆ elapsed_ms() [1/2]

TIME_SHIELD_NODISCARD ts_ms_t time_shield::ElapsedTimer::elapsed_ms ( ) const
inlinenoexcept

Returns elapsed milliseconds since the timer was started.

Definition at line 122 of file ElapsedTimer.hpp.

◆ elapsed_ms() [2/2]

TIME_SHIELD_NODISCARD ts_ms_t time_shield::ElapsedTimer::elapsed_ms ( ts_ms_t now_ms) const
inlinenoexcept

Returns elapsed milliseconds relative to the provided timestamp in milliseconds.

Definition at line 127 of file ElapsedTimer.hpp.

◆ elapsed_ns() [1/2]

TIME_SHIELD_NODISCARD std::int64_t time_shield::ElapsedTimer::elapsed_ns ( ) const
inlinenoexcept

Returns elapsed nanoseconds since the timer was started.

Definition at line 110 of file ElapsedTimer.hpp.

◆ elapsed_ns() [2/2]

TIME_SHIELD_NODISCARD std::int64_t time_shield::ElapsedTimer::elapsed_ns ( std::int64_t now_ns) const
inlinenoexcept

Returns elapsed nanoseconds relative to the provided timestamp in nanoseconds.

Definition at line 115 of file ElapsedTimer.hpp.

◆ elapsed_sec() [1/2]

TIME_SHIELD_NODISCARD ts_t time_shield::ElapsedTimer::elapsed_sec ( ) const
inlinenoexcept

Returns elapsed seconds since the timer was started.

Definition at line 134 of file ElapsedTimer.hpp.

◆ elapsed_sec() [2/2]

TIME_SHIELD_NODISCARD ts_t time_shield::ElapsedTimer::elapsed_sec ( ts_t now_sec) const
inlinenoexcept

Returns elapsed seconds relative to the provided timestamp in seconds.

Definition at line 139 of file ElapsedTimer.hpp.

◆ has_expired()

TIME_SHIELD_NODISCARD bool time_shield::ElapsedTimer::has_expired ( ts_ms_t timeout_ms) const
inlinenoexcept

Checks if the given timeout in milliseconds has expired.

Definition at line 152 of file ElapsedTimer.hpp.

◆ has_expired_sec()

TIME_SHIELD_NODISCARD bool time_shield::ElapsedTimer::has_expired_sec ( ts_t timeout_sec) const
inlinenoexcept

Checks if the given timeout in seconds has expired.

Definition at line 163 of file ElapsedTimer.hpp.

◆ invalidate()

void time_shield::ElapsedTimer::invalidate ( )
inlinenoexcept

Invalidates the timer so subsequent elapsed() calls return zero.

Definition at line 77 of file ElapsedTimer.hpp.

◆ is_running()

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

Checks whether the timer currently measures elapsed time.

Definition at line 82 of file ElapsedTimer.hpp.

◆ is_valid()

TIME_SHIELD_NODISCARD bool time_shield::ElapsedTimer::is_valid ( ) const
inlinenoexcept

Alias for is_running() to match Qt naming conventions.

Definition at line 87 of file ElapsedTimer.hpp.

◆ ms_since_reference()

TIME_SHIELD_NODISCARD std::int64_t time_shield::ElapsedTimer::ms_since_reference ( ) const
inlinenoexcept

Returns milliseconds since the internal clock reference.

Definition at line 174 of file ElapsedTimer.hpp.

◆ restart()

TIME_SHIELD_NODISCARD duration time_shield::ElapsedTimer::restart ( )
inlinenoexcept

Restarts the timer and returns the elapsed duration so far.

Definition at line 48 of file ElapsedTimer.hpp.

◆ restart_ms()

TIME_SHIELD_NODISCARD ts_ms_t time_shield::ElapsedTimer::restart_ms ( ts_ms_t now_ms)
inlinenoexcept

Restarts the timer using a millisecond timestamp and returns elapsed milliseconds.

Definition at line 57 of file ElapsedTimer.hpp.

◆ restart_sec()

TIME_SHIELD_NODISCARD ts_t time_shield::ElapsedTimer::restart_sec ( ts_t now_sec)
inlinenoexcept

Restarts the timer using a second timestamp and returns elapsed seconds.

Definition at line 67 of file ElapsedTimer.hpp.

◆ start()

void time_shield::ElapsedTimer::start ( )
inlinenoexcept

Starts the timer using the current steady clock time.

Definition at line 42 of file ElapsedTimer.hpp.

◆ start_time()

TIME_SHIELD_NODISCARD time_point time_shield::ElapsedTimer::start_time ( ) const
inlinenoexcept

Returns start time stored by the timer.

Definition at line 92 of file ElapsedTimer.hpp.

Member Data Documentation

◆ m_is_running

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

Definition at line 183 of file ElapsedTimer.hpp.

◆ m_start_time

time_point time_shield::ElapsedTimer::m_start_time {}
private

Definition at line 182 of file ElapsedTimer.hpp.


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