byteBuffer.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 "io/byteBuffer.h"
  23. #include "math/mMathFn.h"
  24. namespace Torque
  25. {
  26. class PrivateBBData
  27. {
  28. public:
  29. PrivateBBData()
  30. : refCount( 1 ),
  31. dataSize( 0 ),
  32. data( NULL )
  33. {
  34. }
  35. U32 refCount; ///< Reference count
  36. U32 dataSize; ///< Length of buffer
  37. U8 *data; ///< Our data buffer
  38. };
  39. //--------------------------------------
  40. ByteBuffer::ByteBuffer()
  41. {
  42. _data = new PrivateBBData;
  43. _data->dataSize = 0;
  44. _data->data = NULL;
  45. }
  46. ByteBuffer::ByteBuffer(U8 *dataPtr, U32 bufferSize)
  47. {
  48. _data = new PrivateBBData;
  49. _data->dataSize = bufferSize;
  50. _data->data = new U8[bufferSize];
  51. dMemcpy( _data->data, dataPtr, bufferSize );
  52. }
  53. ByteBuffer::ByteBuffer(U32 bufferSize)
  54. {
  55. _data = new PrivateBBData;
  56. _data->dataSize = bufferSize;
  57. _data->data = new U8[bufferSize];
  58. }
  59. ByteBuffer::ByteBuffer(const ByteBuffer &theBuffer)
  60. {
  61. _data = theBuffer._data;
  62. _data->refCount++;
  63. }
  64. ByteBuffer &ByteBuffer::operator=(const ByteBuffer &theBuffer)
  65. {
  66. _data = theBuffer._data;
  67. _data->refCount++;
  68. return *this;
  69. }
  70. ByteBuffer::~ByteBuffer()
  71. {
  72. if (!--_data->refCount)
  73. {
  74. delete [] _data->data;
  75. delete _data;
  76. _data = NULL;
  77. }
  78. }
  79. void ByteBuffer::setBuffer(U8 *dataPtr, U32 bufferSize, bool copyData)
  80. {
  81. U8 *newData = dataPtr;
  82. if ( copyData )
  83. {
  84. newData = new U8[bufferSize];
  85. dMemcpy( newData, dataPtr, bufferSize );
  86. }
  87. delete [] _data->data;
  88. _data->data = newData;
  89. _data->dataSize = bufferSize;
  90. }
  91. void ByteBuffer::resize(U32 newBufferSize)
  92. {
  93. U8 *newData = new U8[newBufferSize];
  94. U32 copyLen = getMin( newBufferSize, _data->dataSize );
  95. dMemcpy( newData, _data->data, copyLen );
  96. delete [] _data->data;
  97. _data->data = newData;
  98. _data->dataSize = newBufferSize;
  99. }
  100. void ByteBuffer::appendBuffer(const U8 *dataBuffer, U32 bufferSize)
  101. {
  102. U32 start = _data->dataSize;
  103. resize(start + bufferSize);
  104. dMemcpy(_data->data + start, dataBuffer, bufferSize);
  105. }
  106. U32 ByteBuffer::getBufferSize() const
  107. {
  108. return _data->dataSize;
  109. }
  110. U8 *ByteBuffer::getBuffer()
  111. {
  112. return _data->data;
  113. }
  114. const U8 *ByteBuffer::getBuffer() const
  115. {
  116. return _data->data;
  117. }
  118. ByteBuffer ByteBuffer::getCopy() const
  119. {
  120. return ByteBuffer( _data->data, _data->dataSize );
  121. }
  122. void ByteBuffer::clear()
  123. {
  124. dMemset(_data->data, 0, _data->dataSize);
  125. }
  126. }