Merge pull request #28 from libAthena/ownership-flag

Added ownership flag for memory reader
This commit is contained in:
Jack Andersen 2015-08-23 13:55:22 -10:00
commit 92898661cc
2 changed files with 21 additions and 6 deletions

View File

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

View File

@ -14,10 +14,11 @@ namespace Athena
{ {
namespace io namespace io
{ {
MemoryReader::MemoryReader(const atUint8* data, atUint64 length) MemoryReader::MemoryReader(const atUint8* data, atUint64 length, bool takeOwnership)
: m_data(data), : m_data(data),
m_length(length), m_length(length),
m_position(0) m_position(0),
m_owns(takeOwnership)
{ {
if (!data) 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) MemoryCopyReader::MemoryCopyReader(const atUint8* data, atUint64 length)
: MemoryReader(data, length) : MemoryReader(data, length, false)
{ {
if (!data) 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_data = (atUint8*)data;
m_length = length; m_length = length;
m_position = 0; m_position = 0;
m_owns = takeOwnership;
} }
void MemoryCopyReader::setData(const atUint8* data, atUint64 length) void MemoryCopyReader::setData(const atUint8* data, atUint64 length)