libdonut  2.3.2
Application framework for cross-platform game development in C++20
Public Types | Public Member Functions | Friends | List of all members
donut::Time< T, Period > Class Template Reference

Time duration value wrapper. More...

#include <donut/Time.hpp>

Public Types

using Duration = std::chrono::duration< T, Period >
 Underlying duration type. More...
 

Public Member Functions

constexpr Time () noexcept=default
 Construct a time value of 0. More...
 
constexpr Time (Duration duration) noexcept
 Construct a time value from a duration. More...
 
constexpr Time (std::chrono::seconds seconds) noexcept
 Construct a time value from a number of seconds. More...
 
constexpr Time (std::chrono::milliseconds milliseconds) noexcept
 Construct a time value from a number of milliseconds. More...
 
constexpr Time (std::chrono::microseconds microseconds) noexcept
 Construct a time value from a number of microseconds. More...
 
constexpr Time (std::chrono::nanoseconds nanoseconds) noexcept
 Construct a time value from a number of nanoseconds. More...
 
constexpr Time (std::chrono::minutes minutes) noexcept
 Construct a time value from a number of minutes. More...
 
constexpr Time (std::chrono::hours hours) noexcept
 Construct a time value from a number of hours. More...
 
constexpr operator Duration () const noexcept
 Convert a time value to its underlying duration type. More...
 
constexpr operator T () const noexcept
 Convert a time value to its underlying scalar type. More...
 
constexpr Timeoperator+= (Time deltaTime) noexcept
 Add time to this time value. More...
 
constexpr Timeoperator-= (Time deltaTime) noexcept
 Subtract time from this time value. More...
 
constexpr bool countDown (Time deltaTime, Time targetTime=Duration{}) noexcept
 Subtract time from this value and then check if it reached a given target time value. More...
 
constexpr bool countUp (Time deltaTime, Time targetTime) noexcept
 Add time to this value and then check if it reached a given target time value. More...
 
constexpr std::size_t countDownLoop (Time deltaTime, Time interval) noexcept
 Subtract time from this value and then check how many times it reached 0 while looping back to a given time interval. More...
 
constexpr std::size_t countUpLoop (Time deltaTime, Time interval) noexcept
 Add time to this value and then check how many times it reached a given time interval while looping back to 0. More...
 
constexpr std::size_t countDownLoop (Time deltaTime, Time interval, bool active) noexcept
 Update a countdown loop with a boolean trigger that determines whether the loop is active or not. More...
 
constexpr std::size_t countUpLoop (Time deltaTime, Time interval, bool active) noexcept
 Update a countup loop with a boolean trigger that determines whether the loop is active or not. More...
 

Friends

constexpr friend bool operator== (const Time &a, const Time &b) noexcept
 Compare two time values for equality. More...
 
constexpr friend bool operator!= (const Time &a, const Time &b) noexcept
 Compare two time values for inequality. More...
 
constexpr friend bool operator< (const Time &a, const Time &b) noexcept
 Check if a time value is less than another time value. More...
 
constexpr friend bool operator<= (const Time &a, const Time &b) noexcept
 Check if a time value is less than or equal to another time value. More...
 
constexpr friend bool operator> (const Time &a, const Time &b) noexcept
 Check if a time value is greater than another time value. More...
 
constexpr friend bool operator>= (const Time &a, const Time &b) noexcept
 Check if a time value is greater than or equal to another time value. More...
 
constexpr friend Time operator+ (Time a) noexcept
 Get the additive identity of a time value. More...
 
constexpr friend Time operator- (Time a) noexcept
 Get the additive inverse of a time value. More...
 
constexpr friend Time operator+ (Time a, Time b) noexcept
 Add two time values. More...
 
constexpr friend Time operator- (Time a, Time b) noexcept
 Subtract a time value from another. More...
 

Detailed Description

template<typename T, typename Period = std::ratio<1>>
class donut::Time< T, Period >

Time duration value wrapper.

