Helios Engine 0.1.0
A modular ECS based data-oriented C++23 game engine
 
Loading...
Searching...
No Matches
helios::Timer< Clock > Class Template Reference

High–resolution timer with configurable clock and rich elapsed API. More...

#include <timer.hpp>

Public Types

using ClockType = Clock
 
using TimePoint = typename Clock::time_point
 
using Duration = typename Clock::duration
 

Public Member Functions

constexpr Timer () noexcept(noexcept(Clock::now()))=default
 Constructs timer and immediately resets start timestamp.
 
constexpr Timer (const Timer &) noexcept(std::is_nothrow_copy_constructible_v< Clock >)=default
 
constexpr Timer (Timer &&) noexcept(std::is_nothrow_move_constructible_v< Clock >)=default
 
constexpr ~Timer () noexcept(std::is_nothrow_destructible_v< Clock >)=default
 
constexpr Timeroperator= (const Timer &) noexcept(std::is_nothrow_copy_assignable_v< Clock >)=default
 
constexpr Timeroperator= (Timer &&) noexcept(std::is_nothrow_move_assignable_v< Clock >)=default
 
constexpr void Reset () noexcept(noexcept(Clock::now()))
 Reset the timer start point to current time.
 
template<details::DurationTrait Units = Duration>
constexpr Units ElapsedDuration () const
 Get elapsed time as a std::chrono::duration.
 
template<utils::ArithmeticTrait Type = typename Duration::rep, details::DurationTrait Units = Duration>
constexpr Type Elapsed () const
 Get elapsed time converted to an arithmetic value with specified units.
 
constexpr double ElapsedSec () const
 Elapsed time in seconds as double.
 
constexpr double ElapsedMilliSec () const
 Elapsed time in milliseconds as double.
 
constexpr int64_t ElapsedMicroSec () const
 Elapsed time in microseconds as 64-bit integer.
 
constexpr int64_t ElapsedNanoSec () const
 Elapsed time in nanoseconds as 64-bit integer.
 
constexpr TimePoint Start () const noexcept
 Get the raw start timestamp of the timer.
 

Detailed Description

template<details::ClockTrait Clock = std::chrono::steady_clock>
class helios::Timer< Clock >

High–resolution timer with configurable clock and rich elapsed API.

The timer measures elapsed time from the last reset point.

  • The clock type is configurable via the template parameter Clock.
  • Users can query elapsed time as:
    • A raw Clock::duration.
    • Any std::chrono::duration (via ElapsedDuration).
    • Arbitrary arithmetic type with specified time unit (via Elapsed).
    • Convenience helpers (seconds, milliseconds, microseconds, nanoseconds).
Note
The default clock is std::chrono::steady_clock.
Template Parameters
ClockClock type used to measure time. Must satisfy details::ClockTrait.

Definition at line 64 of file timer.hpp.

Member Typedef Documentation

◆ ClockType

template<details::ClockTrait Clock = std::chrono::steady_clock>
using helios::Timer< Clock >::ClockType = Clock

Definition at line 66 of file timer.hpp.

◆ Duration

template<details::ClockTrait Clock = std::chrono::steady_clock>
using helios::Timer< Clock >::Duration = typename Clock::duration

Definition at line 68 of file timer.hpp.

◆ TimePoint

template<details::ClockTrait Clock = std::chrono::steady_clock>
using helios::Timer< Clock >::TimePoint = typename Clock::time_point

Definition at line 67 of file timer.hpp.

Constructor & Destructor Documentation

◆ Timer() [1/3]

template<details::ClockTrait Clock = std::chrono::steady_clock>
constexpr helios::Timer< Clock >::Timer ( )
constexprdefaultnoexcept

Constructs timer and immediately resets start timestamp.

◆ Timer() [2/3]

template<details::ClockTrait Clock = std::chrono::steady_clock>
constexpr helios::Timer< Clock >::Timer ( const Timer< Clock > &  ) const
constexprdefaultnoexcept

◆ Timer() [3/3]

template<details::ClockTrait Clock = std::chrono::steady_clock>
constexpr helios::Timer< Clock >::Timer ( Timer< Clock > &&  ) const
constexprdefaultnoexcept

◆ ~Timer()

template<details::ClockTrait Clock = std::chrono::steady_clock>
constexpr helios::Timer< Clock >::~Timer ( )
constexprdefaultnoexcept

