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