2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FBaseInteger.cpp
19 * @brief This is the implementation file for Integer class.
26 #include <FBaseInteger.h>
27 #include <FBaseResult.h>
28 #include <FBaseCharacter.h>
29 #include <FBaseSysLog.h>
31 namespace Tizen { namespace Base
34 Integer::Integer(int value)
36 , __pIntegerImpl(null)
40 Integer::Integer(const Integer& value)
42 , __pIntegerImpl(null)
46 Integer::~Integer(void)
52 Integer::operator =(const Integer& rhs)
62 Integer::Compare(int i1, int i2)
64 return(i1 < i2 ? -1 : (i1 == i2 ? 0 : 1));
68 Integer::CompareTo(const Integer& value) const
70 return(Integer::Compare(this->value, value.value));
74 Integer::Equals(const Object& obj) const
76 const Integer* pOther = dynamic_cast <const Integer*>(&obj);
82 return value == (*pOther).value;
86 Integer::Decode(const String& s, int& ret)
88 SysTryReturn(NID_BASE, s.GetLength() >= 1, E_NUM_FORMAT, E_NUM_FORMAT,
89 "[%s] The length of s MUST be greater than 0.", GetErrorMessage(E_NUM_FORMAT));
97 if (s[0] == L'-' || s[0] == L'+')
104 if (s[startIndex] == L'#')
106 radix = Character::RADIX_HEXADECIMAL;
109 str.Remove(startIndex, 1);
111 else if (s[startIndex] == L'0' && (s.GetLength() >= minLength))
113 if (s[startIndex + 1] == L'x' || s[startIndex + 1] == L'X')
115 radix = Character::RADIX_HEXADECIMAL;
119 radix = Character::RADIX_OCTAL;
124 radix = Character::RADIX_DECIMAL;
128 ret = wcstol(str.GetPointer(), &pEnd, radix);
129 SysTryReturn(NID_BASE, (pEnd[0] == 0), E_NUM_FORMAT, E_NUM_FORMAT,
130 "[%s] Integer decode failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
131 SysTryReturn(NID_BASE, !((ret == LONG_MAX || ret == LONG_MIN) && (errno != 0)), E_NUM_FORMAT, E_NUM_FORMAT,
132 "[%s] Decoded value cannot fit into an Integer.", GetErrorMessage(E_NUM_FORMAT));
138 Integer::GetHashCode(void) const
144 Integer::GetHashCode(int val)
150 Integer::Parse(const String& s, int& ret)
152 int len = s.GetLength();
153 SysTryReturn(NID_BASE, (len > 0 && len < 12), E_NUM_FORMAT, E_NUM_FORMAT,
154 "[%s] The length of s(%ls) MUST be greater than 0 and less than 12.",
155 GetErrorMessage(E_NUM_FORMAT), s.GetPointer());
157 return Parse(s, Character::RADIX_DECIMAL, ret);
161 Integer::Parse(const String& s, int radix, int& ret)
163 SysTryReturn(NID_BASE, ((radix == Character::RADIX_BINARY) || (radix == Character::RADIX_OCTAL) ||
164 (radix == Character::RADIX_DECIMAL) || (radix == Character::RADIX_HEXADECIMAL)), E_OUT_OF_RANGE, E_OUT_OF_RANGE,
165 "[%s] The radix(%d) MUST be one of 2, 8, 10 and 16.", GetErrorMessage(E_OUT_OF_RANGE), radix);
167 int len = s.GetLength();
168 SysTryReturn(NID_BASE, (len > 0), E_NUM_FORMAT, E_NUM_FORMAT, "[%s] The length of s MUST be greater than 0.",
169 GetErrorMessage(E_NUM_FORMAT));
172 wchar_t* pEnd = null;
173 int tmpRet = wcstol(s.GetPointer(), &pEnd, radix);
174 SysTryReturn(NID_BASE, (pEnd[0] == 0), E_NUM_FORMAT, E_NUM_FORMAT,
175 "[%s] Integer parse failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
176 SysTryReturn(NID_BASE, !((tmpRet == LONG_MAX || tmpRet == LONG_MIN) && (errno != 0)), E_NUM_FORMAT, E_NUM_FORMAT,
177 "[%s] Parsed value cannot fit into an Integer.", GetErrorMessage(E_NUM_FORMAT));
184 Integer::ToChar(void) const
186 return static_cast<char> (value);
190 Integer::ToShort(void) const
192 return static_cast<short> (value);
196 Integer::ToInt(void) const
202 Integer::ToLong(void) const
204 return static_cast<long> (value);
208 Integer::ToLongLong(void) const
210 return static_cast<long long> (value);
214 Integer::ToFloat(void) const
216 return static_cast<float> (value);
220 Integer::ToDouble(void) const
222 return static_cast<double> (value);
226 Integer::ToString(void) const
228 return(Integer::ToString(value));
232 Integer::ToString(int value)
234 const static unsigned int INTEGER_LENGTH_MAX = 11;
236 wchar_t sValue[INTEGER_LENGTH_MAX + 1] = {0, };
237 swprintf(sValue, (sizeof(sValue) / sizeof(sValue[0])), L"%d", value);
239 return String(sValue);