Merge "[ACR][10/09/2013][Versioning]Enhancing Parse() and Decode() API to support...
[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
31 namespace Tizen { namespace Base
32 {
33
34 Int8::Int8(char value)
35         : value(static_cast< signed char >(value))
36         , __pInt8Impl(null)
37 {
38 }
39
40 Int8::Int8(const Int8& value)
41         : value(value.value)
42         , __pInt8Impl(null)
43 {
44 }
45
46 Int8::~Int8(void)
47 {
48 }
49
50 Int8&
51 Int8::operator =(const Int8& rhs)
52 {
53         if (&rhs != this)
54         {
55                 value = rhs.value;
56         }
57         return (*this);
58 }
59
60 int
61 Int8::Compare(char ch1, char ch2)
62 {
63         return ((signed char) ch1 < (signed char) ch2 ? -1 : (ch1 == ch2 ? 0 : 1));
64 }
65
66 int
67 Int8::CompareTo(const Int8& value) const
68 {
69         return (Int8::Compare(this->value, value.value));
70 }
71
72 bool
73 Int8::Equals(const Object& obj) const
74 {
75         const Int8* pOther = dynamic_cast< const Int8* >(&obj);
76         if (pOther == null)
77         {
78                 return (false);
79         }
80
81         return (value == (*pOther).value);
82 }
83
84 int
85 Int8::GetHashCode(void) const
86 {
87         return static_cast< int >(value);
88 }
89
90 int
91 Int8::GetHashCode(char val)
92 {
93         return static_cast< int >(val);
94 }
95
96 result
97 Int8::Decode(const String& s, char& ret)
98 {
99         long value;
100         result r = _NumberUtil::Decode(s, value);
101         SysTryReturnResult(NID_BASE, r == E_SUCCESS, r, "Propagating.");
102
103         if (value > Int8::VALUE_MAX)
104         {
105                 ret = Int8::VALUE_MAX;
106         }
107         else if (value < Int8::VALUE_MIN)
108         {
109                 ret = Int8::VALUE_MIN;
110         }
111         else
112         {
113                 ret = static_cast< char >(value);
114         }
115         return r;
116 }
117
118 result
119 Int8::Parse(const String& s, char& ret)
120 {
121         return Parse(s, Character::RADIX_DECIMAL, ret);
122 }
123
124 result
125 Int8::Parse(const String& s, int radix, char& ret)
126 {
127         long value;
128         result r = _NumberUtil::Parse(s, radix, value);
129         SysTryReturnResult(NID_BASE, r == E_SUCCESS, r, "Propagating.");
130
131         if (value > Int8::VALUE_MAX)
132         {
133                 ret = Int8::VALUE_MAX;
134         }
135         else if (value < Int8::VALUE_MIN)
136         {
137                 ret = Int8::VALUE_MIN;
138         }
139         else
140         {
141                 ret = static_cast< char >(value);
142         }
143         return r;
144 }
145
146 char
147 Int8::ToChar(void) const
148 {
149         return static_cast< char >(value);
150 }
151
152 short
153 Int8::ToShort(void) const
154 {
155         return static_cast< short >(value);
156 }
157
158 int
159 Int8::ToInt(void) const
160 {
161         return static_cast< int >(value);
162 }
163
164 long
165 Int8::ToLong(void) const
166 {
167         return static_cast< long >(value);
168 }
169
170 long long
171 Int8::ToLongLong(void) const
172 {
173         return static_cast< long long >(value);
174 }
175
176 float
177 Int8::ToFloat(void) const
178 {
179         return static_cast< float >(value);
180 }
181
182 double
183 Int8::ToDouble(void) const
184 {
185         return static_cast< double >(value);
186 }
187
188 String
189 Int8::ToString(void) const
190 {
191         return (Int8::ToString(value));
192 }
193
194 String
195 Int8::ToString(char value)
196 {
197         const static unsigned int INT8_LENGTH_MAX = 4;
198
199         wchar_t sValue[INT8_LENGTH_MAX + 1] = {0, };
200         swprintf(sValue, (sizeof(sValue) / sizeof(sValue[0])), L"%d", value);
201
202         return String(sValue);
203 }
204
205 }} //Tizen::Base