From c9a63bc72c04dc44b8d532ec48fc81f3bd1572a6 Mon Sep 17 00:00:00 2001 From: Lynix Date: Fri, 15 Jan 2016 02:24:34 +0100 Subject: [PATCH] Core: Bring back MemoryStream class (It's not perfect but necessary until a better replacement comes out) Former-commit-id: e3106b98c9825e313525298a8d46ff9c40bf5027 --- include/Nazara/Core/MemoryStream.hpp | 58 ++++++++++++++++++ include/Nazara/Core/MemoryStream.inl | 15 +++++ src/Nazara/Core/MemoryStream.cpp | 90 ++++++++++++++++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 include/Nazara/Core/MemoryStream.hpp create mode 100644 include/Nazara/Core/MemoryStream.inl create mode 100644 src/Nazara/Core/MemoryStream.cpp diff --git a/include/Nazara/Core/MemoryStream.hpp b/include/Nazara/Core/MemoryStream.hpp new file mode 100644 index 000000000..7328b7707 --- /dev/null +++ b/include/Nazara/Core/MemoryStream.hpp @@ -0,0 +1,58 @@ +// 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 + +#pragma once + +#ifndef NAZARA_MEMORYSTREAM_HPP +#define NAZARA_MEMORYSTREAM_HPP + +#include +#include +#include + +namespace Nz +{ + class NAZARA_CORE_API MemoryStream : public Stream + { + public: + MemoryStream(); + MemoryStream(const void* ptr, unsigned int size); + MemoryStream(const MemoryStream&) = default; + MemoryStream(MemoryStream&&) = default; + ~MemoryStream() = default; + + void Clear(); + + bool EndOfStream() const override; + + + const ByteArray& GetBuffer() const; + const UInt8* GetData() const; + UInt64 GetCursorPos() const override; + UInt64 GetSize() const override; + + bool SetCursorPos(UInt64 offset) override; + + MemoryStream& operator=(const MemoryStream&) = default; + MemoryStream& operator=(MemoryStream&&) = default; + + private: + void FlushStream() override; + std::size_t ReadBlock(void* buffer, std::size_t size) override; + std::size_t WriteBlock(const void* buffer, std::size_t size) override; + + ByteArray m_buffer; + UInt64 m_pos; + }; + + class AbstractHash; + + inline bool HashAppend(AbstractHash* hash, const String& string); + NAZARA_CORE_API bool Serialize(SerializationContext& context, const String& string); + NAZARA_CORE_API bool Unserialize(UnserializationContext& context, String* string); +} + +#include + +#endif // NAZARA_MEMORYSTREAM_HPP diff --git a/include/Nazara/Core/MemoryStream.inl b/include/Nazara/Core/MemoryStream.inl new file mode 100644 index 000000000..61b2f0bb4 --- /dev/null +++ b/include/Nazara/Core/MemoryStream.inl @@ -0,0 +1,15 @@ +// 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 + +namespace Nz +{ + inline bool HashAppend(AbstractHash* hash, const MemoryStream& stream) + { + return HashAppend(hash, stream.GetBuffer()); + } +} + +#include diff --git a/src/Nazara/Core/MemoryStream.cpp b/src/Nazara/Core/MemoryStream.cpp new file mode 100644 index 000000000..757225609 --- /dev/null +++ b/src/Nazara/Core/MemoryStream.cpp @@ -0,0 +1,90 @@ +// 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 +#include +#include +#include + +namespace Nz +{ + MemoryStream::MemoryStream() : + Stream(StreamOption_None, OpenMode_ReadWrite), + m_pos(0) + { + } + + MemoryStream::MemoryStream(const void* ptr, unsigned int size) : + MemoryStream() + { + m_buffer.Resize(size); + std::memcpy(m_buffer.GetBuffer(), ptr, size); + } + + void MemoryStream::Clear() + { + m_buffer.Clear(); + m_pos = 0; + } + + bool MemoryStream::EndOfStream() const + { + return m_pos >= m_buffer.size(); + } + + const ByteArray& MemoryStream::GetBuffer() const + { + return m_buffer; + } + + UInt64 MemoryStream::GetCursorPos() const + { + return m_pos; + } + + const UInt8* MemoryStream::GetData() const + { + return m_buffer.GetConstBuffer(); + } + + UInt64 MemoryStream::GetSize() const + { + return m_buffer.size(); + } + + bool MemoryStream::SetCursorPos(UInt64 offset) + { + m_pos = std::min(offset, m_buffer.size()); + + return true; + } + + void MemoryStream::FlushStream() + { + // Nothing to flush + } + + std::size_t MemoryStream::ReadBlock(void* buffer, std::size_t size) + { + std::size_t readSize = std::min(size, static_cast(m_buffer.size() - m_pos)); + + if (buffer) + std::memcpy(buffer, m_buffer.GetBuffer() + m_pos, readSize); + + m_pos += readSize; + return readSize; + } + + std::size_t MemoryStream::WriteBlock(const void* buffer, std::size_t size) + { + std::size_t endPos = static_cast(m_pos + size); + if (endPos > m_buffer.size()) + m_buffer.Resize(endPos); + + std::memcpy(m_buffer.GetBuffer(), buffer, size); + + m_pos = endPos; + return size; + } +}