Codechange: Add VectorSaveLoadHandler to simplify handlers for vectors. (#13093)

This reduces the duplication needed for each saved complex vector.
This commit is contained in:
Peter Nelson
2024-11-17 23:46:32 +00:00
committed by GitHub
parent a6c526cfa0
commit d903806e59
4 changed files with 65 additions and 95 deletions
+6 -35
View File
@@ -201,7 +201,7 @@ static void SwapPackets(GoodsEntry *ge)
}
template <typename T>
class SlStationSpecList : public DefaultSaveLoadHandler<SlStationSpecList<T>, BaseStation> {
class SlStationSpecList : public VectorSaveLoadHandler<SlStationSpecList<T>, BaseStation, SpecMapping<T>> {
public:
inline static const SaveLoad description[] = {
SLE_CONDVAR(SpecMapping<T>, grfid, SLE_UINT32, SLV_27, SL_MAX_VERSION),
@@ -212,25 +212,11 @@ public:
static inline uint8_t last_num_specs; ///< Number of specs of the last loaded station.
void Save(BaseStation *bst) const override
{
auto &speclist = GetStationSpecList<T>(bst);
SlSetStructListLength(speclist.size());
for (auto &sm : speclist) {
SlObject(&sm, this->GetDescription());
}
}
std::vector<SpecMapping<T>> &GetVector(BaseStation *bst) const override { return GetStationSpecList<T>(bst); }
void Load(BaseStation *bst) const override
size_t GetLength() const override
{
size_t num_specs = IsSavegameVersionBefore(SLV_SAVELOAD_LIST_LENGTH) ? last_num_specs : SlGetStructListLength(UINT8_MAX);
auto &speclist = GetStationSpecList<T>(bst);
speclist.reserve(num_specs);
for (size_t index = 0; index < num_specs; ++index) {
auto &sm = speclist.emplace_back();
SlObject(&sm, this->GetLoadDescription());
}
return IsSavegameVersionBefore(SLV_SAVELOAD_LIST_LENGTH) ? last_num_specs : SlGetStructListLength(UINT8_MAX);
}
};
@@ -518,7 +504,7 @@ struct STNSChunkHandler : ChunkHandler {
}
};
class SlRoadStopTileData : public DefaultSaveLoadHandler<SlRoadStopTileData, BaseStation> {
class SlRoadStopTileData : public VectorSaveLoadHandler<SlRoadStopTileData, BaseStation, RoadStopTileData> {
public:
inline static const SaveLoad description[] = {
SLE_VAR(RoadStopTileData, tile, SLE_UINT32),
@@ -527,22 +513,7 @@ public:
};
inline const static SaveLoadCompatTable compat_description = {};
void Save(BaseStation *bst) const override
{
SlSetStructListLength(bst->custom_roadstop_tile_data.size());
for (uint i = 0; i < bst->custom_roadstop_tile_data.size(); i++) {
SlObject(&bst->custom_roadstop_tile_data[i], this->GetDescription());
}
}
void Load(BaseStation *bst) const override
{
uint32_t num_tiles = (uint32_t)SlGetStructListLength(UINT32_MAX);
bst->custom_roadstop_tile_data.resize(num_tiles);
for (uint i = 0; i < num_tiles; i++) {
SlObject(&bst->custom_roadstop_tile_data[i], this->GetLoadDescription());
}
}
std::vector<RoadStopTileData> &GetVector(BaseStation *bst) const override { return bst->custom_roadstop_tile_data; }
};
/**