Template Parameters
Tscalar type to use as the representation for the underlying duration.
Periodperiod corresponding to the value 1 of the underlying time representation type, measured in seconds, expressed as a standard ratio type.

Member Typedef Documentation

◆ Duration

template<typename T , typename Period = std::ratio<1>>
using donut::Time< T, Period >::Duration = std::chrono::duration<T, Period>

Underlying duration type.

Constructor & Destructor Documentation

◆ Time() [1/8]

template<typename T , typename Period = std::ratio<1>>
constexpr donut::Time< T, Period >::Time ( )
constexprdefaultnoexcept

Construct a time value of 0.

◆ Time() [2/8]

template<typename T , typename Period = std::ratio<1>>
constexpr donut::Time< T, Period >::Time ( Duration  duration)
inlineconstexprnoexcept

Construct a time value from a duration.

Parameters
durationthe duration to initialize the time value to.

◆ Time() [3/8]

template<typename T , typename Period = std::ratio<1>>
constexpr donut::Time< T, Period >::Time ( std::chrono::seconds  seconds)
inlineconstexprnoexcept

Construct a time value from a number of seconds.

Parameters
secondsthe number of seconds to initialize the time value to.

◆ Time() [4/8]

template<typename T , typename Period = std::ratio<1>>
constexpr donut::Time< T, Period >::Time ( std::chrono::milliseconds  milliseconds)
inlineconstexprnoexcept

Construct a time value from a number of milliseconds.

Parameters
millisecondsthe number of milliseconds to initialize the time value to.

◆ Time() [5/8]

template<typename T , typename Period = std::ratio<1>>
constexpr donut::Time< T, Period >::Time ( std::chrono::microseconds  microseconds)
inlineconstexprnoexcept

Construct a time value from a number of microseconds.

Parameters
microsecondsthe number of microseconds to initialize the time value to.

◆ Time() [6/8]

template<typename T , typename Period = std::ratio<1>>
constexpr donut::Time< T, Period >::Time ( std::chrono::nanoseconds  nanoseconds)
inlineconstexprnoexcept

Construct a time value from a number of nanoseconds.

Parameters
nanosecondsthe number of microseconds to initialize the time value to.

◆ Time() [7/8]

template<typename T , typename Period = std::ratio<1>>
constexpr donut::Time< T, Period >::Time ( std::chrono::minutes  minutes)
inlineconstexprnoexcept

Construct a time value from a number of minutes.

Parameters
minutesthe number of minutes to initialize the time value to.

◆ Time() [8/8]

template<typename T , typename Period = std::ratio<1>>
constexpr donut::Time< T, Period >::Time ( std::chrono::hours  hours)
inlineconstexprnoexcept

Construct a time value from a number of hours.

Parameters
hoursthe number of hours to initialize the time value to.

Member Function Documentation

◆ operator Duration()

template<typename T , typename Period = std::ratio<1>>
constexpr donut::Time< T, Period >::operator Duration ( ) const
inlineconstexprnoexcept

Convert a time value to its underlying duration type.

Returns
the underlying duration value.

◆ operator T()

template<typename T , typename Period = std::ratio<1>>
constexpr donut::Time< T, Period >::operator T ( ) const
inlineconstexprnoexcept

Convert a time value to its underlying scalar type.

Returns
the scalar value of the underlying duration's representation.

◆ operator+=()

template<typename T , typename Period = std::ratio<1>>
constexpr Time& donut::Time< T, Period >::operator+= ( Time< T, Period >  deltaTime)
inlineconstexprnoexcept

Add time to this time value.

Parameters
deltaTimethe time delta to add to this value.
Returns
*this, for chaining.

◆ operator-=()

template<typename T , typename Period = std::ratio<1>>
constexpr Time& donut::Time< T, Period >::operator-= ( Time< T, Period >  deltaTime)
inlineconstexprnoexcept

Subtract time from this time value.

Parameters
deltaTimethe time delta to subtract from this value.
Returns
*this, for chaining.

◆ countDown()

