Fix the boiler plate codes
[platform/framework/native/appfw.git] / src / base / FBaseInt8.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                FBaseInt8.cpp
19  * @brief               This is the implementation file for Int8 class.
20  * @see                 Number class
21  */
22
23 #include <wchar.h>
24 #include <errno.h>
25 #include <limits.h>
26 #include <FBaseInt8.h>
27 #include <FBaseResult.h>
28 #include <FBaseCharacter.h>
29 #include <FBaseSysLog.h>
30
31
32
33 namespace Tizen { namespace Base
34 {
35
36 Int8::Int8(char value)
37         : value((signed char)value)
38         , __pInt8Impl(null)
39 {
40 }
41
42 Int8::Int8(const Int8& value)
43         : value(value.value)
44         , __pInt8Impl(null)
45 {
46 }
47
48 Int8::~Int8(void)
49 {
50 }
51
52 Int8&
53 Int8::operator =(const Int8& rhs)
54 {
55         if (&rhs != this)
56         {
57                 value = rhs.value;
58         }
59         return (*this);
60 }
61
62 int
63 Int8::Compare(char ch1, char ch2)
64 {
65         return ((signed char) ch1 < (signed char) ch2 ? -1 : (ch1 == ch2 ? 0 : 1));
66 }
67
68 int
69 Int8::CompareTo(const Int8& value) const
70 {
71         return (Int8::Compare(this->value, value.value));
72 }
73
74 bool
75 Int8::Equals(const Object& obj) const
76 {
77         const Int8* pOther = dynamic_cast <const Int8*>(&obj);
78         if (pOther == null)
79         {
80                 return (false);
81         }
82
83         return (value == (*pOther).value);
84 }
85
86 int
87 Int8::GetHashCode(void) const
88 {
89         return static_cast<int> (value);
90 }
91
92 int
93 Int8::GetHashCode(char val)
94 {
95         return static_cast<int> (val);
96 }
97
98 result
99 Int8::Decode(const String& s, char& ret)
100 {
101         SysTryReturn(NID_BASE, s.GetLength() >= 1, E_NUM_FORMAT, E_NUM_FORMAT,
102                 "[%s] The length of string s MUST be greater than 0.", GetErrorMessage(E_NUM_FORMAT));
103
104         long value = 0;
105         int radix = 0;
106         int startIndex = 0;
107         int minLength = 2;
108         wchar_t* pEnd = null;
109         String str(s);
110
111         if (s[0] == L'-' || s[0] == L'+')
112         {
113                 startIndex = 1;
114                 minLength = 3;
115         }
116
117         // Find radix
118         if (s[startIndex] == L'#')
119         {
120                 radix = Character::RADIX_HEXADECIMAL;
121
122                 // Remove '#'
123                 str.Remove(startIndex, 1);
124         }
125         else if (s[startIndex] == L'0' && (s.GetLength() >= minLength))
126         {
127                 if (s[startIndex + 1] == L'x' || s[startIndex + 1] == L'X')
128                 {
129                         radix = Character::RADIX_HEXADECIMAL;
130                 }
131                 else
132                 {
133                         radix = Character::RADIX_OCTAL;
134                 }
135         }
136         else
137         {
138                 radix = Character::RADIX_DECIMAL;
139         }
140
141         result r = E_SUCCESS;
142
143         errno = 0;
144         value = wcstol(str.GetPointer(), &pEnd, radix);
145         SysTryCatch(NID_BASE, (pEnd[0] == 0), r = E_NUM_FORMAT, E_NUM_FORMAT,
146                 "[%s] Int8 decode failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
147         SysTryCatch(NID_BASE, !((value == LONG_MAX || value == LONG_MIN) && (errno != 0)), r = E_NUM_FORMAT, E_NUM_FORMAT,
148                 "[%s] Decoded value cannot fit into an Int8.", GetErrorMessage(E_NUM_FORMAT));
149
150 CATCH:
151         if (value > Int8::VALUE_MAX)
152         {
153                 ret = Int8::VALUE_MAX;
154         }
155         else if (value < (signed char) Int8::VALUE_MIN)
156         {
157                 ret = (signed char) Int8::VALUE_MIN;
158         }
159         else
160         {
161                 ret = (char) value;
162         }
163
164         return r;
165 }
166
167 result
168 Int8::Parse(const String& s, char& ret)
169 {
170         int len = s.GetLength();
171         SysTryReturn(NID_BASE, (len > 0 && len < 5), E_NUM_FORMAT, E_NUM_FORMAT,
172                                 "[%s] The length of s(%ls) MUST be greater than 0 and less than 5.", GetErrorMessage(E_NUM_FORMAT), s.GetPointer());
173
174         return Parse(s, Character::RADIX_DECIMAL, ret);
175 }
176
177 result
178 Int8::Parse(const String& s, int radix, char& ret)
179 {
180         SysTryReturn(NID_BASE, ((radix == Character::RADIX_BINARY) || (radix == Character::RADIX_OCTAL) ||
181                 (radix == Character::RADIX_DECIMAL) || (radix == Character::RADIX_HEXADECIMAL)), E_OUT_OF_RANGE, E_OUT_OF_RANGE,
182                 "[%s] The radix(%d) MUST be one of 2, 8, 10 and 16.", GetErrorMessage(E_OUT_OF_RANGE), radix);
183
184         int len = s.GetLength();
185         SysTryReturn(NID_BASE, (len > 0), E_NUM_FORMAT, E_NUM_FORMAT, "[%s] The length of s MUST be greater than 0.",
186                 GetErrorMessage(E_NUM_FORMAT));
187
188         result r = E_SUCCESS;
189
190         errno = 0;
191         wchar_t* pEnd = null;
192         long value = wcstol(s.GetPointer(), &pEnd, radix);
193         SysTryReturn(NID_BASE, (pEnd[0] == 0), E_NUM_FORMAT, E_NUM_FORMAT, "[%s] Int8 parse failed. Scan stopped at (%ls).",
194                 GetErrorMessage(E_NUM_FORMAT), pEnd);
195         SysTryReturn(NID_BASE, !((value == LONG_MAX || value == LONG_MIN) && (errno != 0)), E_NUM_FORMAT, E_NUM_FORMAT,
196                 "[%s] Parsed value cannot fit into an Int8.", GetErrorMessage(E_NUM_FORMAT));
197
198         if (value > Int8::VALUE_MAX)
199         {
200                 ret = Int8::VALUE_MAX;
201         }
202         else if (value < static_cast< signed char >(Int8::VALUE_MIN))
203         {
204                 ret = static_cast< signed char >(Int8::VALUE_MIN);
205         }
206         else
207         {
208                 ret = static_cast< char >(value);
209         }
210
211         return r;
212 }
213
214 char
215 Int8::ToChar(void) const
216 {
217         return static_cast<char> (value);
218 }
219
220 short
221 Int8::ToShort(void) const
222 {
223         return static_cast<short> (value);
224 }
225
226 int
227 Int8::ToInt(void) const
228 {
229         return static_cast<int> (value);
230 }
231
232 long
233 Int8::ToLong(void) const
234 {
235         return static_cast<long> (value);
236 }
237
238 long long
239 Int8::ToLongLong(void) const
240 {
241         return static_cast<long long> (value);
242 }
243
244 float
245 Int8::ToFloat(void) const
246 {
247         return static_cast<float> (value);
248 }
249
250 double
251 Int8::ToDouble(void) const
252 {
253         return static_cast<double> (value);
254 }
255
256 String
257 Int8::ToString(void) const
258 {
259         return (Int8::ToString(value));
260 }
261
262 String
263 Int8::ToString(char value)
264 {
265         const static unsigned int INT8_LENGTH_MAX = 4;
266
267         wchar_t sValue[INT8_LENGTH_MAX + 1] = {0, };
268         swprintf(sValue, (sizeof(sValue) / sizeof(sValue[0])), L"%d", value);
269
270         return String(sValue);
271 }
272
273 }} //Tizen::Base