[devel_3.0_main] Cherry-pick Beautification of source-code. 80383
[platform/framework/native/appfw.git] / src / base / FBaseByteBuffer.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                FBaseByteBuffer.cpp
19  * @brief               This is the implementation for ByteBuffer class.
20  */
21 #include <new>
22 #include <FBaseByteBuffer.h>
23 #include <FBaseSysLog.h>
24 #include <unique_ptr.h>
25
26 static const byte DOUBLE_SIZE = sizeof(double);
27 static const byte FLOAT_SIZE = sizeof(float);
28 static const byte INT_SIZE = sizeof(int);
29 static const byte LONG_SIZE = sizeof(long);
30 static const byte LONGLONG_SIZE = sizeof(long long);
31 static const byte MCHAR_SIZE = sizeof(wchar_t);
32 static const byte SHORT_SIZE = sizeof(short);
33
34 namespace Tizen { namespace Base
35 {
36
37 ByteBuffer::ByteBuffer(void)
38         : __pByteBufferImpl(null)
39 {
40 }
41
42 ByteBuffer::~ByteBuffer(void)
43 {
44 }
45
46 result
47 ByteBuffer::Construct(const ByteBuffer& buffer)
48 {
49         SysTryReturn(NID_BASE, null != buffer._pData, E_INVALID_ARG, E_INVALID_ARG,
50                 "[%s] The source buffer is not constructed.", GetErrorMessage(E_INVALID_ARG));
51
52         _capacity = buffer._capacity;
53         _position = buffer._position;
54         _limit = buffer._limit;
55         _mark = buffer._mark;
56         _pData = buffer._pData;
57
58         AddRef();
59         __pArrayStart = buffer.__pArrayStart;
60
61         return E_SUCCESS;
62 }
63
64 result
65 ByteBuffer::Construct(int capacity)
66 {
67         result r = BufferBase::Construct(capacity);
68         if (r != E_SUCCESS)
69         {
70                 SysLogException(NID_BASE, r, "[%s] Propagated.", GetErrorMessage(r));
71         }
72
73         return r;
74 }
75
76 result
77 ByteBuffer::Construct(const byte* pBuffer, int index, int length, int capacity)
78 {
79         SysTryReturn(NID_BASE, pBuffer != null, E_INVALID_ARG, E_INVALID_ARG, "[%s] The pBuffer is null.",
80                 GetErrorMessage(E_INVALID_ARG));
81         SysTryReturn(NID_BASE, index >= 0 && length >= 0 && capacity >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
82                 "[%s] index(%d), length(%d) and capacity(%d) MUST be greater than or equal to 0.",
83                 GetErrorMessage(E_OUT_OF_RANGE), index, length, capacity);
84         SysTryReturn(NID_BASE, index < capacity && length <= capacity && index + length <= capacity,
85                 E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[%s] index(%d), length(%d) and capacity(%d) MUST be greater than or equal to 0.",
86                 GetErrorMessage(E_OUT_OF_RANGE), index, length, capacity);
87
88         void* pTemp = null;
89         int sizeofBufferData = sizeof(_BufferData);
90
91         __pArrayStart = const_cast< byte* >(pBuffer + index);
92
93         pTemp = malloc(sizeofBufferData);
94         SysTryReturn(NID_BASE, pTemp != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s]", GetErrorMessage(E_OUT_OF_MEMORY));
95
96         memset(pTemp, 0, sizeofBufferData);
97         _pData = static_cast< _BufferData* >(pTemp);
98
99         _pData->refCount = 1;
100         _pData->capacityInByte = capacity;
101
102         _capacity = capacity;
103         _limit = length;
104
105         return E_SUCCESS;
106 }
107
108 byte&
109 ByteBuffer::operator [](int index)
110 {
111         SysAssertf(index < _limit, "index out of range.\n");
112         SysAssertf(index >= 0, "index out of range.\n");
113
114         return __pArrayStart[index];
115 }
116
117 byte
118 ByteBuffer::operator [](int index) const
119 {
120         return const_cast< ByteBuffer& >(*this)[index];
121 }
122
123 bool
124 ByteBuffer::operator ==(const ByteBuffer& buffer) const
125 {
126         bool r = true;
127
128         if (this == &buffer)
129         {
130                 r = true;
131         }
132         else if (GetRemaining() != buffer.GetRemaining())
133         {
134                 r = false;
135         }
136         else
137         {
138                 for (int i = 0; i < GetRemaining(); ++i)
139                 {
140                         if (__pArrayStart[_position + i] != buffer.__pArrayStart[buffer._position + i])
141                         {
142                                 r = false;
143                                 break;
144                         }
145                 }
146         }
147
148         return r;
149 }
150
151 bool
152 ByteBuffer::operator !=(const ByteBuffer& buffer) const
153 {
154         return !(*this == buffer);
155 }
156
157 DoubleBuffer*
158 ByteBuffer::AsDoubleBufferN(void) const
159 {
160         std::unique_ptr< DoubleBuffer > pBuffer(new (std::nothrow) DoubleBuffer());
161         pBuffer->_pData = _pData;
162         AddRef();
163         pBuffer->_capacity = GetRemaining() / DOUBLE_SIZE;
164         pBuffer->_limit = pBuffer->_capacity;
165         if (pBuffer->_capacity > 0)
166         {
167                 pBuffer->__pArrayStart = &__pArrayStart[_position];
168         }
169
170         return pBuffer.release();
171 }
172
173 FloatBuffer*
174 ByteBuffer::AsFloatBufferN(void) const
175 {
176         std::unique_ptr< FloatBuffer > pBuffer(new (std::nothrow) FloatBuffer());
177         pBuffer->_pData = _pData;
178         AddRef();
179         pBuffer->_capacity = GetRemaining() / FLOAT_SIZE;
180         pBuffer->_limit = pBuffer->_capacity;
181         if (pBuffer->_capacity > 0)
182         {
183                 pBuffer->__pArrayStart = &__pArrayStart[_position];
184         }
185
186         return pBuffer.release();
187 }
188
189 IntBuffer*
190 ByteBuffer::AsIntBufferN(void) const
191 {
192         std::unique_ptr< IntBuffer > pBuffer(new (std::nothrow) IntBuffer());
193         pBuffer->_pData = _pData;
194         AddRef();
195         pBuffer->_capacity = GetRemaining() / INT_SIZE;
196         pBuffer->_limit = pBuffer->_capacity;
197         if (pBuffer->_capacity > 0)
198         {
199                 pBuffer->__pArrayStart = &__pArrayStart[_position];
200         }
201
202         return pBuffer.release();
203 }
204
205 LongBuffer*
206 ByteBuffer::AsLongBufferN(void) const
207 {
208         std::unique_ptr< LongBuffer > pBuffer(new (std::nothrow) LongBuffer());
209         pBuffer->_pData = _pData;
210         AddRef();
211         pBuffer->_capacity = GetRemaining() / LONG_SIZE;
212         pBuffer->_limit = pBuffer->_capacity;
213         if (pBuffer->_capacity > 0)
214         {
215                 pBuffer->__pArrayStart = &__pArrayStart[_position];
216         }
217
218         return pBuffer.release();
219 }
220
221 LongLongBuffer*
222 ByteBuffer::AsLongLongBufferN(void) const
223 {
224         std::unique_ptr< LongLongBuffer > pBuffer(new (std::nothrow) LongLongBuffer());
225         pBuffer->_pData = _pData;
226         AddRef();
227         pBuffer->_capacity = GetRemaining() / LONGLONG_SIZE;
228         pBuffer->_limit = pBuffer->_capacity;
229         if (pBuffer->_capacity > 0)
230         {
231                 pBuffer->__pArrayStart = &__pArrayStart[_position];
232         }
233
234         return pBuffer.release();
235 }
236
237 McharBuffer*
238 ByteBuffer::AsMcharBufferN(void) const
239 {
240         std::unique_ptr< McharBuffer > pBuffer(new (std::nothrow) McharBuffer());
241         pBuffer->_pData = _pData;
242         AddRef();
243         pBuffer->_capacity = GetRemaining() / MCHAR_SIZE;
244         pBuffer->_limit = pBuffer->_capacity;
245         if (pBuffer->_capacity > 0)
246         {
247                 pBuffer->__pArrayStart = &__pArrayStart[_position];
248         }
249
250         return pBuffer.release();
251 }
252
253 WcharBuffer*
254 ByteBuffer::AsWcharBufferN(void) const
255 {
256         std::unique_ptr< WcharBuffer > pBuffer(new (std::nothrow) WcharBuffer());
257         pBuffer->_pData = _pData;
258         AddRef();
259         pBuffer->_capacity = GetRemaining() / MCHAR_SIZE;
260         pBuffer->_limit = pBuffer->_capacity;
261         if (pBuffer->_capacity > 0)
262         {
263                 pBuffer->__pArrayStart = &__pArrayStart[_position];
264         }
265
266         return pBuffer.release();
267 }
268
269 ShortBuffer*
270 ByteBuffer::AsShortBufferN(void) const
271 {
272         std::unique_ptr< ShortBuffer > pBuffer(new (std::nothrow) ShortBuffer());
273         pBuffer->_pData = _pData;
274         AddRef();
275         pBuffer->_capacity = GetRemaining() / SHORT_SIZE;
276         pBuffer->_limit = pBuffer->_capacity;
277         if (pBuffer->_capacity > 0)
278         {
279                 pBuffer->__pArrayStart = &__pArrayStart[_position];
280         }
281
282         return pBuffer.release();
283 }
284
285 result
286 ByteBuffer::CopyFrom(ByteBuffer& buffer)
287 {
288         SysTryReturn(NID_BASE, this != static_cast< void* >(&buffer), E_INVALID_ARG, E_INVALID_ARG,
289                 "[%s] The source and target buffers are identical.", GetErrorMessage(E_INVALID_ARG));
290         int copyLength = buffer.GetRemaining();
291         SysTryReturn(NID_BASE,
292                 GetRemaining() >= copyLength, E_OVERFLOW, E_OVERFLOW,
293                 "[%s] The current buffer is smaller than the input buffer.", GetErrorMessage(E_OVERFLOW));
294
295         memcpy(__pArrayStart + _position, buffer.__pArrayStart + buffer._position, copyLength);
296         _position += copyLength;
297         buffer._position += copyLength;
298
299         return E_SUCCESS;
300 }
301
302 result
303 ByteBuffer::GetArray(byte* pArray, int index, int length)
304 {
305         SysTryReturn(NID_BASE, null != pArray, E_INVALID_ARG, E_INVALID_ARG, "[%s] The pArray is null.",
306                 GetErrorMessage(E_INVALID_ARG));
307         SysTryReturn(NID_BASE, index >= 0 && length >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
308                 "[%s] Both of index(%d) and length(%d) MUST be greater than or equal to 0.", GetErrorMessage(E_OUT_OF_RANGE),
309                 index, length);
310         SysTryReturn(NID_BASE, GetRemaining() >= length, E_UNDERFLOW, E_UNDERFLOW, "[%s]", GetErrorMessage(E_UNDERFLOW));
311
312         memcpy(pArray + index, __pArrayStart + _position, length);
313         _position += length;
314
315         return E_SUCCESS;
316 }
317
318 result
319 ByteBuffer::GetByte(byte& value)
320 {
321         SysTryReturn(NID_BASE, _position < _limit, E_UNDERFLOW, E_UNDERFLOW,
322                 "[%s] The current position is not smaller than the limit.", GetErrorMessage(E_UNDERFLOW));
323
324         value = __pArrayStart[_position++];
325
326         return E_SUCCESS;
327 }
328
329 result
330 ByteBuffer::GetByte(int index, byte& value) const
331 {
332         SysTryReturn(NID_BASE, index < _limit && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
333                 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the current limit(%d).", GetErrorMessage(E_OUT_OF_RANGE),
334                 index, _limit);
335
336         value = __pArrayStart[index];
337
338         return E_SUCCESS;
339 }
340
341 result
342 ByteBuffer::GetDouble(double& value)
343 {
344         SysTryReturn(NID_BASE,
345                 GetRemaining() >= DOUBLE_SIZE, E_UNDERFLOW, E_UNDERFLOW,
346                 "[%s] The remaining bytes of this buffer are smaller than the size of double", GetErrorMessage(E_UNDERFLOW));
347
348         value = *reinterpret_cast< double* >(&__pArrayStart[_position]);
349         _position += DOUBLE_SIZE;
350
351         return E_SUCCESS;
352 }
353
354 result
355 ByteBuffer::GetDouble(int index, double& value) const
356 {
357         SysTryReturn(NID_BASE,
358                 index <= (_limit - DOUBLE_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
359                 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of double(%d)'.",
360                 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, DOUBLE_SIZE);
361
362         value = *reinterpret_cast< double* >(&__pArrayStart[index]);
363
364         return E_SUCCESS;
365 }
366
367 result
368 ByteBuffer::GetFloat(float& value)
369 {
370         SysTryReturn(NID_BASE,
371                 GetRemaining() >= FLOAT_SIZE, E_UNDERFLOW, E_UNDERFLOW,
372                 "[%s] The remaining bytes of this buffer are smaller than the size of float.", GetErrorMessage(E_UNDERFLOW));
373
374         value = *reinterpret_cast< float* >(&__pArrayStart[_position]);
375         _position += FLOAT_SIZE;
376
377         return E_SUCCESS;
378 }
379
380 result
381 ByteBuffer::GetFloat(int index, float& value) const
382 {
383         SysTryReturn(NID_BASE,
384                 index <= (_limit - FLOAT_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
385                 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of float(%d)'.",
386                 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, FLOAT_SIZE);
387
388         value = *reinterpret_cast< float* >(&__pArrayStart[index]);
389
390         return E_SUCCESS;
391 }
392
393 result
394 ByteBuffer::GetInt(int& value)
395 {
396         SysTryReturn(NID_BASE,
397                 GetRemaining() >= INT_SIZE, E_UNDERFLOW, E_UNDERFLOW,
398                 "[%s] The remaining bytes of this buffer are smaller than the size of int.", GetErrorMessage(E_UNDERFLOW));
399
400         value = *reinterpret_cast< int* >(&__pArrayStart[_position]);
401         _position += INT_SIZE;
402
403         return E_SUCCESS;
404 }
405
406 result
407 ByteBuffer::GetInt(int index, int& value) const
408 {
409         SysTryReturn(NID_BASE,
410                 index <= (_limit - INT_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
411                 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of int(%d)'.",
412                 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, INT_SIZE);
413
414         value = *reinterpret_cast< int* >(&__pArrayStart[index]);
415
416         return E_SUCCESS;
417 }
418
419 result
420 ByteBuffer::GetLong(long& value)
421 {
422         SysTryReturn(NID_BASE,
423                 GetRemaining() >= LONG_SIZE, E_UNDERFLOW, E_UNDERFLOW,
424                 "[%s] The remaining bytes of this buffer are smaller than the size of long.", GetErrorMessage(E_UNDERFLOW));
425
426         value = *reinterpret_cast< long* >(&__pArrayStart[_position]);
427         _position += LONG_SIZE;
428
429         return E_SUCCESS;
430 }
431
432 result
433 ByteBuffer::GetLong(int index, long& value) const
434 {
435         SysTryReturn(NID_BASE,
436                 index <= (_limit - LONG_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
437                 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of long(%d)'.",
438                 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, LONG_SIZE);
439
440         value = *reinterpret_cast< long* >(&__pArrayStart[index]);
441
442         return E_SUCCESS;
443 }
444
445 result
446 ByteBuffer::GetLongLong(long long& value)
447 {
448         SysTryReturn(NID_BASE,
449                 GetRemaining() >= LONGLONG_SIZE, E_UNDERFLOW, E_UNDERFLOW,
450                 "[%s] The remaining bytes of this buffer are smaller than the size of long long.", GetErrorMessage(E_UNDERFLOW));
451
452         value = *reinterpret_cast< long long* >(&__pArrayStart[_position]);
453         _position += LONGLONG_SIZE;
454
455         return E_SUCCESS;
456 }
457
458 result
459 ByteBuffer::GetLongLong(int index, long long& value) const
460 {
461         SysTryReturn(NID_BASE,
462                 index <= (_limit - LONGLONG_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
463                 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of long long(%d)'.",
464                 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, LONGLONG_SIZE);
465
466         value = *reinterpret_cast< long long* >(&__pArrayStart[index]);
467
468         return E_SUCCESS;
469 }
470
471 result
472 ByteBuffer::GetMchar(wchar_t& value)
473 {
474         SysTryReturn(NID_BASE,
475                 GetRemaining() >= MCHAR_SIZE, E_UNDERFLOW, E_UNDERFLOW,
476                 "[%s] The remaining bytes of this buffer are smaller than the size of wchar_t.", GetErrorMessage(E_UNDERFLOW));
477
478         value = *reinterpret_cast< wchar_t* >(&__pArrayStart[_position]);
479         _position += MCHAR_SIZE;
480
481         return E_SUCCESS;
482 }
483
484 result
485 ByteBuffer::GetWchar(wchar_t& value)
486 {
487         SysTryReturn(NID_BASE,
488                 GetRemaining() >= MCHAR_SIZE, E_UNDERFLOW, E_UNDERFLOW,
489                 "[%s] The remaining bytes of this buffer are smaller than the size of wchar_t.", GetErrorMessage(E_UNDERFLOW));
490
491         value = *reinterpret_cast< wchar_t* >(&__pArrayStart[_position]);
492         _position += MCHAR_SIZE;
493
494         return E_SUCCESS;
495 }
496
497 result
498 ByteBuffer::GetMchar(int index, wchar_t& value) const
499 {
500         SysTryReturn(NID_BASE,
501                 index <= (_limit - MCHAR_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
502                 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of wchar_t(%d)'.",
503                 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, MCHAR_SIZE);
504
505         value = *reinterpret_cast< wchar_t* >(&__pArrayStart[index]);
506
507         return E_SUCCESS;
508 }
509
510 result
511 ByteBuffer::GetWchar(int index, wchar_t& value) const
512 {
513         SysTryReturn(NID_BASE,
514                 index <= (_limit - MCHAR_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
515                 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of wchar_t(%d)'.",
516                 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, MCHAR_SIZE);
517
518         value = *reinterpret_cast< wchar_t* >(&__pArrayStart[index]);
519
520         return E_SUCCESS;
521 }
522
523 result
524 ByteBuffer::GetShort(short& value)
525 {
526         SysTryReturn(NID_BASE,
527                 GetRemaining() >= SHORT_SIZE, E_UNDERFLOW, E_UNDERFLOW,
528                 "[%s] The remaining bytes of this buffer are smaller than the size of short.", GetErrorMessage(E_UNDERFLOW));
529
530         value = *reinterpret_cast< short* >(&__pArrayStart[_position]);
531         _position += SHORT_SIZE;
532
533         return E_SUCCESS;
534 }
535
536 result
537 ByteBuffer::GetShort(int index, short& value) const
538 {
539         SysTryReturn(NID_BASE,
540                 index <= (_limit - SHORT_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
541                 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of short(%d)'.",
542                 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, SHORT_SIZE);
543
544         value = *reinterpret_cast< short* >(&__pArrayStart[index]);
545
546         return E_SUCCESS;
547 }
548
549 result
550 ByteBuffer::ReadFrom(ByteBuffer& buffer)
551 {
552         SysTryReturn(NID_BASE, this != static_cast< void* >(&buffer), E_INVALID_ARG, E_INVALID_ARG,
553                 "[%s] The source and target buffers are identical.", GetErrorMessage(E_INVALID_ARG));
554
555         int copyLength = (GetRemaining() < buffer.GetRemaining()) ? GetRemaining() : buffer.GetRemaining();
556         memcpy(__pArrayStart + _position, buffer.__pArrayStart + buffer._position, copyLength);
557         _position += copyLength;
558         buffer._position += copyLength;
559
560         return E_SUCCESS;
561 }
562
563 result
564 ByteBuffer::SetArray(const byte* pArray, int index, int length)
565 {
566         SysTryReturn(NID_BASE, null != pArray, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The pArray is null.");
567         SysTryReturn(NID_BASE, index >= 0 && length >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
568                 "[%s] Both of index(%d) and length(%d) MUST be greater than or equal to 0.", GetErrorMessage(E_OUT_OF_RANGE),
569                 index, length);
570         SysTryReturn(NID_BASE, GetRemaining() >= length, E_OVERFLOW, E_OVERFLOW, "[%s] The remaining bytes are smaller than length.",
571                 GetErrorMessage(E_OVERFLOW));
572
573         memcpy(__pArrayStart + _position, pArray + index, length);
574         _position += length;
575
576         return E_SUCCESS;
577 }
578
579 result
580 ByteBuffer::SetByte(byte value)
581 {
582         SysTryReturn(NID_BASE, _position < _limit, E_OVERFLOW, E_OVERFLOW,
583                 "[%s] The current position is not smaller than the limit.", GetErrorMessage(E_OVERFLOW));
584
585         __pArrayStart[_position++] = value;
586         return E_SUCCESS;
587 }
588
589 result
590 ByteBuffer::SetByte(int index, byte value)
591 {
592         SysTryReturn(NID_BASE, index < _limit && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
593                 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the current limit(%d).",
594                 GetErrorMessage(E_OUT_OF_RANGE), index, _limit);
595
596         __pArrayStart[index] = value;
597         return E_SUCCESS;
598 }
599
600 result
601 ByteBuffer::SetDouble(double value)
602 {
603         SysTryReturn(NID_BASE,
604                 GetRemaining() >= DOUBLE_SIZE, E_OVERFLOW, E_OVERFLOW,
605                 "[%s] The remaining bytes of this buffer are smaller than size of double.", GetErrorMessage(E_OVERFLOW));
606
607         *reinterpret_cast< double* >(&__pArrayStart[_position]) = value;
608         _position += DOUBLE_SIZE;
609
610         return E_SUCCESS;
611 }
612
613 result
614 ByteBuffer::SetDouble(int index, double value)
615 {
616         SysTryReturn(NID_BASE,
617                 index <= (_limit - DOUBLE_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
618                 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of double(%d)'.",
619                 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, DOUBLE_SIZE);
620
621         *reinterpret_cast< double* >(&__pArrayStart[index]) = value;
622
623         return E_SUCCESS;
624 }
625
626 result
627 ByteBuffer::SetFloat(float value)
628 {
629         SysTryReturn(NID_BASE,
630                 GetRemaining() >= FLOAT_SIZE, E_OVERFLOW, E_OVERFLOW,
631                 "[%s] The remaining bytes of this buffer are smaller than size of float.", GetErrorMessage(E_OVERFLOW));
632
633         *reinterpret_cast< float* >(&__pArrayStart[_position]) = value;
634         _position += FLOAT_SIZE;
635
636         return E_SUCCESS;
637 }
638
639 result
640 ByteBuffer::SetFloat(int index, float value)
641 {
642         SysTryReturn(NID_BASE,
643                 index <= (_limit - FLOAT_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
644                 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of float(%d)'.",
645                 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, FLOAT_SIZE);
646
647         *reinterpret_cast< float* >(&__pArrayStart[index]) = value;
648
649         return E_SUCCESS;
650 }
651
652 result
653 ByteBuffer::SetInt(int value)
654 {
655         SysTryReturn(NID_BASE,
656                 GetRemaining() >= INT_SIZE, E_OVERFLOW, E_OVERFLOW,
657                 "[%s] The remaining bytes of this buffer are smaller than size of int.", GetErrorMessage(E_OVERFLOW));
658
659         *reinterpret_cast< int* >(&__pArrayStart[_position]) = value;
660         _position += INT_SIZE;
661
662         return E_SUCCESS;
663 }
664
665 result
666 ByteBuffer::SetInt(int index, int value)
667 {
668         SysTryReturn(NID_BASE,
669                 index <= (_limit - INT_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
670                 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of int(%d)'.",
671                 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, INT_SIZE);
672
673         *reinterpret_cast< int* >(&__pArrayStart[index]) = value;
674
675         return E_SUCCESS;
676 }
677
678 result
679 ByteBuffer::SetLong(long value)
680 {
681         SysTryReturn(NID_BASE,
682                 GetRemaining() >= LONG_SIZE, E_OVERFLOW, E_OVERFLOW,
683                 "[%s] The remaining bytes of this buffer are smaller than size of long.", GetErrorMessage(E_OVERFLOW));
684
685         *reinterpret_cast< long* >(&__pArrayStart[_position]) = value;
686         _position += LONG_SIZE;
687
688         return E_SUCCESS;
689 }
690
691 result
692 ByteBuffer::SetLong(int index, long value)
693 {
694         SysTryReturn(NID_BASE,
695                 index <= (_limit - LONG_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
696                 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of long(%d)'.",
697                 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, LONG_SIZE);
698
699         *reinterpret_cast< long* >(&__pArrayStart[index]) = value;
700
701         return E_SUCCESS;
702 }
703
704 result
705 ByteBuffer::SetLongLong(long long value)
706 {
707         SysTryReturn(NID_BASE,
708                 GetRemaining() >= LONGLONG_SIZE, E_OVERFLOW, E_OVERFLOW,
709                 "[%s] The remaining bytes of this buffer are smaller than size of long long.", GetErrorMessage(E_OVERFLOW));
710
711         *reinterpret_cast< long long* >(&__pArrayStart[_position]) = value;
712         _position += LONGLONG_SIZE;
713
714         return E_SUCCESS;
715 }
716
717 result
718 ByteBuffer::SetLongLong(int index, long long value)
719 {
720         SysTryReturn(NID_BASE,
721                 index <= (_limit - LONGLONG_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
722                 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of long long(%d)'.",
723                 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, LONGLONG_SIZE);
724
725         *reinterpret_cast< long long* >(&__pArrayStart[index]) = value;
726
727         return E_SUCCESS;
728 }
729
730 result
731 ByteBuffer::SetMchar(wchar_t value)
732 {
733         SysTryReturn(NID_BASE,
734                 GetRemaining() >= MCHAR_SIZE, E_OVERFLOW, E_OVERFLOW,
735                 "[%s] The remaining bytes of this buffer are smaller than size of wchar_t.", GetErrorMessage(E_OVERFLOW));
736
737         *reinterpret_cast< wchar_t* >(&__pArrayStart[_position]) = value;
738         _position += MCHAR_SIZE;
739
740         return E_SUCCESS;
741 }
742
743 result
744 ByteBuffer::SetWchar(wchar_t value)
745 {
746         SysTryReturn(NID_BASE,
747                 GetRemaining() >= MCHAR_SIZE, E_OVERFLOW, E_OVERFLOW,
748                 "[%s] The remaining bytes of this buffer are smaller than size of wchar_t.", GetErrorMessage(E_OVERFLOW));
749
750         *reinterpret_cast< wchar_t* >(&__pArrayStart[_position]) = value;
751         _position += MCHAR_SIZE;
752
753         return E_SUCCESS;
754 }
755
756 result
757 ByteBuffer::SetMchar(int index, wchar_t value)
758 {
759         SysTryReturn(NID_BASE,
760                 index <= (_limit - MCHAR_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
761                 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of wchar_t(%d)'.",
762                 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, MCHAR_SIZE);
763
764         *reinterpret_cast< wchar_t* >(&__pArrayStart[index]) = value;
765
766         return E_SUCCESS;
767 }
768
769 result
770 ByteBuffer::SetWchar(int index, wchar_t value)
771 {
772         SysTryReturn(NID_BASE,
773                 index <= (_limit - MCHAR_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
774                 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of wchar_t(%d)'.",
775                 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, MCHAR_SIZE);
776
777         *reinterpret_cast< wchar_t* >(&__pArrayStart[index]) = value;
778
779         return E_SUCCESS;
780 }
781
782 result
783 ByteBuffer::SetShort(short value)
784 {
785         SysTryReturn(NID_BASE,
786                 GetRemaining() >= SHORT_SIZE, E_OVERFLOW, E_OVERFLOW,
787                 "[%s] The remaining bytes of this buffer are smaller than size of short.", GetErrorMessage(E_OVERFLOW));
788
789         *reinterpret_cast< short* >(&__pArrayStart[_position]) = value;
790         _position += SHORT_SIZE;
791
792         return E_SUCCESS;
793 }
794
795 result
796 ByteBuffer::SetShort(int index, short value)
797 {
798         SysTryReturn(NID_BASE,
799                 index <= (_limit - SHORT_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
800                 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of short(%d)'.",
801                 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, SHORT_SIZE);
802
803         *reinterpret_cast< short* >(&__pArrayStart[index]) = value;
804
805         return E_SUCCESS;
806 }
807
808 ByteBuffer*
809 ByteBuffer::SliceN(void) const
810 {
811         std::unique_ptr< ByteBuffer > pBuffer(new (std::nothrow) ByteBuffer());
812         pBuffer->_pData = _pData;
813         AddRef();
814         pBuffer->_capacity = GetRemaining();
815         pBuffer->_limit = pBuffer->_capacity;
816         if (pBuffer->_capacity > 0)
817         {
818                 pBuffer->__pArrayStart = &__pArrayStart[_position];
819         }
820
821         return pBuffer.release();
822 }
823
824 const byte*
825 ByteBuffer::GetPointer(void) const
826 {
827         return __pArrayStart;
828 }
829
830 byte*
831 ByteBuffer::GetPointer(void)
832 {
833         return __pArrayStart;
834 }
835
836 bool
837 ByteBuffer::Equals(const Tizen::Base::Object& obj) const
838 {
839         bool out = false;
840         const ByteBuffer* pOther = dynamic_cast< const ByteBuffer* >(&obj);
841         if (pOther == null)
842         {
843                 out = false;
844         }
845         else if ((pOther == this) || (*pOther == *this))
846         {
847                 out = true;
848         }
849
850         return out;
851 }
852
853 int
854 ByteBuffer::GetHashCode(void) const
855 {
856         int len = GetRemaining() / sizeof(len);
857
858         int hash = 0;
859         int offset = _position;
860         for (int i = 0; i < len; ++i)
861         {
862                 hash = (hash << 5) - hash + static_cast< int >(__pArrayStart[offset + (i * sizeof(hash))]);
863         }
864
865         return hash;
866 }
867
868 int
869 ByteBuffer::GetTypeSize(void) const
870 {
871         return 1;
872 }
873
874 }} // Tizen::Base