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.
25 #include <FBaseInteger.h>
26 #include <FBaseResult.h>
27 #include <FBaseCharacter.h>
28 #include <FBaseSysLog.h>
30 namespace Tizen { namespace Base
33 Integer::Integer(int value)
35 , __pIntegerImpl(null)
39 Integer::Integer(const Integer& value)
41 , __pIntegerImpl(null)
45 Integer::~Integer(void)
51 Integer::operator =(const Integer& rhs)
61 Integer::Compare(int i1, int i2)
63 return(i1 < i2 ? -1 : (i1 == i2 ? 0 : 1));
67 Integer::CompareTo(const Integer& value) const
69 return(Integer::Compare(this->value, value.value));
73 Integer::Equals(const Object& obj) const
75 const Integer* pOther = dynamic_cast< const Integer* >(&obj);
81 return value == (*pOther).value;
85 Integer::Decode(const String& s, int& ret)
87 SysTryReturn(NID_BASE, s.GetLength() >= 1, E_NUM_FORMAT, E_NUM_FORMAT,
88 "[%s] The length of s MUST be greater than 0.", GetErrorMessage(E_NUM_FORMAT));
96 if (s[0] == L'-' || s[0] == L'+')
103 if (s[startIndex] == L'#')
105 radix = Character::RADIX_HEXADECIMAL;
108 str.Remove(startIndex, 1);
110 else if (s[startIndex] == L'0' && (s.GetLength() >= minLength))
112 if (s[startIndex + 1] == L'x' || s[startIndex + 1] == L'X')
114 radix = Character::RADIX_HEXADECIMAL;
118 radix = Character::RADIX_OCTAL;
123 radix = Character::RADIX_DECIMAL;
127 ret = wcstol(str.GetPointer(), &pEnd, radix);
128 SysTryReturn(NID_BASE, (pEnd[0] == 0), E_NUM_FORMAT, E_NUM_FORMAT,
129 "[%s] Integer decode failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
130 SysTryReturn(NID_BASE, !((ret == LONG_MAX || ret == LONG_MIN) && (errno != 0)), E_NUM_FORMAT, E_NUM_FORMAT,
131 "[%s] Decoded value cannot fit into an Integer.", GetErrorMessage(E_NUM_FORMAT));
137 Integer::GetHashCode(void) const
143 Integer::GetHashCode(int val)
149 Integer::Parse(const String& s, int& ret)
151 int len = s.GetLength();
152 SysTryReturn(NID_BASE, (len > 0 && len < 12), E_NUM_FORMAT, E_NUM_FORMAT,
153 "[%s] The length of s(%ls) MUST be greater than 0 and less than 12.",
154 GetErrorMessage(E_NUM_FORMAT), s.GetPointer());
156 return Parse(s, Character::RADIX_DECIMAL, ret);
160 Integer::Parse(const String& s, int radix, int& ret)
162 SysTryReturn(NID_BASE, ((radix == Character::RADIX_BINARY) || (radix == Character::RADIX_OCTAL) ||
163 (radix == Character::RADIX_DECIMAL) || (radix == Character::RADIX_HEXADECIMAL)), E_OUT_OF_RANGE, E_OUT_OF_RANGE,
164 "[%s] The radix(%d) MUST be one of 2, 8, 10 and 16.", GetErrorMessage(E_OUT_OF_RANGE), radix);
166 int len = s.GetLength();
167 SysTryReturn(NID_BASE, (len > 0), E_NUM_FORMAT, E_NUM_FORMAT, "[%s] The length of s MUST be greater than 0.",
168 GetErrorMessage(E_NUM_FORMAT));
171 wchar_t* pEnd = null;
172 int tmpRet = wcstol(s.GetPointer(), &pEnd, radix);
173 SysTryReturn(NID_BASE, (pEnd[0] == 0), E_NUM_FORMAT, E_NUM_FORMAT,
174 "[%s] Integer parse failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
175 SysTryReturn(NID_BASE, !((tmpRet == LONG_MAX || tmpRet == LONG_MIN) && (errno != 0)), E_NUM_FORMAT, E_NUM_FORMAT,
176 "[%s] Parsed value cannot fit into an Integer.", GetErrorMessage(E_NUM_FORMAT));
183 Integer::ToChar(void) const
185 return static_cast< char >(value);
189 Integer::ToShort(void) const
191 return static_cast< short >(value);
195 Integer::ToInt(void) const
201 Integer::ToLong(void) const
203 return static_cast< long >(value);
207 Integer::ToLongLong(void) const
209 return static_cast< long long >(value);
213 Integer::ToFloat(void) const
215 return static_cast< float >(value);
219 Integer::ToDouble(void) const
221 return static_cast< double >(value);
225 Integer::ToString(void) const
227 return(Integer::ToString(value));
231 Integer::ToString(int value)
233 const static unsigned int INTEGER_LENGTH_MAX = 11;
235 wchar_t sValue[INTEGER_LENGTH_MAX + 1] = {0, };
236 swprintf(sValue, (sizeof(sValue) / sizeof(sValue[0])), L"%d", value);
238 return String(sValue);