byteBuffer.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "core/util/byteBuffer.h"
  23. namespace Torque
  24. {
  25. class PrivateBBData
  26. {
  27. public:
  28. PrivateBBData()
  29. : refCount( 1 ),
  30. dataSize( 0 ),
  31. data( NULL )
  32. {
  33. }
  34. U32 refCount; ///< Reference count
  35. U32 dataSize; ///< Length of buffer
  36. U8 *data; ///< Our data buffer
  37. };
  38. //--------------------------------------
  39. ByteBuffer::ByteBuffer()
  40. {
  41. _data = new PrivateBBData;
  42. _data->dataSize = 0;
  43. _data->data = NULL;
  44. }
  45. ByteBuffer::ByteBuffer(U8 *dataPtr, U32 bufferSize)
  46. {
  47. _data = new PrivateBBData;
  48. _data->dataSize = bufferSize;
  49. _data->data = new U8[bufferSize];
  50. dMemcpy( _data->data, dataPtr, bufferSize );
  51. }
  52. ByteBuffer::ByteBuffer(U32 bufferSize)
  53. {
  54. _data = new PrivateBBData;
  55. _data->dataSize = bufferSize;
  56. _data->data = new U8[bufferSize];
  57. }
  58. ByteBuffer::ByteBuffer(const ByteBuffer &theBuffer)
  59. {
  60. _data = theBuffer._data;
  61. _data->refCount++;
  62. }
  63. ByteBuffer &ByteBuffer::operator=(const ByteBuffer &theBuffer)
  64. {
  65. _data = theBuffer._data;
  66. _data->refCount++;
  67. return *this;
  68. }
  69. ByteBuffer::~ByteBuffer()
  70. {
  71. if (!--_data->refCount)
  72. {
  73. delete [] _data->data;
  74. delete _data;
  75. _data = NULL;
  76. }
  77. }
  78. void ByteBuffer::setBuffer(U8 *dataPtr, U32 bufferSize, bool copyData)
  79. {
  80. U8 *newData = dataPtr;
  81. if ( copyData )
  82. {
  83. newData = new U8[bufferSize];
  84. dMemcpy( newData, dataPtr, bufferSize );
  85. }
  86. delete [] _data->data;
  87. _data->data = newData;
  88. _data->dataSize = bufferSize;
  89. }
  90. void ByteBuffer::resize(U32 newBufferSize)
  91. {
  92. U8 *newData = new U8[newBufferSize];
  93. U32 copyLen = getMin( newBufferSize, _data->dataSize );
  94. dMemcpy( newData, _data->data, copyLen );
  95. delete [] _data->data;
  96. _data->data = newData;
  97. _data->dataSize = newBufferSize;
  98. }
  99. void ByteBuffer::appendBuffer(const U8 *dataBuffer, U32 bufferSize)
  100. {
  101. U32 start = _data->dataSize;
  102. resize(start + bufferSize);
  103. dMemcpy(_data->data + start, dataBuffer, bufferSize);
  104. }
  105. U32 ByteBuffer::getBufferSize() const
  106. {
  107. return _data->dataSize;
  108. }
  109. U8 *ByteBuffer::getBuffer()
  110. {
  111. return _data->data;
  112. }
  113. const U8 *ByteBuffer::getBuffer() const
  114. {
  115. return _data->data;
  116. }
  117. ByteBuffer ByteBuffer::getCopy() const
  118. {
  119. return ByteBuffer( _data->data, _data->dataSize );
  120. }
  121. void ByteBuffer::clear()
  122. {
  123. dMemset(_data->data, 0, _data->dataSize);
  124. }
  125. }