Time Shield Library 2.0.0
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 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 24 of file ElapsedTimer.hpp.

Member Typedef Documentation

◆ clock

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

Definition at line 26 of file ElapsedTimer.hpp.

◆ duration

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

Definition at line 27 of file ElapsedTimer.hpp.

◆ time_point

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

Definition at line 28 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 34 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 96 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 101 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 146 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 121 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 126 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 109 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 114 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 133 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 138 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 151 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 162 of file ElapsedTimer.hpp.

◆ invalidate()

void time_shield::ElapsedTimer::invalidate ( )
inlinenoexcept

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

Definition at line 76 of file ElapsedTimer.hpp.

◆ is_running()

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

Checks whether the timer measures elapsed time.

Definition at line 81 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 86 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 173 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 47 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 56 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 66 of file ElapsedTimer.hpp.

◆ start()

void time_shield::ElapsedTimer::start ( )
inlinenoexcept

Starts the timer using the current steady clock time.

Definition at line 41 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 91 of file ElapsedTimer.hpp.

Member Data Documentation

◆ m_is_running

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

Definition at line 182 of file ElapsedTimer.hpp.

◆ m_start_time

time_point time_shield::ElapsedTimer::m_start_time {}
private

Definition at line 181 of file ElapsedTimer.hpp.


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