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 FBaseLongLong.cpp
20 * @brief This is the implementation file for LongLong class.
27 #include <FBaseLongLong.h>
28 #include <FBaseResult.h>
29 #include <FBaseCharacter.h>
30 #include <FBaseSysLog.h>
32 namespace Tizen { namespace Base
35 LongLong::LongLong(long long value)
37 , __pLongLongImpl(null)
41 LongLong::LongLong(const LongLong& value)
43 , __pLongLongImpl(null)
47 LongLong::~LongLong(void)
52 LongLong::operator =(const LongLong& rhs)
62 LongLong::Compare(long long l1, long long l2)
64 return (int) (l1 < l2 ? -1 : (l1 == l2 ? 0 : 1));
68 LongLong::CompareTo(const LongLong& value) const
70 return(LongLong::Compare(this->value, value.value));
74 LongLong::Equals(const Object& obj) const
76 const LongLong* pOther = dynamic_cast <const LongLong*>(&obj);
82 return value == (*pOther).value;
86 LongLong::GetHashCode(void) const
88 return static_cast<int> (value);
92 LongLong::GetHashCode(long long val)
94 return static_cast<int> (val);
98 LongLong::ToChar(void) const
100 return static_cast<char> (value);
104 LongLong::ToShort(void) const
106 return static_cast<short> (value);
110 LongLong::ToInt(void) const
112 return static_cast<int> (value);
116 LongLong::ToLong(void) const
118 return static_cast<long> (value);
122 LongLong::ToFloat(void) const
124 return static_cast<float> (value);
128 LongLong::ToDouble(void) const
130 return static_cast<double> (value);
134 LongLong::ToLongLong(void) const
140 LongLong::ToString(void) const
142 return(LongLong::ToString(value));
146 LongLong::ToString(long long value)
148 const static unsigned int LONG_LONG_LENGTH_MAX = 20;
150 wchar_t sValue[LONG_LONG_LENGTH_MAX + 1];
152 wmemset(sValue, 0, sizeof(sValue) / sizeof(sValue[0]));
153 swprintf(sValue, (sizeof(sValue) / sizeof(sValue[0])), L"%lld", value);
155 return String(sValue);
159 LongLong::Parse(const String& s, long long& ret)
161 wchar_t* pEnd = null;
163 int len = s.GetLength();
164 SysTryReturnResult(NID_BASE, (len > 0), E_NUM_FORMAT, "[%s] The length of s MUST be greater than 0.",
165 GetErrorMessage(E_NUM_FORMAT));
167 return Parse(s, Character::RADIX_DECIMAL, ret);
171 LongLong::Parse(const String& s, int radix, long long& ret)
173 SysTryReturnResult(NID_BASE, radix == Character::RADIX_BINARY || radix == Character::RADIX_OCTAL ||
174 radix == Character::RADIX_DECIMAL || radix == Character::RADIX_HEXADECIMAL, 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 int len = s.GetLength();
178 SysTryReturnResult(NID_BASE, (len > 0), E_NUM_FORMAT, "[%s] The length of s MUST be greater than 0.",
179 GetErrorMessage(E_NUM_FORMAT));
182 wchar_t* pEnd = null;
183 long long tmpRet = wcstoll(s.GetPointer(), &pEnd, radix);
184 SysTryReturnResult(NID_BASE, (pEnd[0] == 0), E_NUM_FORMAT, "[%s] LongLong parse failed. Scan stopped at (%ls).",
185 GetErrorMessage(E_NUM_FORMAT), pEnd);
186 SysTryReturnResult(NID_BASE, !(errno == ERANGE && (tmpRet == LLONG_MAX || tmpRet == LLONG_MIN)), E_NUM_FORMAT,
187 "[%s] Parsed value cannot fit into a long long.", GetErrorMessage(E_NUM_FORMAT));