fix byte patch class being only usable in a single unit (#542)

This commit is contained in:
Quentin E. / iDeath 2022-10-30 19:14:19 +01:00 committed by GitHub
parent 0bd683afca
commit 3112ad4cfe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 28 deletions

View File

@ -0,0 +1,38 @@
#include "byte_patch.hpp"
namespace memory
{
byte_patch::~byte_patch()
{
restore();
}
void byte_patch::apply() const
{
memcpy(m_address, m_value.get(), m_size);
}
void byte_patch::restore() const
{
memcpy(m_address, m_original_bytes.get(), m_size);
}
void byte_patch::remove() const
{
if (const auto it = std::find(m_patches.begin(), m_patches.end(), this); it != m_patches.end())
{
m_patches.erase(it);
}
}
void byte_patch::restore_all()
{
m_patches.clear();
}
bool operator==(const std::unique_ptr<byte_patch>& a, const byte_patch* b)
{
return a->m_address == b->m_address;
}
}

View File

@ -5,28 +5,13 @@ namespace memory
class byte_patch
{
public:
virtual ~byte_patch()
{
restore();
}
virtual ~byte_patch();
void apply() const
{
memcpy(m_address, m_value.get(), m_size);
}
void apply() const;
void restore() const
{
memcpy(m_address, m_original_bytes.get(), m_size);
}
void restore() const;
void remove() const
{
if (const auto it = std::find(m_patches.begin(), m_patches.end(), this); it != m_patches.end())
{
m_patches.erase(it);
}
}
void remove() const;
template <typename TAddr>
static const std::unique_ptr<byte_patch>& make(TAddr address, std::remove_pointer_t<std::remove_reference_t<TAddr>> value)
@ -35,10 +20,7 @@ namespace memory
std::unique_ptr<byte_patch>(new byte_patch(address, value)));
}
static void restore_all()
{
m_patches.clear();
}
static void restore_all();
private:
template <typename TAddr>
@ -64,9 +46,4 @@ namespace memory
friend bool operator== (const std::unique_ptr<byte_patch>& a, const byte_patch* b);
};
bool operator== (const std::unique_ptr<byte_patch>& a, const byte_patch* b)
{
return a->m_address == b->m_address;
}
}