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