2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FBaseByteBuffer.cpp
19 * @brief This is the implementation for ByteBuffer class.
23 #include <FBaseByteBuffer.h>
24 #include <FBaseSysLog.h>
25 #include <unique_ptr.h>
28 static const byte DOUBLE_SIZE = sizeof(double);
29 static const byte FLOAT_SIZE = sizeof(float);
30 static const byte INT_SIZE = sizeof(int);
31 static const byte LONG_SIZE = sizeof(long);
32 static const byte LONGLONG_SIZE = sizeof(long long);
33 static const byte MCHAR_SIZE = sizeof(wchar_t);
34 static const byte SHORT_SIZE = sizeof(short);
36 namespace Tizen { namespace Base
39 ByteBuffer::ByteBuffer(void)
40 : __pByteBufferImpl(null)
44 ByteBuffer::~ByteBuffer(void)
49 ByteBuffer::Construct(const ByteBuffer& buffer)
51 SysTryReturn(NID_BASE, null != buffer._pData, E_INVALID_ARG, E_INVALID_ARG,
52 "[%s] The source buffer is not constructed.", GetErrorMessage(E_INVALID_ARG));
54 _capacity = buffer._capacity;
55 _position = buffer._position;
56 _limit = buffer._limit;
58 _pData = buffer._pData;
61 __pArrayStart = buffer.__pArrayStart;
67 ByteBuffer::Construct(int capacity)
69 result r = BufferBase::Construct(capacity);
72 SysLogException(NID_BASE, r, "[%s] Propagated.", GetErrorMessage(r));
79 ByteBuffer::Construct(const byte *pBuffer, int index, int length, int capacity)
81 SysTryReturn(NID_BASE, pBuffer != null, E_INVALID_ARG, E_INVALID_ARG, "[%s] The pBuffer is null.",
82 GetErrorMessage(E_INVALID_ARG));
83 SysTryReturn(NID_BASE, index >= 0 && length >= 0 && capacity >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
84 "[%s] index(%d), length(%d) and capacity(%d) MUST be greater than or equal to 0.",
85 GetErrorMessage(E_OUT_OF_RANGE), index, length, capacity);
86 SysTryReturn(NID_BASE, index < capacity && length <= capacity && index + length <= capacity,
87 E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[%s] index(%d), length(%d) and capacity(%d) MUST be greater than or equal to 0.",
88 GetErrorMessage(E_OUT_OF_RANGE), index, length, capacity);
91 int sizeofBufferData = sizeof(_BufferData);
93 __pArrayStart = const_cast<byte *> (pBuffer + index);
95 pTemp = malloc(sizeofBufferData);
96 SysTryReturn(NID_BASE, pTemp != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s]", GetErrorMessage(E_OUT_OF_MEMORY));
98 memset(pTemp, 0, sizeofBufferData);
99 _pData = static_cast <_BufferData*>(pTemp);
101 _pData->refCount = 1;
102 _pData->capacityInByte = capacity;
104 _capacity = capacity;
111 ByteBuffer::operator [](int index)
113 SysAssertf(index < _limit, "index out of range.\n");
114 SysAssertf(index >= 0, "index out of range.\n");
116 return __pArrayStart[index];
120 ByteBuffer::operator [](int index) const
122 return const_cast<ByteBuffer&>(*this)[index];
126 ByteBuffer::operator ==(const ByteBuffer& buffer) const
134 else if (GetRemaining() != buffer.GetRemaining())
140 for (int i = 0; i < GetRemaining(); ++i)
142 if (__pArrayStart[_position + i] != buffer.__pArrayStart[buffer._position + i])
154 ByteBuffer::operator !=(const ByteBuffer& buffer) const
156 return !(*this == buffer);
160 ByteBuffer::AsDoubleBufferN(void) const
162 std::unique_ptr<DoubleBuffer> pBuffer(new (std::nothrow) DoubleBuffer());
163 pBuffer->_pData = _pData;
165 pBuffer->_capacity = GetRemaining() / DOUBLE_SIZE;
166 pBuffer->_limit = pBuffer->_capacity;
167 if (pBuffer->_capacity > 0)
169 pBuffer->__pArrayStart = &__pArrayStart[_position];
172 return pBuffer.release();
176 ByteBuffer::AsFloatBufferN(void) const
178 std::unique_ptr<FloatBuffer> pBuffer(new (std::nothrow) FloatBuffer());
179 pBuffer->_pData = _pData;
181 pBuffer->_capacity = GetRemaining() / FLOAT_SIZE;
182 pBuffer->_limit = pBuffer->_capacity;
183 if (pBuffer->_capacity > 0)
185 pBuffer->__pArrayStart = &__pArrayStart[_position];
188 return pBuffer.release();
192 ByteBuffer::AsIntBufferN(void) const
194 std::unique_ptr<IntBuffer> pBuffer(new (std::nothrow) IntBuffer());
195 pBuffer->_pData = _pData;
197 pBuffer->_capacity = GetRemaining() / INT_SIZE;
198 pBuffer->_limit = pBuffer->_capacity;
199 if (pBuffer->_capacity > 0)
201 pBuffer->__pArrayStart = &__pArrayStart[_position];
204 return pBuffer.release();
208 ByteBuffer::AsLongBufferN(void) const
210 std::unique_ptr<LongBuffer> pBuffer(new (std::nothrow) LongBuffer());
211 pBuffer->_pData = _pData;
213 pBuffer->_capacity = GetRemaining() / LONG_SIZE;
214 pBuffer->_limit = pBuffer->_capacity;
215 if (pBuffer->_capacity > 0)
217 pBuffer->__pArrayStart = &__pArrayStart[_position];
220 return pBuffer.release();
224 ByteBuffer::AsLongLongBufferN(void) const
226 std::unique_ptr<LongLongBuffer> pBuffer(new (std::nothrow) LongLongBuffer());
227 pBuffer->_pData = _pData;
229 pBuffer->_capacity = GetRemaining() / LONGLONG_SIZE;
230 pBuffer->_limit = pBuffer->_capacity;
231 if (pBuffer->_capacity > 0)
233 pBuffer->__pArrayStart = &__pArrayStart[_position];
236 return pBuffer.release();
240 ByteBuffer::AsMcharBufferN(void) const
242 std::unique_ptr<McharBuffer> pBuffer(new (std::nothrow) McharBuffer());
243 pBuffer->_pData = _pData;
245 pBuffer->_capacity = GetRemaining() / MCHAR_SIZE;
246 pBuffer->_limit = pBuffer->_capacity;
247 if (pBuffer->_capacity > 0)
249 pBuffer->__pArrayStart = &__pArrayStart[_position];
252 return pBuffer.release();
256 ByteBuffer::AsWcharBufferN(void) const
258 std::unique_ptr<WcharBuffer> pBuffer(new (std::nothrow) WcharBuffer());
259 pBuffer->_pData = _pData;
261 pBuffer->_capacity = GetRemaining() / MCHAR_SIZE;
262 pBuffer->_limit = pBuffer->_capacity;
263 if (pBuffer->_capacity > 0)
265 pBuffer->__pArrayStart = &__pArrayStart[_position];
268 return pBuffer.release();
272 ByteBuffer::AsShortBufferN(void) const
274 std::unique_ptr<ShortBuffer> pBuffer(new (std::nothrow) ShortBuffer());
275 pBuffer->_pData = _pData;
277 pBuffer->_capacity = GetRemaining() / SHORT_SIZE;
278 pBuffer->_limit = pBuffer->_capacity;
279 if (pBuffer->_capacity > 0)
281 pBuffer->__pArrayStart = &__pArrayStart[_position];
284 return pBuffer.release();
288 ByteBuffer::CopyFrom(ByteBuffer& buffer)
290 SysTryReturn(NID_BASE, this != static_cast <void*>(&buffer), E_INVALID_ARG, E_INVALID_ARG,
291 "[%s] The source and target buffers are identical.", GetErrorMessage(E_INVALID_ARG));
292 int copyLength = buffer.GetRemaining();
293 SysTryReturn(NID_BASE,
294 GetRemaining() >= copyLength, E_OVERFLOW, E_OVERFLOW,
295 "[%s] The current buffer is smaller than the input buffer.", GetErrorMessage(E_OVERFLOW));
297 memcpy(__pArrayStart + _position, buffer.__pArrayStart + buffer._position, copyLength);
298 _position += copyLength;
299 buffer._position += copyLength;
305 ByteBuffer::GetArray(byte* pArray, int index, int length)
307 SysTryReturn(NID_BASE, null != pArray, E_INVALID_ARG, E_INVALID_ARG, "[%s] The pArray is null.",
308 GetErrorMessage(E_INVALID_ARG));
309 SysTryReturn(NID_BASE, index >= 0 && length >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
310 "[%s] Both of index(%d) and length(%d) MUST be greater than or equal to 0.", GetErrorMessage(E_OUT_OF_RANGE),
312 SysTryReturn(NID_BASE, GetRemaining() >= length, E_UNDERFLOW, E_UNDERFLOW, "[%s]", GetErrorMessage(E_UNDERFLOW));
314 memcpy(pArray + index, __pArrayStart + _position, length);
321 ByteBuffer::GetByte(byte& value)
323 SysTryReturn(NID_BASE, _position < _limit, E_UNDERFLOW, E_UNDERFLOW,
324 "[%s] The current position is not smaller than the limit.", GetErrorMessage(E_UNDERFLOW));
326 value = __pArrayStart[_position++];
332 ByteBuffer::GetByte(int index, byte& value) const
334 SysTryReturn(NID_BASE, index < _limit && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
335 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the current limit(%d).", GetErrorMessage(E_OUT_OF_RANGE),
338 value = __pArrayStart[index];
344 ByteBuffer::GetDouble(double& value)
346 SysTryReturn(NID_BASE,
347 GetRemaining() >= DOUBLE_SIZE, E_UNDERFLOW, E_UNDERFLOW,
348 "[%s] The remaining bytes of this buffer are smaller than the size of double", GetErrorMessage(E_UNDERFLOW));
350 value = *reinterpret_cast <double*>(&__pArrayStart[_position]);
351 _position += DOUBLE_SIZE;
357 ByteBuffer::GetDouble(int index, double& value) const
359 SysTryReturn(NID_BASE,
360 index <= (_limit - DOUBLE_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
361 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of double(%d)'.",
362 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, DOUBLE_SIZE);
364 value = *reinterpret_cast <double*>(&__pArrayStart[index]);
370 ByteBuffer::GetFloat(float& value)
372 SysTryReturn(NID_BASE,
373 GetRemaining() >= FLOAT_SIZE, E_UNDERFLOW, E_UNDERFLOW,
374 "[%s] The remaining bytes of this buffer are smaller than the size of float.", GetErrorMessage(E_UNDERFLOW));
376 value = *reinterpret_cast <float*>(&__pArrayStart[_position]);
377 _position += FLOAT_SIZE;
383 ByteBuffer::GetFloat(int index, float& value) const
385 SysTryReturn(NID_BASE,
386 index <= (_limit - FLOAT_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
387 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of float(%d)'.",
388 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, FLOAT_SIZE);
390 value = *reinterpret_cast <float*>(&__pArrayStart[index]);
396 ByteBuffer::GetInt(int& value)
398 SysTryReturn(NID_BASE,
399 GetRemaining() >= INT_SIZE, E_UNDERFLOW, E_UNDERFLOW,
400 "[%s] The remaining bytes of this buffer are smaller than the size of int.", GetErrorMessage(E_UNDERFLOW));
402 value = *reinterpret_cast <int*>(&__pArrayStart[_position]);
403 _position += INT_SIZE;
409 ByteBuffer::GetInt(int index, int& value) const
411 SysTryReturn(NID_BASE,
412 index <= (_limit - INT_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
413 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of int(%d)'.",
414 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, INT_SIZE);
416 value = *reinterpret_cast <int*>(&__pArrayStart[index]);
422 ByteBuffer::GetLong(long& value)
424 SysTryReturn(NID_BASE,
425 GetRemaining() >= LONG_SIZE, E_UNDERFLOW, E_UNDERFLOW,
426 "[%s] The remaining bytes of this buffer are smaller than the size of long.", GetErrorMessage(E_UNDERFLOW));
428 value = *reinterpret_cast <long*>(&__pArrayStart[_position]);
429 _position += LONG_SIZE;
435 ByteBuffer::GetLong(int index, long& value) const
437 SysTryReturn(NID_BASE,
438 index <= (_limit - LONG_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
439 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of long(%d)'.",
440 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, LONG_SIZE);
442 value = *reinterpret_cast <long*>(&__pArrayStart[index]);
448 ByteBuffer::GetLongLong(long long& value)
450 SysTryReturn(NID_BASE,
451 GetRemaining() >= LONGLONG_SIZE, E_UNDERFLOW, E_UNDERFLOW,
452 "[%s] The remaining bytes of this buffer are smaller than the size of long long.", GetErrorMessage(E_UNDERFLOW));
454 value = *reinterpret_cast <long long*>(&__pArrayStart[_position]);
455 _position += LONGLONG_SIZE;
461 ByteBuffer::GetLongLong(int index, long long& value) const
463 SysTryReturn(NID_BASE,
464 index <= (_limit - LONGLONG_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
465 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of long long(%d)'.",
466 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, LONGLONG_SIZE);
468 value = *reinterpret_cast <long long*>(&__pArrayStart[index]);
474 ByteBuffer::GetMchar(wchar_t& value)
476 SysTryReturn(NID_BASE,
477 GetRemaining() >= MCHAR_SIZE, E_UNDERFLOW, E_UNDERFLOW,
478 "[%s] The remaining bytes of this buffer are smaller than the size of wchar_t.", GetErrorMessage(E_UNDERFLOW));
480 value = *reinterpret_cast <wchar_t*>(&__pArrayStart[_position]);
481 _position += MCHAR_SIZE;
487 ByteBuffer::GetWchar(wchar_t& value)
489 SysTryReturn(NID_BASE,
490 GetRemaining() >= MCHAR_SIZE, E_UNDERFLOW, E_UNDERFLOW,
491 "[%s] The remaining bytes of this buffer are smaller than the size of wchar_t.", GetErrorMessage(E_UNDERFLOW));
493 value = *reinterpret_cast <wchar_t*>(&__pArrayStart[_position]);
494 _position += MCHAR_SIZE;
500 ByteBuffer::GetMchar(int index, wchar_t& value) const
502 SysTryReturn(NID_BASE,
503 index <= (_limit - MCHAR_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
504 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of wchar_t(%d)'.",
505 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, MCHAR_SIZE);
507 value = *reinterpret_cast <wchar_t*>(&__pArrayStart[index]);
513 ByteBuffer::GetWchar(int index, wchar_t& value) const
515 SysTryReturn(NID_BASE,
516 index <= (_limit - MCHAR_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
517 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of wchar_t(%d)'.",
518 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, MCHAR_SIZE);
520 value = *reinterpret_cast <wchar_t*>(&__pArrayStart[index]);
526 ByteBuffer::GetShort(short& value)
528 SysTryReturn(NID_BASE,
529 GetRemaining() >= SHORT_SIZE, E_UNDERFLOW, E_UNDERFLOW,
530 "[%s] The remaining bytes of this buffer are smaller than the size of short.", GetErrorMessage(E_UNDERFLOW));
532 value = *reinterpret_cast <short*>(&__pArrayStart[_position]);
533 _position += SHORT_SIZE;
539 ByteBuffer::GetShort(int index, short& value) const
541 SysTryReturn(NID_BASE,
542 index <= (_limit - SHORT_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
543 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of short(%d)'.",
544 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, SHORT_SIZE);
546 value = *reinterpret_cast <short*>(&__pArrayStart[index]);
552 ByteBuffer::ReadFrom(ByteBuffer& buffer)
554 SysTryReturn(NID_BASE, this != static_cast <void*>(&buffer), E_INVALID_ARG, E_INVALID_ARG,
555 "[%s] The source and target buffers are identical.", GetErrorMessage(E_INVALID_ARG));
557 int copyLength = (GetRemaining() < buffer.GetRemaining()) ? GetRemaining() : buffer.GetRemaining();
558 memcpy(__pArrayStart + _position, buffer.__pArrayStart + buffer._position, copyLength);
559 _position += copyLength;
560 buffer._position += copyLength;
566 ByteBuffer::SetArray(const byte* pArray, int index, int length)
568 SysTryReturn(NID_BASE, null != pArray, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The pArray is null.");
569 SysTryReturn(NID_BASE, index >= 0 && length >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
570 "[%s] Both of index(%d) and length(%d) MUST be greater than or equal to 0.", GetErrorMessage(E_OUT_OF_RANGE),
572 SysTryReturn(NID_BASE, GetRemaining() >= length, E_OVERFLOW, E_OVERFLOW, "[%s] The remaining bytes are smaller than length.",
573 GetErrorMessage(E_OVERFLOW));
575 memcpy(__pArrayStart + _position, pArray + index, length);
582 ByteBuffer::SetByte(byte value)
584 SysTryReturn(NID_BASE, _position < _limit, E_OVERFLOW, E_OVERFLOW,
585 "[%s] The current position is not smaller than the limit.", GetErrorMessage(E_OVERFLOW));
587 __pArrayStart[_position++] = value;
592 ByteBuffer::SetByte(int index, byte value)
594 SysTryReturn(NID_BASE, index < _limit && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
595 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the current limit(%d).",
596 GetErrorMessage(E_OUT_OF_RANGE), index, _limit);
598 __pArrayStart[index] = value;
603 ByteBuffer::SetDouble(double value)
605 SysTryReturn(NID_BASE,
606 GetRemaining() >= DOUBLE_SIZE, E_OVERFLOW, E_OVERFLOW,
607 "[%s] The remaining bytes of this buffer are smaller than size of double.", GetErrorMessage(E_OVERFLOW));
609 *reinterpret_cast <double*>(&__pArrayStart[_position]) = value;
610 _position += DOUBLE_SIZE;
616 ByteBuffer::SetDouble(int index, double value)
618 SysTryReturn(NID_BASE,
619 index <= (_limit - DOUBLE_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
620 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of double(%d)'.",
621 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, DOUBLE_SIZE);
623 *reinterpret_cast <double*>(&__pArrayStart[index]) = value;
629 ByteBuffer::SetFloat(float value)
631 SysTryReturn(NID_BASE,
632 GetRemaining() >= FLOAT_SIZE, E_OVERFLOW, E_OVERFLOW,
633 "[%s] The remaining bytes of this buffer are smaller than size of float.", GetErrorMessage(E_OVERFLOW));
635 *reinterpret_cast <float*>(&__pArrayStart[_position]) = value;
636 _position += FLOAT_SIZE;
642 ByteBuffer::SetFloat(int index, float value)
644 SysTryReturn(NID_BASE,
645 index <= (_limit - FLOAT_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
646 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of float(%d)'.",
647 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, FLOAT_SIZE);
649 *reinterpret_cast <float*>(&__pArrayStart[index]) = value;
655 ByteBuffer::SetInt(int value)
657 SysTryReturn(NID_BASE,
658 GetRemaining() >= INT_SIZE, E_OVERFLOW, E_OVERFLOW,
659 "[%s] The remaining bytes of this buffer are smaller than size of int.", GetErrorMessage(E_OVERFLOW));
661 *reinterpret_cast <int*>(&__pArrayStart[_position]) = value;
662 _position += INT_SIZE;
668 ByteBuffer::SetInt(int index, int value)
670 SysTryReturn(NID_BASE,
671 index <= (_limit - INT_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
672 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of int(%d)'.",
673 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, INT_SIZE);
675 *reinterpret_cast <int*>(&__pArrayStart[index]) = value;
681 ByteBuffer::SetLong(long value)
683 SysTryReturn(NID_BASE,
684 GetRemaining() >= LONG_SIZE, E_OVERFLOW, E_OVERFLOW,
685 "[%s] The remaining bytes of this buffer are smaller than size of long.", GetErrorMessage(E_OVERFLOW));
687 *reinterpret_cast <long*>(&__pArrayStart[_position]) = value;
688 _position += LONG_SIZE;
694 ByteBuffer::SetLong(int index, long value)
696 SysTryReturn(NID_BASE,
697 index <= (_limit - LONG_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
698 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of long(%d)'.",
699 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, LONG_SIZE);
701 *reinterpret_cast <long*>(&__pArrayStart[index]) = value;
707 ByteBuffer::SetLongLong(long long value)
709 SysTryReturn(NID_BASE,
710 GetRemaining() >= LONGLONG_SIZE, E_OVERFLOW, E_OVERFLOW,
711 "[%s] The remaining bytes of this buffer are smaller than size of long long.", GetErrorMessage(E_OVERFLOW));
713 *reinterpret_cast <long long*>(&__pArrayStart[_position]) = value;
714 _position += LONGLONG_SIZE;
720 ByteBuffer::SetLongLong(int index, long long value)
722 SysTryReturn(NID_BASE,
723 index <= (_limit - LONGLONG_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
724 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of long long(%d)'.",
725 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, LONGLONG_SIZE);
727 *reinterpret_cast <long long*>(&__pArrayStart[index]) = value;
733 ByteBuffer::SetMchar(wchar_t value)
735 SysTryReturn(NID_BASE,
736 GetRemaining() >= MCHAR_SIZE, E_OVERFLOW, E_OVERFLOW,
737 "[%s] The remaining bytes of this buffer are smaller than size of wchar_t.", GetErrorMessage(E_OVERFLOW));
739 *reinterpret_cast <wchar_t*>(&__pArrayStart[_position]) = value;
740 _position += MCHAR_SIZE;
746 ByteBuffer::SetWchar(wchar_t value)
748 SysTryReturn(NID_BASE,
749 GetRemaining() >= MCHAR_SIZE, E_OVERFLOW, E_OVERFLOW,
750 "[%s] The remaining bytes of this buffer are smaller than size of wchar_t.", GetErrorMessage(E_OVERFLOW));
752 *reinterpret_cast <wchar_t*>(&__pArrayStart[_position]) = value;
753 _position += MCHAR_SIZE;
759 ByteBuffer::SetMchar(int index, wchar_t value)
761 SysTryReturn(NID_BASE,
762 index <= (_limit - MCHAR_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
763 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of wchar_t(%d)'.",
764 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, MCHAR_SIZE);
766 *reinterpret_cast <wchar_t*>(&__pArrayStart[index]) = value;
772 ByteBuffer::SetWchar(int index, wchar_t value)
774 SysTryReturn(NID_BASE,
775 index <= (_limit - MCHAR_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
776 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of wchar_t(%d)'.",
777 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, MCHAR_SIZE);
779 *reinterpret_cast <wchar_t*>(&__pArrayStart[index]) = value;
785 ByteBuffer::SetShort(short value)
787 SysTryReturn(NID_BASE,
788 GetRemaining() >= SHORT_SIZE, E_OVERFLOW, E_OVERFLOW,
789 "[%s] The remaining bytes of this buffer are smaller than size of short.", GetErrorMessage(E_OVERFLOW));
791 *reinterpret_cast <short*>(&__pArrayStart[_position]) = value;
792 _position += SHORT_SIZE;
798 ByteBuffer::SetShort(int index, short value)
800 SysTryReturn(NID_BASE,
801 index <= (_limit - SHORT_SIZE) && index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE,
802 "[%s] The index(%d) MUST be greater than or equal to 0, and less then the 'current limit(%d) - size of short(%d)'.",
803 GetErrorMessage(E_OUT_OF_RANGE), index, _limit, SHORT_SIZE);
805 *reinterpret_cast <short*>(&__pArrayStart[index]) = value;
811 ByteBuffer::SliceN(void) const
813 std::unique_ptr<ByteBuffer> pBuffer(new (std::nothrow) ByteBuffer());
814 pBuffer->_pData = _pData;
816 pBuffer->_capacity = GetRemaining();
817 pBuffer->_limit = pBuffer->_capacity;
818 if (pBuffer->_capacity > 0)
820 pBuffer->__pArrayStart = &__pArrayStart[_position];
823 return pBuffer.release();
827 ByteBuffer::GetPointer(void) const
829 return __pArrayStart;
833 ByteBuffer::GetPointer(void)
835 return __pArrayStart;
839 ByteBuffer::Equals(const Tizen::Base::Object& obj) const
842 const ByteBuffer* pOther = dynamic_cast <const ByteBuffer*>(&obj);
847 else if ((pOther == this) || (*pOther == *this))
856 ByteBuffer::GetHashCode(void) const
858 int len = GetRemaining() / sizeof(len);
861 int offset = _position;
862 for (int i = 0; i < len; ++i)
864 hash = (hash << 5) - hash + static_cast <int>(__pArrayStart[offset + (i * sizeof(hash))]);
871 ByteBuffer::GetTypeSize(void) const