template<typename T , typename Period = std::ratio<1>>
constexpr bool donut::Time< T, Period >::countDown ( Time< T, Period >  deltaTime,
Time< T, Period >  targetTime = Duration{} 
)
inlineconstexprnoexcept

Subtract time from this value and then check if it reached a given target time value.

Parameters
deltaTimethe time delta to subtract from this value.
targetTimetarget time.
Returns
true if the time value reached below or equal to the target time, false otherwise.
Note
The time value is clamped to the target time when the target is reached.
See also
countUp()
countDownLoop()

◆ countUp()

template<typename T , typename Period = std::ratio<1>>
constexpr bool donut::Time< T, Period >::countUp ( Time< T, Period >  deltaTime,
Time< T, Period >  targetTime 
)
inlineconstexprnoexcept

Add time to this value and then check if it reached a given target time value.

Parameters
deltaTimethe time delta to add to this value.
targetTimetarget time.
Returns
true if the time value reached above or equal to the target time, false otherwise.
Note
The time value is clamped to the target time when the target is reached.
See also
countDown()
countUpLoop()

◆ countDownLoop() [1/2]

template<typename T , typename Period = std::ratio<1>>
constexpr std::size_t donut::Time< T, Period >::countDownLoop ( Time< T, Period >  deltaTime,
Time< T, Period >  interval 
)
inlineconstexprnoexcept

Subtract time from this value and then check how many times it reached 0 while looping back to a given time interval.

Parameters
deltaTimethe time delta to subtract from this value.
intervalloop interval duration.
Returns
the number of times that the time value reached below or equal to 0 and looped back around. This may be any non-negative integer, including 0.
Note
An interval duration of 0 results in the number 1 being returned every time.
See also
countDown()
countUpLoop()
countDownLoopTrigger()

◆ countUpLoop() [1/2]

template<typename T , typename Period = std::ratio<1>>
constexpr std::size_t donut::Time< T, Period >::countUpLoop ( Time< T, Period >  deltaTime,
Time< T, Period >  interval 
)
inlineconstexprnoexcept

Add time to this value and then check how many times it reached a given time interval while looping back to 0.

Parameters
deltaTimethe time delta to add to this value.
intervalloop interval duration.
Returns
the number of times that the time value reached above or equal to the interval time and looped back around. This may be any non-negative integer, including 0.
Note
An interval duration of 0 results in the number 1 being returned every time.
See also
countUp()
countDownLoop()
countUpLoopTrigger()

◆ countDownLoop() [2/2]

template<typename T , typename Period = std::ratio<1>>
constexpr std::size_t donut::Time< T, Period >::countDownLoop ( Time< T, Period >  deltaTime,
Time< T, Period >  interval,
bool  active 
)
inlineconstexprnoexcept

Update a countdown loop with a boolean trigger that determines whether the loop is active or not.

An inactive loop will continue counting down to 0 but will not loop back around to the interval time and will always return 0.

Parameters
deltaTimethe time delta to subtract from this value.
intervalloop interval duration.
activewhether the loop is currently active or not.
Returns
the number of times that the time value reached below or equal to 0 while active. This may be any non-negative integer, including 0.
Note
An interval duration of 0 results in the number 1 being returned every time when the loop is active.
Remarks
This function can be used to simulate something like the trigger mechanism of a fully automatic firearm firing from a closed bolt, since it will fire once as soon as it is activated and then keep firing at a fixed cyclic rate until the trigger is released, at which point the mechanism will continue to cycle into the closed position even if the trigger is not held, where it will then stop without firing the next round, and be ready to fire immediately when the trigger is activated again. Reactivating the trigger before the mechanism has fully cycled does not make it fire more quickly.
See also
countDownLoop()
countUpLoopTrigger()

◆ countUpLoop() [2/2]

template<typename T , typename Period = std::ratio<1>>
constexpr std::size_t donut::Time< T, Period >::countUpLoop ( Time< T, Period >  deltaTime,
Time< T, Period >  interval,
bool  active 
)
inlineconstexprnoexcept

