Fix the boiler plate codes
[platform/framework/native/appfw.git] / src / base / FBaseFloat.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                FBaseFloat.cpp
19  * @brief               This is the implementation file for Float class.
20  * @see                 Number class
21  */
22
23 // Includes
24 #include <cfloat>
25 #include <math.h>
26 #include <stdio.h>
27 #include <wchar.h>
28 #include <errno.h>
29 #include <limits.h>
30 #include <FBaseFloat.h>
31 #include <FBaseResult.h>
32 #include <FBaseSysLog.h>
33
34 namespace Tizen { namespace Base
35 {
36
37 union FloatBitRep
38 {
39         explicit
40         FloatBitRep(float f = 0.0)
41                 : value(f)
42         {
43         };
44
45         FloatBitRep(unsigned int i)
46                 : rep(i)
47         {
48         };
49
50         float value;
51         unsigned int rep;
52 };
53
54 Float::Float(float value)
55         : value(value)
56         , __pFloatImpl(null)
57 {
58 }
59
60 Float::Float(const Float& value)
61         : value(value.value)
62         , __pFloatImpl(null)
63 {
64 }
65
66 Float::~Float(void)
67 {
68 }
69
70 Float&
71 Float::operator =(const Float& rhs)
72 {
73         if (&rhs != this)
74         {
75                 value = rhs.value;
76         }
77         return *this;
78 }
79
80 int
81 Float::Compare(float f1, float f2)
82 {
83         int ret = 0;
84
85         if (f1 > f2)
86         {
87                 ret = 1;
88         }
89         else if (f1 < f2)
90         {
91                 ret = -1;
92         }
93         else
94         {
95                 ret = 0;
96         }
97
98         return ret;
99 }
100
101 int
102 Float::CompareTo(const Float& value) const
103 {
104         return(Float::Compare(this->value, value.value));
105 }
106
107 bool
108 Float::Equals(const Object& obj) const
109 {
110         const Float* pOther = dynamic_cast <const Float*>(&obj);
111
112         if (pOther == null)
113         {
114                 return false;
115         }
116
117         if (Float::Compare(this->value, (*pOther).value) == 0)
118         {
119                 return true;
120         }
121         else
122         {
123                 return false;
124         }
125 }
126
127 int
128 Float::GetHashCode(void) const
129 {
130         float* pTemp = const_cast<float*> (&value);
131         int* pValue = reinterpret_cast<int*> (pTemp);
132         return *pValue;
133 }
134
135 int
136 Float::GetHashCode(float val)
137 {
138         int* pValue = reinterpret_cast<int*> (&val);
139         return *pValue;
140 }
141
142 result
143 Float::Parse(const String& s, float& ret)
144 {
145         SysTryReturn(NID_BASE, s.GetLength() >= 1, E_NUM_FORMAT, E_NUM_FORMAT,
146                 "[%s] The length of s MUST be greater than 0.", GetErrorMessage(E_NUM_FORMAT));
147
148         errno = 0;
149         wchar_t* pEnd = null;
150         float tmpRet = wcstof(s.GetPointer(), &pEnd);
151         SysTryReturn(NID_BASE, (!Float::Compare(pEnd[0], 0)), E_NUM_FORMAT, E_NUM_FORMAT,
152                 "[%s] Float parse failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
153         SysTryReturn(NID_BASE, !((!Float::Compare(tmpRet, HUGE_VAL) || !Float::Compare(tmpRet, -HUGE_VAL)) && (errno != 0)),
154                 E_NUM_FORMAT, E_NUM_FORMAT, "[%s] Parsed value cannot fit into a Float.", GetErrorMessage(E_NUM_FORMAT));
155
156         ret = tmpRet;
157         return E_SUCCESS;
158 }
159
160 char
161 Float::ToChar(void) const
162 {
163         return static_cast<char> (value);
164 }
165
166 short
167 Float::ToShort(void) const
168 {
169         return  static_cast<short> (value);
170 }
171
172 int
173 Float::ToInt(void) const
174 {
175         return static_cast<int> (value);
176 }
177
178 long
179 Float::ToLong(void) const
180 {
181         return static_cast<long> (value);
182 }
183
184 long long
185 Float::ToLongLong(void) const
186 {
187         return static_cast<long long> (value);
188 }
189
190 float
191 Float::ToFloat(void) const
192 {
193         return value;
194 }
195
196 double
197 Float::ToDouble(void) const
198 {
199         return static_cast<double> (value);
200 }
201
202 String
203 Float::ToString(void) const
204 {
205         return(Float::ToString(value));
206 }
207
208 String
209 Float::ToString(float value)
210 {
211         const static unsigned int MAX_DIG = 7 + 3;
212         const static unsigned int FLOAT_LENGTH_MAX = __DBL_MAX_10_EXP + MAX_DIG;
213
214         if (Float::IsNaN(value))
215         {
216                 return String(L"NaN");
217         }
218         else if (Float::IsInfinity(value))
219         {
220                 return String(L"Infinity");
221         }
222         else
223         {
224                 wchar_t sValue[FLOAT_LENGTH_MAX + 1] = {0, };
225                 swprintf(sValue, (sizeof(sValue) / sizeof(sValue[0])), L"%g", value);
226
227                 return String(sValue);
228         }
229 }
230
231 int
232 Float::ToBits(float value)
233 {
234         FloatBitRep bitRep;
235         bitRep.value = value;
236         return bitRep.rep;
237 }
238
239 float
240 Float::ToFloatFromBits(int value)
241 {
242         FloatBitRep bitRep;
243         bitRep.rep = value;
244         return bitRep.value;
245 }
246
247 bool
248 Float::IsFinite(float value)
249 {
250         return((isfinite(value) != 0) ? true : false);
251 }
252
253 bool
254 Float::IsInfinity(void) const
255 {
256         return(Float::IsInfinity(value));
257 }
258
259 bool
260 Float::IsInfinity(float value)
261 {
262         return(!Float::IsFinite(value));
263 }
264
265 bool
266 Float::IsNaN(void) const
267 {
268         return(Float::IsNaN(value));
269 }
270
271 bool
272 Float::IsNaN(float value)
273 {
274         return((isnan(value) != 0) ? true : false);
275 }
276
277 float
278 Float::GetMaxValue(void)
279 {
280         return FLT_MAX;
281 }
282
283 float
284 Float::GetMinValue(void)
285 {
286         return FLT_MIN;
287 }
288
289 }} // Tizen::Base