Gamedev Framework (gf)  0.3.0
A C++11 framework for 2D games
Time.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016 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  Time();
81 
82  /**
83  * @brief Constructor with a duration
84  *
85  * @param duration A duration expressed with a `std::chrono` type
86  */
87  explicit Time(std::chrono::steady_clock::duration duration);
88 
89  /**
90  * @brief Return the time value as a number of seconds
91  *
92  * @return Time in seconds
93  *
94  * @sa asMilliseconds(), asMicroseconds()
95  */
96  float asSeconds() const;
97 
98  /**
99  * @brief Return the time value as a number of milliseconds
100  *
101  * @return Time in milliseconds
102  *
103  * @sa asSeconds(), asMicroseconds()
104  */
105  int32_t asMilliseconds() const;
106 
107  /**
108  * @brief Return the time value as a number of microseconds
109  *
110  * @return Time in microseconds
111  *
112  * @sa asSeconds(), asMilliseconds()
113  */
114  int64_t asMicroseconds() const;
115 
116  /**
117  * @brief Return the time value as a duration
118  *
119  * @return Time as a duration (`std::chrono` type)
120  */
121  std::chrono::steady_clock::duration asDuration() const {
122  return m_duration;
123  }
124 
125  private:
126  std::chrono::steady_clock::duration m_duration;
127  };
128 
129  /**
130  * @relates Time
131  * @brief Construct a time value from a number of seconds
132  *
133  * @param amount Number of seconds
134  *
135  * @return Time value constructed from the amount of seconds
136  *
137  * @sa milliseconds(), microseconds()
138  */
139  GF_API Time seconds(float amount);
140 
141  /**
142  * @relates Time
143  * @brief Construct a time value from a number of milliseconds
144  *
145  * @param amount Number of milliseconds
146  *
147  * @return Time value constructed from the amount of milliseconds
148  *
149  * @sa seconds(), microseconds()
150  */
151  GF_API Time milliseconds(int32_t amount);
152 
153  /**
154  * @relates Time
155  * @brief Construct a time value from a number of microseconds
156  *
157  * @param amount Number of microseconds
158  *
159  * @return Time value constructed from the amount of microseconds
160  *
161  * @sa seconds(), milliseconds()
162  */
163  GF_API Time microseconds(int64_t amount);
164 
165  /**
166  * @relates Time
167  * @brief Equality operator
168  *
169  * @param rhs First time
170  * @param lhs Second time
171  * @return True if the first time and the second time are the same
172  */
173  inline
174  bool operator==(const Time& rhs, const Time& lhs) {
175  return rhs.asDuration() == lhs.asDuration();
176  }
177 
178  /**
179  * @relates Time
180  * @brief Inequality operator
181  *
182  * @param rhs First time
183  * @param lhs Second time
184  * @return True if the first time and the second time are different
185  */
186  inline
187  bool operator!=(const Time& rhs, const Time& lhs) {
188  return rhs.asDuration() != lhs.asDuration();
189  }
190 
191  /**
192  * @relates Time
193  * @brief Lesser than operator
194  *
195  * @param rhs First time
196  * @param lhs Second time
197  * @return True if the first time is lesser than the second time
198  */
199  inline
200  bool operator<(const Time& rhs, const Time& lhs) {
201  return rhs.asDuration() < lhs.asDuration();
202  }
203 
204  /**
205  * @relates Time
206  * @brief Greater than operator
207  *
208  * @param rhs First time
209  * @param lhs Second time
210  * @return True if the first time is greater than the second time
211  */
212  inline
213  bool operator>(const Time& rhs, const Time& lhs) {
214  return rhs.asDuration() > lhs.asDuration();
215  }
216 
217  /**
218  * @relates Time
219  * @brief Lesser or equal operator
220  *
221  * @param rhs First time
222  * @param lhs Second time
223  * @return True if the first time is lesser or equal than the second time
224  */
225  inline
226  bool operator<=(const Time& rhs, const Time& lhs) {
227  return rhs.asDuration() <= lhs.asDuration();
228  }
229 
230  /**
231  * @relates Time
232  * @brief Greater or equal than operator
233  *
234  * @param rhs First time
235  * @param lhs Second time
236  * @return True if the first time is greater or equal than the second time
237  */
238  inline
239  bool operator>=(const Time& rhs, const Time& lhs) {
240  return rhs.asDuration() >= lhs.asDuration();
241  }
242 
243 #ifndef DOXYGEN_SHOULD_SKIP_THIS
244 }
245 #endif
246 }
247 
248 #endif // GF_TIME_H
int32_t asMilliseconds() const
Return the time value as a number of milliseconds.
std::chrono::steady_clock::duration asDuration() const
Return the time value as a duration.
Definition: Time.h:121
bool operator>=(const Time &rhs, const Time &lhs)
Greater or equal than operator.
Definition: Time.h:239
bool operator==(const Time &rhs, const Time &lhs)
Equality operator.
Definition: Time.h:174
bool operator<=(const Time &rhs, const Time &lhs)
Lesser or equal operator.
Definition: Time.h:226
bool operator<(const Time &rhs, const Time &lhs)
Lesser than operator.
Definition: Time.h:200
Represents a time value.
Definition: Time.h:73
Time(std::chrono::steady_clock::duration duration)
Constructor with a duration.
float asSeconds() const
Return the time value as a number of seconds.
Time milliseconds(int32_t amount)
Construct a time value from a number of milliseconds.
Time()
Default constructor.
Definition: Action.h:34
Time microseconds(int64_t amount)
Construct a time value from a number of microseconds.
bool operator>(const Time &rhs, const Time &lhs)
Greater than operator.
Definition: Time.h:213
int64_t asMicroseconds() const
Return the time value as a number of microseconds.
#define GF_API
Definition: Portability.h:35
Time seconds(float amount)
Construct a time value from a number of seconds.
bool operator!=(const Time &rhs, const Time &lhs)
Inequality operator.
Definition: Time.h:187