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 FBaseShort.cpp
19 * @brief This is the implementation file for Short class.
25 #include <FBaseShort.h>
26 #include <FBaseResult.h>
27 #include <FBaseCharacter.h>
28 #include <FBaseSysLog.h>
30 namespace Tizen { namespace Base
33 Short::Short(short value)
39 Short::Short(const Short& value)
50 Short::operator =(const Short& rhs)
60 Short::Compare(short s1, short s2)
62 return(s1 < s2 ? -1 : (s1 == s2 ? 0 : 1));
66 Short::CompareTo(const Short& value) const
68 return(Short::Compare(this->value, value.value));
72 Short::Equals(const Object& obj) const
74 const Short* pOther = dynamic_cast< const Short* >(&obj);
80 return value == (*pOther).value;
84 Short::Equals(short value) const
86 return this->value == value;
90 Short::GetHashCode(void) const
92 return static_cast< int >(value);
96 Short::GetHashCode(short val)
98 return static_cast< int >(val);
102 Short::Decode(const String& s, short& ret)
104 SysTryReturn(NID_BASE, s.GetLength() >= 1, E_NUM_FORMAT, E_NUM_FORMAT,
105 "[%s] The length of s MUST be greater than 0.", GetErrorMessage(E_NUM_FORMAT));
111 wchar_t* pEnd = null;
114 if (s[0] == L'-' || s[0] == L'+')
121 if (s[startIndex] == L'#')
123 radix = Character::RADIX_HEXADECIMAL;
126 str.Remove(startIndex, 1);
128 else if (s[startIndex] == L'0' && (s.GetLength() >= minLength))
130 if (s[startIndex + 1] == L'x' || s[startIndex + 1] == L'X')
132 radix = Character::RADIX_HEXADECIMAL;
136 radix = Character::RADIX_OCTAL;
141 radix = Character::RADIX_DECIMAL;
144 result r = E_SUCCESS;
147 value = wcstol(str.GetPointer(), &pEnd, radix);
148 SysTryCatch(NID_BASE, (pEnd[0] == 0), r = E_NUM_FORMAT, E_NUM_FORMAT,
149 "[%s] Short decode failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
150 SysTryCatch(NID_BASE, !((value == LONG_MAX || value == LONG_MIN) && (errno != 0)), r = E_NUM_FORMAT, E_NUM_FORMAT,
151 "[%s] Decoded value cannot fit into Short.", GetErrorMessage(E_NUM_FORMAT));
154 if (value > Short::VALUE_MAX)
156 ret = Short::VALUE_MAX;
158 else if (value < Short::VALUE_MIN)
160 ret = Short::VALUE_MIN;
171 Short::Parse(const String& s, short& ret)
173 int len = s.GetLength();
174 SysTryReturn(NID_BASE, (len > 0 && len < 7), E_NUM_FORMAT, E_NUM_FORMAT,
175 "[%s] The length of s(%ls) MUST be greater than 0 and less than 7.", GetErrorMessage(E_NUM_FORMAT), s.GetPointer());
177 return Parse(s, Character::RADIX_DECIMAL, ret);
181 Short::Parse(const String& s, int radix, short& ret)
183 SysTryReturn(NID_BASE, ((radix == Character::RADIX_BINARY) || (radix == Character::RADIX_OCTAL) ||
184 (radix == Character::RADIX_DECIMAL) || (radix == Character::RADIX_HEXADECIMAL)), E_OUT_OF_RANGE, E_OUT_OF_RANGE,
185 "[%s] The radix(%d) MUST be one of 2, 8, 10 and 16.", GetErrorMessage(E_OUT_OF_RANGE), radix);
187 int len = s.GetLength();
188 SysTryReturn(NID_BASE, (len > 0), E_NUM_FORMAT, E_NUM_FORMAT, "[%s] The length of s MUST be greater than 0.",
189 GetErrorMessage(E_NUM_FORMAT));
191 result r = E_SUCCESS;
194 wchar_t* pEnd = null;
195 long value = wcstol(s.GetPointer(), &pEnd, radix);
196 SysTryReturn(NID_BASE, (pEnd[0] == 0), E_NUM_FORMAT, E_NUM_FORMAT,
197 "[%s] Short parse failed. Scan stopped at (%ls).", GetErrorMessage(E_NUM_FORMAT), pEnd);
198 SysTryReturn(NID_BASE, !(value > Short::VALUE_MAX || value < Short::VALUE_MIN) || (errno != 0), E_NUM_FORMAT,
199 E_NUM_FORMAT, "[%s] Parsed value cannot fit into Short.", GetErrorMessage(E_NUM_FORMAT));
201 if (value > Short::VALUE_MAX)
203 ret = Short::VALUE_MAX;
205 else if (value < Short::VALUE_MIN)
207 ret = Short::VALUE_MIN;
211 ret = static_cast< short >(value);
218 Short::ToChar(void) const
220 return static_cast< char >(value);
224 Short::ToShort(void) const
230 Short::ToInt(void) const
232 return static_cast< int >(value);
236 Short::ToLong(void) const
238 return static_cast< long >(value);
242 Short::ToLongLong(void) const
244 return static_cast< long long >(value);
248 Short::ToFloat(void) const
250 return static_cast< float >(value);
254 Short::ToDouble(void) const
256 return static_cast< double >(value);
260 Short::ToString(void) const
262 return(Short::ToString(value));
266 Short::ToString(short value)
268 const static unsigned int SHORT_LENGTH_MAX = 6;
270 wchar_t sValue[SHORT_LENGTH_MAX + 1] = {0, };
271 swprintf(sValue, (sizeof(sValue) / sizeof(sValue[0])), L"%d", value);
273 return String(sValue);