Gamedev Framework (gf)  0.5.0
A C++11 framework for 2D games
Time.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2017 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  *
21  * Part of this file comes from SFML, with the same license:
22  * Copyright (C) 2007-2015 Laurent Gomila (laurent@sfml-dev.org)
23  */
24 #ifndef GF_TIME_H
25 #define GF_TIME_H
26 
27 #include <cstdint>
28 #include <chrono>
29 #include <ratio>
30 
31 #include "Portability.h"
32 
33 namespace gf {
34 #ifndef DOXYGEN_SHOULD_SKIP_THIS
35 inline namespace v1 {
36 #endif
37 
73  class GF_API Time {
74  public:
80  constexpr Time()
81  : m_duration(std::chrono::steady_clock::duration::zero())
82  {
83 
84  }
85 
91  constexpr explicit Time(std::chrono::steady_clock::duration duration)
92  : m_duration(duration)
93  {
94 
95  }
96 
104  constexpr float asSeconds() const {
105  return std::chrono::duration_cast<std::chrono::duration<float>>(m_duration).count();
106  }
107 
115  constexpr int32_t asMilliseconds() const {
116  return std::chrono::duration_cast<std::chrono::duration<int32_t, std::milli>>(m_duration).count();
117  }
118 
126  constexpr int64_t asMicroseconds() const {
127  return std::chrono::duration_cast<std::chrono::duration<int64_t, std::micro>>(m_duration).count();
128  }
129 
135  constexpr std::chrono::steady_clock::duration asDuration() const {
136  return m_duration;
137  }
138 
145  Time& addTo(Time other) {
146  m_duration += other.m_duration;
147  return *this;
148  }
149 
156  Time& subTo(Time other) {
157  m_duration -= other.m_duration;
158  return *this;
159  }
160 
161  static const Time Zero;
162 
163  static constexpr Time zero() {
164  return Time();
165  }
166 
167  private:
168  std::chrono::steady_clock::duration m_duration;
169  };
170 
181  constexpr
182  Time seconds(float amount) {
183  return Time(std::chrono::duration_cast<std::chrono::steady_clock::duration>(std::chrono::duration<float>(amount)));
184  }
185 
196  constexpr
197  Time milliseconds(int32_t amount) {
198  return Time(std::chrono::duration_cast<std::chrono::steady_clock::duration>(std::chrono::duration<int32_t, std::milli>(amount)));
199  }
200 
211  constexpr
212  Time microseconds(int64_t amount) {
213  return Time(std::chrono::duration_cast<std::chrono::steady_clock::duration>(std::chrono::duration<int64_t, std::micro>(amount)));
214  }
215 
224  constexpr
225  bool operator==(Time rhs, Time lhs) {
226  return rhs.asDuration() == lhs.asDuration();
227  }
228 
237  constexpr
238  bool operator!=(Time rhs, Time lhs) {
239  return rhs.asDuration() != lhs.asDuration();
240  }
241 
250  constexpr
251  bool operator<(Time rhs, Time lhs) {
252  return rhs.asDuration() < lhs.asDuration();
253  }
254 
263  constexpr
264  bool operator>(Time rhs, Time lhs) {
265  return rhs.asDuration() > lhs.asDuration();
266  }
267 
276  constexpr
277  bool operator<=(Time rhs, Time lhs) {
278  return rhs.asDuration() <= lhs.asDuration();
279  }
280 
289  constexpr
290  bool operator>=(Time rhs, Time lhs) {
291  return rhs.asDuration() >= lhs.asDuration();
292  }
293 
302  constexpr
303  Time operator+(Time rhs, Time lhs) {
304  return Time(rhs.asDuration() + lhs.asDuration());
305  }
306 
315  inline
316  Time operator+=(Time& rhs, Time lhs) {
317  return rhs.addTo(lhs);
318  }
319 
328  constexpr
329  Time operator-(Time rhs, Time lhs) {
330  return Time(rhs.asDuration() - lhs.asDuration());
331  }
332 
341  inline
342  Time operator-=(Time& rhs, Time lhs) {
343  return rhs.subTo(lhs);
344  }
345 
346 #ifndef DOXYGEN_SHOULD_SKIP_THIS
347 }
348 #endif
349 }
350 
351 #endif // GF_TIME_H
Time & subTo(Time other)
Substract another time.
Definition: Time.h:156
constexpr Time microseconds(int64_t amount)
Construct a time value from a number of microseconds.
Definition: Time.h:212
constexpr int32_t asMilliseconds() const
Return the time value as a number of milliseconds.
Definition: Time.h:115
static constexpr Time zero()
Definition: Time.h:163
constexpr Time milliseconds(int32_t amount)
Construct a time value from a number of milliseconds.
Definition: Time.h:197
STL namespace.
constexpr float asSeconds() const
Return the time value as a number of seconds.
Definition: Time.h:104
constexpr Time()
Default constructor.
Definition: Time.h:80
constexpr bool operator<=(Time rhs, Time lhs)
Lesser or equal operator.
Definition: Time.h:277
Time operator-=(Time &rhs, Time lhs)
Substraction and assignment.
Definition: Time.h:342
constexpr bool operator<(Time rhs, Time lhs)
Lesser than operator.
Definition: Time.h:251
constexpr bool operator>(Time rhs, Time lhs)
Greater than operator.
Definition: Time.h:264
constexpr Time operator-(Time rhs, Time lhs)
Substraction of two times.
Definition: Time.h:329
Represents a time value.
Definition: Time.h:73
constexpr std::chrono::steady_clock::duration asDuration() const
Return the time value as a duration.
Definition: Time.h:135
constexpr int64_t asMicroseconds() const
Return the time value as a number of microseconds.
Definition: Time.h:126
The namespace for gf classes.
Definition: Action.h:34
constexpr Time operator+(Time rhs, Time lhs)
Addition of two times.
Definition: Time.h:303
constexpr bool operator==(Time rhs, Time lhs)
Equality operator.
Definition: Time.h:225
constexpr bool operator!=(Time rhs, Time lhs)
Inequality operator.
Definition: Time.h:238
constexpr bool operator>=(Time rhs, Time lhs)
Greater or equal than operator.
Definition: Time.h:290
static const Time Zero
Definition: Time.h:161
Time operator+=(Time &rhs, Time lhs)
Addition and assignement.
Definition: Time.h:316
Time & addTo(Time other)
Add another time.
Definition: Time.h:145
constexpr Time seconds(float amount)
Construct a time value from a number of seconds.
Definition: Time.h:182
constexpr Time(std::chrono::steady_clock::duration duration)
Constructor with a duration.
Definition: Time.h:91