diff --git a/src/openrct2/Context.cpp b/src/openrct2/Context.cpp index 0eb9d15942..23fb133900 100644 --- a/src/openrct2/Context.cpp +++ b/src/openrct2/Context.cpp @@ -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); diff --git a/src/openrct2/core/MemoryStream.cpp b/src/openrct2/core/MemoryStream.cpp index 527409c288..c3ad06a8e7 100644 --- a/src/openrct2/core/MemoryStream.cpp +++ b/src/openrct2/core/MemoryStream.cpp @@ -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(_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(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(length), _dataCapacity * 2); diff --git a/src/openrct2/core/MemoryStream.h b/src/openrct2/core/MemoryStream.h index 212afecf46..ee5af581b8 100644 --- a/src/openrct2/core/MemoryStream.h +++ b/src/openrct2/core/MemoryStream.h @@ -9,6 +9,7 @@ #pragma once +#include "../core/FlagHolder.hpp" #include "IStream.hpp" #include @@ -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; /** * 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& 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(data), dataSize, MEMORY_ACCESS::READ) + : MemoryStream(const_cast(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