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.
19 * @file FBaseInteger.cpp
20 * @brief This is the implementation file for Integer class.
27 #include <FBaseInteger.h>
28 #include <FBaseResult.h>
29 #include <FBaseCharacter.h>
30 #include <FBaseSysLog.h>
32 namespace Tizen { namespace Base
35 Integer::Integer(int value)
37 , __pIntegerImpl(null)
41 Integer::Integer(const Integer& value)
43 , __pIntegerImpl(null)
47 Integer::~Integer(void)
53 Integer::operator =(const Integer& rhs)
63 Integer::Compare(int i1, int i2)
65 return(i1 < i2 ? -1 : (i1 == i2 ? 0 : 1));
69 Integer::CompareTo(const Integer& value) const
71 return(Integer::Compare(this->value, value.value));
75 Integer::Equals(const Object& obj) const
77 const Integer* pOther = dynamic_cast <const Integer*>(&obj);
83 return value == (*pOther).value;
87 Integer::Decode(const String& s, int& ret)
89 SysTryReturn(NID_BASE, s.GetLength() >= 1, E_NUM_FORMAT, E_NUM_FORMAT,
90 "[%s] The length of s MUST be greater than 0.", GetErrorMessage(E_NUM_FORMAT));
99 radix = Character::RADIX_HEXADECIMAL;
104 else if (s[0] == L'0' && (s.GetLength() >= 2))
106 if (s[1] == L'x' || s[1] == L'X')
108 radix = Character::RADIX_HEXADECIMAL;
112 radix = Character::RADIX_OCTAL;
117 radix = Character::RADIX_DECIMAL;
121 ret = wcstol(str.GetPointer(), &pEnd, radix);
122 SysTryReturn(NID_BASE, (pEnd[0] == 0), E_NUM_FORMAT, E_NUM_FORMAT,
123 "[%s] Integer decode failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
124 SysTryReturn(NID_BASE, !((ret == LONG_MAX || ret == LONG_MIN) && (errno != 0)), E_NUM_FORMAT, E_NUM_FORMAT,
125 "[%s] Decoded value cannot fit into an Integer.", GetErrorMessage(E_NUM_FORMAT));
131 Integer::GetHashCode(void) const
137 Integer::GetHashCode(int val)
143 Integer::Parse(const String& s, int& ret)
145 int len = s.GetLength();
146 SysTryReturn(NID_BASE, (len > 0 && len < 12), E_NUM_FORMAT, E_NUM_FORMAT,
147 "[%s] The length of s(%ls) MUST be greater than 0 and less than 12.",
148 GetErrorMessage(E_NUM_FORMAT), s.GetPointer());
150 return Parse(s, Character::RADIX_DECIMAL, ret);
154 Integer::Parse(const String& s, int radix, int& ret)
156 SysTryReturn(NID_BASE, ((radix == Character::RADIX_BINARY) || (radix == Character::RADIX_OCTAL) ||
157 (radix == Character::RADIX_DECIMAL) || (radix == Character::RADIX_HEXADECIMAL)), E_OUT_OF_RANGE, E_OUT_OF_RANGE,
158 "[%s] The radix(%d) MUST be one of 2, 8, 10 and 16.", GetErrorMessage(E_OUT_OF_RANGE), radix);
160 int len = s.GetLength();
161 SysTryReturn(NID_BASE, (len > 0), E_NUM_FORMAT, E_NUM_FORMAT, "[%s] The length of s MUST be greater than 0.",
162 GetErrorMessage(E_NUM_FORMAT));
165 wchar_t* pEnd = null;
166 int tmpRet = wcstol(s.GetPointer(), &pEnd, radix);
167 SysTryReturn(NID_BASE, (pEnd[0] == 0), E_NUM_FORMAT, E_NUM_FORMAT,
168 "[%s] Integer parse failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
169 SysTryReturn(NID_BASE, !((tmpRet == LONG_MAX || tmpRet == LONG_MIN) && (errno != 0)), E_NUM_FORMAT, E_NUM_FORMAT,
170 "[%s] Parsed value cannot fit into an Integer.", GetErrorMessage(E_NUM_FORMAT));
177 Integer::ToChar(void) const
179 return static_cast<char> (value);
183 Integer::ToShort(void) const
185 return static_cast<short> (value);
189 Integer::ToInt(void) const
195 Integer::ToLong(void) const
197 return static_cast<long> (value);
201 Integer::ToLongLong(void) const
203 return static_cast<long long> (value);
207 Integer::ToFloat(void) const
209 return static_cast<float> (value);
213 Integer::ToDouble(void) const
215 return static_cast<double> (value);
219 Integer::ToString(void) const
221 return(Integer::ToString(value));
225 Integer::ToString(int value)
227 const static unsigned int INTEGER_LENGTH_MAX = 11;
229 wchar_t sValue[INTEGER_LENGTH_MAX + 1] = {0, };
230 swprintf(sValue, (sizeof(sValue) / sizeof(sValue[0])), L"%d", value);
232 return String(sValue);