1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2025-12-10 09:32:29 +01:00

Refactor MEMORY_ACCESS into a FlagHolder

This commit is contained in:
Gymnasiast
2025-07-02 19:41:26 +02:00
parent 8820659a3d
commit 96e5d124b9
3 changed files with 19 additions and 17 deletions

View File

@@ -739,7 +739,7 @@ namespace OpenRCT2
if (String::iequals(Path::GetExtension(path), ".sea"))
{
auto data = DecryptSea(fs::u8path(path));
auto ms = MemoryStream(data.data(), data.size(), MEMORY_ACCESS::READ);
auto ms = MemoryStream(data.data(), data.size(), MemoryAccess::read);
if (!LoadParkFromStream(&ms, path, loadTitleScreenOnFail, asScenario))
{
throw std::runtime_error(".sea file may have been renamed.");
@@ -1088,7 +1088,7 @@ namespace OpenRCT2
break;
}
auto ms = MemoryStream(data.data(), data.size(), MEMORY_ACCESS::READ);
auto ms = MemoryStream(data.data(), data.size(), MemoryAccess::read);
if (!LoadParkFromStream(&ms, gOpenRCT2StartupActionPath, true))
{
Console::Error::WriteLine("Failed to load '%s'", gOpenRCT2StartupActionPath);

View File

@@ -22,7 +22,7 @@ namespace OpenRCT2
_dataCapacity = copy._dataCapacity;
_dataSize = copy._dataSize;
if (_access & MEMORY_ACCESS::OWNER)
if (_access.has(MemoryAccess::owner))
{
_data = Memory::Allocate<uint8_t>(_dataCapacity);
std::memcpy(_data, copy._data, _dataCapacity);
@@ -43,7 +43,7 @@ namespace OpenRCT2
std::memcpy(_data, v.data(), v.size());
}
MemoryStream::MemoryStream(void* data, size_t dataSize, uint8_t access)
MemoryStream::MemoryStream(void* data, size_t dataSize, AccessFlags access)
{
_access = access;
_dataCapacity = dataSize;
@@ -66,7 +66,7 @@ namespace OpenRCT2
MemoryStream::~MemoryStream()
{
if (_access & MEMORY_ACCESS::OWNER)
if (_access.has(MemoryAccess::owner))
Memory::Free(_data);
_data = nullptr;
@@ -79,7 +79,7 @@ namespace OpenRCT2
{
if (this != &mv)
{
if (_access & MEMORY_ACCESS::OWNER)
if (_access.has(MemoryAccess::owner))
Memory::Free(_data);
_access = mv._access;
@@ -188,7 +188,7 @@ namespace OpenRCT2
{
if (_position + static_cast<size_t>(length) > _dataCapacity)
{
if (_access & MEMORY_ACCESS::OWNER)
if (_access.has(MemoryAccess::owner))
{
// resize to the larger of the requested capacity and double the current capacity, with a minimum of 8.
size_t newCapacity = std::max(_position + static_cast<size_t>(length), _dataCapacity * 2);

View File

@@ -9,6 +9,7 @@
#pragma once
#include "../core/FlagHolder.hpp"
#include "IStream.hpp"
#include <algorithm>
@@ -17,12 +18,13 @@
namespace OpenRCT2
{
namespace MEMORY_ACCESS
enum class MemoryAccess : uint8_t
{
constexpr uint8_t READ = 1 << 0;
constexpr uint8_t WRITE = 1 << 1;
constexpr uint8_t OWNER = 1 << 2;
}; // namespace MEMORY_ACCESS
read,
write,
owner,
};
using AccessFlags = FlagHolder<uint8_t, MemoryAccess>;
/**
* A stream for reading and writing to a buffer in memory. By default this buffer can grow.
@@ -30,7 +32,7 @@ namespace OpenRCT2
class MemoryStream final : public IStream
{
private:
uint8_t _access = MEMORY_ACCESS::READ | MEMORY_ACCESS::WRITE | MEMORY_ACCESS::OWNER;
AccessFlags _access = { MemoryAccess::read, MemoryAccess::write, MemoryAccess::owner };
size_t _dataCapacity = 0;
size_t _dataSize = 0;
uint8_t* _data = nullptr;
@@ -42,9 +44,9 @@ namespace OpenRCT2
MemoryStream(MemoryStream&& mv) noexcept;
explicit MemoryStream(size_t capacity);
MemoryStream(const std::vector<uint8_t>& v);
MemoryStream(void* data, size_t dataSize, uint8_t access = MEMORY_ACCESS::READ);
MemoryStream(void* data, size_t dataSize, AccessFlags access = { MemoryAccess::read });
MemoryStream(const void* data, size_t dataSize)
: MemoryStream(const_cast<void*>(data), dataSize, MEMORY_ACCESS::READ)
: MemoryStream(const_cast<void*>(data), dataSize, MemoryAccess::read)
{
}
virtual ~MemoryStream();
@@ -62,11 +64,11 @@ namespace OpenRCT2
bool CanRead() const override
{
return (_access & MEMORY_ACCESS::READ) != 0;
return _access.has(MemoryAccess::read);
}
bool CanWrite() const override
{
return (_access & MEMORY_ACCESS::WRITE) != 0;
return _access.has(MemoryAccess::write);
}
uint64_t GetLength() const override