1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     fnd_TimeSpan.h
4 
5   Copyright (C)2009 Nintendo Co., Ltd.  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   $Rev: 23609 $
14  *---------------------------------------------------------------------------*/
15 
16 /*! @file
17     @brief  TimeSpan に関するAPI の宣言
18 
19 */
20 
21 #ifndef NN_FND_TIMESPAN_H_
22 #define NN_FND_TIMESPAN_H_
23 
24 #include <nn/types.h>
25 #include <nn/config.h>
26 #include <nn/os/os_HardwareParamsSelect.h>
27 
28 #ifdef __cplusplus
29 
30 namespace nn { namespace fnd {
31 
32 /*!
33     @brief 時間を表すクラスです。
34 
35            このクラスの内部では、64bit 整数としてナノ秒単位で時間を表しています。
36            SDK 内の時間を指定する関数に渡す値は、単位を間違えないためにもこのクラスを使うようになっています。
37            このクラスのインスタンスは From* 関数を使うことで、各単位で表された整数値から作成することができます。
38            単位があいまいになるのを防ぐため、整数からこの型への暗黙的な変換は用意されていませんが、
39            0 はこの型への暗黙的な変換をすることができます。
40 */
41 class TimeSpan
42 {
43 private:
44     static const s64 TICKS_PER_SECOND = NN_HW_TICKS_PER_SECOND;
45 
46     typedef const class ZeroOnlyTag {} * ZeroOnly;
47 
48 public:
49 
50     /*!
51         @brief        時間を 0 で初期化するコンストラクタです。
52 
53                       引数を省略した場合、デフォルトコンストラクタとなり、時間 0 で初期化されます。
54                       引数つきのこのコンストラクタは、時間を指定するべき場所で 0 を指定できるようにするために用意されています。
55                       0 以外の引数を与えることはできません。
56 
57         @param[in]    zeroOnly 引数を省略するか、0 を指定してください。
58     */
59     TimeSpan(ZeroOnly zeroOnly = 0) : m_NanoSeconds(0) { NN_UNUSED_VAR(zeroOnly); }
60 
61     /*!
62     @brief        ナノ秒数から TimeSpan オブジェクトを生成します。
63 
64     @param[in]    nanoSeconds   時間の長さをナノ秒数で指定します。
65 
66     @return       TimeSpan      指定された時間の長さを表す TimeSpan オブジェクトを返します。
67     */
FromNanoSeconds(s64 nanoSeconds)68     static TimeSpan FromNanoSeconds(s64 nanoSeconds) { TimeSpan ret; ret.m_NanoSeconds = nanoSeconds; return ret; }
69 
70     /*!
71     @brief        マイクロ秒数から TimeSpan オブジェクトを生成します。
72 
73     @param[in]    microSeconds  時間の長さをマイクロ秒数で指定します。
74 
75     @return       TimeSpan      指定された時間の長さを表す TimeSpan オブジェクトを返します。
76     */
FromMicroSeconds(s64 microSeconds)77     static TimeSpan FromMicroSeconds(s64 microSeconds) { return FromNanoSeconds(microSeconds * 1000); }
78 
79     /*!
80     @brief        ミリ秒数から TimeSpan オブジェクトを生成します。
81 
82     @param[in]    milliSeconds  時間の長さをミリ秒数で指定します。
83 
84     @return       TimeSpan      指定された時間の長さを表す TimeSpan オブジェクトを返します。
85     */
FromMilliSeconds(s64 milliSeconds)86     static TimeSpan FromMilliSeconds(s64 milliSeconds) { return FromNanoSeconds(milliSeconds * 1000 * 1000); }
87 
88     /*!
89     @brief        秒数から TimeSpan オブジェクトを生成します。
90 
91     @param[in]    seconds       時間の長さを秒数で指定します。
92 
93     @return       TimeSpan      指定された時間の長さを表す TimeSpan オブジェクトを返します。
94     */
FromSeconds(s64 seconds)95     static TimeSpan FromSeconds(s64 seconds) { return FromNanoSeconds(seconds * 1000 * 1000 * 1000); }
96 
97     /*!
98     @brief        分か数ら TimeSpan オブジェクトを生成します。
99 
100     @param[in]    minutes       時間の長さを分数で指定します。
101 
102     @return       TimeSpan      指定された時間の長さを表す TimeSpan オブジェクトを返します。
103     */
FromMinutes(s64 minutes)104     static TimeSpan FromMinutes(s64 minutes) { return FromNanoSeconds(minutes * 1000 * 1000 * 1000 * 60); }
105 
106     /*!
107     @brief        時間数から TimeSpan オブジェクトを生成します。
108 
109     @param[in]    hours         時間の長さを時数で指定します。
110 
111     @return       TimeSpan      指定された時間の長さを表す TimeSpan オブジェクトを返します。
112     */
FromHours(s64 hours)113     static TimeSpan FromHours(s64 hours) { return FromNanoSeconds(hours * 1000 * 1000 * 1000 * 60 * 60); }
114 
115     /*!
116     @brief        日数から TimeSpan オブジェクトを生成します。
117 
118     @param[in]    days         時間(日数)を指定します。
119 
120     @return       TimeSpan      指定された時間の長さを表す TimeSpan オブジェクトを返します。
121     */
FromDays(s64 days)122     static TimeSpan FromDays(s64 days) { return FromNanoSeconds(days * 1000 * 1000 * 1000 * 60 * 60 * 24); }
123 
124     /*!
125     @brief        時間を日数で取得します。
126 
127     @return       s64           この TimeSpan オブジェクトが表す時間の長さを日数に換算した値を返します。
128     */
GetDays()129     s64 GetDays() const { return m_NanoSeconds / (1000LL * 1000 * 1000 * 60 * 60 * 24); }
130 
131     /*!
132     @brief        時間を時数で取得します。
133 
134     @return       s64           この TimeSpan オブジェクトが表す時間の長さを時数に換算した値を返します。
135     */
GetHours()136     s64 GetHours() const { return m_NanoSeconds / (1000LL * 1000 * 1000 * 60 * 60); }
137 
138     /*!
139     @brief        時間を分数で取得します。
140 
141     @return       s64           この TimeSpan オブジェクトが表す時間の長さを分数に換算した値を返します。
142     */
GetMinutes()143     s64 GetMinutes() const { return m_NanoSeconds / (1000LL * 1000 * 1000 * 60); }
144 
145     /*!
146     @brief        時間を秒数で取得します。
147 
148     @return       s64           この TimeSpan オブジェクトが表す時間の長さを秒数に換算した値を返します。
149     */
GetSeconds()150     s64 GetSeconds() const { return m_NanoSeconds / (1000 * 1000 * 1000); }
151     /*!
152     @brief        時間をミリ秒数で取得します。
153 
154     @return       TimeSpan      この TimeSpan オブジェクトが表す時間の長さをミリ秒数に換算した値を返します。
155     */
GetMilliSeconds()156     s64 GetMilliSeconds() const { return m_NanoSeconds / (1000 * 1000); }
157     /*!
158     @brief        時間をマイクロ秒数で取得します。
159 
160     @return       TimeSpan      この TimeSpan オブジェクトが表す時間の長さをマイクロ秒数に換算した値を返します。
161     */
GetMicroSeconds()162     s64 GetMicroSeconds() const { return m_NanoSeconds / 1000; }
163     /*!
164     @brief        時間をナノ秒数で取得します。
165 
166     @return       TimeSpan      この TimeSpan オブジェクトが表す時間の長さをナノ秒数に換算した値を返します。
167     */
GetNanoSeconds()168     s64 GetNanoSeconds() const { return m_NanoSeconds; }
169 
170     friend bool operator==(const TimeSpan& lhs, const TimeSpan& rhs) { return lhs.m_NanoSeconds == rhs.m_NanoSeconds; }
171     friend bool operator!=(const TimeSpan& lhs, const TimeSpan& rhs) { return !(lhs == rhs); }
172     friend bool operator< (const TimeSpan& lhs, const TimeSpan& rhs) { return lhs.m_NanoSeconds <  rhs.m_NanoSeconds; }
173     friend bool operator> (const TimeSpan& lhs, const TimeSpan& rhs) { return rhs < lhs; }
174     friend bool operator<=(const TimeSpan& lhs, const TimeSpan& rhs) { return !(lhs > rhs); }
175     friend bool operator>=(const TimeSpan& lhs, const TimeSpan& rhs) { return !(lhs < rhs); }
176 
177     TimeSpan& operator+=(const TimeSpan& rhs) { this->m_NanoSeconds += rhs.m_NanoSeconds; return *this; }
178     friend TimeSpan operator+(const TimeSpan& lhs, const TimeSpan& rhs) { TimeSpan ret(lhs); return ret += rhs; }
179 
180     TimeSpan& operator-=(const TimeSpan& rhs) { this->m_NanoSeconds -= rhs.m_NanoSeconds; return *this; }
181     friend TimeSpan operator-(const TimeSpan& lhs, const TimeSpan& rhs) { TimeSpan ret(lhs); return ret -= rhs; }
182 
183 private:
184 
185     s64 m_NanoSeconds;
186 
187 };
188 
189 }}	// end of namespace nn
190 
191 #endif // __cplusplus
192 
193 #endif
194