2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
20 * @brief This is the implementation file for Int8 class.
27 #include <FBaseInt8.h>
28 #include <FBaseResult.h>
29 #include <FBaseCharacter.h>
30 #include <FBaseSysLog.h>
34 namespace Tizen { namespace Base
37 Int8::Int8(char value)
38 : value((signed char)value)
43 Int8::Int8(const Int8& value)
54 Int8::operator =(const Int8& rhs)
64 Int8::Compare(char ch1, char ch2)
66 return ((signed char) ch1 < (signed char) ch2 ? -1 : (ch1 == ch2 ? 0 : 1));
70 Int8::CompareTo(const Int8& value) const
72 return (Int8::Compare(this->value, value.value));
76 Int8::Equals(const Object& obj) const
78 const Int8* pOther = dynamic_cast <const Int8*>(&obj);
84 return (value == (*pOther).value);
88 Int8::GetHashCode(void) const
90 return static_cast<int> (value);
94 Int8::GetHashCode(char val)
96 return static_cast<int> (val);
100 Int8::Decode(const String& s, char& ret)
102 SysTryReturn(NID_BASE, s.GetLength() >= 1, E_NUM_FORMAT, E_NUM_FORMAT,
103 "[%s] The length of string s MUST be greater than 0.", GetErrorMessage(E_NUM_FORMAT));
107 wchar_t* pEnd = null;
113 radix = Character::RADIX_HEXADECIMAL;
118 else if (s[0] == L'0' && (s.GetLength() >= 2))
120 if (s[1] == L'x' || s[1] == L'X')
122 radix = Character::RADIX_HEXADECIMAL;
126 radix = Character::RADIX_OCTAL;
131 radix = Character::RADIX_DECIMAL;
134 result r = E_SUCCESS;
137 value = wcstol(str.GetPointer(), &pEnd, radix);
138 SysTryCatch(NID_BASE, (pEnd[0] == 0), r = E_NUM_FORMAT, E_NUM_FORMAT,
139 "[%s] Int8 decode failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
140 SysTryCatch(NID_BASE, !((value == LONG_MAX || value == LONG_MIN) && (errno != 0)), r = E_NUM_FORMAT, E_NUM_FORMAT,
141 "[%s] Decoded value cannot fit into an Int8.", GetErrorMessage(E_NUM_FORMAT));
144 if (value > Int8::VALUE_MAX)
146 ret = Int8::VALUE_MAX;
148 else if (value < (signed char) Int8::VALUE_MIN)
150 ret = (signed char) Int8::VALUE_MIN;
161 Int8::Parse(const String& s, char& ret)
163 int len = s.GetLength();
164 SysTryReturn(NID_BASE, (len > 0 && len < 5), E_NUM_FORMAT, E_NUM_FORMAT,
165 "[%s] The length of s(%ls) MUST be greater than 0 and less than 5.", GetErrorMessage(E_NUM_FORMAT), s.GetPointer());
167 return Parse(s, Character::RADIX_DECIMAL, ret);
171 Int8::Parse(const String& s, int radix, char& ret)
173 SysTryReturn(NID_BASE, ((radix == Character::RADIX_BINARY) || (radix == Character::RADIX_OCTAL) ||
174 (radix == Character::RADIX_DECIMAL) || (radix == Character::RADIX_HEXADECIMAL)), E_OUT_OF_RANGE, E_OUT_OF_RANGE,
175 "[%s] The radix(%d) MUST be one of 2, 8, 10 and 16.", GetErrorMessage(E_OUT_OF_RANGE), radix);
177 wchar_t* pEnd = null;
180 int len = s.GetLength();
181 SysTryReturn(NID_BASE, (len > 0), E_NUM_FORMAT, E_NUM_FORMAT, "[%s] The length of s MUST be greater than 0.",
182 GetErrorMessage(E_NUM_FORMAT));
184 result r = E_SUCCESS;
187 value = wcstol(s.GetPointer(), &pEnd, radix);
188 SysTryCatch(NID_BASE, (pEnd[0] == 0), r = E_NUM_FORMAT, E_NUM_FORMAT, "[%s] Int8 parse failed. Scan stopped at (%ls).",
189 GetErrorMessage(E_NUM_FORMAT), pEnd);
190 SysTryCatch(NID_BASE, !((value == LONG_MAX || value == LONG_MIN) && (errno != 0)), r = E_NUM_FORMAT, E_NUM_FORMAT,
191 "[%s] Parsed value cannot fit into an Int8.", GetErrorMessage(E_NUM_FORMAT));
194 if (value > Int8::VALUE_MAX)
196 ret = Int8::VALUE_MAX;
198 else if (value < (signed char) Int8::VALUE_MIN)
200 ret = (signed char) Int8::VALUE_MIN;
211 Int8::ToChar(void) const
213 return static_cast<char> (value);
217 Int8::ToShort(void) const
219 return static_cast<short> (value);
223 Int8::ToInt(void) const
225 return static_cast<int> (value);
229 Int8::ToLong(void) const
231 return static_cast<long> (value);
235 Int8::ToLongLong(void) const
237 return static_cast<long long> (value);
241 Int8::ToFloat(void) const
243 return static_cast<float> (value);
247 Int8::ToDouble(void) const
249 return static_cast<double> (value);
253 Int8::ToString(void) const
255 return (Int8::ToString(value));
259 Int8::ToString(char value)
261 const static unsigned int INT8_LENGTH_MAX = 4;
263 wchar_t sValue[INT8_LENGTH_MAX + 1] = {0, };
264 swprintf(sValue, (sizeof(sValue) / sizeof(sValue[0])), L"%d", value);
266 return String(sValue);