55c1bf22882c437a99d4146b80ed352207119b47
[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 #include <cfloat>
23 #include <math.h>
24 #include <stdio.h>
25 #include <wchar.h>
26 #include <errno.h>
27 #include <limits.h>
28 #include <FBaseDouble.h>
29 #include <FBaseResult.h>
30 #include <FBaseSysLog.h>
31 #include "FBase_LocalizedNumParser.h"
32
33 namespace Tizen { namespace Base
34 {
35
36 union DoubleBitRep
37 {
38         explicit
39         DoubleBitRep(double d = 0.0)
40                 : value(d)
41         {
42         };
43
44         DoubleBitRep(long long l)
45                 : rep(l)
46         {
47         };
48
49         double value;
50         long long rep;
51 };
52
53 Double::Double(double value)
54         : value(value)
55         , __pDoubleImpl(null)
56 {
57 }
58
59 Double::Double(const Double& value)
60         : value(value.value)
61         , __pDoubleImpl(null)
62 {
63 }
64
65 Double::~Double(void)
66 {
67 }
68
69 Double&
70 Double::operator =(const Double& rhs)
71 {
72         if (&rhs != this)
73         {
74                 value = rhs.value;
75         }
76         return *this;
77 }
78
79 int
80 Double::Compare(double d1, double d2)
81 {
82         int ret = 0;
83         if (d1 > d2)
84         {
85                 ret = 1;
86         }
87         else if (d1 < d2)
88         {
89                 ret = -1;
90         }
91         else
92         {
93                 ret = 0;
94         }
95         return ret;
96 }
97
98 int
99 Double::CompareTo(double value) const
100 {
101         return(Double::Compare(this->value, value));
102 }
103
104 int
105 Double::CompareTo(const Double& value) const
106 {
107         return(Double::Compare(this->value, value.value));
108 }
109
110 bool
111 Double::Equals(const Object& obj) const
112 {
113         const Double* pOther = dynamic_cast< const Double* >(&obj);
114
115         if (pOther == null)
116         {
117                 return false;
118         }
119
120         if (Double::Compare(this->value, (*pOther).value) == 0)
121         {
122                 return true;
123         }
124         else
125         {
126                 return false;
127         }
128 }
129
130 int
131 Double::GetHashCode(void) const
132 {
133         double* pTemp = const_cast< double* >(&value);
134         int* pValueLow = reinterpret_cast< int* >(pTemp);
135         int* pValueHigh = pValueLow + 1;
136         return *pValueLow + *pValueHigh;
137 }
138
139 int
140 Double::GetHashCode(double val)
141 {
142         int* pValueLow = reinterpret_cast< int* >(&val);
143         int* pValueHigh = pValueLow + 1;
144         return *pValueLow + *pValueHigh;
145 }
146
147 result
148 Double::Parse(const String& s, double& ret)
149 {
150         double tmpRet = _LocalizedNumParser::ToDouble(s, "");
151
152         if (IsNaN(tmpRet))
153         {
154                 result r = GetLastResult();
155                 SysLogException(NID_BASE, r, "[%s] Propagating.", GetErrorMessage(r));
156                 return r;
157         }
158
159         ret = tmpRet;
160         return E_SUCCESS;
161 }
162
163 char
164 Double::ToChar(void) const
165 {
166         return static_cast< char >(value);
167 }
168
169 short
170 Double::ToShort(void) const
171 {
172         return static_cast< short >(value);
173 }
174
175 int
176 Double::ToInt(void) const
177 {
178         return static_cast< int >(value);
179 }
180
181 long
182 Double::ToLong(void) const
183 {
184         return static_cast< long >(value);
185 }
186
187 long long
188 Double::ToLongLong(void) const
189 {
190         return static_cast< long long >(value);
191 }
192
193 float
194 Double::ToFloat(void) const
195 {
196         return static_cast< float >(value);
197 }
198
199 double
200 Double::ToDouble(void) const
201 {
202         return value;
203 }
204
205 String
206 Double::ToString(void) const
207 {
208         return Double::ToString(value);
209 }
210
211 String
212 Double::ToString(int precision) const
213 {
214 //Dummy implementation to resolve build break
215         return String(L"");
216 }
217
218 String
219 Double::ToString(double value)
220 {
221         return _LocalizedNumParser::ToString(value, "");
222 }
223
224 String
225 Double::ToString(double value, int precision)
226 {
227 //Dummy implementation to resolve build break
228         return String(L"");
229 }
230
231
232 long long
233 Double::ToBits(double value)
234 {
235         DoubleBitRep bitRep;
236         bitRep.value = value;
237         return (long long) bitRep.rep;
238 }
239
240 double
241 Double::ToDoubleFromBits(long long value)
242 {
243         DoubleBitRep bitRep;
244         bitRep.rep = value;
245         return bitRep.value;
246 }
247
248 bool
249 Double::IsFinite(double d)
250 {
251         return isfinite(d);
252 }
253
254 bool
255 Double::IsInfinity(void) const
256 {
257         return Double::IsInfinity(value);
258 }
259
260 bool
261 Double::IsInfinity(double value)
262 {
263         return !Double::IsFinite(value);
264 }
265
266 bool
267 Double::IsNaN(void) const
268 {
269         return Double::IsNaN(value);
270 }
271
272 bool
273 Double::IsNaN(double value)
274 {
275         return isnan(value);
276 }
277
278 double
279 Double::GetMaxValue(void)
280 {
281         return DBL_MAX;
282 }
283
284 double
285 Double::GetMinValue(void)
286 {
287         return DBL_MIN;
288 }
289
290 }} // Tizen::Base