Member Function Documentation

◆ Elapsed()

template<details::ClockTrait Clock>
template<utils::ArithmeticTrait Type, details::DurationTrait Units>
constexpr Type helios::Timer< Clock >::Elapsed ( ) const
constexpr

Get elapsed time converted to an arithmetic value with specified units.

This method allows querying elapsed time in arbitrary precision and units, for example:

  • Elapsed<double, std::chrono::seconds>()
  • Elapsed<std::uint64_t, std::chrono::microseconds>()
    Template Parameters
    TypeArithmetic type used for the returned value.
    UnitsDuration type representing the desired time unit.
    Returns
    Elapsed time as Type, where the underlying duration is Units.

Definition at line 153 of file timer.hpp.

153 {
154 const auto duration = ElapsedDuration<Units>();
155 return static_cast<Type>(duration.count());
156}

◆ ElapsedDuration()

template<details::ClockTrait Clock>
template<details::DurationTrait Units>
constexpr Units helios::Timer< Clock >::ElapsedDuration ( ) const
constexpr

Get elapsed time as a std::chrono::duration.

This is the primary API for time measurement and should be preferred when working with std::chrono.

Template Parameters
UnitsDuration type to convert to. Must satisfy details::DurationTrait. Defaults to Clock::duration.
Returns
Elapsed time since last reset as Units.

Definition at line 145 of file timer.hpp.

145 {
146 const auto now = Clock::now();
147 const auto diff = now - start_time_;
148 return std::chrono::duration_cast<Units>(diff);
149}

◆ ElapsedMicroSec()

template<details::ClockTrait Clock = std::chrono::steady_clock>
constexpr int64_t helios::Timer< Clock >::ElapsedMicroSec ( ) const
inlineconstexpr

Elapsed time in microseconds as 64-bit integer.

Returns
Elapsed time since last reset in microseconds.

Definition at line 125 of file timer.hpp.

125{ return Elapsed<int64_t, std::chrono::microseconds>(); }

◆ ElapsedMilliSec()

template<details::ClockTrait Clock = std::chrono::steady_clock>
constexpr double helios::Timer< Clock >::ElapsedMilliSec ( ) const
inlineconstexpr

Elapsed time in milliseconds as double.

Returns
Elapsed time since last reset in milliseconds.

Definition at line 117 of file timer.hpp.

117 {
118 return Elapsed<double, std::chrono::duration<double, std::milli>>();
119 }

◆ ElapsedNanoSec()

template<details::ClockTrait Clock = std::chrono::steady_clock>
constexpr int64_t helios::Timer< Clock >::ElapsedNanoSec ( ) const
inlineconstexpr

Elapsed time in nanoseconds as 64-bit integer.

Returns
Elapsed time since last reset in nanoseconds.

Definition at line 131 of file timer.hpp.

131{ return Elapsed<int64_t, std::chrono::nanoseconds>(); }

◆ ElapsedSec()

template<details::ClockTrait Clock = std::chrono::steady_clock>
constexpr double helios::Timer< Clock >::ElapsedSec ( ) const
inlineconstexpr

Elapsed time in seconds as double.

Returns
Elapsed time since last reset in seconds.

Definition at line 111 of file timer.hpp.

111{ return Elapsed<double, std::chrono::duration<double>>(); }

◆ operator=() [1/2]

template<details::ClockTrait Clock = std::chrono::steady_clock>
constexpr Timer & helios::Timer< Clock >::operator= ( const Timer< Clock > &  )
constexprdefaultnoexcept

◆ operator=() [2/2]

template<details::ClockTrait Clock = std::chrono::steady_clock>
constexpr Timer & helios::Timer< Clock >::operator= ( Timer< Clock > &&  )
constexprdefaultnoexcept

◆ Reset()

template<details::ClockTrait Clock = std::chrono::steady_clock>
constexpr void helios::Timer< Clock >::Reset ( )
inlineconstexprnoexcept

Reset the timer start point to current time.

Definition at line 84 of file timer.hpp.

84{ start_time_ = Clock::now(); }

◆ Start()

template<details::ClockTrait Clock = std::chrono::steady_clock>
constexpr TimePoint helios::Timer< Clock >::Start ( ) const
inlineconstexprnoexcept

Get the raw start timestamp of the timer.

Returns
Starting time point.

Definition at line 137 of file timer.hpp.

137{ return start_time_; }