Gamedev Framework (gf)  0.5.0
A C++11 framework for 2D games
Public Member Functions | Static Public Member Functions | Static Public Attributes | Related Functions | List of all members
gf::Time Class Reference

Represents a time value. More...

#include <gf/Time.h>

Public Member Functions

constexpr Time ()
 Default constructor. More...
 
constexpr Time (std::chrono::steady_clock::duration duration)
 Constructor with a duration. More...
 
constexpr float asSeconds () const
 Return the time value as a number of seconds. More...
 
constexpr int32_t asMilliseconds () const
 Return the time value as a number of milliseconds. More...
 
constexpr int64_t asMicroseconds () const
 Return the time value as a number of microseconds. More...
 
constexpr std::chrono::steady_clock::duration asDuration () const
 Return the time value as a duration. More...
 
TimeaddTo (Time other)
 Add another time. More...
 
TimesubTo (Time other)
 Substract another time. More...
 

Static Public Member Functions

static constexpr Time zero ()
 

Static Public Attributes

static const Time Zero
 

Related Functions

(Note that these are not member functions.)

constexpr Time seconds (float amount)
 Construct a time value from a number of seconds. More...
 
constexpr Time milliseconds (int32_t amount)
 Construct a time value from a number of milliseconds. More...
 
constexpr Time microseconds (int64_t amount)
 Construct a time value from a number of microseconds. More...
 
constexpr bool operator== (Time rhs, Time lhs)
 Equality operator. More...
 
constexpr bool operator!= (Time rhs, Time lhs)
 Inequality operator. More...
 
constexpr bool operator< (Time rhs, Time lhs)
 Lesser than operator. More...
 
constexpr bool operator> (Time rhs, Time lhs)
 Greater than operator. More...
 
constexpr bool operator<= (Time rhs, Time lhs)
 Lesser or equal operator. More...
 
constexpr bool operator>= (Time rhs, Time lhs)
 Greater or equal than operator. More...
 
constexpr Time operator+ (Time rhs, Time lhs)
 Addition of two times. More...
 
Time operator+= (Time &rhs, Time lhs)
 Addition and assignement. More...
 
constexpr Time operator- (Time rhs, Time lhs)
 Substraction of two times. More...
 
Time operator-= (Time &rhs, Time lhs)
 Substraction and assignment. More...
 

Detailed Description

Represents a time value.

gf::Time encapsulates a time value in a flexible way. It allows to define a time value either as a number of seconds, milliseconds or microseconds. It also works the other way round: you can read a time value as either a number of seconds, milliseconds or microseconds.

By using such a flexible interface, the API doesn't impose any fixed type or resolution for time values, and let the user choose its own favorite representation.

Since they represent a time span and not an absolute time value, times can also be negative.

gf::Time is a thin wrapper around C++11 std::chrono::steady_clock.

Usage example:

gf::Time t1 = gf::seconds(0.1f);
int32_t milli = t1.asMilliseconds(); // 100
gf::Time t2 = gf::milliseconds(30);
int64_t micro = t2.asMicroseconds(); // 30000
gf::Time t3 = gf::microseconds(-800000);
float sec = t3.asSeconds(); // -0.8
See also
gf::Clock

Constructor & Destructor Documentation

◆ Time() [1/2]

constexpr gf::Time::Time ( )
inline

Default constructor.

Sets the time value to zero.

◆ Time() [2/2]

constexpr gf::Time::Time ( std::chrono::steady_clock::duration  duration)
inlineexplicit

Constructor with a duration.

Parameters
durationA duration expressed with a std::chrono type

Member Function Documentation

◆ addTo()

Time& gf::Time::addTo ( Time  other)
inline

Add another time.

Parameters
otherThe time to add to the current time
Returns
The current time

◆ asDuration()

constexpr std::chrono::steady_clock::duration gf::Time::asDuration ( ) const
inline

Return the time value as a duration.

