Merge "Update deprecated libprivilege-control API functions." into tizen
[platform/framework/native/appfw.git] / src / base / FBaseShort.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 //
16
17 /**
18  * @file                FBaseShort.cpp
19  * @brief               This is the implementation file for Short class.
20  * @see                 Number
21  */
22 #include <wchar.h>
23 #include <limits.h>
24 #include <errno.h>
25 #include <FBaseShort.h>
26 #include <FBaseResult.h>
27 #include <FBaseCharacter.h>
28 #include <FBaseSysLog.h>
29 #include "FBase_NumberUtil.h"
30 #include "FApp_AppInfo.h"
31
32 namespace Tizen { namespace Base
33 {
34
35 Short::Short(short value)
36         : value(value)
37         , __pShortImpl(null)
38 {
39 }
40
41 Short::Short(const Short& value)
42         : value(value.value)
43         , __pShortImpl(null)
44 {
45 }
46
47 Short::~Short(void)
48 {
49 }
50
51 Short&
52 Short::operator =(const Short& rhs)
53 {
54         if (&rhs != this)
55         {
56                 value = rhs.value;
57         }
58         return *this;
59 }
60
61 int
62 Short::Compare(short s1, short s2)
63 {
64         return(s1 < s2 ? -1 : (s1 == s2 ? 0 : 1));
65 }
66
67 int
68 Short::CompareTo(const Short& value) const
69 {
70         return(Short::Compare(this->value, value.value));
71 }
72
73 bool
74 Short::Equals(const Object& obj) const
75 {
76         const Short* pOther = dynamic_cast< const Short* >(&obj);
77         if (pOther == null)
78         {
79                 return false;
80         }
81
82         return value == (*pOther).value;
83 }
84
85 bool
86 Short::Equals(short value) const
87 {
88         return this->value == value;
89 }
90
91 int
92 Short::GetHashCode(void) const
93 {
94         return static_cast< int >(value);
95 }
96
97 int
98 Short::GetHashCode(short val)
99 {
100         return static_cast< int >(val);
101 }
102
103 result
104 Short::Decode(const String& s, short& ret)
105 {
106         long value;
107         result r = _NumberUtil::Decode(s, value);
108         SysTryReturnResult(NID_BASE, r == E_SUCCESS, r, "Propagating.");
109         SysTryReturnResult(NID_BASE, (value >= Short::VALUE_MIN) && (value <= Short::VALUE_MAX), E_OUT_OF_RANGE, "The value(%d) is out of range.", value);
110         ret = static_cast< short >(value);
111         return E_SUCCESS;
112 }
113
114 result
115 Short::Parse(const String& s, short& ret)
116 {
117         return Parse(s, Character::RADIX_DECIMAL, ret);
118 }
119
120 result
121 Short::Parse(const String& s, int radix, short& ret)
122 {
123         long value;
124         result r = _NumberUtil::Parse(s, radix, value);
125         SysTryReturnResult(NID_BASE, r == E_SUCCESS, r, "Propagating.");
126         SysTryReturnResult(NID_BASE, (value >= Short::VALUE_MIN) && (value <= Short::VALUE_MAX), E_OUT_OF_RANGE, "The value(%d) is out of range.", value);
127         ret = static_cast< short >(value);
128         return E_SUCCESS;
129 }
130
131 char
132 Short::ToChar(void) const
133 {
134         return static_cast< char >(value);
135 }
136
137 int8_t
138 Short::ToInt8(void) const
139 {
140         return static_cast< int8_t >(value);
141 }
142
143 short
144 Short::ToShort(void) const
145 {
146         return value;
147 }
148
149 int
150 Short::ToInt(void) const
151 {
152         return static_cast< int >(value);
153 }
154
155 long
156 Short::ToLong(void) const
157 {
158         return static_cast< long >(value);
159 }
160
161 long long
162 Short::ToLongLong(void) const
163 {
164         return static_cast< long long >(value);
165 }
166
167 float
168 Short::ToFloat(void) const
169 {
170         return static_cast< float >(value);
171 }
172
173 double
174 Short::ToDouble(void) const
175 {
176         return static_cast< double >(value);
177 }
178
179 String
180 Short::ToString(void) const
181 {
182         return(Short::ToString(value));
183 }
184
185 String
186 Short::ToString(short value)
187 {
188         const static unsigned int SHORT_LENGTH_MAX = 6;
189
190         wchar_t sValue[SHORT_LENGTH_MAX + 1] = {0, };
191         swprintf(sValue, (sizeof(sValue) / sizeof(sValue[0])), L"%d", value);
192
193         return String(sValue);
194 }
195
196 }} //Tizen::Base