Skip to content
Permalink
Browse files
Merge pull request #10668 from Dentomologist/convert_pointerwrap_mode…
…_to_enum_class

Convert PointerWrap::Mode to enum class
  • Loading branch information
Tilka committed May 28, 2022
2 parents 3dbc180 + c8e20c5 commit e17a4f4
Show file tree
Hide file tree
Showing 38 changed files with 126 additions and 124 deletions.
@@ -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 @@ class PointerWrap
}
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 @@ class PointerWrap
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 @@ class PointerWrap
}
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 @@ class PointerWrap
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 @@ class PointerWrap
}
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 @@ class PointerWrap
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 @@ class PointerWrap
{
bool s = flag.IsSet();
Do(s);
if (mode == MODE_READ)
if (IsReadMode())
flag.Set(s);
}

@@ -237,7 +242,7 @@ class PointerWrap
{
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 @@ class PointerWrap

Do(stable);

if (mode == MODE_READ)
if (IsReadMode())
x = stable != 0;
}

@@ -278,7 +283,7 @@ class PointerWrap
// much range
ptrdiff_t offset = x - base;
Do(offset);
if (mode == MODE_READ)
if (IsReadMode())
{
x = base + offset;
}
@@ -289,13 +294,13 @@ class PointerWrap
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 @@ class PointerWrap

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);
@@ -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>());
}

@@ -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);
}
@@ -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;
}

@@ -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;
@@ -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;
}
}
@@ -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);
@@ -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())
@@ -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;
}

@@ -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;
@@ -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);

0 comments on commit e17a4f4

Please sign in to comment.