Returns
Time as a duration (std::chrono type)

◆ asMicroseconds()

constexpr int64_t gf::Time::asMicroseconds ( ) const
inline

Return the time value as a number of microseconds.

Returns
Time in microseconds
See also
asSeconds(), asMilliseconds()

◆ asMilliseconds()

constexpr int32_t gf::Time::asMilliseconds ( ) const
inline

Return the time value as a number of milliseconds.

Returns
Time in milliseconds
See also
asSeconds(), asMicroseconds()

◆ asSeconds()

constexpr float gf::Time::asSeconds ( ) const
inline

Return the time value as a number of seconds.

Returns
Time in seconds
See also
asMilliseconds(), asMicroseconds()

◆ subTo()

Time& gf::Time::subTo ( Time  other)
inline

Substract another time.

Parameters
otherThe time to substract to the current time
Returns
The current time

◆ zero()

static constexpr Time gf::Time::zero ( )
inlinestatic

Friends And Related Function Documentation

◆ microseconds()

constexpr Time microseconds ( int64_t  amount)
related

Construct a time value from a number of microseconds.

Parameters
amountNumber of microseconds
Returns
Time value constructed from the amount of microseconds
See also
seconds(), milliseconds()

◆ milliseconds()

constexpr Time milliseconds ( int32_t  amount)
related

Construct a time value from a number of milliseconds.

Parameters
amountNumber of milliseconds
Returns
Time value constructed from the amount of milliseconds
See also
seconds(), microseconds()

◆ operator!=()

constexpr bool operator!= ( Time  rhs,
Time  lhs 
)
related

Inequality operator.

Parameters
rhsFirst time
lhsSecond time
Returns
True if the first time and the second time are different

◆ operator+()

constexpr Time operator+ ( Time  rhs,
Time  lhs 
)
related

Addition of two times.

Parameters
rhsFirst time
lhsSecond time
Returns
The sum of two times

◆ operator+=()

Time operator+= ( Time rhs,
Time  lhs 
)
related

Addition and assignement.

Parameters
rhsFirst time
lhsSecond time
Returns
The sum of two times

◆ operator-()

constexpr Time operator- ( Time  rhs,
Time  lhs 
)
related

Substraction of two times.

Parameters
rhsFirst time
lhsSecond time
Returns
The difference of two times

◆ operator-=()

Time operator-= ( Time rhs,
Time  lhs 
)
related

Substraction and assignment.

Parameters
rhsFirst time
lhsSecond time
Returns
The difference of two times

◆ operator<()

constexpr bool operator< ( Time  rhs,
Time  lhs 
)
related

Lesser than operator.

Parameters
rhsFirst time
lhsSecond time
Returns
True if the first time is lesser than the second time

◆ operator<=()

constexpr bool operator<= ( Time  rhs,
Time  lhs 
)
related

Lesser or equal operator.

Parameters
rhsFirst time
lhsSecond time
Returns
True if the first time is lesser or equal than the second time

◆ operator==()

constexpr bool operator== ( Time  rhs,
Time  lhs 
)
related

Equality operator.

Parameters
rhsFirst time
lhsSecond time
Returns
True if the first time and the second time are the same

◆ operator>()

constexpr bool operator> ( Time  rhs,
Time  lhs 
)
related

Greater than operator.

Parameters
rhsFirst time
lhsSecond time
Returns
True if the first time is greater than the second time

◆ operator>=()

constexpr bool operator>= ( Time  rhs,
Time  lhs 
)
related

Greater or equal than operator.

Parameters
rhsFirst time
lhsSecond time
Returns
True if the first time is greater or equal than the second time

◆ seconds()

constexpr Time seconds ( float  amount)
related

Construct a time value from a number of seconds.

Parameters
amountNumber of seconds
Returns
Time value constructed from the amount of seconds
See also
milliseconds(), microseconds()

Member Data Documentation

◆ Zero

const Time gf::Time::Zero
static