Core: Add ByteArrayPool and PoolByteStream classes

This commit is contained in:
Lynix
2020-02-04 11:42:05 +01:00
parent 518b8697de
commit e35caebdcf
7 changed files with 207 additions and 3 deletions

View File

@@ -68,7 +68,7 @@ namespace Nz
void ByteStream::SetStream(ByteArray* byteArray, OpenModeFlags openMode)
{
std::unique_ptr<Stream> stream(new MemoryStream(byteArray, openMode));
std::unique_ptr<MemoryStream> stream = std::make_unique<MemoryStream>(byteArray, openMode);
SetStream(stream.get());
// SetStream reset our smart pointer, set it after calling it
@@ -86,7 +86,7 @@ namespace Nz
void ByteStream::SetStream(void* ptr, Nz::UInt64 size)
{
std::unique_ptr<Stream> stream(new MemoryView(ptr, size));
std::unique_ptr<MemoryView> stream = std::make_unique<MemoryView>(ptr, size);
SetStream(stream.get());
// SetStream reset our smart pointer, set it after calling it
@@ -104,7 +104,7 @@ namespace Nz
void ByteStream::SetStream(const void* ptr, Nz::UInt64 size)
{
std::unique_ptr<Stream> stream(new MemoryView(ptr, size));
std::unique_ptr<MemoryView> stream = std::make_unique<MemoryView>(ptr, size);
SetStream(stream.get());
// SetStream reset our smart pointer, set it after calling it

View File

@@ -0,0 +1,40 @@
// Copyright (C) 2017 Jérôme Leclercq
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/PoolByteStream.hpp>
#include <Nazara/Core/ByteArrayPool.hpp>
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
/*!
* \ingroup core
* \class Nz::PoolByteStream
* \brief ByteStream allocated using a pool
*/
void PoolByteStream::Reset()
{
if (m_buffer.GetCapacity() > 0)
{
m_pool.ReturnByteArray(std::move(m_buffer));
m_buffer.Clear(false);
}
SetStream(static_cast<Nz::Stream*>(nullptr));
}
void PoolByteStream::Reset(std::size_t capacity)
{
if (m_buffer.GetCapacity() < capacity)
m_buffer = m_pool.GetByteArray(capacity);
SetStream(&m_buffer, Nz::OpenMode_ReadWrite);
}
void PoolByteStream::OnEmptyStream()
{
Reset(0);
}
}