Merge pull request #10668 from Dentomologist/convert_pointerwrap_mode_to_enum_class

Convert PointerWrap::Mode to enum class
This commit is contained in:
Tilka
2022-05-29 00:57:43 +01:00
committed by GitHub
38 changed files with 126 additions and 124 deletions
+45 -40
View File
@@ -38,36 +38,41 @@
class PointerWrap
{
public:
enum Mode
enum class Mode
{
MODE_READ = 1, // load
MODE_WRITE, // save
MODE_MEASURE, // calculate size
MODE_VERIFY, // compare
Read,
Write,
Measure,
Verify,
};
private:
u8** m_ptr_current;
u8* m_ptr_end;
Mode mode;
Mode m_mode;
public:
PointerWrap(u8** ptr, size_t size, Mode mode_)
: m_ptr_current(ptr), m_ptr_end(*ptr + size), mode(mode_)
PointerWrap(u8** ptr, size_t size, Mode mode)
: m_ptr_current(ptr), m_ptr_end(*ptr + size), m_mode(mode)
{
}
void SetMode(Mode mode_) { mode = mode_; }
Mode GetMode() const { return mode; }
void SetMeasureMode() { m_mode = Mode::Measure; }
void SetVerifyMode() { m_mode = Mode::Verify; }
bool IsReadMode() const { return m_mode == Mode::Read; }
bool IsWriteMode() const { return m_mode == Mode::Write; }
bool IsMeasureMode() const { return m_mode == Mode::Measure; }
bool IsVerifyMode() const { return m_mode == Mode::Verify; }
template <typename K, class V>
void Do(std::map<K, V>& x)
{
u32 count = (u32)x.size();
Do(count);
switch (mode)
switch (m_mode)
{
case MODE_READ:
case Mode::Read:
for (x.clear(); count != 0; --count)
{
std::pair<K, V> pair;
@@ -77,9 +82,9 @@ public:
}
break;
case MODE_WRITE:
case MODE_MEASURE:
case MODE_VERIFY:
case Mode::Write:
case Mode::Measure:
case Mode::Verify:
for (auto& elem : x)
{
Do(elem.first);
@@ -95,9 +100,9 @@ public:
u32 count = (u32)x.size();
Do(count);
switch (mode)
switch (m_mode)
{
case MODE_READ:
case Mode::Read:
for (x.clear(); count != 0; --count)
{
V value;
@@ -106,9 +111,9 @@ public:
}
break;
case MODE_WRITE:
case MODE_MEASURE:
case MODE_VERIFY:
case Mode::Write:
case Mode::Measure:
case Mode::Verify:
for (const V& val : x)
{
Do(val);
@@ -154,9 +159,9 @@ public:
bool present = x.has_value();
Do(present);
switch (mode)
switch (m_mode)
{
case MODE_READ:
case Mode::Read:
if (present)
{
x = std::make_optional<T>();
@@ -168,9 +173,9 @@ public:
}
break;
case MODE_WRITE:
case MODE_MEASURE:
case MODE_VERIFY:
case Mode::Write:
case Mode::Measure:
case Mode::Verify:
if (present)
Do(x.value());
@@ -216,10 +221,10 @@ public:
Do(count);
u8* current = *m_ptr_current;
*m_ptr_current += count;
if (mode != MODE_MEASURE && *m_ptr_current > m_ptr_end)
if (!IsMeasureMode() && *m_ptr_current > m_ptr_end)
{
// trying to read/write past the end of the buffer, prevent this
mode = MODE_MEASURE;
SetMeasureMode();
}
return current;
}
@@ -228,7 +233,7 @@ public:
{
bool s = flag.IsSet();
Do(s);
if (mode == MODE_READ)
if (IsReadMode())
flag.Set(s);
}
@@ -237,7 +242,7 @@ public:
{
T temp = atomic.load(std::memory_order_relaxed);
Do(temp);
if (mode == MODE_READ)
if (IsReadMode())
atomic.store(temp, std::memory_order_relaxed);
}
@@ -267,7 +272,7 @@ public:
Do(stable);
if (mode == MODE_READ)
if (IsReadMode())
x = stable != 0;
}
@@ -278,7 +283,7 @@ public:
// much range
ptrdiff_t offset = x - base;
Do(offset);
if (mode == MODE_READ)
if (IsReadMode())
{
x = base + offset;
}
@@ -289,13 +294,13 @@ public:
u32 cookie = arbitraryNumber;
Do(cookie);
if (mode == PointerWrap::MODE_READ && cookie != arbitraryNumber)
if (IsReadMode() && cookie != arbitraryNumber)
{
PanicAlertFmtT(
"Error: After \"{0}\", found {1} ({2:#x}) instead of save marker {3} ({4:#x}). Aborting "
"savestate load...",
prevName, cookie, cookie, arbitraryNumber, arbitraryNumber);
mode = PointerWrap::MODE_MEASURE;
SetMeasureMode();
}
}
@@ -330,26 +335,26 @@ private:
DOLPHIN_FORCE_INLINE void DoVoid(void* data, u32 size)
{
if (mode != MODE_MEASURE && (*m_ptr_current + size) > m_ptr_end)
if (!IsMeasureMode() && (*m_ptr_current + size) > m_ptr_end)
{
// trying to read/write past the end of the buffer, prevent this
mode = MODE_MEASURE;
SetMeasureMode();
}
switch (mode)
switch (m_mode)
{
case MODE_READ:
case Mode::Read:
memcpy(data, *m_ptr_current, size);
break;
case MODE_WRITE:
case Mode::Write:
memcpy(*m_ptr_current, data, size);
break;
case MODE_MEASURE:
case Mode::Measure:
break;
case MODE_VERIFY:
case Mode::Verify:
DEBUG_ASSERT_MSG(COMMON, !memcmp(data, *m_ptr_current, size),
"Savestate verification failure: buf {} != {} (size {}).\n", fmt::ptr(data),
fmt::ptr(*m_ptr_current), size);
+3 -3
View File
@@ -192,11 +192,11 @@ void DoState(PointerWrap& p)
// order (or at all) every time.
// so, we savestate the event's type's name, and derive ev.type from that when loading.
std::string name;
if (pw.GetMode() != PointerWrap::MODE_READ)
if (!pw.IsReadMode())
name = *ev.type->name;
pw.Do(name);
if (pw.GetMode() == PointerWrap::MODE_READ)
if (pw.IsReadMode())
{
auto itr = s_event_types.find(name);
if (itr != s_event_types.end())
@@ -217,7 +217,7 @@ void DoState(PointerWrap& p)
// When loading from a save state, we must assume the Event order is random and meaningless.
// The exact layout of the heap in memory is implementation defined, therefore it is platform
// and library version specific.
if (p.GetMode() == PointerWrap::MODE_READ)
if (p.IsReadMode())
std::make_heap(s_event_queue.begin(), s_event_queue.end(), std::greater<Event>());
}
+1 -1
View File
@@ -398,7 +398,7 @@ void SDSP::DoState(PointerWrap& p)
Common::WriteProtectMemory(iram, DSP_IRAM_BYTE_SIZE, false);
// TODO: This uses the wrong endianness (producing bad disassembly)
// and a bogus byte count (producing bad hashes)
if (p.GetMode() == PointerWrap::MODE_READ)
if (p.IsReadMode())
Host::CodeLoaded(m_dsp_core, reinterpret_cast<const u8*>(iram), DSP_IRAM_BYTE_SIZE);
p.DoArray(dram, DSP_DRAM_SIZE);
}
+2 -2
View File
@@ -95,11 +95,11 @@ void DSPHLE::DoState(PointerWrap& p)
{
bool is_hle = true;
p.Do(is_hle);
if (!is_hle && p.GetMode() == PointerWrap::MODE_READ)
if (!is_hle && p.IsReadMode())
{
Core::DisplayMessage("State is incompatible with current DSP engine. Aborting load state.",
3000);
p.SetMode(PointerWrap::MODE_VERIFY);
p.SetVerifyMode();
return;
}
+1 -1
View File
@@ -91,7 +91,7 @@ void CMailHandler::Halt(bool _Halt)
void CMailHandler::DoState(PointerWrap& p)
{
if (p.GetMode() == PointerWrap::MODE_READ)
if (p.IsReadMode())
{
Clear();
int sz = 0;
+2 -3
View File
@@ -747,15 +747,14 @@ void AXUCode::DoAXState(PointerWrap& p)
auto old_checksum = m_coeffs_checksum;
p.Do(m_coeffs_checksum);
if (p.GetMode() == PointerWrap::MODE_READ && m_coeffs_checksum &&
old_checksum != m_coeffs_checksum)
if (p.IsReadMode() && m_coeffs_checksum && old_checksum != m_coeffs_checksum)
{
if (!LoadResamplingCoefficients(true, *m_coeffs_checksum))
{
Core::DisplayMessage("Could not find the DSP polyphase resampling coefficients used by the "
"savestate. Aborting load state.",
3000);
p.SetMode(PointerWrap::MODE_VERIFY);
p.SetVerifyMode();
return;
}
}
+2 -2
View File
@@ -42,11 +42,11 @@ void DSPLLE::DoState(PointerWrap& p)
{
bool is_hle = false;
p.Do(is_hle);
if (is_hle && p.GetMode() == PointerWrap::MODE_READ)
if (is_hle && p.IsReadMode())
{
Core::DisplayMessage("State is incompatible with current DSP engine. Aborting load state.",
3000);
p.SetMode(PointerWrap::MODE_VERIFY);
p.SetVerifyMode();
return;
}
m_dsp_core.DoState(p);
+6 -7
View File
@@ -651,7 +651,7 @@ void Core::DoState(PointerWrap& p)
{
::Core::DisplayMessage(fmt::format("GBA{} core not started. Aborting.", m_device_number + 1),
3000);
p.SetMode(PointerWrap::MODE_VERIFY);
p.SetVerifyMode();
return;
}
@@ -662,14 +662,13 @@ void Core::DoState(PointerWrap& p)
auto old_title = m_game_title;
p.Do(m_game_title);
if (p.GetMode() == PointerWrap::MODE_READ &&
(has_rom != !m_rom_path.empty() ||
(has_rom && (old_hash != m_rom_hash || old_title != m_game_title))))
if (p.IsReadMode() && (has_rom != !m_rom_path.empty() ||
(has_rom && (old_hash != m_rom_hash || old_title != m_game_title))))
{
::Core::DisplayMessage(
fmt::format("Incompatible ROM state in GBA{}. Aborting load state.", m_device_number + 1),
3000);
p.SetMode(PointerWrap::MODE_VERIFY);
p.SetVerifyMode();
return;
}
@@ -684,14 +683,14 @@ void Core::DoState(PointerWrap& p)
std::vector<u8> core_state;
core_state.resize(m_core->stateSize(m_core));
if (p.GetMode() == PointerWrap::MODE_WRITE || p.GetMode() == PointerWrap::MODE_VERIFY)
if (p.IsWriteMode() || p.IsVerifyMode())
{
m_core->saveState(m_core, core_state.data());
}
p.Do(core_state);
if (p.GetMode() == PointerWrap::MODE_READ && m_core->stateSize(m_core) == core_state.size())
if (p.IsReadMode() && m_core->stateSize(m_core) == core_state.size())
{
m_core->loadState(m_core, core_state.data());
if (auto host = m_host.lock())
+1 -1
View File
@@ -428,7 +428,7 @@ void DoState(PointerWrap& p)
Core::DisplayMessage("State is incompatible with current memory settings (MMU and/or memory "
"overrides). Aborting load state.",
3000);
p.SetMode(PointerWrap::MODE_VERIFY);
p.SetVerifyMode();
return;
}
+1 -1
View File
@@ -226,7 +226,7 @@ void DoState(PointerWrap& p)
static_cast<WiimoteEmu::Wiimote*>(s_config.GetController(i))->DoState(p);
}
if (p.GetMode() == PointerWrap::MODE_READ)
if (p.IsReadMode())
{
// If using a real wiimote or the save-state source does not match the current source,
// then force a reconnection on load.
@@ -559,7 +559,7 @@ void Wiimote::DoState(PointerWrap& p)
m_speaker_logic.DoState(p);
m_camera_logic.DoState(p);
if (p.GetMode() == PointerWrap::MODE_READ)
if (p.IsReadMode())
m_camera_logic.SetEnabled(m_status.ir);
p.Do(m_is_motion_plus_attached);
@@ -136,7 +136,7 @@ void EncryptedExtension::DoState(PointerWrap& p)
{
p.Do(m_reg);
if (p.GetMode() == PointerWrap::MODE_READ)
if (p.IsReadMode())
{
// No need to sync the key when we can just regenerate it.
m_is_key_dirty = true;
+1 -1
View File
@@ -260,7 +260,7 @@ void HostFileSystem::DoState(PointerWrap& p)
// handle /tmp
std::string Path = BuildFilename("/tmp").host_path;
if (p.GetMode() == PointerWrap::MODE_READ)
if (p.IsReadMode())
{
File::DeleteDirRecursively(Path);
File::CreateDir(Path);
+1 -1
View File
@@ -807,7 +807,7 @@ void Kernel::DoState(PointerWrap& p)
for (const auto& entry : m_device_map)
entry.second->DoState(p);
if (p.GetMode() == PointerWrap::MODE_READ)
if (p.IsReadMode())
{
for (u32 i = 0; i < IPC_MAX_FDS; i++)
{
+1 -2
View File
@@ -994,8 +994,7 @@ void WiiSockMan::Convert(sockaddr_in const& from, WiiSockAddrIn& to, s32 addrlen
void WiiSockMan::DoState(PointerWrap& p)
{
bool saving = p.GetMode() == PointerWrap::Mode::MODE_WRITE ||
p.GetMode() == PointerWrap::Mode::MODE_MEASURE;
bool saving = p.IsWriteMode() || p.IsMeasureMode();
auto size = pending_polls.size();
p.Do(size);
if (!saving)
+1 -1
View File
@@ -57,7 +57,7 @@ void SDIOSlot0Device::RefreshConfig()
void SDIOSlot0Device::DoState(PointerWrap& p)
{
DoStateShared(p);
if (p.GetMode() == PointerWrap::MODE_READ)
if (p.IsReadMode())
{
OpenInternal();
}
+2 -2
View File
@@ -90,10 +90,10 @@ void BluetoothEmuDevice::DoState(PointerWrap& p)
{
bool passthrough_bluetooth = false;
p.Do(passthrough_bluetooth);
if (passthrough_bluetooth && p.GetMode() == PointerWrap::MODE_READ)
if (passthrough_bluetooth && p.IsReadMode())
{
Core::DisplayMessage("State needs Bluetooth passthrough to be enabled. Aborting load.", 4000);
p.SetMode(PointerWrap::MODE_VERIFY);
p.SetVerifyMode();
return;
}
@@ -268,28 +268,28 @@ void BluetoothRealDevice::DoState(PointerWrap& p)
{
bool passthrough_bluetooth = true;
p.Do(passthrough_bluetooth);
if (!passthrough_bluetooth && p.GetMode() == PointerWrap::MODE_READ)
if (!passthrough_bluetooth && p.IsReadMode())
{
Core::DisplayMessage("State needs Bluetooth passthrough to be disabled. Aborting load.", 4000);
p.SetMode(PointerWrap::MODE_VERIFY);
p.SetVerifyMode();
return;
}
// Prevent the transfer callbacks from messing with m_current_transfers after we have started
// writing a savestate. We cannot use a scoped lock here because DoState is called twice and
// we would lose the lock between the two calls.
if (p.GetMode() == PointerWrap::MODE_MEASURE || p.GetMode() == PointerWrap::MODE_VERIFY)
if (p.IsMeasureMode() || p.IsVerifyMode())
m_transfers_mutex.lock();
std::vector<u32> addresses_to_discard;
if (p.GetMode() != PointerWrap::MODE_READ)
if (!p.IsReadMode())
{
// Save addresses of transfer commands to discard on savestate load.
for (const auto& transfer : m_current_transfers)
addresses_to_discard.push_back(transfer.second.command->ios_request.address);
}
p.Do(addresses_to_discard);
if (p.GetMode() == PointerWrap::MODE_READ)
if (p.IsReadMode())
{
// On load, discard any pending transfer to make sure the emulated software is not stuck
// waiting for the previous request to complete. This is usually not an issue as long as
@@ -305,7 +305,7 @@ void BluetoothRealDevice::DoState(PointerWrap& p)
OSD::Duration::NORMAL);
}
if (!s_has_shown_savestate_warning && p.GetMode() == PointerWrap::MODE_WRITE)
if (!s_has_shown_savestate_warning && p.IsWriteMode())
{
OSD::AddMessage("Savestates may not work with Bluetooth passthrough in all cases.\n"
"They will only work if no remote is connected when restoring the state,\n"
@@ -315,7 +315,7 @@ void BluetoothRealDevice::DoState(PointerWrap& p)
}
// We have finished the savestate now, so the transfers mutex can be unlocked.
if (p.GetMode() == PointerWrap::MODE_WRITE)
if (p.IsWriteMode())
m_transfers_mutex.unlock();
}
@@ -19,6 +19,6 @@ std::optional<IPCReply> BluetoothStubDevice::Open(const OpenRequest& request)
void BluetoothStubDevice::DoState(PointerWrap& p)
{
Core::DisplayMessage("The current IPC_HLE_Device_usb is a stub. Aborting load.", 4000);
p.SetMode(PointerWrap::MODE_VERIFY);
p.SetVerifyMode();
}
} // namespace IOS::HLE
+1 -1
View File
@@ -55,7 +55,7 @@ void USBHost::UpdateWantDeterminism(const bool new_want_determinism)
void USBHost::DoState(PointerWrap& p)
{
if (IsOpened() && p.GetMode() == PointerWrap::MODE_READ)
if (IsOpened() && p.IsReadMode())
{
// After a state has loaded, there may be insertion hooks for devices that were
// already plugged in, and which need to be triggered.

Some files were not shown because too many files have changed in this diff Show More