Saveload: Use template function to implement SlAutolength

This commit is contained in:
Jonathan G Rennison 2024-07-14 18:45:17 +01:00
parent 74047028dd
commit 99c6cc5bdb
9 changed files with 34 additions and 24 deletions

View File

@ -491,7 +491,7 @@ static void Save_PLYR()
{
for (Company *c : Company::Iterate()) {
SlSetArrayIndex(c->index);
SlAutolength((AutolengthProc*)SaveLoad_PLYR, c);
SlAutolength(SaveLoad_PLYR, c);
}
}

View File

@ -336,7 +336,7 @@ static void Save_LGRP()
FilterDescs();
for (LinkGraph *lg : LinkGraph::Iterate()) {
SlSetArrayIndex(lg->index);
SlAutolength((AutolengthProc*)DoSave_LGRP, lg);
SlAutolength(DoSave_LGRP, lg);
}
}
@ -348,7 +348,7 @@ static void Save_LGRJ()
FilterDescs();
for (LinkGraphJob *lgj : LinkGraphJob::Iterate()) {
SlSetArrayIndex(lgj->index);
SlAutolength((AutolengthProc*)DoSave_LGRJ, lgj);
SlAutolength(DoSave_LGRJ, lgj);
}
}

View File

@ -364,14 +364,13 @@ static void Save_ORDL()
SetupDescs_ORDL();
for (OrderList *list : OrderList::Iterate()) {
SlSetArrayIndex(list->index);
SlAutolength([](void *data) {
OrderList *list = static_cast<OrderList *>(data);
SlAutolength([&]() {
SlObjectSaveFiltered(list, _filtered_ordl_desc);
SlWriteUint32(list->GetScheduledDispatchScheduleCount());
for (DispatchSchedule &ds : list->GetScheduledDispatchScheduleSet()) {
SaveDispatchSchedule(ds);
}
}, list);
});
}
}
@ -458,14 +457,13 @@ void Save_BKOR()
for (OrderBackup *ob : OrderBackup::Iterate()) {
SlSetArrayIndex(ob->index);
SlAutolength([](void *data) {
OrderBackup *ob = static_cast<OrderBackup *>(data);
SlAutolength([&]() {
SlObject(ob, GetOrderBackupDescription());
SlWriteUint32((uint)ob->dispatch_schedules.size());
for (DispatchSchedule &ds : ob->dispatch_schedules) {
SaveDispatchSchedule(ds);
}
}, ob);
});
}
}

View File

@ -40,7 +40,7 @@ static void Save_PLAN()
{
for (Plan *p : Plan::Iterate()) {
SlSetArrayIndex(p->index);
SlAutolength((AutolengthProc*) RealSave_PLAN, p);
SlAutolength(RealSave_PLAN, p);
}
}

View File

@ -2383,19 +2383,17 @@ void SlGlobList(const SaveLoadTable &slt)
SlObject(nullptr, slt);
}
/**
* Do something of which I have no idea what it is :P
* @param proc The callback procedure that is called
* @param arg The variable that will be used for the callback procedure
*/
void SlAutolength(AutolengthProc *proc, void *arg)
void SlAutolengthSetup()
{
assert(_sl.action == SLA_SAVE);
assert(_sl.need_length == NL_WANTLENGTH);
_sl.need_length = NL_NONE;
_sl.dumper->StartAutoLength();
proc(arg);
}
void SlAutolengthCompletion()
{
auto result = _sl.dumper->StopAutoLength();
/* Setup length */
_sl.need_length = NL_WANTLENGTH;

View File

@ -83,7 +83,6 @@ bool IsNetworkServerSave();
bool IsScenarioSave();
typedef void ChunkSaveLoadProc();
typedef void AutolengthProc(void *arg);
void SlUnreachablePlaceholder();
@ -980,12 +979,27 @@ void WriteValue(void *ptr, VarType conv, int64_t val);
void SlSetArrayIndex(uint index);
int SlIterateArray();
void SlAutolength(AutolengthProc *proc, void *arg);
size_t SlGetFieldLength();
void SlSetLength(size_t length);
size_t SlCalcObjMemberLength(const void *object, const SaveLoad &sld);
size_t SlCalcObjLength(const void *object, const SaveLoadTable &slt);
/**
* Run proc, automatically prepending the written length
* @param proc The callback procedure that is called
* @param args Any
*/
template <typename F, typename... Args>
void SlAutolength(F proc, Args... args)
{
extern void SlAutolengthSetup();
extern void SlAutolengthCompletion();
SlAutolengthSetup();
proc(std::forward<Args>(args)...);
SlAutolengthCompletion();
}
/**
* Run proc, saving result in the autolength temp buffer
* @param proc The callback procedure that is called

View File

@ -622,7 +622,7 @@ static void Save_STNN()
/* Write the stations */
for (BaseStation *st : BaseStation::Iterate()) {
SlSetArrayIndex(st->index);
SlAutolength((AutolengthProc*)RealSave_STNN, st);
SlAutolength(RealSave_STNN, st);
}
}

View File

@ -317,7 +317,7 @@ static void Save_TOWN()
SetupDescs_TOWN();
for (Town *t : Town::Iterate()) {
SlSetArrayIndex(t->index);
SlAutolength((AutolengthProc*)RealSave_Town, t);
SlAutolength(RealSave_Town, t);
}
}

View File

@ -1316,7 +1316,7 @@ void Save_VENC()
return;
}
SlAutolength([](void *) {
SlAutolength([]() {
int types[4] = {};
int total = 0;
for (Vehicle *v : Vehicle::Iterate()) {
@ -1377,7 +1377,7 @@ void Save_VENC()
SlWriteUint32(a->index);
SlWriteUint16(a->acache.cached_max_range);
}
}, nullptr);
});
}
void Load_VENC()
@ -1610,7 +1610,7 @@ void Save_VLKA()
for (Train *t : Train::Iterate()) {
if (t->lookahead != nullptr) {
SlSetArrayIndex(t->index);
SlAutolength((AutolengthProc*) RealSave_VLKA, t->lookahead.get());
SlAutolength(RealSave_VLKA, t->lookahead.get());
}
}
}