Add ToSpan for MemoryWriteStream and simplify code with it

This commit is contained in:
Wojtek Figat
2025-01-16 17:35:28 +01:00
parent 39419787fa
commit 6111f67e33
30 changed files with 59 additions and 51 deletions

View File

@@ -1,6 +1,7 @@
// Copyright (c) 2012-2024 Wojciech Figat. All rights reserved.
#include "MemoryReadStream.h"
#include "Engine/Platform/Platform.h"
MemoryReadStream::MemoryReadStream()
: _buffer(nullptr)
@@ -22,6 +23,14 @@ void MemoryReadStream::Init(const byte* bytes, uint32 length)
_length = length;
}
void* MemoryReadStream::Move(uint32 bytes)
{
ASSERT(GetLength() - GetPosition() >= bytes);
const auto result = (void*)_position;
_position += bytes;
return result;
}
void MemoryReadStream::Flush()
{
}

View File

@@ -3,7 +3,6 @@
#pragma once
#include "ReadStream.h"
#include "Engine/Platform/Platform.h"
/// <summary>
/// Direct memory reading stream that uses a single allocation buffer.
@@ -47,7 +46,7 @@ public:
/// <param name="data">Span with data to read from.</param>
template<typename T>
MemoryReadStream(const Span<T>& data)
: MemoryReadStream(data.Get(), data.Count() * sizeof(T))
: MemoryReadStream(data.Get(), data.Length() * sizeof(T))
{
}
@@ -83,13 +82,7 @@ public:
/// </summary>
/// <param name="bytes">The amount of bytes to read.</param>
/// <returns>The pointer to the data in memory.</returns>
void* Move(uint32 bytes)
{
ASSERT(GetLength() - GetPosition() >= bytes);
const auto result = (void*)_position;
_position += bytes;
return result;
}
void* Move(uint32 bytes);
/// <summary>
/// Skips the data from the target buffer without reading from it. Moves the read pointer in the buffer forward.

View File

@@ -3,6 +3,7 @@
#pragma once
#include "WriteStream.h"
#include "Engine/Core/Types/Span.h"
/// <summary>
/// Direct memory writing stream that uses a single allocation buffer.
@@ -115,3 +116,8 @@ public:
void SetPosition(uint32 seek) override;
void WriteBytes(const void* data, uint32 bytes) override;
};
inline Span<byte> ToSpan(MemoryWriteStream& stream)
{
return Span<byte>(stream.GetHandle(), (int32)stream.GetPosition());
}