Gamedev Framework (gf)  0.6.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 
38  /**
39  * @ingroup core
40  * @brief Represents a time value
41  *
42  * gf::Time encapsulates a time value in a flexible way.
43  * It allows to define a time value either as a number of
44  * seconds, milliseconds or microseconds. It also works the
45  * other way round: you can read a time value as either
46  * a number of seconds, milliseconds or microseconds.
47  *
48  * By using such a flexible interface, the API doesn't
49  * impose any fixed type or resolution for time values,
50  * and let the user choose its own favorite representation.
51  *
52  * Since they represent a time span and not an absolute time
53  * value, times can also be negative.
54  *
55  * gf::Time is a thin wrapper around C++11
56  * [std::chrono::steady_clock](http://en.cppreference.com/w/cpp/chrono/steady_clock).
57  *
58  * Usage example:
59  *
60  * ~~~{.cc}
61  * gf::Time t1 = gf::seconds(0.1f);
62  * int32_t milli = t1.asMilliseconds(); // 100
63  *
64  * gf::Time t2 = gf::milliseconds(30);
65  * int64_t micro = t2.asMicroseconds(); // 30000
66  *
67  * gf::Time t3 = gf::microseconds(-800000);
68  * float sec = t3.asSeconds(); // -0.8
69  * ~~~
70  *
71  * @sa gf::Clock
72  */
73  class GF_API Time {
74  public:
75  /**
76  * @brief Default constructor
77  *
78  * Sets the time value to zero.
79  */
80  constexpr Time()
82  {
83 
84  }
85 
86  /**
87  * @brief Constructor with a duration
88  *
89  * @param duration A duration expressed with a `std::chrono` type
90  */
91  constexpr explicit Time(std::chrono::steady_clock::duration duration)
93  {
94 
95  }
96 
97  /**
98  * @brief Return the time value as a number of seconds
99  *
100  * @return Time in seconds
101  *
102  * @sa asMilliseconds(), asMicroseconds()
103  */
104  constexpr float asSeconds() const {
105  return std::chrono::duration_cast<std::chrono::duration<float>>(m_duration).count();
106  }
107 
108  /**
109  * @brief Return the time value as a number of milliseconds
110  *
111  * @return Time in milliseconds
112  *
113  * @sa asSeconds(), asMicroseconds()
114  */
115  constexpr int32_t asMilliseconds() const {
116  return std::chrono::duration_cast<std::chrono::duration<int32_t, std::milli>>(m_duration).count();
117  }
118 
119  /**
120  * @brief Return the time value as a number of microseconds
121  *
122  * @return Time in microseconds
123  *
124  * @sa asSeconds(), asMilliseconds()
125  */
126  constexpr int64_t asMicroseconds() const {
127  return std::chrono::duration_cast<std::chrono::duration<int64_t, std::micro>>(m_duration).count();
128  }
129 
130  /**
131  * @brief Return the time value as a duration
132  *
133  * @return Time as a duration (`std::chrono` type)
134  */
135  constexpr std::chrono::steady_clock::duration asDuration() const {
136  return m_duration;
137  }
138 
139  /**
140  * @brief Add another time
141  *
142  * @param other The time to add to the current time
143  * @return The current time
144  */
145  Time& addTo(Time other) {
146  m_duration += other.m_duration;
147  return *this;
148  }
149 
150  /**
151  * @brief Substract another time
152  *
153  * @param other The time to substract to the current time
154  * @return The current time
155  */
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 
171  /**
172  * @relates Time
173  * @brief Construct a time value from a number of seconds
174  *
175  * @param amount Number of seconds
176  *
177  * @return Time value constructed from the amount of seconds
178  *
179  * @sa milliseconds(), microseconds()
180  */
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 
186  /**
187  * @relates Time
188  * @brief Construct a time value from a number of milliseconds
189  *
190  * @param amount Number of milliseconds
191  *
192  * @return Time value constructed from the amount of milliseconds
193  *
194  * @sa seconds(), microseconds()
195  */
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 
201  /**
202  * @relates Time
203  * @brief Construct a time value from a number of microseconds
204  *
205  * @param amount Number of microseconds
206  *
207  * @return Time value constructed from the amount of microseconds
208  *
209  * @sa seconds(), milliseconds()
210  */
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 
216  /**
217  * @relates Time
218  * @brief Equality operator
219  *
220  * @param rhs First time
221  * @param lhs Second time
222  * @return True if the first time and the second time are the same
223  */
224  constexpr
225  bool operator==(Time rhs, Time lhs) {
226  return rhs.asDuration() == lhs.asDuration();
227  }
228 
229  /**
230  * @relates Time
231  * @brief Inequality operator
232  *
233  * @param rhs First time
234  * @param lhs Second time
235  * @return True if the first time and the second time are different
236  */
237  constexpr
238  bool operator!=(Time rhs, Time lhs) {
239  return rhs.asDuration() != lhs.asDuration();
240  }
241 
242  /**
243  * @relates Time
244  * @brief Lesser than operator
245  *
246  * @param rhs First time
247  * @param lhs Second time
248  * @return True if the first time is lesser than the second time
249  */
250  constexpr
251  bool operator<(Time rhs, Time lhs) {
252  return rhs.asDuration() < lhs.asDuration();
253  }
254 
255  /**
256  * @relates Time
257  * @brief Greater than operator
258  *
259  * @param rhs First time
260  * @param lhs Second time
261  * @return True if the first time is greater than the second time
262  */
263  constexpr
264  bool operator>(Time rhs, Time lhs) {
265  return rhs.asDuration() > lhs.asDuration();
266  }
267 
268  /**
269  * @relates Time
270  * @brief Lesser or equal operator
271  *
272  * @param rhs First time
273  * @param lhs Second time
274  * @return True if the first time is lesser or equal than the second time
275  */
276  constexpr
277  bool operator<=(Time rhs, Time lhs) {
278  return rhs.asDuration() <= lhs.asDuration();
279  }
280 
281  /**
282  * @relates Time
283  * @brief Greater or equal than operator
284  *
285  * @param rhs First time
286  * @param lhs Second time
287  * @return True if the first time is greater or equal than the second time
288  */
289  constexpr
290  bool operator>=(Time rhs, Time lhs) {
291  return rhs.asDuration() >= lhs.asDuration();
292  }
293 
294  /**
295  * @relates Time
296  * @brief Addition of two times
297  *
298  * @param rhs First time
299  * @param lhs Second time
300  * @return The sum of two times
301  */
302  constexpr
303  Time operator+(Time rhs, Time lhs) {
304  return Time(rhs.asDuration() + lhs.asDuration());
305  }
306 
307  /**
308  * @relates Time
309  * @brief Addition and assignement
310  *
311  * @param rhs First time
312  * @param lhs Second time
313  * @return The sum of two times
314  */
315  inline
316  Time operator+=(Time& rhs, Time lhs) {
317  return rhs.addTo(lhs);
318  }
319 
320  /**
321  * @relates Time
322  * @brief Substraction of two times
323  *
324  * @param rhs First time
325  * @param lhs Second time
326  * @return The difference of two times
327  */
328  constexpr
329  Time operator-(Time rhs, Time lhs) {
330  return Time(rhs.asDuration() - lhs.asDuration());
331  }
332 
333  /**
334  * @relates Time
335  * @brief Substraction and assignment
336  *
337  * @param rhs First time
338  * @param lhs Second time
339  * @return The difference of two times
340  */
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
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
#define GF_API
Definition: Portability.h:35
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