Core: Rename MemoryStream to MemoryView, add MemoryStream

Former-commit-id: c180d5f34fa7c477f35c4b70ebf7b64e3f35fe3d
This commit is contained in:
Lynix
2015-11-17 14:06:57 +01:00
parent a1bb104255
commit ddc343a630
13 changed files with 153 additions and 41 deletions

View File

@@ -161,5 +161,5 @@ namespace Nz
{
static Log log;
return &log;
}
}
}

View File

@@ -9,21 +9,22 @@
namespace Nz
{
MemoryStream::MemoryStream(const void* ptr, UInt64 size) :
Stream(OpenMode_ReadOnly),
m_ptr(reinterpret_cast<const UInt8*>(ptr)),
m_pos(0),
m_size(size)
{
}
MemoryStream::~MemoryStream()
MemoryStream::MemoryStream(const void* ptr, unsigned int size) :
Stream(OpenMode_ReadWrite),
m_pos(0)
{
m_buffer.resize(size);
std::memcpy(m_buffer.data(), ptr, size);
}
bool MemoryStream::EndOfStream() const
{
return m_pos == m_size;
return m_pos >= m_buffer.size();
}
void MemoryStream::Flush()
{
// Nothing to flush
}
UInt64 MemoryStream::GetCursorPos() const
@@ -33,25 +34,36 @@ namespace Nz
UInt64 MemoryStream::GetSize() const
{
return m_size;
return m_buffer.size();
}
std::size_t MemoryStream::Read(void* buffer, std::size_t size)
{
unsigned int readSize = std::min(static_cast<unsigned int>(size), static_cast<unsigned int>(m_size-m_pos));
std::size_t readSize = std::min<std::size_t>(size, static_cast<std::size_t>(m_buffer.size() - m_pos));
if (buffer)
std::memcpy(buffer, &m_ptr[m_pos], readSize);
std::memcpy(buffer, m_buffer.data() + m_pos, readSize);
m_pos += readSize;
return readSize;
}
bool MemoryStream::SetCursorPos(UInt64 offset)
{
m_pos = std::min(offset, m_size);
m_pos = std::min<UInt64>(offset, m_buffer.size());
return true;
}
std::size_t MemoryStream::Write(const void* buffer, std::size_t size)
{
std::size_t endPos = static_cast<std::size_t>(m_pos + size);
if (endPos > m_buffer.size())
m_buffer.resize(endPos);
std::memcpy(m_buffer.data(), buffer, size);
m_pos = endPos;
return size;
}
}

View File

@@ -0,0 +1,52 @@
// Copyright (C) 2015 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/MemoryView.hpp>
#include <algorithm>
#include <cstring>
#include <Nazara/Core/Debug.hpp>
namespace Nz
{
MemoryView::MemoryView(const void* ptr, UInt64 size) :
Stream(OpenMode_ReadOnly),
m_ptr(reinterpret_cast<const UInt8*>(ptr)),
m_pos(0),
m_size(size)
{
}
bool MemoryView::EndOfStream() const
{
return m_pos >= m_size;
}
UInt64 MemoryView::GetCursorPos() const
{
return m_pos;
}
UInt64 MemoryView::GetSize() const
{
return m_size;
}
std::size_t MemoryView::Read(void* buffer, std::size_t size)
{
std::size_t readSize = std::min<std::size_t>(size, static_cast<std::size_t>(m_size - m_pos));
if (buffer)
std::memcpy(buffer, &m_ptr[m_pos], readSize);
m_pos += readSize;
return readSize;
}
bool MemoryView::SetCursorPos(UInt64 offset)
{
m_pos = std::min(offset, m_size);
return true;
}
}