mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2026-01-20 21:43:06 +01:00
Refactor memory stream
Clean up a few things.
This commit is contained in:
@@ -24,7 +24,7 @@ MemoryStream::MemoryStream(const MemoryStream ©)
|
||||
_dataCapacity = copy._dataCapacity;
|
||||
_dataSize = copy._dataSize;
|
||||
|
||||
if (_access == MEMORY_ACCESS_OWNER)
|
||||
if (_access == MEMORY_ACCESS::OWNER)
|
||||
{
|
||||
_data = Memory::Duplicate(copy._data, _dataCapacity);
|
||||
_position = (void*)((uintptr_t)_data + copy.GetPosition());
|
||||
@@ -38,7 +38,7 @@ MemoryStream::MemoryStream(size_t capacity)
|
||||
_position = _data;
|
||||
}
|
||||
|
||||
MemoryStream::MemoryStream(void * data, size_t dataSize, uint32 access)
|
||||
MemoryStream::MemoryStream(void * data, size_t dataSize, uint8 access)
|
||||
{
|
||||
_access = access;
|
||||
_dataCapacity = dataSize;
|
||||
@@ -48,13 +48,13 @@ MemoryStream::MemoryStream(void * data, size_t dataSize, uint32 access)
|
||||
}
|
||||
|
||||
MemoryStream::MemoryStream(const void * data, size_t dataSize)
|
||||
: MemoryStream((void *)data, dataSize, MEMORY_ACCESS_READ)
|
||||
: MemoryStream((void *)data, dataSize, MEMORY_ACCESS::READ)
|
||||
{
|
||||
}
|
||||
|
||||
MemoryStream::~MemoryStream()
|
||||
{
|
||||
if (_access & MEMORY_ACCESS_OWNER)
|
||||
if (_access & MEMORY_ACCESS::OWNER)
|
||||
{
|
||||
Memory::Free(_data);
|
||||
}
|
||||
@@ -70,18 +70,18 @@ void * MemoryStream::GetData() const
|
||||
|
||||
void * MemoryStream::TakeData()
|
||||
{
|
||||
_access &= ~MEMORY_ACCESS_OWNER;
|
||||
_access &= ~MEMORY_ACCESS::OWNER;
|
||||
return _data;
|
||||
}
|
||||
|
||||
bool MemoryStream::CanRead() const
|
||||
{
|
||||
return (_access & MEMORY_ACCESS_READ) != 0;
|
||||
return (_access & MEMORY_ACCESS::READ) != 0;
|
||||
}
|
||||
|
||||
bool MemoryStream::CanWrite() const
|
||||
{
|
||||
return (_access & MEMORY_ACCESS_WRITE) != 0;
|
||||
return (_access & MEMORY_ACCESS::WRITE) != 0;
|
||||
}
|
||||
|
||||
uint64 MemoryStream::GetLength() const
|
||||
@@ -148,7 +148,7 @@ void MemoryStream::Write(const void * buffer, uint64 length)
|
||||
uint64 nextPosition = position + length;
|
||||
if (nextPosition > _dataCapacity)
|
||||
{
|
||||
if (_access & MEMORY_ACCESS_OWNER)
|
||||
if (_access & MEMORY_ACCESS::OWNER)
|
||||
{
|
||||
EnsureCapacity((size_t)nextPosition);
|
||||
}
|
||||
|
||||
@@ -19,30 +19,30 @@
|
||||
#include "../common.h"
|
||||
#include "IStream.hpp"
|
||||
|
||||
enum MEMORY_ACCESS
|
||||
namespace MEMORY_ACCESS
|
||||
{
|
||||
MEMORY_ACCESS_READ = 1 << 0,
|
||||
MEMORY_ACCESS_WRITE = 1 << 1,
|
||||
MEMORY_ACCESS_OWNER = 1 << 2,
|
||||
constexpr uint8 READ = 1 << 0;
|
||||
constexpr uint8 WRITE = 1 << 1;
|
||||
constexpr uint8 OWNER = 1 << 2;
|
||||
};
|
||||
|
||||
/**
|
||||
* A stream for reading and writing to files. Wraps an SDL_RWops, SDL2's cross platform file stream.
|
||||
* A stream for reading and writing to a buffer in memory. By default this buffer can grow.
|
||||
*/
|
||||
class MemoryStream final : public IStream
|
||||
{
|
||||
private:
|
||||
uint16 _access = MEMORY_ACCESS_READ | MEMORY_ACCESS_WRITE | MEMORY_ACCESS_OWNER;
|
||||
uint8 _access = MEMORY_ACCESS::READ | MEMORY_ACCESS::WRITE | MEMORY_ACCESS::OWNER;
|
||||
size_t _dataCapacity = 0;
|
||||
size_t _dataSize = 0;
|
||||
void * _data = nullptr;
|
||||
void * _position = nullptr;
|
||||
|
||||
public:
|
||||
MemoryStream();
|
||||
MemoryStream() = default;
|
||||
MemoryStream(const MemoryStream & copy);
|
||||
explicit MemoryStream(size_t capacity);
|
||||
MemoryStream(void * data, size_t dataSize, uint32 access = MEMORY_ACCESS_READ);
|
||||
MemoryStream(void * data, size_t dataSize, uint8 access = MEMORY_ACCESS::READ);
|
||||
MemoryStream(const void * data, size_t dataSize);
|
||||
virtual ~MemoryStream();
|
||||
|
||||
|
||||
@@ -107,7 +107,7 @@ public:
|
||||
void * data = GetFileData(path, &dataSize);
|
||||
if (data != nullptr)
|
||||
{
|
||||
stream = new MemoryStream(data, dataSize, MEMORY_ACCESS_READ | MEMORY_ACCESS_OWNER);
|
||||
stream = new MemoryStream(data, dataSize, MEMORY_ACCESS::READ | MEMORY_ACCESS::OWNER);
|
||||
}
|
||||
return stream;
|
||||
}
|
||||
|
||||
@@ -122,7 +122,7 @@ namespace ObjectFactory
|
||||
else
|
||||
{
|
||||
buffer = Memory::Reallocate(buffer, bufferSize);
|
||||
return new MemoryStream(buffer, bufferSize, MEMORY_ACCESS_READ | MEMORY_ACCESS_OWNER);
|
||||
return new MemoryStream(buffer, bufferSize, MEMORY_ACCESS::READ | MEMORY_ACCESS::OWNER);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -350,7 +350,7 @@ private:
|
||||
// Buffer the rest of file into memory to speed up item reading
|
||||
size_t dataSize = (size_t)(fs.GetLength() - fs.GetPosition());
|
||||
void * data = fs.ReadArray<uint8>(dataSize);
|
||||
auto ms = MemoryStream(data, dataSize, MEMORY_ACCESS_READ | MEMORY_ACCESS_OWNER);
|
||||
auto ms = MemoryStream(data, dataSize, MEMORY_ACCESS::READ | MEMORY_ACCESS::OWNER);
|
||||
|
||||
// Read items
|
||||
for (uint32 i = 0; i < header.NumItems; i++)
|
||||
|
||||
Reference in New Issue
Block a user