CTR Pia  4.11.3
Game Communication Engine
common_TimeSpan.h
1 /*---------------------------------------------------------------------------*
2  Project: Pia
3  File: common_TimeSpan.h
4 
5  Copyright Nintendo. All rights reserved.
6 
7  These coded instructions, statements, and computer programs contain
8  proprietary information of Nintendo of America Inc. and/or Nintendo
9  Company Ltd., and are protected by Federal copyright law. They may
10  not be disclosed to third parties or copied or duplicated in any form,
11  in whole or in part, without the prior written consent of Nintendo.
12  *---------------------------------------------------------------------------*/
13 
14 
15 #pragma once
16 
17 #include <pia/common/common_RootObject.h>
18 
19 namespace nn
20 {
21 namespace pia
22 {
23 namespace common
24 {
25 
26 /*!
27 @brief Type that represents a time interval. The value is in milliseconds.
28 */
29 typedef s32 TimeInterval;
30 
31 
32 class Time;
33 
34 /*!
35 @brief Class for representing time spans. This class can perform type conversions with numeric values in milliseconds.
36 
37 @date 2012-07-12 Added support so that this instance can be initialized as a static variable without problems.
38 @date 2012-05-24 Added the addition and subtraction operators to the <tt>TimeSpan</tt> class.
39 @date 2012-04-20 Initial version.
40 */
42 {
43 
44  friend class Time;
45 
46 public:
47 /*!
48 @brief Default constructor.
49 */
51  : m_TickSpan(0)
52  {
53  }
54 
55 
56 /*!
57 @brief Constructor that initializes the instance from a numeric value representing milliseconds.
58 
59 @param[in] msec Specifies the initial value, in milliseconds.
60 */
61  TimeSpan(s32 msec)
62  {
63  SetFromMSec(msec);
64  }
65 
66 
67 /*!
68 @brief Constructor that initializes the instance from a numeric value representing milliseconds.
69 
70 @param[in] msec Specifies the initial value, in milliseconds.
71 */
72  TimeSpan(s64 msec)
73  {
74  SetFromMSec(msec);
75  }
76 
77 
78 /*!
79 @brief Copy constructor.
80 
81 @param[in] rhs A <tt>TimeSpan</tt> instance that holds the values used to initialize this instance.
82 */
83  TimeSpan(const TimeSpan& rhs)
84  : m_TickSpan(rhs.m_TickSpan)
85  {
86  }
87 
88 
89 /*!
90 @brief Assignment operator.
91 
92 @param[in] rhs The <tt>TimeSpan</tt> instance to assign to this instance.
93 
94 @return Returns a reference to this instance.
95 */
97  {
98  m_TickSpan = rhs.m_TickSpan;
99  return *this;
100  }
101 
102 
103 /*!
104 @brief An operator that assigns a numeric value in milliseconds.
105 
106 @param[in] msec The time in milliseconds.
107 
108 @return Returns a reference to this instance.
109 */
110  TimeSpan& operator=(s32 msec)
111  {
112  SetFromMSec(msec);
113  return *this;
114  }
115 
116 
117 /*!
118 @brief An operator that assigns a numeric value in milliseconds.
119 
120 @param[in] msec The time in milliseconds.
121 
122 @return Returns a reference to this instance.
123 */
124  TimeSpan& operator=(s64 msec)
125  {
126  SetFromMSec(msec);
127  return *this;
128  }
129 
130 
131 /*!
132 @brief An operator that converts a time object to an <tt>s32</tt> value in milliseconds.
133 
134 @return The time in milliseconds.
135 */
136  operator s32() const
137  {
138  return GetMSec();
139  }
140 
141 
142 /*!
143 @brief An operator that converts a time object to an <tt>s64</tt> value in milliseconds.
144 
145 @return The time in milliseconds.
146 */
147  operator s64() const
148  {
149  return GetMSecS64();
150  }
151 
152 
153 /*!
154 @brief Sets the time in milliseconds from an <tt>s32</tt> value.
155 
156 @param[in] msec The time in milliseconds.
157 */
158  void SetFromMSec(s32 msec)
159  {
160  m_TickSpan = GetTicksPerMSec() * msec;
161  }
162 
163 
164 /*!
165 @brief Sets the time in milliseconds from an <tt>s64</tt> value.
166 
167 @param[in] msec The time in milliseconds.
168 */
169  void SetFromMSec(s64 msec)
170  {
171  m_TickSpan = GetTicksPerMSec() * msec;
172  }
173 
174 
175 /*!
176 @brief Sets the time in seconds from an <tt>s32</tt> value.
177 
178 @param[in] sec The time in seconds.
179 */
180  void SetFromSec(s32 sec)
181  {
182  m_TickSpan = GetTicksPerSec() * sec;
183  }
184 
185 
186 /*!
187 @brief Sets the time in seconds from an <tt>s64</tt> value.
188 
189 @param[in] sec The time in seconds.
190 */
191  void SetFromSec(s64 sec)
192  {
193  m_TickSpan = GetTicksPerSec() * sec;
194  }
195 
196 
197 /*!
198 @brief Sets the time in seconds from an <tt>f32</tt> value.
199 
200 @param[in] sec The time in seconds.
201 */
202  void SetFromSec(f32 sec)
203  {
204  m_TickSpan = static_cast<s64>(GetTicksPerSec() * sec);
205  }
206 
207 
208 /*!
209 @brief Gets the time in milliseconds as an <tt>s32</tt> value.
210 
211 @return The time in milliseconds.
212 */
213  s32 GetMSec() const
214  {
215  return static_cast<s32>(m_TickSpan / GetTicksPerMSec());
216  }
217 
218 
219 /*!
220 @brief Gets the time in milliseconds as an <tt>s64</tt> value.
221 
222 @return The time in milliseconds.
223 */
224  s64 GetMSecS64() const
225  {
226  return m_TickSpan / GetTicksPerMSec();
227  }
228 
229 
230 /*!
231 @brief Gets the time in seconds as an <tt>s32</tt> value.
232 
233 @return The time in seconds.
234 */
235  s32 GetSec() const
236  {
237  return static_cast<s32>(m_TickSpan / GetTicksPerSec());
238  }
239 
240 
241 /*!
242 @brief Gets the time in seconds as an <tt>s64</tt> value.
243 
244 @return The time in seconds.
245 */
246  s64 GetSecS64() const
247  {
248  return m_TickSpan / GetTicksPerSec();
249  }
250 
251 
252 /*!
253 @brief Gets the time in seconds as an <tt>f32</tt> value.
254 
255 @return The time in seconds.
256 */
257  f32 GetSecF32() const
258  {
259  return static_cast<f32>(m_TickSpan) / static_cast<f32>(GetTicksPerSec());
260  }
261 
262 
263 /*!
264 @brief Equality operator. It determines whether two <tt>TimeSpan</tt> objects are equal.
265 
266 @param[in] rhs The <tt>TimeSpan</tt> instance to compare with.
267 
268 @return Returns <tt>true</tt> if they represent the same time, and <tt>false</tt> otherwise.
269 */
270  bool operator==(const TimeSpan& rhs) const
271  {
272  return (m_TickSpan == rhs.m_TickSpan);
273  }
274 
275 
276 /*!
277 @brief Equality operator. It determines whether two <tt>TimeSpan</tt> objects are equal.
278 
279 @param[in] rhs An <tt>s32</tt> value in milliseconds to compare with.
280 
281 @return Returns <tt>true</tt> if they represent the same time, and <tt>false</tt> otherwise.
282 */
283  bool operator==(s32 rhs) const
284  {
285  return (*this == TimeSpan(rhs));
286  }
287 
288 
289 /*!
290 @brief Equality operator. Determines whether two <tt>TimeSpan</tt> objects are equal.
291 
292 @param[in] rhs An <tt>s64</tt> value in milliseconds to compare with.
293 
294 @return Returns <tt>true</tt> if they represent the same time, and <tt>false</tt> otherwise.
295 */
296  bool operator==(s64 rhs) const
297  {
298  return (*this == TimeSpan(rhs));
299  }
300 
301 
302 /*!
303 @brief Equality operator. It determines whether two <tt>TimeSpan</tt> objects differ.
304 
305 @param[in] rhs The <tt>TimeSpan</tt> instance to compare with.
306 
307 @return <tt>true</tt> if they represent different times, and <tt>false</tt> otherwise.
308 */
309  bool operator!=(const TimeSpan& rhs) const
310  {
311  return (m_TickSpan != rhs.m_TickSpan);
312  }
313 
314 
315 /*!
316 @brief Equality operator. Determines whether two <tt>TimeSpan</tt> objects differ.
317 
318 @param[in] rhs An <tt>s32</tt> value in milliseconds to compare with.
319 
320 @return <tt>true</tt> if they represent different times, and <tt>false</tt> otherwise.
321 */
322  bool operator!=(s32 rhs) const
323  {
324  return (*this != TimeSpan(rhs));
325  }
326 
327 
328 /*!
329 @brief Equality operator. Determines whether two <tt>TimeSpan</tt> objects differ.
330 
331 @param[in] rhs An <tt>s64</tt> value in milliseconds to compare with.
332 
333 @return <tt>true</tt> if they represent different times, and <tt>false</tt> otherwise.
334 */
335  bool operator!=(s64 rhs) const
336  {
337  return (*this != TimeSpan(rhs));
338  }
339 
340 
341 /*!
342 @brief Comparison operator. Returns <tt>true</tt> if the time represented by this instance is shorter; otherwise, returns <tt>false</tt>.
343 
344 @param[in] rhs The <tt>TimeSpan</tt> instance to compare with.
345 
346 @return Returns <tt>true</tt> if the time represented by this instance is shorter, and <tt>false</tt> otherwise.
347 */
348  bool operator<(const TimeSpan& rhs) const
349  {
350  return (m_TickSpan < rhs.m_TickSpan);
351  }
352 
353 
354 /*!
355 @brief Comparison operator. Returns <tt>true</tt> if the time represented by this instance is shorter; otherwise, returns <tt>false</tt>.
356 
357 @param[in] rhs An <tt>s32</tt> value in milliseconds to compare with.
358 
359 @return Returns <tt>true</tt> if the time represented by this instance is shorter, and <tt>false</tt> otherwise.
360 */
361  bool operator<(s32 rhs) const
362  {
363  return (*this < TimeSpan(rhs));
364  }
365 
366 
367 /*!
368 @brief Comparison operator. Returns <tt>true</tt> if the time represented by this instance is shorter; otherwise, returns <tt>false</tt>.
369 
370 @param[in] rhs An <tt>s64</tt> value in milliseconds to compare with.
371 
372 @return Returns <tt>true</tt> if the time represented by this instance is shorter, and <tt>false</tt> otherwise.
373 */
374  bool operator<(s64 rhs) const
375  {
376  return (*this < TimeSpan(rhs));
377  }
378 
379 
380 /*!
381 @brief Comparison operator. Returns <tt>true</tt> if the time represented by this instance is less than or equal, and <tt>false</tt> otherwise.
382 
383 @param[in] rhs The <tt>TimeSpan</tt> instance to compare with.
384 
385 @return Returns <tt>true</tt> if the time represented by this instance is less than or equal, and <tt>false</tt> otherwise.
386 */
387  bool operator<=(const TimeSpan& rhs) const
388  {
389  return (m_TickSpan <= rhs.m_TickSpan);
390  }
391 
392 
393 /*!
394 @brief Comparison operator. Returns <tt>true</tt> if the time represented by this instance is less than or equal, and <tt>false</tt> otherwise.
395 
396 @param[in] rhs An <tt>s32</tt> value in milliseconds to compare with.
397 
398 @return Returns <tt>true</tt> if the time represented by this instance is less than or equal, and <tt>false</tt> otherwise.
399 */
400  bool operator<=(s32 rhs) const
401  {
402  return (*this <= TimeSpan(rhs));
403  }
404 
405 
406 /*!
407 @brief Comparison operator. Returns <tt>true</tt> if the time represented by this instance is less than or equal, and <tt>false</tt> otherwise.
408 
409 @param[in] rhs An <tt>s64</tt> value in milliseconds to compare with.
410 
411 @return Returns <tt>true</tt> if the time represented by this instance is less than or equal, and <tt>false</tt> otherwise.
412 */
413  bool operator<=(s64 rhs) const
414  {
415  return (*this <= TimeSpan(rhs));
416  }
417 
418 
419 /*!
420 @brief Comparison operator. Returns <tt>true</tt> if the time represented by this instance is longer; otherwise returns <tt>false</tt>.
421 
422 @param[in] rhs The <tt>TimeSpan</tt> instance to compare with.
423 
424 @return Returns <tt>true</tt> if the time represented by this instance is longer, and <tt>false</tt> otherwise.
425 */
426  bool operator>(const TimeSpan& rhs) const
427  {
428  return (m_TickSpan > rhs.m_TickSpan);
429  }
430 
431 
432 /*!
433 @brief Comparison operator. Returns <tt>true</tt> if the time represented by this instance is longer; otherwise returns <tt>false</tt>.
434 
435 @param[in] rhs An <tt>s32</tt> value in milliseconds to compare with.
436 
437 @return Returns <tt>true</tt> if the time represented by this instance is longer, and <tt>false</tt> otherwise.
438 */
439  bool operator>(s32 rhs) const
440  {
441  return (*this > TimeSpan(rhs));
442  }
443 
444 
445 /*!
446 @brief Comparison operator. Returns <tt>true</tt> if the time represented by this instance is longer; otherwise returns <tt>false</tt>.
447 
448 @param[in] rhs An <tt>s64</tt> value in milliseconds to compare with.
449 
450 @return Returns <tt>true</tt> if the time represented by this instance is longer, and <tt>false</tt> otherwise.
451 */
452  bool operator>(s64 rhs) const
453  {
454  return (*this > TimeSpan(rhs));
455  }
456 
457 
458 /*!
459 @brief Comparison operator. Returns <tt>true</tt> if the time represented by this instance is greater than or equal; otherwise returns <tt>false</tt>.
460 
461 @param[in] rhs The <tt>TimeSpan</tt> instance to compare with.
462 
463 @return Returns <tt>true</tt> if the time represented by this instance is greater than or equal, and <tt>false</tt> otherwise.
464 */
465  bool operator>=(const TimeSpan& rhs) const
466  {
467  return (m_TickSpan >= rhs.m_TickSpan);
468  }
469 
470 
471 /*!
472 @brief Comparison operator. Returns <tt>true</tt> if the time represented by this instance is greater than or equal; otherwise returns <tt>false</tt>.
473 
474 @param[in] rhs An <tt>s32</tt> value in milliseconds to compare with.
475 
476 @return Returns <tt>true</tt> if the time represented by this instance is greater than or equal, and <tt>false</tt> otherwise.
477 */
478  bool operator>=(s32 rhs) const
479  {
480  return (*this >= TimeSpan(rhs));
481  }
482 
483 
484 /*!
485 @brief Comparison operator. Returns <tt>true</tt> if the time represented by this instance is greater than or equal; otherwise returns <tt>false</tt>.
486 
487 @param[in] rhs An <tt>s64</tt> value in milliseconds to compare with.
488 
489 @return Returns <tt>true</tt> if the time represented by this instance is greater than or equal, and <tt>false</tt> otherwise.
490 */
491  bool operator>=(s64 rhs) const
492  {
493  return (*this >= TimeSpan(rhs));
494  }
495 
496 
497 /*!
498 @brief Adds time to this instance.
499 
500 @param[in] rhs The time span to add.
501 
502 @return Returns a reference to this instance.
503 */
505  {
506  m_TickSpan += rhs.m_TickSpan;
507  return *this;
508  }
509 
510 
511 /*!
512 @brief Subtracts time from this instance.
513 
514 @param[in] rhs The time span to subtract.
515 
516 @return Returns a reference to this instance.
517 */
519  {
520  m_TickSpan -= rhs.m_TickSpan;
521  return *this;
522  }
523 
524 
525 /*!
526 @brief Calculates a <tt>TimeSpan</tt> object holding the amount of time added to this instance.
527 
528 @param[in] rhs The time span to add.
529 
530 @return A <tt>Time</tt> object representing the amount of time that was added.
531 */
532  TimeSpan operator+(const TimeSpan& rhs) const
533  {
534  TimeSpan t;
535  t.m_TickSpan = m_TickSpan + rhs.m_TickSpan;
536  return t;
537  }
538 
539 
540 /*!
541 @brief Calculates a <tt>TimeSpan</tt> object holding the amount of time subtracted from this instance.
542 
543 @param[in] rhs The time span to subtract.
544 
545 @return A <tt>TimeSpan</tt> object representing the amount of time that was subtracted.
546 */
547  TimeSpan operator-(const TimeSpan& rhs) const
548  {
549  TimeSpan t;
550  t.m_TickSpan = m_TickSpan - rhs.m_TickSpan;
551  return t;
552  }
553 
554 
555 /*!
556 @cond PRIVATE
557 @brief Gets the raw tick count.
558 
559 @return The raw tick count.
560 */
561  s64 GetTick() const
562  {
563  return m_TickSpan;
564  }
565  //! @endcond
566 
567 
568 /*!
569 @cond PRIVATE
570 @brief Sets the raw tick count.
571 
572 @param[in] tick The raw tick count.
573 */
574  void SetTick(s64 tick)
575  {
576  m_TickSpan = tick;
577  }
578  //! @endcond
579 
580 
581 /*!
582 @brief Prints information useful for debugging.
583 
584 @param[in] flag Bitwise OR of trace flags. For more information, see the <tt>@ref TraceFlag</tt> type.
585 */
586  void Trace(u64 flag) const;
587 
588 private:
589  s64 m_TickSpan;
590 
591  static const s64& GetTicksPerMSec();
592  static const s64& GetTicksPerSec();
593 };
594 }
595 }
596 } // end of namespace nn::pia::common
bool operator>(s64 rhs) const
Comparison operator. Returns true if the time represented by this instance is longer; otherwise retur...
Definition: common_TimeSpan.h:452
bool operator>(s32 rhs) const
Comparison operator. Returns true if the time represented by this instance is longer; otherwise retur...
Definition: common_TimeSpan.h:439
s64 GetSecS64() const
Gets the time in seconds as an s64 value.
Definition: common_TimeSpan.h:246
void SetFromSec(f32 sec)
Sets the time in seconds from an f32 value.
Definition: common_TimeSpan.h:202
bool operator<(s32 rhs) const
Comparison operator. Returns true if the time represented by this instance is shorter; otherwise...
Definition: common_TimeSpan.h:361
s64 GetMSecS64() const
Gets the time in milliseconds as an s64 value.
Definition: common_TimeSpan.h:224
bool operator>=(s32 rhs) const
Comparison operator. Returns true if the time represented by this instance is greater than or equal; ...
Definition: common_TimeSpan.h:478
Class that represents time.
Definition: common_Time.h:39
bool operator==(s64 rhs) const
Equality operator. Determines whether two TimeSpan objects are equal.
Definition: common_TimeSpan.h:296
bool operator!=(const TimeSpan &rhs) const
Equality operator. It determines whether two TimeSpan objects differ.
Definition: common_TimeSpan.h:309
Definition: assert.h:115
bool operator!=(s64 rhs) const
Equality operator. Determines whether two TimeSpan objects differ.
Definition: common_TimeSpan.h:335
void SetFromSec(s32 sec)
Sets the time in seconds from an s32 value.
Definition: common_TimeSpan.h:180
void SetFromSec(s64 sec)
Sets the time in seconds from an s64 value.
Definition: common_TimeSpan.h:191
void SetFromMSec(s64 msec)
Sets the time in milliseconds from an s64 value.
Definition: common_TimeSpan.h:169
TimeSpan & operator+=(const TimeSpan &rhs)
Adds time to this instance.
Definition: common_TimeSpan.h:504
bool operator<(const TimeSpan &rhs) const
Comparison operator. Returns true if the time represented by this instance is shorter; otherwise...
Definition: common_TimeSpan.h:348
TimeSpan()
Default constructor.
Definition: common_TimeSpan.h:50
TimeSpan(s64 msec)
Constructor that initializes the instance from a numeric value representing milliseconds.
Definition: common_TimeSpan.h:72
bool operator==(const TimeSpan &rhs) const
Equality operator. It determines whether two TimeSpan objects are equal.
Definition: common_TimeSpan.h:270
bool operator>=(s64 rhs) const
Comparison operator. Returns true if the time represented by this instance is greater than or equal; ...
Definition: common_TimeSpan.h:491
bool operator!=(s32 rhs) const
Equality operator. Determines whether two TimeSpan objects differ.
Definition: common_TimeSpan.h:322
s32 TimeInterval
Type that represents a time interval. The value is in milliseconds.
Definition: common_TimeSpan.h:29
s32 GetMSec() const
Gets the time in milliseconds as an s32 value.
Definition: common_TimeSpan.h:213
bool operator<(s64 rhs) const
Comparison operator. Returns true if the time represented by this instance is shorter; otherwise...
Definition: common_TimeSpan.h:374
bool operator>=(const TimeSpan &rhs) const
Comparison operator. Returns true if the time represented by this instance is greater than or equal; ...
Definition: common_TimeSpan.h:465
Class for representing time spans. This class can perform type conversions with numeric values in mil...
Definition: common_TimeSpan.h:41
bool operator<=(s64 rhs) const
Comparison operator. Returns true if the time represented by this instance is less than or equal...
Definition: common_TimeSpan.h:413
TimeSpan operator+(const TimeSpan &rhs) const
Calculates a TimeSpan object holding the amount of time added to this instance.
Definition: common_TimeSpan.h:532
bool operator==(s32 rhs) const
Equality operator. It determines whether two TimeSpan objects are equal.
Definition: common_TimeSpan.h:283
TimeSpan & operator=(s64 msec)
An operator that assigns a numeric value in milliseconds.
Definition: common_TimeSpan.h:124
TimeSpan & operator=(s32 msec)
An operator that assigns a numeric value in milliseconds.
Definition: common_TimeSpan.h:110
void SetFromMSec(s32 msec)
Sets the time in milliseconds from an s32 value.
Definition: common_TimeSpan.h:158
bool operator>(const TimeSpan &rhs) const
Comparison operator. Returns true if the time represented by this instance is longer; otherwise retur...
Definition: common_TimeSpan.h:426
s32 GetSec() const
Gets the time in seconds as an s32 value.
Definition: common_TimeSpan.h:235
TimeSpan(const TimeSpan &rhs)
Copy constructor.
Definition: common_TimeSpan.h:83
TimeSpan & operator=(const TimeSpan &rhs)
Assignment operator.
Definition: common_TimeSpan.h:96
TimeSpan operator-(const TimeSpan &rhs) const
Calculates a TimeSpan object holding the amount of time subtracted from this instance.
Definition: common_TimeSpan.h:547
bool operator<=(const TimeSpan &rhs) const
Comparison operator. Returns true if the time represented by this instance is less than or equal...
Definition: common_TimeSpan.h:387
TimeSpan(s32 msec)
Constructor that initializes the instance from a numeric value representing milliseconds.
Definition: common_TimeSpan.h:61
bool operator<=(s32 rhs) const
Comparison operator. Returns true if the time represented by this instance is less than or equal...
Definition: common_TimeSpan.h:400
TimeSpan & operator-=(const TimeSpan &rhs)
Subtracts time from this instance.
Definition: common_TimeSpan.h:518
This is the common base class used inside the Pia library.
Definition: common_RootObject.h:40
void Trace(u64 flag) const
Prints information useful for debugging.
f32 GetSecF32() const
Gets the time in seconds as an f32 value.
Definition: common_TimeSpan.h:257