[devel_3.0_main] Cherry-pick Beautification of source-code. 80383
[platform/framework/native/appfw.git] / src / base / FBaseLongLong.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                FBaseLongLong.cpp
19  * @brief               This is the implementation file for LongLong class.
20  * @see                 Number
21  */
22 #include <wchar.h>
23 #include <limits.h>
24 #include <errno.h>
25 #include <FBaseLongLong.h>
26 #include <FBaseResult.h>
27 #include <FBaseCharacter.h>
28 #include <FBaseSysLog.h>
29
30 namespace Tizen { namespace Base
31 {
32
33 LongLong::LongLong(long long value)
34         : value(value)
35         , __pLongLongImpl(null)
36 {
37 }
38
39 LongLong::LongLong(const LongLong& value)
40         : value(value.value)
41         , __pLongLongImpl(null)
42 {
43 }
44
45 LongLong::~LongLong(void)
46 {
47 }
48
49 LongLong&
50 LongLong::operator =(const LongLong& rhs)
51 {
52         if (&rhs != this)
53         {
54                 value = rhs.value;
55         }
56         return *this;
57 }
58
59 int
60 LongLong::Compare(long long l1, long long l2)
61 {
62         return (int) (l1 < l2 ? -1 : (l1 == l2 ? 0 : 1));
63 }
64
65 int
66 LongLong::CompareTo(const LongLong& value) const
67 {
68         return(LongLong::Compare(this->value, value.value));
69 }
70
71 bool
72 LongLong::Equals(const Object& obj) const
73 {
74         const LongLong* pOther = dynamic_cast< const LongLong* >(&obj);
75         if (pOther == null)
76         {
77                 return false;
78         }
79
80         return value == (*pOther).value;
81 }
82
83 int
84 LongLong::GetHashCode(void) const
85 {
86         return static_cast< int >(value);
87 }
88
89 int
90 LongLong::GetHashCode(long long val)
91 {
92         return static_cast< int >(val);
93 }
94
95 char
96 LongLong::ToChar(void) const
97 {
98         return static_cast< char >(value);
99 }
100
101 short
102 LongLong::ToShort(void) const
103 {
104         return static_cast< short >(value);
105 }
106
107 int
108 LongLong::ToInt(void) const
109 {
110         return static_cast< int >(value);
111 }
112
113 long
114 LongLong::ToLong(void) const
115 {
116         return static_cast< long >(value);
117 }
118
119 float
120 LongLong::ToFloat(void) const
121 {
122         return static_cast< float >(value);
123 }
124
125 double
126 LongLong::ToDouble(void) const
127 {
128         return static_cast< double >(value);
129 }
130
131 long long
132 LongLong::ToLongLong(void) const
133 {
134         return value;
135 }
136
137 String
138 LongLong::ToString(void) const
139 {
140         return(LongLong::ToString(value));
141 }
142
143 String
144 LongLong::ToString(long long value)
145 {
146         const static unsigned int LONG_LONG_LENGTH_MAX = 20;
147
148         wchar_t sValue[LONG_LONG_LENGTH_MAX + 1];
149
150         wmemset(sValue, 0, sizeof(sValue) / sizeof(sValue[0]));
151         swprintf(sValue, (sizeof(sValue) / sizeof(sValue[0])), L"%lld", value);
152
153         return String(sValue);
154 }
155
156 result
157 LongLong::Parse(const String& s, long long& ret)
158 {
159         wchar_t* pEnd = null;
160
161         int len = s.GetLength();
162         SysTryReturnResult(NID_BASE, (len > 0), E_NUM_FORMAT, "[%s] The length of s MUST be greater than 0.",
163                 GetErrorMessage(E_NUM_FORMAT));
164
165         return Parse(s, Character::RADIX_DECIMAL, ret);
166 }
167
168 result
169 LongLong::Parse(const String& s, int radix, long long& ret)
170 {
171         SysTryReturnResult(NID_BASE, radix == Character::RADIX_BINARY || radix == Character::RADIX_OCTAL ||
172                 radix == Character::RADIX_DECIMAL || radix == Character::RADIX_HEXADECIMAL, E_OUT_OF_RANGE,
173                 "[%s] The radix(%d) MUST be one of 2, 8, 10 and 16.", GetErrorMessage(E_OUT_OF_RANGE), radix);
174
175         int len = s.GetLength();
176         SysTryReturnResult(NID_BASE, (len > 0), E_NUM_FORMAT, "[%s] The length of s MUST be greater than 0.",
177                 GetErrorMessage(E_NUM_FORMAT));
178
179         errno = 0;
180         wchar_t* pEnd = null;
181         long long tmpRet = wcstoll(s.GetPointer(), &pEnd, radix);
182         SysTryReturnResult(NID_BASE, (pEnd[0] == 0), E_NUM_FORMAT, "[%s] LongLong parse failed. Scan stopped at (%ls).",
183                 GetErrorMessage(E_NUM_FORMAT), pEnd);
184         SysTryReturnResult(NID_BASE, !(errno == ERANGE && (tmpRet == LLONG_MAX || tmpRet == LLONG_MIN)), E_NUM_FORMAT,
185                 "[%s] Parsed value cannot fit into a long long.", GetErrorMessage(E_NUM_FORMAT));
186
187         ret = tmpRet;
188         return E_SUCCESS;
189 }
190
191 }} //Tizen::Base