Update a countup loop with a boolean trigger that determines whether the loop is active or not.

An inactive loop will reset itself to 0 and will always return 0.

Parameters
deltaTimethe time delta to add to this value.
intervalloop interval duration.
activewhether the loop is currently active or not.
Returns
the number of times that the time value reached above or equal to the interval time while active. This may be any non-negative integer, including 0.
Note
An interval duration of 0 results in the number 1 being returned every time when the loop is active.
Remarks
This function can be used to simulate something like the trigger mechanism of a hypothetical "railgun" that needs to be fully charged before it can fire. Activating the trigger starts the charging process, which can be canceled at any time by deactivating the trigger, which immediately resets the charge back to 0. The mechanism fires as soon as it is fully charged and then immediately starts charging the next round if the trigger is still held.
See also
countUpLoop()
countDownLoopTrigger()

Friends And Related Function Documentation

◆ operator==

template<typename T , typename Period = std::ratio<1>>
constexpr friend bool operator== ( const Time< T, Period > &  a,
const Time< T, Period > &  b 
)
friend

Compare two time values for equality.

Parameters
afirst time value.
bsecond time value.
Returns
true if the first and second time values are equal, false otherwise.

◆ operator!=

template<typename T , typename Period = std::ratio<1>>
constexpr friend bool operator!= ( const Time< T, Period > &  a,
const Time< T, Period > &  b 
)
friend

Compare two time values for inequality.

Parameters
afirst time value.
bsecond time value.
Returns
true if the first and second time values are not equal, false otherwise.

◆ operator<

template<typename T , typename Period = std::ratio<1>>
constexpr friend bool operator< ( const Time< T, Period > &  a,
const Time< T, Period > &  b 
)
friend

Check if a time value is less than another time value.

Parameters
afirst time value.
bsecond time value.
Returns
true if the first time value is less than the second time value, false otherwise.

◆ operator<=

template<typename T , typename Period = std::ratio<1>>
constexpr friend bool operator<= ( const Time< T, Period > &  a,
const Time< T, Period > &  b 
)
friend

Check if a time value is less than or equal to another time value.

Parameters
afirst time value.
bsecond time value.
Returns
true if the first time value is less than or equal to the second time value, false otherwise.

◆ operator>

template<typename T , typename Period = std::ratio<1>>
constexpr friend bool operator> ( const Time< T, Period > &  a,
const Time< T, Period > &  b 
)
friend

Check if a time value is greater than another time value.

Parameters
afirst time value.
bsecond time value.
Returns
true if the first time value is greater than the second time value, false otherwise.

◆ operator>=

template<typename T , typename Period = std::ratio<1>>
constexpr friend bool operator>= ( const Time< T, Period > &  a,
const Time< T, Period > &  b 
)
friend

Check if a time value is greater than or equal to another time value.

Parameters
afirst time value.
bsecond time value.
Returns
true if the first time value is greater than or equal to the second time value, false otherwise.

◆ operator+ [1/2]

template<typename T , typename Period = std::ratio<1>>
constexpr friend Time operator+ ( Time< T, Period >  a)
friend

Get the additive identity of a time value.

Parameters
atime value.
Returns
plus a.

◆ operator- [1/2]

template<typename T , typename Period = std::ratio<1>>
constexpr friend Time operator- ( Time< T, Period >  a)
friend

Get the additive inverse of a time value.

Parameters
atime value.
Returns
minus a.

◆ operator+ [2/2]

template<typename T , typename Period = std::ratio<1>>
constexpr friend Time operator+ ( Time< T, Period >  a,
Time< T, Period >  b 
)
friend

Add two time values.

Parameters
afirst time value.
bsecond time value.
Returns
the sum of the first and second time values.

◆ operator- [2/2]

template<typename T , typename Period = std::ratio<1>>
constexpr friend Time operator- ( Time< T, Period >  a,
Time< T, Period >  b 
)
friend

Subtract a time value from another.

Parameters
afirst time value.
bsecond time value.
Returns
the difference between the first and second time values.

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