Kiwano Engine v1.3.x
Duration.h
1// Copyright (c) 2016-2018 Kiwano - Nomango
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21#pragma once
22#include <kiwano/core/Common.h>
23
24namespace kiwano
25{
26
47struct KGE_API Duration
48{
51 Duration();
52
56 Duration(int64_t milliseconds);
57
60 int64_t GetMilliseconds() const;
61
64 float GetSeconds() const;
65
68 float GetMinutes() const;
69
72 float GetHours() const;
73
77 bool IsZero() const;
78
82 void SetMilliseconds(int64_t ms);
83
87 void SetSeconds(float seconds);
88
92 void SetMinutes(float minutes);
93
97 void SetHours(float hours);
98
101 void Sleep() const;
102
105 String ToString() const;
106
116 static Duration Parse(StringView str);
117
118 bool operator==(const Duration&) const;
119 bool operator!=(const Duration&) const;
120 bool operator>(const Duration&) const;
121 bool operator>=(const Duration&) const;
122 bool operator<(const Duration&) const;
123 bool operator<=(const Duration&) const;
124
125 float operator/(const Duration&) const;
126
127 const Duration operator+(const Duration&) const;
128 const Duration operator-(const Duration&) const;
129 const Duration operator-() const;
130 const Duration operator*(int) const;
131 const Duration operator*(unsigned long long) const;
132 const Duration operator*(float) const;
133 const Duration operator*(double) const;
134 const Duration operator*(long double) const;
135 const Duration operator/(int) const;
136 const Duration operator/(float) const;
137 const Duration operator/(double) const;
138
139 Duration& operator+=(const Duration&);
140 Duration& operator-=(const Duration&);
141 Duration& operator*=(int);
142 Duration& operator*=(float);
143 Duration& operator*=(double);
144 Duration& operator/=(int);
145 Duration& operator/=(float);
146 Duration& operator/=(double);
147
148 friend const Duration operator*(int, const Duration&);
149 friend const Duration operator*(float, const Duration&);
150 friend const Duration operator*(double, const Duration&);
151 friend const Duration operator*(long double, const Duration&);
152 friend const Duration operator/(int, const Duration&);
153 friend const Duration operator/(float, const Duration&);
154 friend const Duration operator/(double, const Duration&);
155
156private:
157 int64_t milliseconds_;
158};
159
160namespace time
161{
162
163extern const Duration Millisecond;
164extern const Duration Second;
165extern const Duration Minute;
166extern const Duration Hour;
167
168} // namespace time
169
171 : milliseconds_(0)
172{
173}
174
175inline Duration::Duration(int64_t milliseconds)
176 : milliseconds_(milliseconds)
177{
178}
179
180inline int64_t Duration::GetMilliseconds() const
181{
182 return milliseconds_;
183}
184
185inline bool Duration::IsZero() const
186{
187 return milliseconds_ == 0LL;
188}
189
190inline void Duration::SetMilliseconds(int64_t ms)
191{
192 milliseconds_ = ms;
193}
194
195inline void Duration::SetSeconds(float seconds)
196{
197 milliseconds_ = static_cast<int64_t>(seconds * 1000.f);
198}
199
200inline void Duration::SetMinutes(float minutes)
201{
202 milliseconds_ = static_cast<int64_t>(minutes * 60 * 1000.f);
203}
204
205inline void Duration::SetHours(float hours)
206{
207 milliseconds_ = static_cast<int64_t>(hours * 60 * 60 * 1000.f);
208}
209
210inline bool Duration::operator==(const Duration& other) const
211{
212 return milliseconds_ == other.milliseconds_;
213}
214
215inline bool Duration::operator!=(const Duration& other) const
216{
217 return milliseconds_ != other.milliseconds_;
218}
219
220inline bool Duration::operator>(const Duration& other) const
221{
222 return milliseconds_ > other.milliseconds_;
223}
224
225inline bool Duration::operator>=(const Duration& other) const
226{
227 return milliseconds_ >= other.milliseconds_;
228}
229
230inline bool Duration::operator<(const Duration& other) const
231{
232 return milliseconds_ < other.milliseconds_;
233}
234
235inline bool Duration::operator<=(const Duration& other) const
236{
237 return milliseconds_ <= other.milliseconds_;
238}
239
240inline float Duration::operator/(const Duration& other) const
241{
242 return static_cast<float>(milliseconds_) / other.milliseconds_;
243}
244
245inline const Duration Duration::operator+(const Duration& other) const
246{
247 return Duration(milliseconds_ + other.milliseconds_);
248}
249
250inline const Duration Duration::operator-(const Duration& other) const
251{
252 return Duration(milliseconds_ - other.milliseconds_);
253}
254
255inline const Duration Duration::operator-() const
256{
257 return Duration(-milliseconds_);
258}
259
260inline const Duration Duration::operator*(int val) const
261{
262 return Duration(milliseconds_ * val);
263}
264
265inline const Duration Duration::operator*(unsigned long long val) const
266{
267 return Duration(static_cast<int64_t>(milliseconds_ * val));
268}
269
270inline const Duration Duration::operator*(float val) const
271{
272 return Duration(static_cast<int64_t>(milliseconds_ * val));
273}
274
275inline const Duration Duration::operator*(double val) const
276{
277 return Duration(static_cast<int64_t>(milliseconds_ * val));
278}
279
280inline const Duration Duration::operator*(long double val) const
281{
282 return Duration(static_cast<int64_t>(milliseconds_ * val));
283}
284
285inline const Duration Duration::operator/(int val) const
286{
287 return Duration(milliseconds_ / val);
288}
289
290inline const Duration Duration::operator/(float val) const
291{
292 return Duration(static_cast<int64_t>(milliseconds_ / val));
293}
294
295inline const Duration Duration::operator/(double val) const
296{
297 return Duration(static_cast<int64_t>(milliseconds_ / val));
298}
299
300inline Duration& Duration::operator+=(const Duration& other)
301{
302 milliseconds_ += other.milliseconds_;
303 return (*this);
304}
305
306inline Duration& Duration::operator-=(const Duration& other)
307{
308 milliseconds_ -= other.milliseconds_;
309 return (*this);
310}
311
312inline Duration& Duration::operator*=(int val)
313{
314 milliseconds_ *= val;
315 return (*this);
316}
317
318inline Duration& Duration::operator/=(int val)
319{
320 milliseconds_ = static_cast<int64_t>(milliseconds_ / val);
321 return (*this);
322}
323
324inline Duration& Duration::operator*=(float val)
325{
326 milliseconds_ = static_cast<int64_t>(milliseconds_ * val);
327 return (*this);
328}
329
330inline Duration& Duration::operator/=(float val)
331{
332 milliseconds_ = static_cast<int64_t>(milliseconds_ / val);
333 return (*this);
334}
335
336inline Duration& Duration::operator*=(double val)
337{
338 milliseconds_ = static_cast<int64_t>(milliseconds_ * val);
339 return (*this);
340}
341
342inline Duration& Duration::operator/=(double val)
343{
344 milliseconds_ = static_cast<int64_t>(milliseconds_ / val);
345 return (*this);
346}
347
348inline const Duration operator*(int val, const Duration& dur)
349{
350 return dur * val;
351}
352
353inline const Duration operator/(int val, const Duration& dur)
354{
355 return dur / val;
356}
357
358inline const Duration operator*(float val, const Duration& dur)
359{
360 return dur * val;
361}
362
363inline const Duration operator/(float val, const Duration& dur)
364{
365 return dur / val;
366}
367
368inline const Duration operator*(double val, const Duration& dur)
369{
370 return dur * val;
371}
372
373inline const Duration operator/(double val, const Duration& dur)
374{
375 return dur / val;
376}
377
378inline const Duration operator*(long double val, const Duration& dur)
379{
380 return dur * val;
381}
382
383} // namespace kiwano
384
385#if defined(KGE_HAS_LITERALS)
386
387namespace kiwano
388{
389inline namespace literals
390{
391inline const kiwano::Duration operator"" _msec(long double val)
392{
393 return kiwano::time::Millisecond * val;
394}
395
396inline const kiwano::Duration operator"" _msec(unsigned long long val)
397{
398 return kiwano::time::Millisecond * val;
399}
400
401inline const kiwano::Duration operator"" _sec(long double val)
402{
403 return kiwano::time::Second * val;
404}
405
406inline const kiwano::Duration operator"" _sec(unsigned long long val)
407{
408 return kiwano::time::Second * val;
409}
410
411inline const kiwano::Duration operator"" _min(long double val)
412{
413 return kiwano::time::Minute * val;
414}
415
416inline const kiwano::Duration operator"" _min(unsigned long long val)
417{
418 return kiwano::time::Minute * val;
419}
420
421inline const kiwano::Duration operator"" _hour(long double val)
422{
423 return kiwano::time::Hour * val;
424}
425
426inline const kiwano::Duration operator"" _hour(unsigned long long val)
427{
428 return kiwano::time::Hour * val;
429}
430} // namespace literals
431} // namespace kiwano
432
433#endif
时间段
Definition: Duration.h:48
void SetMilliseconds(int64_t ms)
设置毫秒数
Definition: Duration.h:190
void SetSeconds(float seconds)
设置秒数
Definition: Duration.h:195
bool IsZero() const
时长是否是零
Definition: Duration.h:185
Duration()
构造时间段
Definition: Duration.h:170
void SetMinutes(float minutes)
设置分钟数
Definition: Duration.h:200
void SetHours(float hours)
设置小时数
Definition: Duration.h:205
int64_t GetMilliseconds() const
获取毫秒数
Definition: Duration.h:180