Introduce try_read/try_write for emulator_object

This commit is contained in:
66hh
2025-12-28 19:40:53 +08:00
parent f95081cb6e
commit c3464dd5c3

View File

@@ -104,6 +104,16 @@ class emulator_object
return this->address_ != 0;
}
std::optional<T> try_read(const size_t index = 0) const
{
T obj{};
if (this->memory_->try_read_memory(this->address_ + index * this->size(), &obj, sizeof(obj)))
{
return obj;
}
return std::nullopt;
}
T read(const size_t index = 0) const
{
T obj{};
@@ -111,6 +121,11 @@ class emulator_object
return obj;
}
bool try_write(const T& value, const size_t index = 0) const
{
return this->memory_->try_write_memory(this->address_ + index * this->size(), &value, sizeof(value));
}
void write(const T& value, const size_t index = 0) const
{
this->memory_->write_memory(this->address_ + index * this->size(), &value, sizeof(value));