Fix the boiler plate codes
[platform/framework/native/appfw.git] / src / base / FBaseDouble.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file                FBaseDouble.cpp
19  * @brief               This is the implementation file for Double class.
20  * @see                 Number
21  */
22
23 #include <cfloat>
24 #include <math.h>
25 #include <stdio.h>
26 #include <wchar.h>
27 #include <errno.h>
28 #include <limits.h>
29 #include <FBaseDouble.h>
30 #include <FBaseResult.h>
31 #include "FBase_NativeError.h"
32 #include <FBaseSysLog.h>
33
34
35 namespace Tizen { namespace Base
36 {
37
38 union DoubleBitRep
39 {
40         explicit
41         DoubleBitRep(double d = 0.0)
42                 : value(d)
43         {
44         };
45
46         DoubleBitRep(long long l)
47                 : rep(l)
48         {
49         };
50
51         double value;
52         long long rep;
53 };
54
55 Double::Double(double value)
56         : value(value)
57         , __pDoubleImpl(null)
58 {
59 }
60
61 Double::Double(const Double& value)
62         : value(value.value)
63         , __pDoubleImpl(null)
64 {
65 }
66
67 Double::~Double(void)
68 {
69 }
70
71 Double&
72 Double::operator =(const Double& rhs)
73 {
74         if (&rhs != this)
75         {
76                 value = rhs.value;
77         }
78         return *this;
79 }
80
81 int
82 Double::Compare(double d1, double d2)
83 {
84         int ret = 0;
85         if (d1 > d2)
86         {
87                 ret = 1;
88         }
89         else if (d1 < d2)
90         {
91                 ret = -1;
92         }
93         else
94         {
95                 ret = 0;
96         }
97         return ret;
98 }
99
100 int
101 Double::CompareTo(double value) const
102 {
103         return(Double::Compare(this->value, value));
104 }
105
106 int
107 Double::CompareTo(const Double& value) const
108 {
109         return(Double::Compare(this->value, value.value));
110 }
111
112 bool
113 Double::Equals(const Object& obj) const
114 {
115         const Double* pOther = dynamic_cast <const Double*>(&obj);
116
117         if (pOther == null)
118         {
119                 return false;
120         }
121
122         if (Double::Compare(this->value, (*pOther).value) == 0)
123         {
124                 return true;
125         }
126         else
127         {
128                 return false;
129         }
130 }
131
132 int
133 Double::GetHashCode(void) const
134 {
135         double* pTemp = const_cast<double*> (&value);
136         int* pValueLow = reinterpret_cast<int*> (pTemp);
137         int* pValueHigh = pValueLow + 1;
138         return *pValueLow + *pValueHigh;
139 }
140
141 int
142 Double::GetHashCode(double val)
143 {
144         int* pValueLow = reinterpret_cast<int*> (&val);
145         int* pValueHigh = pValueLow + 1;
146         return *pValueLow + *pValueHigh;
147 }
148
149 result
150 Double::Parse(const String& s, double& ret)
151 {
152         wchar_t* pEnd = null;
153         errno = 0;
154         double tmpRet = wcstod(s.GetPointer(), &pEnd);
155         SysTryReturnResult(NID_BASE, ((!(!Double::Compare(tmpRet, 0)  && errno == EINVAL)) && (pEnd[0] == 0)), E_NUM_FORMAT,
156                 "Double parse failed with reason (%s). Scan stopped at (%ls)", __ConvertNativeErrorToMessage(errno), pEnd);
157         SysTryReturnResult(NID_BASE, !(errno != 0 && (!Double::Compare(tmpRet, HUGE_VAL)|| !Double::Compare(tmpRet, -HUGE_VAL))),
158                 E_NUM_FORMAT, "Parsed value cannot fit into a Double.");
159
160         ret = tmpRet;
161         return E_SUCCESS;
162 }
163
164 char
165 Double::ToChar(void) const
166 {
167         return static_cast<char> (value);
168 }
169
170 short
171 Double::ToShort(void) const
172 {
173         return static_cast<short> (value);
174 }
175
176 int
177 Double::ToInt(void) const
178 {
179         return static_cast<int> (value);
180 }
181
182 long
183 Double::ToLong(void) const
184 {
185         return static_cast<long> (value);
186 }
187
188 long long
189 Double::ToLongLong(void) const
190 {
191         return static_cast<long long> (value);
192 }
193
194 float
195 Double::ToFloat(void) const
196 {
197         return static_cast<float> (value);
198 }
199
200 double
201 Double::ToDouble(void) const
202 {
203         return value;
204 }
205
206 String
207 Double::ToString(void) const
208 {
209         return(Double::ToString(value));
210 }
211
212 String
213 Double::ToString(double value)
214 {
215         const static unsigned int MAX_DIG = 17 + 3;
216         const static unsigned int DOUBLE_LENGTH_MAX = __DBL_MAX_10_EXP + MAX_DIG;
217
218         if (Double::IsNaN(value))
219         {
220                 return String(L"NaN");
221         }
222         else if (Double::IsInfinity(value))
223         {
224                 return String(L"Infinity");
225         }
226         else
227         {
228                 wchar_t sValue[DOUBLE_LENGTH_MAX + 1] = {0, };
229                 swprintf(sValue, (sizeof(sValue) / sizeof(sValue[0])), L"%#lg", value);
230                 return String(sValue);
231         }
232 }
233
234 long long
235 Double::ToBits(double value)
236 {
237         DoubleBitRep bitRep;
238         bitRep.value = value;
239         return (long long) bitRep.rep;
240 }
241
242 double
243 Double::ToDoubleFromBits(long long value)
244 {
245         DoubleBitRep bitRep;
246         bitRep.rep = value;
247         return bitRep.value;
248 }
249
250 bool
251 Double::IsFinite(double d)
252 {
253         return((isfinite(d) != 0) ? true : false);
254 }
255
256 bool
257 Double::IsInfinity(void) const
258 {
259         return(Double::IsInfinity(value));
260 }
261
262 bool
263 Double::IsInfinity(double value)
264 {
265         return(!Double::IsFinite(value));
266 }
267
268 bool
269 Double::IsNaN(void) const
270 {
271         return(Double::IsNaN(value));
272 }
273
274 bool
275 Double::IsNaN(double value)
276 {
277         return((isnan(value) != 0) ? true : false);
278 }
279
280 double
281 Double::GetMaxValue(void)
282 {
283         return DBL_MAX;
284 }
285
286 double
287 Double::GetMinValue(void)
288 {
289         return DBL_MIN;
290 }
291
292 }} // Tizen::Base