Cleaned ByteArray class
Former-commit-id: a155b789634cb1e7d875be2f4d1ba6cc896b4212
This commit is contained in:
parent
c358e5ab6d
commit
ccb3adfc1c
|
|
@ -21,14 +21,13 @@ class NAZARA_API NzByteArray : public NzHashable
|
||||||
struct SharedArray;
|
struct SharedArray;
|
||||||
|
|
||||||
NzByteArray();
|
NzByteArray();
|
||||||
NzByteArray(const nzUInt8* buffer, unsigned int length);
|
NzByteArray(const void* buffer, unsigned int size);
|
||||||
NzByteArray(const NzByteArray& buffer);
|
NzByteArray(const NzByteArray& buffer);
|
||||||
NzByteArray(NzByteArray&& buffer) noexcept;
|
NzByteArray(NzByteArray&& buffer) noexcept;
|
||||||
NzByteArray(SharedArray* sharedArray);
|
NzByteArray(SharedArray* sharedArray);
|
||||||
~NzByteArray();
|
~NzByteArray();
|
||||||
|
|
||||||
NzByteArray& Append(nzUInt8 byte);
|
NzByteArray& Append(const void* buffer, unsigned int size);
|
||||||
NzByteArray& Append(const nzUInt8* buffer, unsigned int length);
|
|
||||||
NzByteArray& Append(const NzByteArray& array);
|
NzByteArray& Append(const NzByteArray& array);
|
||||||
|
|
||||||
void Clear(bool keepBuffer = false);
|
void Clear(bool keepBuffer = false);
|
||||||
|
|
@ -38,14 +37,12 @@ class NAZARA_API NzByteArray : public NzHashable
|
||||||
const nzUInt8* GetConstBuffer() const;
|
const nzUInt8* GetConstBuffer() const;
|
||||||
unsigned int GetSize() const;
|
unsigned int GetSize() const;
|
||||||
|
|
||||||
NzByteArray& Insert(int pos, nzUInt8 byte);
|
NzByteArray& Insert(int pos, const void* buffer, unsigned int size);
|
||||||
NzByteArray& Insert(int pos, const nzUInt8* buffer, unsigned int length);
|
|
||||||
NzByteArray& Insert(int pos, const NzByteArray& array);
|
NzByteArray& Insert(int pos, const NzByteArray& array);
|
||||||
|
|
||||||
bool IsEmpty() const;
|
bool IsEmpty() const;
|
||||||
|
|
||||||
NzByteArray& Prepend(nzUInt8 byte);
|
NzByteArray& Prepend(const void* buffer, unsigned int size);
|
||||||
NzByteArray& Prepend(const nzUInt8* buffer, unsigned int length);
|
|
||||||
NzByteArray& Prepend(const NzByteArray& array);
|
NzByteArray& Prepend(const NzByteArray& array);
|
||||||
|
|
||||||
void Reserve(unsigned int bufferSize);
|
void Reserve(unsigned int bufferSize);
|
||||||
|
|
@ -83,9 +80,7 @@ class NAZARA_API NzByteArray : public NzHashable
|
||||||
NzByteArray& operator=(const NzByteArray& array);
|
NzByteArray& operator=(const NzByteArray& array);
|
||||||
NzByteArray& operator=(NzByteArray&& array) noexcept;
|
NzByteArray& operator=(NzByteArray&& array) noexcept;
|
||||||
|
|
||||||
NzByteArray operator+(nzUInt8 byte) const;
|
|
||||||
NzByteArray operator+(const NzByteArray& array) const;
|
NzByteArray operator+(const NzByteArray& array) const;
|
||||||
NzByteArray& operator+=(nzUInt8 byte);
|
|
||||||
NzByteArray& operator+=(const NzByteArray& array);
|
NzByteArray& operator+=(const NzByteArray& array);
|
||||||
|
|
||||||
static int Compare(const NzByteArray& first, const NzByteArray& second);
|
static int Compare(const NzByteArray& first, const NzByteArray& second);
|
||||||
|
|
|
||||||
|
|
@ -29,15 +29,15 @@ m_sharedArray(&emptyArray)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
NzByteArray::NzByteArray(const nzUInt8* buffer, unsigned int length)
|
NzByteArray::NzByteArray(const void* buffer, unsigned int size)
|
||||||
{
|
{
|
||||||
if (length > 0)
|
if (size > 0)
|
||||||
{
|
{
|
||||||
m_sharedArray = new SharedArray;
|
m_sharedArray = new SharedArray;
|
||||||
m_sharedArray->buffer = new nzUInt8[length];
|
m_sharedArray->buffer = new nzUInt8[size];
|
||||||
m_sharedArray->capacity = length;
|
m_sharedArray->capacity = size;
|
||||||
m_sharedArray->size = length;
|
m_sharedArray->size = size;
|
||||||
std::memcpy(m_sharedArray->buffer, buffer, length);
|
std::memcpy(m_sharedArray->buffer, buffer, size);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
m_sharedArray = &emptyArray;
|
m_sharedArray = &emptyArray;
|
||||||
|
|
@ -66,14 +66,9 @@ NzByteArray::~NzByteArray()
|
||||||
ReleaseArray();
|
ReleaseArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
NzByteArray& NzByteArray::Append(nzUInt8 byte)
|
NzByteArray& NzByteArray::Append(const void* buffer, unsigned int size)
|
||||||
{
|
{
|
||||||
return Insert(m_sharedArray->size, byte);
|
return Insert(m_sharedArray->size, buffer, size);
|
||||||
}
|
|
||||||
|
|
||||||
NzByteArray& NzByteArray::Append(const nzUInt8* buffer, unsigned int length)
|
|
||||||
{
|
|
||||||
return Insert(m_sharedArray->size, buffer, length);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
NzByteArray& NzByteArray::Append(const NzByteArray& array)
|
NzByteArray& NzByteArray::Append(const NzByteArray& array)
|
||||||
|
|
@ -114,14 +109,9 @@ unsigned int NzByteArray::GetSize() const
|
||||||
return m_sharedArray->size;
|
return m_sharedArray->size;
|
||||||
}
|
}
|
||||||
|
|
||||||
NzByteArray& NzByteArray::Insert(int pos, nzUInt8 byte)
|
NzByteArray& NzByteArray::Insert(int pos, const void* buffer, unsigned int size)
|
||||||
{
|
{
|
||||||
return Insert(pos, &byte, 1);
|
if (size == 0)
|
||||||
}
|
|
||||||
|
|
||||||
NzByteArray& NzByteArray::Insert(int pos, const nzUInt8* buffer, unsigned int length)
|
|
||||||
{
|
|
||||||
if (length == 0)
|
|
||||||
return *this;
|
return *this;
|
||||||
|
|
||||||
if (pos < 0)
|
if (pos < 0)
|
||||||
|
|
@ -130,18 +120,18 @@ NzByteArray& NzByteArray::Insert(int pos, const nzUInt8* buffer, unsigned int le
|
||||||
unsigned int start = std::min(static_cast<unsigned int>(pos), m_sharedArray->size);
|
unsigned int start = std::min(static_cast<unsigned int>(pos), m_sharedArray->size);
|
||||||
|
|
||||||
// Si le buffer est déjà suffisamment grand
|
// Si le buffer est déjà suffisamment grand
|
||||||
if (m_sharedArray->capacity >= m_sharedArray->size + length)
|
if (m_sharedArray->capacity >= m_sharedArray->size + size)
|
||||||
{
|
{
|
||||||
EnsureOwnership();
|
EnsureOwnership();
|
||||||
|
|
||||||
std::memmove(&m_sharedArray->buffer[start+length], &m_sharedArray->buffer[start], m_sharedArray->size - start);
|
std::memmove(&m_sharedArray->buffer[start+size], &m_sharedArray->buffer[start], m_sharedArray->size - start);
|
||||||
std::memcpy(&m_sharedArray->buffer[start], buffer, length);
|
std::memcpy(&m_sharedArray->buffer[start], buffer, size);
|
||||||
|
|
||||||
m_sharedArray->size += length;
|
m_sharedArray->size += size;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
unsigned int newSize = m_sharedArray->size + length;
|
unsigned int newSize = m_sharedArray->size + size;
|
||||||
nzUInt8* newBuffer = new nzUInt8[newSize];
|
nzUInt8* newBuffer = new nzUInt8[newSize];
|
||||||
|
|
||||||
nzUInt8* ptr = newBuffer;
|
nzUInt8* ptr = newBuffer;
|
||||||
|
|
@ -152,8 +142,8 @@ NzByteArray& NzByteArray::Insert(int pos, const nzUInt8* buffer, unsigned int le
|
||||||
ptr += start;
|
ptr += start;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::memcpy(ptr, buffer, length*sizeof(nzUInt8));
|
std::memcpy(ptr, buffer, size*sizeof(nzUInt8));
|
||||||
ptr += length;
|
ptr += size;
|
||||||
|
|
||||||
if (m_sharedArray->size > 0)
|
if (m_sharedArray->size > 0)
|
||||||
std::memcpy(ptr, &m_sharedArray->buffer[start], m_sharedArray->size - start);
|
std::memcpy(ptr, &m_sharedArray->buffer[start], m_sharedArray->size - start);
|
||||||
|
|
@ -180,14 +170,9 @@ bool NzByteArray::IsEmpty() const
|
||||||
return m_sharedArray->size == 0;
|
return m_sharedArray->size == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
NzByteArray& NzByteArray::Prepend(nzUInt8 byte)
|
NzByteArray& NzByteArray::Prepend(const void* buffer, unsigned int size)
|
||||||
{
|
{
|
||||||
return Insert(0, byte);
|
return Insert(0, buffer, size);
|
||||||
}
|
|
||||||
|
|
||||||
NzByteArray& NzByteArray::Prepend(const nzUInt8* buffer, unsigned int length)
|
|
||||||
{
|
|
||||||
return Insert(0, buffer, length);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
NzByteArray& NzByteArray::Prepend(const NzByteArray& array)
|
NzByteArray& NzByteArray::Prepend(const NzByteArray& array)
|
||||||
|
|
@ -383,16 +368,6 @@ const nzUInt8* NzByteArray::end() const
|
||||||
return &m_sharedArray->buffer[m_sharedArray->size];
|
return &m_sharedArray->buffer[m_sharedArray->size];
|
||||||
}
|
}
|
||||||
|
|
||||||
void NzByteArray::push_front(nzUInt8 byte)
|
|
||||||
{
|
|
||||||
Prepend(byte);
|
|
||||||
}
|
|
||||||
|
|
||||||
void NzByteArray::push_back(nzUInt8 byte)
|
|
||||||
{
|
|
||||||
Append(byte);
|
|
||||||
}
|
|
||||||
|
|
||||||
nzUInt8& NzByteArray::operator[](unsigned int pos)
|
nzUInt8& NzByteArray::operator[](unsigned int pos)
|
||||||
{
|
{
|
||||||
EnsureOwnership();
|
EnsureOwnership();
|
||||||
|
|
@ -434,19 +409,6 @@ NzByteArray& NzByteArray::operator=(NzByteArray&& array) noexcept
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
NzByteArray NzByteArray::operator+(nzUInt8 byte) const
|
|
||||||
{
|
|
||||||
if (m_sharedArray->size == 0)
|
|
||||||
return NzByteArray(&byte, 1);
|
|
||||||
|
|
||||||
unsigned int totalSize = m_sharedArray->size + 1;
|
|
||||||
nzUInt8* buffer = new nzUInt8[totalSize];
|
|
||||||
std::memcpy(buffer, m_sharedArray->buffer, m_sharedArray->size);
|
|
||||||
buffer[m_sharedArray->size] = byte;
|
|
||||||
|
|
||||||
return NzByteArray(new SharedArray(1, totalSize, totalSize, buffer));
|
|
||||||
}
|
|
||||||
|
|
||||||
NzByteArray NzByteArray::operator+(const NzByteArray& array) const
|
NzByteArray NzByteArray::operator+(const NzByteArray& array) const
|
||||||
{
|
{
|
||||||
if (array.m_sharedArray->size == 0)
|
if (array.m_sharedArray->size == 0)
|
||||||
|
|
@ -463,11 +425,6 @@ NzByteArray NzByteArray::operator+(const NzByteArray& array) const
|
||||||
return NzByteArray(new SharedArray(1, totalSize, totalSize, buffer));
|
return NzByteArray(new SharedArray(1, totalSize, totalSize, buffer));
|
||||||
}
|
}
|
||||||
|
|
||||||
NzByteArray& NzByteArray::operator+=(nzUInt8 byte)
|
|
||||||
{
|
|
||||||
return Append(byte);
|
|
||||||
}
|
|
||||||
|
|
||||||
NzByteArray& NzByteArray::operator+=(const NzByteArray& array)
|
NzByteArray& NzByteArray::operator+=(const NzByteArray& array)
|
||||||
{
|
{
|
||||||
return Append(array);
|
return Append(array);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue