Saveload: Use std::span for autosave temporary buffer view

This commit is contained in:
Jonathan G Rennison
2024-02-19 18:43:17 +00:00
parent 75dd135e8d
commit 8a85055c68
3 changed files with 11 additions and 12 deletions

View File

@@ -200,10 +200,10 @@ void MemoryDumper::StartAutoLength()
this->bufe = this->autolen_buf_end; this->bufe = this->autolen_buf_end;
} }
std::pair<byte *, size_t> MemoryDumper::StopAutoLength() std::span<byte> MemoryDumper::StopAutoLength()
{ {
assert(this->saved_buf != nullptr); assert(this->saved_buf != nullptr);
auto res = std::make_pair(this->autolen_buf, this->buf - this->autolen_buf); auto res = std::span(this->autolen_buf, this->buf - this->autolen_buf);
this->buf = this->saved_buf; this->buf = this->saved_buf;
this->bufe = this->saved_bufe; this->bufe = this->saved_bufe;
@@ -2003,8 +2003,8 @@ void SlObjectSaveFiltered(void *object, const SaveLoadTable &slt)
SlObjectIterateBase<SLA_SAVE, false>(object, slt); SlObjectIterateBase<SLA_SAVE, false>(object, slt);
auto result = _sl.dumper->StopAutoLength(); auto result = _sl.dumper->StopAutoLength();
_sl.need_length = NL_WANTLENGTH; _sl.need_length = NL_WANTLENGTH;
SlSetLength(result.second); SlSetLength(result.size());
_sl.dumper->CopyBytes(result.first, result.second); _sl.dumper->CopyBytes(result);
} else { } else {
SlObjectIterateBase<SLA_SAVE, false>(object, slt); SlObjectIterateBase<SLA_SAVE, false>(object, slt);
} }
@@ -2275,8 +2275,8 @@ void SlAutolength(AutolengthProc *proc, void *arg)
auto result = _sl.dumper->StopAutoLength(); auto result = _sl.dumper->StopAutoLength();
/* Setup length */ /* Setup length */
_sl.need_length = NL_WANTLENGTH; _sl.need_length = NL_WANTLENGTH;
SlSetLength(result.second); SlSetLength(result.size());
_sl.dumper->CopyBytes(result.first, result.second); _sl.dumper->CopyBytes(result);
} }
uint8_t SlSaveToTempBufferSetup() uint8_t SlSaveToTempBufferSetup()
@@ -2290,7 +2290,7 @@ uint8_t SlSaveToTempBufferSetup()
return (uint8_t) orig_need_length; return (uint8_t) orig_need_length;
} }
std::pair<byte *, size_t> SlSaveToTempBufferRestore(uint8_t state) std::span<byte> SlSaveToTempBufferRestore(uint8_t state)
{ {
NeedLength orig_need_length = (NeedLength)state; NeedLength orig_need_length = (NeedLength)state;
@@ -2316,7 +2316,7 @@ extern void SlConditionallySaveCompletion(const SlConditionallySaveState &state,
if (!save) _sl.dumper->buf = _sl.dumper->autolen_buf + state.current_len; if (!save) _sl.dumper->buf = _sl.dumper->autolen_buf + state.current_len;
} else { } else {
auto result = SlSaveToTempBufferRestore(state.need_length); auto result = SlSaveToTempBufferRestore(state.need_length);
if (save) _sl.dumper->CopyBytes(result.first, result.second); if (save) _sl.dumper->CopyBytes(result);
} }
} }

View File

@@ -975,12 +975,11 @@ template <typename F>
std::span<byte> SlSaveToTempBuffer(F proc) std::span<byte> SlSaveToTempBuffer(F proc)
{ {
extern uint8_t SlSaveToTempBufferSetup(); extern uint8_t SlSaveToTempBufferSetup();
extern std::pair<byte *, size_t> SlSaveToTempBufferRestore(uint8_t state); extern std::span<byte> SlSaveToTempBufferRestore(uint8_t state);
uint8_t state = SlSaveToTempBufferSetup(); uint8_t state = SlSaveToTempBufferSetup();
proc(); proc();
auto result = SlSaveToTempBufferRestore(state); return SlSaveToTempBufferRestore(state);
return std::span<byte>(result.first, result.second);
} }
/** /**

View File

@@ -268,7 +268,7 @@ struct MemoryDumper {
void Flush(SaveFilter &writer); void Flush(SaveFilter &writer);
size_t GetSize() const; size_t GetSize() const;
void StartAutoLength(); void StartAutoLength();
std::pair<byte *, size_t> StopAutoLength(); std::span<byte> StopAutoLength();
bool IsAutoLengthActive() const { return this->saved_buf != nullptr; } bool IsAutoLengthActive() const { return this->saved_buf != nullptr; }
}; };