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 FBaseLongLong.cpp
19 * @brief This is the implementation file for LongLong class.
25 #include <FBaseLongLong.h>
26 #include <FBaseResult.h>
27 #include <FBaseCharacter.h>
28 #include <FBaseSysLog.h>
30 namespace Tizen { namespace Base
33 LongLong::LongLong(long long value)
35 , __pLongLongImpl(null)
39 LongLong::LongLong(const LongLong& value)
41 , __pLongLongImpl(null)
45 LongLong::~LongLong(void)
50 LongLong::operator =(const LongLong& rhs)
60 LongLong::Compare(long long l1, long long l2)
62 return (int) (l1 < l2 ? -1 : (l1 == l2 ? 0 : 1));
66 LongLong::CompareTo(const LongLong& value) const
68 return(LongLong::Compare(this->value, value.value));
72 LongLong::Equals(const Object& obj) const
74 const LongLong* pOther = dynamic_cast< const LongLong* >(&obj);
80 return value == (*pOther).value;
84 LongLong::GetHashCode(void) const
86 return static_cast< int >(value);
90 LongLong::GetHashCode(long long val)
92 return static_cast< int >(val);
96 LongLong::ToChar(void) const
98 return static_cast< char >(value);
102 LongLong::ToShort(void) const
104 return static_cast< short >(value);
108 LongLong::ToInt(void) const
110 return static_cast< int >(value);
114 LongLong::ToLong(void) const
116 return static_cast< long >(value);
120 LongLong::ToFloat(void) const
122 return static_cast< float >(value);
126 LongLong::ToDouble(void) const
128 return static_cast< double >(value);
132 LongLong::ToLongLong(void) const
138 LongLong::ToString(void) const
140 return(LongLong::ToString(value));
144 LongLong::ToString(long long value)
146 const static unsigned int LONG_LONG_LENGTH_MAX = 20;
148 wchar_t sValue[LONG_LONG_LENGTH_MAX + 1];
150 wmemset(sValue, 0, sizeof(sValue) / sizeof(sValue[0]));
151 swprintf(sValue, (sizeof(sValue) / sizeof(sValue[0])), L"%lld", value);
153 return String(sValue);
157 LongLong::Parse(const String& s, long long& ret)
159 wchar_t* pEnd = null;
161 int len = s.GetLength();
162 SysTryReturnResult(NID_BASE, (len > 0), E_NUM_FORMAT, "[%s] The length of s MUST be greater than 0.",
163 GetErrorMessage(E_NUM_FORMAT));
165 return Parse(s, Character::RADIX_DECIMAL, ret);
169 LongLong::Parse(const String& s, int radix, long long& ret)
171 SysTryReturnResult(NID_BASE, radix == Character::RADIX_BINARY || radix == Character::RADIX_OCTAL ||
172 radix == Character::RADIX_DECIMAL || radix == Character::RADIX_HEXADECIMAL, E_OUT_OF_RANGE,
173 "[%s] The radix(%d) MUST be one of 2, 8, 10 and 16.", GetErrorMessage(E_OUT_OF_RANGE), radix);
175 int len = s.GetLength();
176 SysTryReturnResult(NID_BASE, (len > 0), E_NUM_FORMAT, "[%s] The length of s MUST be greater than 0.",
177 GetErrorMessage(E_NUM_FORMAT));
180 wchar_t* pEnd = null;
181 long long tmpRet = wcstoll(s.GetPointer(), &pEnd, radix);
182 SysTryReturnResult(NID_BASE, (pEnd[0] == 0), E_NUM_FORMAT, "[%s] LongLong parse failed. Scan stopped at (%ls).",
183 GetErrorMessage(E_NUM_FORMAT), pEnd);
184 SysTryReturnResult(NID_BASE, !(errno == ERANGE && (tmpRet == LLONG_MAX || tmpRet == LLONG_MIN)), E_NUM_FORMAT,
185 "[%s] Parsed value cannot fit into a long long.", GetErrorMessage(E_NUM_FORMAT));