Fix the boiler plate codes
[platform/framework/native/appfw.git] / src / base / FBaseBufferBase.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                FBaseBufferBase.cpp
19  * @brief               This is the implementation for BufferBase class.
20  */
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <math.h>
25 #include <FBaseBufferBase.h>
26 #include <FBaseSysLog.h>
27
28
29 namespace Tizen { namespace Base
30 {
31
32 BufferBase::BufferBase(void)
33         : _capacity(0)
34         , _position(0)
35         , _limit(0)
36         , _mark(-1)
37         , _pData(null)
38         , __pArrayStart(null)
39         , __pBufferBaseImpl(null)
40 {
41 }
42
43 BufferBase::~BufferBase(void)
44 {
45         if ((_pData != null) && (Release() == 0))
46         {
47                 free(_pData);
48                 _pData = null;
49         }
50 }
51
52 result
53 BufferBase::Construct(int capacity)
54 {
55         SysTryReturn(NID_BASE, capacity >= 0, E_INVALID_ARG, E_INVALID_ARG,
56                 "[%s] Invalid argument is used. The capacity is negative", GetErrorMessage(E_INVALID_ARG));
57
58         result r = E_SUCCESS;
59         void* pTemp = null;
60
61         // check whether the size of memory is larger than the maximum of unsigned int
62         unsigned long long size = static_cast <unsigned long long>(sizeof(_BufferData)) +
63                 static_cast <unsigned long long>(capacity) * static_cast <unsigned long long>(GetTypeSize());
64         SysTryReturnResult(NID_BASE, size <= static_cast <unsigned long long>((unsigned int) -1), E_OUT_OF_MEMORY,
65                 "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
66
67         pTemp = malloc(static_cast <unsigned int>(size));
68         SysTryReturnResult(NID_BASE, 0 != pTemp, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
69                 GetErrorMessage(E_OUT_OF_MEMORY));
70
71         memset(pTemp, 0, static_cast <unsigned int>(size));
72
73         _pData = static_cast <_BufferData*>(pTemp);
74
75         _pData->refCount = 1;
76         _pData->capacityInByte = capacity * GetTypeSize();
77
78         _capacity = capacity;
79         _limit = capacity;
80
81         if (capacity > 0)
82         {
83                 __pArrayStart = _pData->GetArray();
84         }
85
86         return r;
87 }
88
89 void
90 BufferBase::Clear(void)
91 {
92         _position = 0;
93         _limit = _capacity;
94         _mark = -1;
95 }
96
97 void
98 BufferBase::Compact(void)
99 {
100         int remaining = GetRemaining();
101         if (HasRemaining() && (0 != _position))
102         {
103                 int offset = _position * GetTypeSize();
104                 int byteNum = remaining * GetTypeSize();
105                 for (int i = 0; i < byteNum; i++)
106                 {
107                         __pArrayStart[i] = __pArrayStart[offset + i];
108                 }
109         }
110         _position = remaining;
111         _mark = -1;
112         _limit = _capacity;
113 }
114
115 void
116 BufferBase::Flip(PositionTo to)
117 {
118         _limit = _position;
119         if ((to == POSITION_TO_MARK) && (_mark > -1))
120         {
121                 _position = _mark;
122         }
123         else
124         {
125                 _position = 0;
126                 _mark = -1;
127         }
128 }
129
130 int
131 BufferBase::GetHashCode(void) const
132 {
133         int len = (GetRemaining() * GetTypeSize()) / sizeof(len);
134
135         // s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
136         int hash = 0;
137         int offset = _position * GetTypeSize();
138         for (int i = 0; i < len; ++i)
139         {
140                 hash += (int) __pArrayStart[offset + (i * sizeof(hash))] * (int) pow((double) 31, len - (i + 1));
141         }
142
143         return hash;
144 }
145
146 void
147 BufferBase::InvalidateMark(void)
148 {
149         _mark = -1;
150 }
151
152 result
153 BufferBase::Reset(void)
154 {
155         SysTryReturnResult(NID_BASE, _mark >= 0, E_INVALID_OPERATION, "[%s] The mark has not been set.",
156                 GetErrorMessage(E_INVALID_OPERATION));
157
158         _position = _mark;
159         return E_SUCCESS;
160 }
161
162 void
163 BufferBase::Rewind(void)
164 {
165         _position = 0;
166         _mark = -1;
167 }
168
169 result
170 BufferBase::ShiftLimit(int amount)
171 {
172         SysTryReturnResult(NID_BASE, (((_limit + amount) <= _capacity) && ((_limit + amount) >= 0)), E_OUT_OF_RANGE,
173                 "The amount(%d) is larger than the capacity(%d), or smaller than zero starting from the current limit(%d).",
174                 amount, _capacity, _limit);
175
176         return SetLimit(_limit + amount);
177 }
178
179 int
180 BufferBase::GetCapacity(void) const
181 {
182         return _capacity;
183 }
184
185 int
186 BufferBase::GetLimit(void) const
187 {
188         return _limit;
189 }
190
191 int
192 BufferBase::GetMark(void) const
193 {
194         return _mark;
195 }
196
197 int
198 BufferBase::GetPosition(void) const
199 {
200         return _position;
201 }
202
203 int
204 BufferBase::GetRemaining(void) const
205 {
206         return _limit - _position;
207 }
208
209 result
210 BufferBase::SetLimit(int limit)
211 {
212         SysTryReturnResult(NID_BASE, limit <= _capacity && limit >= 0, E_OUT_OF_RANGE,
213                 "The limit(%d) MUST be greater than or equal to 0 and less than the current capacity(%d).", limit, _capacity);
214
215         if (limit != _limit)
216         {
217                 _limit = limit;
218                 if (_position > limit)
219                 {
220                         _position = limit;
221                         if (_mark > limit)
222                         {
223                                 _mark = -1;
224                         }
225                 }
226         }
227         return E_SUCCESS;
228 }
229
230 void
231 BufferBase::SetMark(void)
232 {
233         _mark = _position;
234 }
235
236 result
237 BufferBase::SetPosition(int position)
238 {
239         SysTryReturnResult(NID_BASE, position <= _limit && position >= 0, E_OUT_OF_RANGE,
240                 "The position(%d) MUST be greater than or equal to 0 and less than the current limit(%d).", position, _limit);
241
242         _position = position;
243         if (_mark > _position)
244         {
245                 _mark = -1;
246         }
247
248         return E_SUCCESS;
249 }
250
251 bool
252 BufferBase::HasRemaining(void) const
253 {
254         return _limit > _position;
255 }
256
257 result
258 BufferBase::ExpandCapacity(int newCapacity)
259 {
260         SysTryReturnResult(NID_BASE, newCapacity > _capacity, E_INVALID_ARG,
261                 "The capacity is less than the current capacity.");
262
263         // check whether the size of memory is larger than the maximum of unsigned int
264         unsigned long long size = static_cast <unsigned long long>(sizeof(_BufferData)) +
265                 static_cast <unsigned long long>(newCapacity) * static_cast <unsigned long long>(GetTypeSize());
266
267         SysTryReturnResult(NID_BASE, size <= static_cast <unsigned long long>((unsigned int) -1), E_OUT_OF_MEMORY,
268                 "Memory allocation failed.");
269
270         _pData = static_cast<_BufferData*>(realloc(_pData, static_cast<unsigned int>(size)));
271         SysTryReturnResult(NID_BASE, 0 != _pData, E_OUT_OF_MEMORY, "Memory allocation failed.");
272
273         _capacity = newCapacity;
274         _limit = newCapacity;
275
276         __pArrayStart = _pData->GetArray();
277
278         return E_SUCCESS;
279 }
280
281 long
282 BufferBase::AddRef(void) const
283 {
284         SysAssertf(_pData->refCount > 0, "refCount(%d) is not greater than zero.", _pData->refCount);
285
286         _pData->refCount++;
287
288         return _pData->refCount;
289 }
290
291 long
292 BufferBase::Release(void) const
293 {
294         SysAssertf(_pData->refCount > 0, "refCount is not greater than zero(impossible!).");
295
296         _pData->refCount--;
297
298         return _pData->refCount;
299 }
300
301 void
302 BufferBase::Dispose(void)
303 {
304 }
305
306 BufferBase::_BufferData::_BufferData()
307         :capacityInByte(0)
308         ,refCount(0)
309 {
310 }
311
312 BufferBase::_BufferData::~_BufferData()
313 {
314 }
315
316 byte*
317 BufferBase::_BufferData::GetArray(void)
318 {
319         return reinterpret_cast <byte*>(this + 1);
320 }
321
322 }} // Tizen::Base