Added ownership flag for memory reader

This commit is contained in:
Jack Andersen 2015-08-23 13:52:01 -10:00
parent b664c7df8f
commit a145f337c0
2 changed files with 21 additions and 6 deletions

View File

@ -21,12 +21,15 @@ namespace io
class MemoryReader : public IStreamReader
{
public:
virtual ~MemoryReader();
/*! \brief This constructor references an existing buffer to read from.
*
* \param data The existing buffer
* \param length The length of the existing buffer
* \param takeOwnership Memory will be freed with the reader if set
*/
MemoryReader(const atUint8* data, atUint64 length);
MemoryReader(const atUint8* data, atUint64 length, bool takeOwnership=false);
/*! \brief Sets the buffers position relative to the specified position.<br />
* It seeks relative to the current position by default.
@ -57,9 +60,10 @@ public:
* as Stream now owns the address, this is done to keep memory usage down.
* \param data The new buffer.
* \param length The length of the new buffer.
* \param takeOwnership Memory will be freed with the reader if set
* \throw IOException
*/
void setData(const atUint8* data, atUint64 length);
void setData(const atUint8* data, atUint64 length, bool takeOwnership=false);
/*! \brief Returns a copy of the current buffer.<br />
@ -82,6 +86,7 @@ protected:
const atUint8* m_data;
atUint64 m_length;
atUint64 m_position;
bool m_owns;
};
class MemoryCopyReader : public MemoryReader

View File

@ -14,10 +14,11 @@ namespace Athena
{
namespace io
{
MemoryReader::MemoryReader(const atUint8* data, atUint64 length)
MemoryReader::MemoryReader(const atUint8* data, atUint64 length, bool takeOwnership)
: m_data(data),
m_length(length),
m_position(0)
m_position(0),
m_owns(takeOwnership)
{
if (!data)
{
@ -34,8 +35,14 @@ MemoryReader::MemoryReader(const atUint8* data, atUint64 length)
}
}
MemoryReader::~MemoryReader()
{
if (m_owns)
delete[] m_data;
}
MemoryCopyReader::MemoryCopyReader(const atUint8* data, atUint64 length)
: MemoryReader(data, length)
: MemoryReader(data, length, false)
{
if (!data)
{
@ -95,11 +102,14 @@ void MemoryReader::seek(atInt64 position, SeekOrigin origin)
}
}
void MemoryReader::setData(const atUint8* data, atUint64 length)
void MemoryReader::setData(const atUint8* data, atUint64 length, bool takeOwnership)
{
if (m_owns && m_data)
delete[] m_data;
m_data = (atUint8*)data;
m_length = length;
m_position = 0;
m_owns = takeOwnership;
}
void MemoryCopyReader::setData(const atUint8* data, atUint64 length)