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 FBaseShort.cpp
20 * @brief This is the implementation file for Short class.
27 #include <FBaseShort.h>
28 #include <FBaseResult.h>
29 #include <FBaseCharacter.h>
30 #include <FBaseSysLog.h>
32 namespace Tizen { namespace Base
35 Short::Short(short value)
41 Short::Short(const Short& value)
52 Short::operator =(const Short& rhs)
62 Short::Compare(short s1, short s2)
64 return(s1 < s2 ? -1 : (s1 == s2 ? 0 : 1));
68 Short::CompareTo(const Short& value) const
70 return(Short::Compare(this->value, value.value));
74 Short::Equals(const Object& obj) const
76 const Short* pOther = dynamic_cast <const Short*>(&obj);
82 return value == (*pOther).value;
86 Short::Equals(short value) const
88 return this->value == value;
92 Short::GetHashCode(void) const
94 return static_cast<int> (value);
98 Short::GetHashCode(short val)
100 return static_cast<int> (val);
104 Short::Decode(const String& s, short& ret)
106 SysTryReturn(NID_BASE, s.GetLength() >= 1, E_NUM_FORMAT, E_NUM_FORMAT,
107 "[%s] The length of s MUST be greater than 0.", GetErrorMessage(E_NUM_FORMAT));
113 wchar_t* pEnd = null;
116 if (s[0] == L'-' || s[0] == L'+')
123 if (s[startIndex] == L'#')
125 radix = Character::RADIX_HEXADECIMAL;
128 str.Remove(startIndex, 1);
130 else if (s[startIndex] == L'0' && (s.GetLength() >= minLength))
132 if (s[startIndex + 1] == L'x' || s[startIndex + 1] == L'X')
134 radix = Character::RADIX_HEXADECIMAL;
138 radix = Character::RADIX_OCTAL;
143 radix = Character::RADIX_DECIMAL;
146 result r = E_SUCCESS;
149 value = wcstol(str.GetPointer(), &pEnd, radix);
150 SysTryCatch(NID_BASE, (pEnd[0] == 0), r = E_NUM_FORMAT, E_NUM_FORMAT,
151 "[%s] Short decode failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
152 SysTryCatch(NID_BASE, !((value == LONG_MAX || value == LONG_MIN) && (errno != 0)), r = E_NUM_FORMAT, E_NUM_FORMAT,
153 "[%s] Decoded value cannot fit into Short.", GetErrorMessage(E_NUM_FORMAT));
156 if (value > Short::VALUE_MAX)
158 ret = Short::VALUE_MAX;
160 else if (value < Short::VALUE_MIN)
162 ret = Short::VALUE_MIN;
173 Short::Parse(const String& s, short& ret)
175 int len = s.GetLength();
176 SysTryReturn(NID_BASE, (len > 0 && len < 7), E_NUM_FORMAT, E_NUM_FORMAT,
177 "[%s] The length of s(%ls) MUST be greater than 0 and less than 7.", GetErrorMessage(E_NUM_FORMAT), s.GetPointer());
179 return Parse(s, Character::RADIX_DECIMAL, ret);
183 Short::Parse(const String& s, int radix, short& ret)
185 SysTryReturn(NID_BASE, ((radix == Character::RADIX_BINARY) || (radix == Character::RADIX_OCTAL) ||
186 (radix == Character::RADIX_DECIMAL) || (radix == Character::RADIX_HEXADECIMAL)), E_OUT_OF_RANGE, E_OUT_OF_RANGE,
187 "[%s] The radix(%d) MUST be one of 2, 8, 10 and 16.", GetErrorMessage(E_OUT_OF_RANGE), radix);
189 int len = s.GetLength();
190 SysTryReturn(NID_BASE, (len > 0), E_NUM_FORMAT, E_NUM_FORMAT, "[%s] The length of s MUST be greater than 0.",
191 GetErrorMessage(E_NUM_FORMAT));
193 result r = E_SUCCESS;
196 wchar_t* pEnd = null;
197 long value = wcstol(s.GetPointer(), &pEnd, radix);
198 SysTryReturn(NID_BASE, (pEnd[0] == 0), E_NUM_FORMAT, E_NUM_FORMAT,
199 "[%s] Short parse failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
200 SysTryReturn(NID_BASE, !(value > Short::VALUE_MAX || value < Short::VALUE_MIN) || (errno != 0), E_NUM_FORMAT,
201 E_NUM_FORMAT, "[%s] Parsed value cannot fit into Short.", GetErrorMessage(E_NUM_FORMAT));
203 if (value > Short::VALUE_MAX)
205 ret = Short::VALUE_MAX;
207 else if (value < Short::VALUE_MIN)
209 ret = Short::VALUE_MIN;
213 ret = static_cast< short >(value);
220 Short::ToChar(void) const
222 return static_cast<char> (value);
226 Short::ToShort(void) const
232 Short::ToInt(void) const
234 return static_cast<int> (value);
238 Short::ToLong(void) const
240 return static_cast<long> (value);
244 Short::ToLongLong(void) const
246 return static_cast<long long> (value);
250 Short::ToFloat(void) const
252 return static_cast<float> (value);
256 Short::ToDouble(void) const
258 return static_cast<double> (value);
262 Short::ToString(void) const
264 return(Short::ToString(value));
268 Short::ToString(short value)
270 const static unsigned int SHORT_LENGTH_MAX = 6;
272 wchar_t sValue[SHORT_LENGTH_MAX + 1] = {0, };
273 swprintf(sValue, (sizeof(sValue) / sizeof(sValue[0])), L"%d", value);
275 return String(sValue);