Gamedev Framework (gf)  0.11.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 #include "SerializationFwd.h"
33 
34 namespace gf {
35 #ifndef DOXYGEN_SHOULD_SKIP_THIS
36 inline namespace v1 {
37 #endif
38 
74  class GF_API Time {
75  public:
81  constexpr Time()
82  : m_duration(std::chrono::steady_clock::duration::zero())
83  {
84 
85  }
86 
90  Time(const Time&) = default;
91 
95  Time& operator=(const Time&) = default;
96 
102  constexpr explicit Time(std::chrono::steady_clock::duration duration)
103  : m_duration(duration)
104  {
105 
106  }
107 
115  constexpr float asSeconds() const {
116  return std::chrono::duration_cast<std::chrono::duration<float>>(m_duration).count();
117  }
118 
126  constexpr int32_t asMilliseconds() const {
127  return std::chrono::duration_cast<std::chrono::duration<int32_t, std::milli>>(m_duration).count();
128  }
129 
137  constexpr int64_t asMicroseconds() const {
138  return std::chrono::duration_cast<std::chrono::duration<int64_t, std::micro>>(m_duration).count();
139  }
140 
146  constexpr std::chrono::steady_clock::duration asDuration() const {
147  return m_duration;
148  }
149 
156  Time& addTo(Time other) {
157  m_duration += other.m_duration;
158  return *this;
159  }
160 
167  Time& subTo(Time other) {
168  m_duration -= other.m_duration;
169  return *this;
170  }
171 
178  static const Time Zero;
179 
186  static constexpr Time zero() {
187  return Time();
188  }
189 
190  private:
191  std::chrono::steady_clock::duration m_duration;
192  };
193 
204  constexpr
205  Time seconds(float amount) {
206  return Time(std::chrono::duration_cast<std::chrono::steady_clock::duration>(std::chrono::duration<float>(amount)));
207  }
208 
219  constexpr
220  Time milliseconds(int32_t amount) {
221  return Time(std::chrono::duration_cast<std::chrono::steady_clock::duration>(std::chrono::duration<int32_t, std::milli>(amount)));
222  }
223 
234  constexpr
235  Time microseconds(int64_t amount) {
236  return Time(std::chrono::duration_cast<std::chrono::steady_clock::duration>(std::chrono::duration<int64_t, std::micro>(amount)));
237  }
238 
247  constexpr
248  bool operator==(Time rhs, Time lhs) {
249  return rhs.asDuration() == lhs.asDuration();
250  }
251 
260  constexpr
261  bool operator!=(Time rhs, Time lhs) {
262  return rhs.asDuration() != lhs.asDuration();
263  }
264 
273  constexpr
274  bool operator<(Time rhs, Time lhs) {
275  return rhs.asDuration() < lhs.asDuration();
276  }
277 
286  constexpr
287  bool operator>(Time rhs, Time lhs) {
288  return rhs.asDuration() > lhs.asDuration();
289  }
290 
299  constexpr
300  bool operator<=(Time rhs, Time lhs) {
301  return rhs.asDuration() <= lhs.asDuration();
302  }
303 
312  constexpr
313  bool operator>=(Time rhs, Time lhs) {
314  return rhs.asDuration() >= lhs.asDuration();
315  }
316 
325  constexpr
326  Time operator+(Time rhs, Time lhs) {
327  return Time(rhs.asDuration() + lhs.asDuration());
328  }
329 
338  inline
339  Time operator+=(Time& rhs, Time lhs) {
340  return rhs.addTo(lhs);
341  }
342 
351  constexpr
352  Time operator-(Time rhs, Time lhs) {
353  return Time(rhs.asDuration() - lhs.asDuration());
354  }
355 
364  inline
365  Time operator-=(Time& rhs, Time lhs) {
366  return rhs.subTo(lhs);
367  }
368 
373  GF_API Serializer& operator|(Serializer& ar, Time time);
374 
379  GF_API Deserializer& operator|(Deserializer& ar, Time& time);
380 
381 #ifndef DOXYGEN_SHOULD_SKIP_THIS
382 }
383 #endif
384 }
385 
386 #endif // GF_TIME_H
Time & subTo(Time other)
Substract another time.
Definition: Time.h:167
constexpr Time microseconds(int64_t amount)
Construct a time value from a number of microseconds.
Definition: Time.h:235
A deserializer from a binary file.
Definition: Serialization.h:153
constexpr int32_t asMilliseconds() const
Return the time value as a number of milliseconds.
Definition: Time.h:126
static constexpr Time zero()
Get a time of zero.
Definition: Time.h:186
constexpr Time milliseconds(int32_t amount)
Construct a time value from a number of milliseconds.
Definition: Time.h:220
STL namespace.
constexpr float asSeconds() const
Return the time value as a number of seconds.
Definition: Time.h:115
constexpr Time()
Default constructor.
Definition: Time.h:81
constexpr bool operator<=(Time rhs, Time lhs)
Lesser or equal operator.
Definition: Time.h:300
Time operator-=(Time &rhs, Time lhs)
Substraction and assignment.
Definition: Time.h:365
constexpr bool operator<(Time rhs, Time lhs)
Lesser than operator.
Definition: Time.h:274
constexpr bool operator>(Time rhs, Time lhs)
Greater than operator.
Definition: Time.h:287
constexpr Time operator-(Time rhs, Time lhs)
Substraction of two times.
Definition: Time.h:352
Represents a time value.
Definition: Time.h:74
constexpr std::chrono::steady_clock::duration asDuration() const
Return the time value as a duration.
Definition: Time.h:146
constexpr int64_t asMicroseconds() const
Return the time value as a number of microseconds.
Definition: Time.h:137
A serializer to a binary file.
Definition: Serialization.h:45
The namespace for gf classes.
Definition: Action.h:35
constexpr Time operator+(Time rhs, Time lhs)
Addition of two times.
Definition: Time.h:326
constexpr bool operator==(Time rhs, Time lhs)
Equality operator.
Definition: Time.h:248
constexpr bool operator!=(Time rhs, Time lhs)
Inequality operator.
Definition: Time.h:261
Deserializer & operator|(Deserializer &ar, T &data)
Definition: SerializationOps.h:315
constexpr bool operator>=(Time rhs, Time lhs)
Greater or equal than operator.
Definition: Time.h:313
static const Time Zero
A time of zero as a variable.
Definition: Time.h:178
Time operator+=(Time &rhs, Time lhs)
Addition and assignement.
Definition: Time.h:339
Time & addTo(Time other)
Add another time.
Definition: Time.h:156
constexpr Time seconds(float amount)
Construct a time value from a number of seconds.
Definition: Time.h:205
constexpr Time(std::chrono::steady_clock::duration duration)
Constructor with a duration.
Definition: Time.h:102