CTR Pia  4.11.3
Game Communication Engine
common_Time.h
1 /*---------------------------------------------------------------------------*
2  Project: Pia
3  File: common_Time.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_TimeSpan.h>
18 
19 namespace nn
20 {
21 namespace pia
22 {
23 namespace common
24 {
25 
26 /*!
27 @brief Class that represents time.
28 
29 @date 2012-06-06 Made the class inherit from \c RootObject.
30 @date 2012-04-20 Changed the internal representation to be a direct representation of a system tick. Consequently, removed <tt>Time(u64)</tt> and <tt>operator u64</tt>, which handled the raw values directly.
31 @date 2012-04-20 Added a \c TimeSpan class to represent time spans (deltas). The arguments and return values of operators that handle time spans have been changed to instances of the \c TimeSpan class.
32 @date 2012-04-20 Added the \c SetNow function.
33 @date 2012-04-20 Removed \c ConvertFromSeconds and \c ConvertToSeconds functions, and gave equivalent functionality to <tt>TimeSpan::SetFromSec(f32)</tt> and <tt>TimeSpan::GetSecF32</tt> functions.
34 @date 2012-04-13 Changed the specifications of <tt>GetTime</tt> function to return the time elapsed since the <tt>nn::pia::common::Initialize</tt> function was called.
35 @date 2012-04-13 Removed the <tt>Reset</tt> function.
36 @date 2012-04-13 The <tt>nn::pia::common::Initialize</tt> and <tt>nn::pia::common::Finalize</tt> functions now initialize and finalize the \c Time class.
37 @date 2012-04-04 Initial version.
38 */
39 class Time : public RootObject
40 {
41 public:
42 /*!
43 @brief Instantiates the object with default parameters (default constructor).
44 */
45  Time()
46  : m_Tick(0)
47  {
48  }
49 
50 
51  explicit Time(u64 tick)
52  : m_Tick(tick)
53  {
54  }
55 
56 /*!
57 @brief Copy constructor.
58 
59 @param[in] rhs A <tt>Time</tt> instance that holds the value used to initialize this instance.
60 */
61  Time(const Time& rhs)
62  : m_Tick(rhs.m_Tick)
63  {
64  }
65 
66 
67 /*!
68 @brief Makes this object represent the current time.
69 */
70  void SetNow();
71 
72 
73 /*!
74 @brief Gets a \c Time instance representing the current time.
75 
76 @return A <tt>Time</tt> instance representing the current time.
77 */
78  static Time GetTime()
79  {
80  Time time;
81  time.SetNow();
82  return time;
83  }
84 
85 
86 /*!
87 @brief Assignment operator.
88 
89 @param[in] rhs The <tt>Time</tt> instance to assign to this instance.
90 
91 @return Returns a reference to this instance.
92 */
93  Time& operator=(const Time& rhs)
94  {
95  m_Tick = rhs.m_Tick;
96  return *this;
97  }
98 
99 
100 /*!
101 @brief Adds time to this instance's time.
102 
103 @param[in] span The time to add.
104 
105 @return Returns a reference to this instance.
106 */
107  Time& operator+=(const TimeSpan& span)
108  {
109  m_Tick += span.m_TickSpan;
110  return *this;
111  }
112 
113 
114 /*!
115 @brief Subtracts time from this instance.
116 
117 @param[in] span The time to subtract.
118 
119 @return Returns a reference to this instance.
120 */
121  Time& operator-=(const TimeSpan& span)
122  {
123  m_Tick -= span.m_TickSpan;
124  return *this;
125  }
126 
127 
128 /*!
129 @brief Calculates a <tt>Time</tt> object holding the amount of time added to this instance.
130 
131 @param[in] span The time to add.
132 
133 @return A <tt>Time</tt> representing the amount of time that was added.
134 */
135  Time operator+(const TimeSpan& span) const
136  {
137  Time time;
138  time.m_Tick = m_Tick + span.m_TickSpan;
139  return time;
140  }
141 
142 
143 /*!
144 @brief Calculates a <tt>Time</tt> object holding the amount of time subtracted from this instance.
145 
146 @param[in] span The time to subtract.
147 
148 @return A <tt>Time</tt> representing the amount of time that was subtracted.
149 */
150  Time operator-(const TimeSpan& span) const
151  {
152  Time time;
153  time.m_Tick = m_Tick - span.m_TickSpan;
154  return time;
155  }
156 
157 
158 /*!
159 @brief Calculates a time delta.
160 
161 @param[in] rhs The base time.
162 
163 @return A <tt>TimeSpan</tt> object representing the time delta.
164 */
165  TimeSpan operator-(const Time& rhs) const
166  {
167  TimeSpan span;
168  span.m_TickSpan = m_Tick - rhs.m_Tick;
169  return span;
170  }
171 
172 
173 /*!
174 @brief Equality operator. Determines whether two <tt>Time</tt> objects are equal.
175 
176 @param[in] rhs The <tt>Time</tt> instance to compare with.
177 
178 @return Returns <tt>true</tt> if they represent the same time, and <tt>false</tt> otherwise.
179 */
180  bool operator==(const Time& rhs) const
181  {
182  return (m_Tick == rhs.m_Tick);
183  }
184 
185 
186 /*!
187 @brief Equality operator. Determines whether two <tt>Time</tt> objects differ.
188 
189 @param[in] rhs The <tt>Time</tt> instance to compare with.
190 
191 @return <tt>true</tt> if they represent different times, and <tt>false</tt> otherwise.
192 */
193  bool operator!=(const Time& rhs) const
194  {
195  return (m_Tick != rhs.m_Tick);
196  }
197 
198 
199 /*!
200 @brief Comparison operator. Returns \c true if this instance is earlier.
201 
202 @param[in] rhs The <tt>Time</tt> instance to compare with.
203 
204 @return \c true if this instance is earlier, and \c false otherwise.
205 */
206  bool operator<(const Time& rhs) const
207  {
208  return (m_Tick < rhs.m_Tick);
209  }
210 
211 
212 /*!
213 @brief Comparison operator. Returns \c true if this instance is earlier or equal.
214 
215 @param[in] rhs The <tt>Time</tt> instance to compare with.
216 
217 @return \c true if this instance is earlier or equal, and \c false otherwise.
218 */
219  bool operator<=(const Time& rhs) const
220  {
221  return (m_Tick <= rhs.m_Tick);
222  }
223 
224 
225 /*!
226 @brief Comparison operator. Returns \c true if this instance is later.
227 
228 @param[in] rhs The <tt>Time</tt> instance to compare with.
229 
230 @return \c true if this instance is later, and \c false otherwise.
231 */
232  bool operator>(const Time& rhs) const
233  {
234  return (m_Tick > rhs.m_Tick);
235  }
236 
237 
238 /*!
239 @brief Comparison operator. Returns \c true if this instance is later or equal.
240 
241 @param[in] rhs The <tt>Time</tt> instance to compare with.
242 
243 @return \c true if this instance is later or equal, and \c false otherwise.
244 */
245  bool operator>=(const Time& rhs) const
246  {
247  return (m_Tick >= rhs.m_Tick);
248  }
249 
250 
251 /*!
252 @cond PRIVATE
253 @brief Gets the time from the current time to the specified time.
254 
255 @param[in] timeout A time span (delta).
256 
257 @return The time from now until <SPAN class="argument">timeout</SPAN>.
258 */
259  static Time ConvertTimeoutToDeadline(const TimeSpan& timeout)
260  {
261  return (GetTime() += timeout);
262  }
263  //! @endcond
264 
265 
266 /*!
267 @cond PRIVATE
268 @brief Gets the raw tick value stored in the instance.
269 
270 @return The tick value stored in the instance.
271 @see SetTick
272 */
273  u64 GetTick(void) const
274  {
275  return m_Tick;
276  }
277  //! @endcond
278 
279 
280 /*!
281 @cond PRIVATE
282 @brief Sets the raw tick value of the instance.
283 
284 @param[in] tick The raw tick count.
285 @see GetTick
286 */
287  void SetTick(u64 tick)
288  {
289  m_Tick = tick;
290  }
291  //! @endcond
292 
293 /*!
294 @brief Prints information useful for debugging.
295 
296 @param[in] flag Specifies the bitwise OR of trace flags. For more information, see the <tt>@ref TraceFlag</tt> type.
297 */
298  void Trace(u64 flag) const;
299 
300  static const Time c_Max; //< maximum time value.
301  static const Time c_Min; //< minimum time value.
302 
303 private:
304  static Time Create(u64 tick);
305 
306  u64 m_Tick;
307 };
308 }
309 }
310 } // end of namespace nn::pia::common
Class that represents time.
Definition: common_Time.h:39
Definition: assert.h:115
Time operator-(const TimeSpan &span) const
Calculates a Time object holding the amount of time subtracted from this instance.
Definition: common_Time.h:150
void Trace(u64 flag) const
Prints information useful for debugging.
bool operator!=(const Time &rhs) const
Equality operator. Determines whether two Time objects differ.
Definition: common_Time.h:193
Time(const Time &rhs)
Copy constructor.
Definition: common_Time.h:61
bool operator>(const Time &rhs) const
Comparison operator. Returns true if this instance is later.
Definition: common_Time.h:232
TimeSpan operator-(const Time &rhs) const
Calculates a time delta.
Definition: common_Time.h:165
Time & operator=(const Time &rhs)
Assignment operator.
Definition: common_Time.h:93
bool operator<(const Time &rhs) const
Comparison operator. Returns true if this instance is earlier.
Definition: common_Time.h:206
bool operator==(const Time &rhs) const
Equality operator. Determines whether two Time objects are equal.
Definition: common_Time.h:180
Class for representing time spans. This class can perform type conversions with numeric values in mil...
Definition: common_TimeSpan.h:41
Time operator+(const TimeSpan &span) const
Calculates a Time object holding the amount of time added to this instance.
Definition: common_Time.h:135
void SetNow()
Makes this object represent the current time.
Time()
Instantiates the object with default parameters (default constructor).
Definition: common_Time.h:45
Time & operator-=(const TimeSpan &span)
Subtracts time from this instance.
Definition: common_Time.h:121
bool operator>=(const Time &rhs) const
Comparison operator. Returns true if this instance is later or equal.
Definition: common_Time.h:245
bool operator<=(const Time &rhs) const
Comparison operator. Returns true if this instance is earlier or equal.
Definition: common_Time.h:219
This is the common base class used inside the Pia library.
Definition: common_RootObject.h:40
static Time GetTime()
Gets a Time instance representing the current time.
Definition: common_Time.h:78
Time & operator+=(const TimeSpan &span)
Adds time to this instance&#39;s time.
Definition: common_Time.h:107