Merge branch 'temp' into nkw
[platform/framework/native/appfw.git] / src / base / FBaseInt8.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                FBaseInt8.cpp
19  * @brief               This is the implementation file for Int8 class.
20  * @see                 Number class
21  */
22 #include <wchar.h>
23 #include <errno.h>
24 #include <limits.h>
25 #include <FBaseInt8.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 Int8::Int8(char value)
36         : value(static_cast< signed char >(value))
37         , __pInt8Impl(null)
38 {
39 }
40
41 Int8::Int8(const Int8& value)
42         : value(value.value)
43         , __pInt8Impl(null)
44 {
45 }
46
47 Int8::~Int8(void)
48 {
49 }
50
51 Int8&
52 Int8::operator =(const Int8& rhs)
53 {
54         if (&rhs != this)
55         {
56                 value = rhs.value;
57         }
58         return (*this);
59 }
60
61 int
62 Int8::Compare(char ch1, char ch2)
63 {
64         return ((signed char) ch1 < (signed char) ch2 ? -1 : (ch1 == ch2 ? 0 : 1));
65 }
66
67 int
68 Int8::CompareTo(const Int8& value) const
69 {
70         return (Int8::Compare(this->value, value.value));
71 }
72
73 bool
74 Int8::Equals(const Object& obj) const
75 {
76         const Int8* pOther = dynamic_cast< const Int8* >(&obj);
77         if (pOther == null)
78         {
79                 return (false);
80         }
81
82         return (value == (*pOther).value);
83 }
84
85 int
86 Int8::GetHashCode(void) const
87 {
88         return static_cast< int >(value);
89 }
90
91 int
92 Int8::GetHashCode(char val)
93 {
94         return static_cast< int >(val);
95 }
96
97 result
98 Int8::Decode(const String& s, char& ret)
99 {
100         long value;
101         result r = _NumberUtil::Decode(s, value);
102         SysTryReturnResult(NID_BASE, r == E_SUCCESS, r, "Propagating.");
103
104         if ((Tizen::App::_AppInfo::GetApiVersion() >= _API_VERSION_2_0) && (Tizen::App::_AppInfo::GetApiVersion() <= _API_VERSION_2_2))
105         {
106                 if (value > Int8::VALUE_MAX)
107                 {
108                         ret = Int8::VALUE_MAX;
109                 }
110                 else if (value < Int8::VALUE_MIN)
111                 {
112                         ret = Int8::VALUE_MIN;
113                 }
114                 else
115                 {
116                         ret = static_cast< char >(value);
117                 }
118         }
119         else
120         {
121                 SysTryReturnResult(NID_BASE, (value >= Int8::VALUE_MIN) && (value <= Int8::VALUE_MAX), E_OUT_OF_RANGE, "The value(%d) is out of range.", value);
122                 ret = static_cast< char >(value);
123         }
124         return E_SUCCESS;
125 }
126
127 result
128 Int8::Parse(const String& s, char& ret)
129 {
130         return Parse(s, Character::RADIX_DECIMAL, ret);
131 }
132
133 result
134 Int8::Parse(const String& s, int radix, char& ret)
135 {
136         long value;
137         result r = _NumberUtil::Parse(s, radix, value);
138         SysTryReturnResult(NID_BASE, r == E_SUCCESS, r, "Propagating.");
139
140         if ((Tizen::App::_AppInfo::GetApiVersion() >= _API_VERSION_2_0) && (Tizen::App::_AppInfo::GetApiVersion() <= _API_VERSION_2_2))
141         {
142                 if (value > Int8::VALUE_MAX)
143                 {
144                         ret = Int8::VALUE_MAX;
145                 }
146                 else if (value < Int8::VALUE_MIN)
147                 {
148                         ret = Int8::VALUE_MIN;
149                 }
150                 else
151                 {
152                         ret = static_cast< char >(value);
153                 }
154         }
155         else
156         {
157                 SysTryReturnResult(NID_BASE, (value >= Int8::VALUE_MIN) && (value <= Int8::VALUE_MAX), E_OUT_OF_RANGE, "The value(%d) is out of range.", value);
158                 ret = static_cast< char >(value);
159         }
160         return E_SUCCESS;
161 }
162
163 char
164 Int8::ToChar(void) const
165 {
166         return static_cast< char >(value);
167 }
168
169 short
170 Int8::ToShort(void) const
171 {
172         return static_cast< short >(value);
173 }
174
175 int
176 Int8::ToInt(void) const
177 {
178         return static_cast< int >(value);
179 }
180
181 long
182 Int8::ToLong(void) const
183 {
184         return static_cast< long >(value);
185 }
186
187 long long
188 Int8::ToLongLong(void) const
189 {
190         return static_cast< long long >(value);
191 }
192
193 float
194 Int8::ToFloat(void) const
195 {
196         return static_cast< float >(value);
197 }
198
199 double
200 Int8::ToDouble(void) const
201 {
202         return static_cast< double >(value);
203 }
204
205 String
206 Int8::ToString(void) const
207 {
208         return (Int8::ToString(value));
209 }
210
211 String
212 Int8::ToString(char value)
213 {
214         const static unsigned int INT8_LENGTH_MAX = 4;
215
216         wchar_t sValue[INT8_LENGTH_MAX + 1] = {0, };
217         swprintf(sValue, (sizeof(sValue) / sizeof(sValue[0])), L"%d", value);
218
219         return String(sValue);
220 }
221
222 }} //Tizen::Base