Merge "[ACR][01/10/2013][Add|Deprecate]Adding ToInt8() method in Number, Int8, Intege...
[platform/framework/native/appfw.git] / src / base / FBaseInteger.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                FBaseInteger.cpp
19  * @brief               This is the implementation file for Integer class.
20  * @see                 Number
21  */
22 #include <wchar.h>
23 #include <limits.h>
24 #include <errno.h>
25 #include <FBaseInteger.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 Integer::Integer(int value)
35         : value(value)
36         , __pIntegerImpl(null)
37 {
38 }
39
40 Integer::Integer(const Integer& value)
41         : value(value.value)
42         , __pIntegerImpl(null)
43 {
44 }
45
46 Integer::~Integer(void)
47 {
48
49 }
50
51 Integer&
52 Integer::operator =(const Integer& rhs)
53 {
54         if (&rhs != this)
55         {
56                 value = rhs.value;
57         }
58         return *this;
59 }
60
61 int
62 Integer::Compare(int i1, int i2)
63 {
64         return(i1 < i2 ? -1 : (i1 == i2 ? 0 : 1));
65 }
66
67 int
68 Integer::CompareTo(const Integer& value) const
69 {
70         return(Integer::Compare(this->value, value.value));
71 }
72
73 bool
74 Integer::Equals(const Object& obj) const
75 {
76         const Integer* pOther = dynamic_cast< const Integer* >(&obj);
77         if (pOther == null)
78         {
79                 return false;
80         }
81
82         return value == (*pOther).value;
83 }
84
85 result
86 Integer::Decode(const String& s, int& ret)
87 {
88         long value;
89         result r = _NumberUtil::Decode(s, value);
90         SysTryReturnResult(NID_BASE, r == E_SUCCESS, r, "Propagating.");
91         ret = static_cast< int >(value);
92         return r;
93 }
94
95 int
96 Integer::GetHashCode(void) const
97 {
98         return value;
99 }
100
101 int
102 Integer::GetHashCode(int val)
103 {
104         return val;
105 }
106
107 result
108 Integer::Parse(const String& s, int& ret)
109 {
110         return Parse(s, Character::RADIX_DECIMAL, ret);
111 }
112
113 result
114 Integer::Parse(const String& s, int radix, int& ret)
115 {
116         long value;
117         result r = _NumberUtil::Parse(s, radix, value);
118         SysTryReturnResult(NID_BASE, r == E_SUCCESS, r, "Propagating.");
119         ret = static_cast< int >(value);
120         return E_SUCCESS;
121 }
122
123 char
124 Integer::ToChar(void) const
125 {
126         return static_cast< char >(value);
127 }
128
129 int8_t
130 Integer::ToInt8(void) const
131 {
132         return static_cast< int8_t >(value);
133 }
134
135 short
136 Integer::ToShort(void) const
137 {
138         return static_cast< short >(value);
139 }
140
141 int
142 Integer::ToInt(void) const
143 {
144         return value;
145 }
146
147 long
148 Integer::ToLong(void) const
149 {
150         return static_cast< long >(value);
151 }
152
153 long long
154 Integer::ToLongLong(void) const
155 {
156         return static_cast< long long >(value);
157 }
158
159 float
160 Integer::ToFloat(void) const
161 {
162         return static_cast< float >(value);
163 }
164
165 double
166 Integer::ToDouble(void) const
167 {
168         return static_cast< double >(value);
169 }
170
171 String
172 Integer::ToString(void) const
173 {
174         return(Integer::ToString(value));
175 }
176
177 String
178 Integer::ToString(int value)
179 {
180         const static unsigned int INTEGER_LENGTH_MAX = 11;
181
182         wchar_t sValue[INTEGER_LENGTH_MAX + 1] = {0, };
183         swprintf(sValue, (sizeof(sValue) / sizeof(sValue[0])), L"%d", value);
184
185         return String(sValue);
186 }
187
188 }} //Tizen::Base