Gamedev Framework (gf)  0.8.0
A C++14 framework for 2D games
Time.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2018 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 
89  Time(const Time&) = default;
90 
94  Time& operator=(const Time&) = default;
95 
101  constexpr explicit Time(std::chrono::steady_clock::duration duration)
102  : m_duration(duration)
103  {
104 
105  }
106 
114  constexpr float asSeconds() const {
115  return std::chrono::duration_cast<std::chrono::duration<float>>(m_duration).count();
116  }
117 
125  constexpr int32_t asMilliseconds() const {
126  return std::chrono::duration_cast<std::chrono::duration<int32_t, std::milli>>(m_duration).count();
127  }
128 
136  constexpr int64_t asMicroseconds() const {
137  return std::chrono::duration_cast<std::chrono::duration<int64_t, std::micro>>(m_duration).count();
138  }
139 
145  constexpr std::chrono::steady_clock::duration asDuration() const {
146  return m_duration;
147  }
148 
155  Time& addTo(Time other) {
156  m_duration += other.m_duration;
157  return *this;
158  }
159 
166  Time& subTo(Time other) {
167  m_duration -= other.m_duration;
168  return *this;
169  }
170 
177  static const Time Zero;
178 
185  static constexpr Time zero() {
186  return Time();
187  }
188 
189  private:
190  std::chrono::steady_clock::duration m_duration;
191  };
192 
203  constexpr
204  Time seconds(float amount) {
205  return Time(std::chrono::duration_cast<std::chrono::steady_clock::duration>(std::chrono::duration<float>(amount)));
206  }
207 
218  constexpr
219  Time milliseconds(int32_t amount) {
220  return Time(std::chrono::duration_cast<std::chrono::steady_clock::duration>(std::chrono::duration<int32_t, std::milli>(amount)));
221  }
222 
233  constexpr
234  Time microseconds(int64_t amount) {
235  return Time(std::chrono::duration_cast<std::chrono::steady_clock::duration>(std::chrono::duration<int64_t, std::micro>(amount)));
236  }
237 
246  constexpr
247  bool operator==(Time rhs, Time lhs) {
248  return rhs.asDuration() == lhs.asDuration();
249  }
250 
259  constexpr
260  bool operator!=(Time rhs, Time lhs) {
261  return rhs.asDuration() != lhs.asDuration();
262  }
263 
272  constexpr
273  bool operator<(Time rhs, Time lhs) {
274  return rhs.asDuration() < lhs.asDuration();
275  }
276 
285  constexpr
286  bool operator>(Time rhs, Time lhs) {
287  return rhs.asDuration() > lhs.asDuration();
288  }
289 
298  constexpr
299  bool operator<=(Time rhs, Time lhs) {
300  return rhs.asDuration() <= lhs.asDuration();
301  }
302 
311  constexpr
312  bool operator>=(Time rhs, Time lhs) {
313  return rhs.asDuration() >= lhs.asDuration();
314  }
315 
324  constexpr
325  Time operator+(Time rhs, Time lhs) {
326  return Time(rhs.asDuration() + lhs.asDuration());
327  }
328 
337  inline
338  Time operator+=(Time& rhs, Time lhs) {
339  return rhs.addTo(lhs);
340  }
341 
350  constexpr
351  Time operator-(Time rhs, Time lhs) {
352  return Time(rhs.asDuration() - lhs.asDuration());
353  }
354 
363  inline
364  Time operator-=(Time& rhs, Time lhs) {
365  return rhs.subTo(lhs);
366  }
367 
368 #ifndef DOXYGEN_SHOULD_SKIP_THIS
369 }
370 #endif
371 }
372 
373 #endif // GF_TIME_H
Time & subTo(Time other)
Substract another time.
Definition: Time.h:166
constexpr Time microseconds(int64_t amount)
Construct a time value from a number of microseconds.
Definition: Time.h:234
constexpr int32_t asMilliseconds() const
Return the time value as a number of milliseconds.
Definition: Time.h:125
static constexpr Time zero()
Get a time of zero.
Definition: Time.h:185
constexpr Time milliseconds(int32_t amount)
Construct a time value from a number of milliseconds.
Definition: Time.h:219
STL namespace.
constexpr float asSeconds() const
Return the time value as a number of seconds.
Definition: Time.h:114
constexpr Time()
Default constructor.
Definition: Time.h:80
constexpr bool operator<=(Time rhs, Time lhs)
Lesser or equal operator.
Definition: Time.h:299
Time operator-=(Time &rhs, Time lhs)
Substraction and assignment.
Definition: Time.h:364
constexpr bool operator<(Time rhs, Time lhs)
Lesser than operator.
Definition: Time.h:273
constexpr bool operator>(Time rhs, Time lhs)
Greater than operator.
Definition: Time.h:286
constexpr Time operator-(Time rhs, Time lhs)
Substraction of two times.
Definition: Time.h:351
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:145
constexpr int64_t asMicroseconds() const
Return the time value as a number of microseconds.
Definition: Time.h:136
The namespace for gf classes.
Definition: Action.h:34
constexpr Time operator+(Time rhs, Time lhs)
Addition of two times.
Definition: Time.h:325
constexpr bool operator==(Time rhs, Time lhs)
Equality operator.
Definition: Time.h:247
constexpr bool operator!=(Time rhs, Time lhs)
Inequality operator.
Definition: Time.h:260
constexpr bool operator>=(Time rhs, Time lhs)
Greater or equal than operator.
Definition: Time.h:312
static const Time Zero
A time of zero as a variable.
Definition: Time.h:177
Time operator+=(Time &rhs, Time lhs)
Addition and assignement.
Definition: Time.h:338
Time & addTo(Time other)
Add another time.
Definition: Time.h:155
constexpr Time seconds(float amount)
Construct a time value from a number of seconds.
Definition: Time.h:204
constexpr Time(std::chrono::steady_clock::duration duration)
Constructor with a duration.
Definition: Time.h:101