Merge "[2.2.1][P130807-00904] Fail to get the host of the uri which has only ? charac...
[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(double value)
213 {
214         return _LocalizedNumParser::ToString(value, "");
215 }
216
217 long long
218 Double::ToBits(double value)
219 {
220         DoubleBitRep bitRep;
221         bitRep.value = value;
222         return (long long) bitRep.rep;
223 }
224
225 double
226 Double::ToDoubleFromBits(long long value)
227 {
228         DoubleBitRep bitRep;
229         bitRep.rep = value;
230         return bitRep.value;
231 }
232
233 bool
234 Double::IsFinite(double d)
235 {
236         return isfinite(d);
237 }
238
239 bool
240 Double::IsInfinity(void) const
241 {
242         return Double::IsInfinity(value);
243 }
244
245 bool
246 Double::IsInfinity(double value)
247 {
248         return !Double::IsFinite(value);
249 }
250
251 bool
252 Double::IsNaN(void) const
253 {
254         return Double::IsNaN(value);
255 }
256
257 bool
258 Double::IsNaN(double value)
259 {
260         return isnan(value);
261 }
262
263 double
264 Double::GetMaxValue(void)
265 {
266         return DBL_MAX;
267 }
268
269 double
270 Double::GetMinValue(void)
271 {
272         return DBL_MIN;
273 }
274
275 }} // Tizen::Base