SDL Utility
Time.hpp
Go to the documentation of this file.
1 /*****************************************************************/
10 #pragma once
11 
12 #include <chrono>
13 
14 #include "Util.hpp"
15 
17 
18 typedef Int64 TimeRep;
19 typedef std::nano TimePeriod;
20 
24 class Time {
25 public:
29  Time();
30 
39  template<typename Rep = Int64, typename Period = std::ratio<1>>
40  Rep AsValue() const;
41 
45  float AsSeconds() const;
46 
51 
56 
57 
66  template<typename Rep = Int64, typename Period = std::ratio<1>>
67  std::chrono::duration<Rep, Period> AsChrono() const;
68 
72  std::chrono::seconds AsChronoSeconds() const;
73 
77  std::chrono::milliseconds AsChronoMilliseconds() const;
78 
82  std::chrono::microseconds AsChronoMicroseconds() const;
83 
84 
93  template<typename Rep = Int64, typename Period = std::ratio<1>>
94  static Time Create(const Rep& duration);
95 
104  template<typename Rep = Int64, typename Period = std::ratio<1>>
105  static Time Create(const std::chrono::duration<Rep, Period>& duration);
106 
112  static Time Now();
113 
121  friend Time Seconds(float seconds);
122 
130  friend Time Milliseconds(Int32 milliseconds);
131 
139  friend Time Microseconds(Int64 microseconds);
140 
141 
150  friend bool operator==(const Time& left, const Time& right);
151 
160  friend bool operator!=(const Time& left, const Time& right);
161 
170  friend bool operator<(const Time& left, const Time& right);
171 
180  friend bool operator>(const Time& left, const Time& right);
181 
190  friend bool operator<=(const Time& left, const Time& right);
191 
200  friend bool operator>=(const Time& left, const Time& right);
201 
202 
210  friend Time operator-(const Time& right);
211 
212 
221  friend Time operator+(const Time& left, const Time& right);
222 
231  friend Time& operator+=(Time& left, const Time& right);
232 
233 
242  friend Time operator-(const Time& left, const Time& right);
243 
252  friend Time& operator-=(Time& left, const Time& right);
253 
254 
263  friend Time operator*(const Time& left, float right);
264 
273  friend Time operator*(const Time& left, Int64 right);
274 
283  friend Time operator*(float left, const Time& right);
284 
293  friend Time operator*(Int64 left, const Time& right);
294 
303  friend Time& operator*=(Time& left, float right);
304 
313  friend Time& operator*=(Time& left, Int64 right);
314 
315 
324  friend Time operator/(const Time& left, float right);
325 
334  friend Time operator/(const Time& left, Int64 right);
335 
344  friend Time& operator/=(Time& left, float right);
345 
354  friend Time& operator/=(Time& left, Int64 right);
355 
364  friend float operator/(const Time& left, const Time& right);
365 
374  friend Time operator%(const Time& left, const Time& right);
375 
384  friend Time& operator%=(Time& left, const Time& right);
385 
386 private:
387  std::chrono::duration<TimeRep, TimePeriod> microseconds;
388 };
389 
390 
391 template<typename Rep, typename Period>
392 inline Rep Time::AsValue() const
393 {
394  return std::chrono::duration_cast<std::chrono::duration<Rep, Period>>(microseconds).count();
395 }
396 
397 template<typename Rep, typename Period>
398 inline std::chrono::duration<Rep, Period> Time::AsChrono() const
399 {
400  return std::chrono::duration_cast<std::chrono::duration<Rep, Period>>(microseconds);
401 }
402 
403 template<typename Rep, typename Period>
404 inline Time Time::Create(const Rep& duration)
405 {
406  Time newTime;
407  newTime.microseconds = std::chrono::duration_cast<std::chrono::duration<TimeRep, TimePeriod>>(std::chrono::duration<Rep, Period>(duration));
408  return newTime;
409 }
410 
411 template<typename Rep, typename Period>
412 inline Time Time::Create(const std::chrono::duration<Rep, Period>& duration)
413 {
414  Time newTime;
415  newTime.microseconds = std::chrono::duration_cast<std::chrono::duration<TimeRep, TimePeriod>>(duration);
416  return newTime;
417 }
418 
419 inline Time Time::Now()
420 {
421  return Create(std::chrono::steady_clock::now().time_since_epoch());
422 }
423 
424 SDLU_END
std::nano TimePeriod
Definition: Time.hpp:19
SDLU_BEGIN typedef Int64 TimeRep
Definition: Time.hpp:18
Basic utility macros, typedefs...
int32_t Int32
Definition: Util.hpp:24
int64_t Int64
Definition: Util.hpp:27
#define SDLU_BEGIN
Definition: Util.hpp:32
This class wraps std::chrono::duration and defines some conversions for commonly used times.
Definition: Time.hpp:24
static Time Create(const Rep &duration)
Stores the given scalar as a time, using the given format.
Definition: Time.hpp:404
friend Time operator-(const Time &left, const Time &right)
Overload of - operator to subtract two Times.
friend bool operator!=(const Time &left, const Time &right)
Overload of != operator comparing two Times.
friend Time & operator/=(Time &left, Int64 right)
Overload of /= operator to divide a Time by a scalar.
float AsSeconds() const
Returns the stored time in seconds.
friend Time operator*(const Time &left, Int64 right)
Overload of * operator to multiply a Time with a scalar.
friend Time operator+(const Time &left, const Time &right)
Overload of + operator to add two Times.
Rep AsValue() const
Returns the stored time in the specified format as a scalar.
Definition: Time.hpp:392
std::chrono::milliseconds AsChronoMilliseconds() const
Returns the stored time in std::chrono::milliseconds.
Time()
Default constructor. Constructs a time with value 0.
friend Time & operator/=(Time &left, float right)
Overload of /= operator to divide a Time by a scalar.
Int32 AsMilliseconds() const
Returns the stored time in milliseconds.
Int64 AsMicroseconds() const
Returns the stored time in microseconds.
friend Time operator%(const Time &left, const Time &right)
Overload of % operator to calculate the modulo of a Time.
friend Time Milliseconds(Int32 milliseconds)
Creates a Time object from a given number of milliseconds.
friend bool operator==(const Time &left, const Time &right)
Overload of == operator comparing two Times.
std::chrono::seconds AsChronoSeconds() const
Returns the stored time in std::chrono::seconds.
friend Time & operator*=(Time &left, Int64 right)
Overload of *= operator to multiply a Time with a scalar.
static Time Now()
Creates a Time object storing the current point in time.
Definition: Time.hpp:419
std::chrono::microseconds AsChronoMicroseconds() const
Returns the stored time in std::chrono::microseconds.
friend Time operator*(float left, const Time &right)
Overload of * operator to multiply a Time with a scalar.
friend Time operator*(Int64 left, const Time &right)
Overload of * operator to multiply a Time with a scalar.
friend Time operator*(const Time &left, float right)
Overload of * operator to multiply a Time with a scalar.
friend Time Seconds(float seconds)
Creates a Time object from a given number of seconds.
std::chrono::duration< Rep, Period > AsChrono() const
Returns the stored time in the specified format as a std::duration.
Definition: Time.hpp:398
friend Time Microseconds(Int64 microseconds)
Creates a Time object from a given number of microseconds.
friend Time operator/(const Time &left, float right)
Overload of / operator to divide a Time by a scalar.
friend float operator/(const Time &left, const Time &right)
Overload of / operator to divide two Times.
friend Time & operator+=(Time &left, const Time &right)
Overload of += operator to add two Times.
friend bool operator<(const Time &left, const Time &right)
Overload of < operator comparing two Times.
friend Time & operator-=(Time &left, const Time &right)
Overload of -= operator to subtract two Times.
friend Time operator-(const Time &right)
Overload of the unary - operator.
friend Time & operator*=(Time &left, float right)
Overload of *= operator to multiply a Time with a scalar.
friend Time operator/(const Time &left, Int64 right)
Overload of / operator to divide a Time by a scalar.
friend Time & operator%=(Time &left, const Time &right)
Overload of % operator to calculate the modulo of a Time.
friend bool operator>=(const Time &left, const Time &right)
Overload of < operator comparing two Times.
friend bool operator<=(const Time &left, const Time &right)
Overload of <= operator comparing two Times.
friend bool operator>(const Time &left, const Time &right)
Overload of > operator comparing two Times.