Codechange: Use find_if to get default writeable saveload format. (#12849)

* Codechange: Use find_if to get default writeable savegame format.

This removes the last of lastof, and so the lastof macro is removed.
This commit is contained in:
Peter Nelson 2024-07-09 17:07:40 +01:00 committed by GitHub
parent 56b0eac2e9
commit 100dd7b6d1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 13 additions and 20 deletions

View File

@ -2680,14 +2680,15 @@ static const SaveLoadFormat _saveload_formats[] = {
* uncompressed, or another type
* @param full_name Name of the savegame format. If empty it picks the first available one
* @param compression_level Output for telling what compression level we want.
* @return Pointer to SaveLoadFormat struct giving all characteristics of this type of savegame
* @return Reference to SaveLoadFormat struct giving all characteristics of this type of savegame
*/
static const SaveLoadFormat *GetSavegameFormat(const std::string &full_name, uint8_t *compression_level)
static const SaveLoadFormat &GetSavegameFormat(const std::string &full_name, uint8_t *compression_level)
{
const SaveLoadFormat *def = lastof(_saveload_formats);
/* Find default savegame format, the highest one with which files can be written. */
auto it = std::find_if(std::rbegin(_saveload_formats), std::rend(_saveload_formats), [](const auto &slf) { return slf.init_write != nullptr; });
if (it == std::rend(_saveload_formats)) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "no writeable savegame formats");
/* find default savegame format, the highest one with which files can be written */
while (!def->init_write) def--;
const SaveLoadFormat &def = *it;
if (!full_name.empty()) {
/* Get the ":..." of the compression level out of the way */
@ -2711,15 +2712,15 @@ static const SaveLoadFormat *GetSavegameFormat(const std::string &full_name, uin
*compression_level = level;
}
}
return &slf;
return slf;
}
}
SetDParamStr(0, name);
SetDParamStr(1, def->name);
SetDParamStr(1, def.name);
ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_SAVEGAME_COMPRESSION_ALGORITHM, WL_CRITICAL);
}
*compression_level = def->default_compression;
*compression_level = def.default_compression;
return def;
}
@ -2805,13 +2806,13 @@ static SaveOrLoadResult SaveFileToDisk(bool threaded)
{
try {
uint8_t compression;
const SaveLoadFormat *fmt = GetSavegameFormat(_savegame_format, &compression);
const SaveLoadFormat &fmt = GetSavegameFormat(_savegame_format, &compression);
/* We have written our stuff to memory, now write it to file! */
uint32_t hdr[2] = { fmt->tag, TO_BE32(SAVEGAME_VERSION << 16) };
uint32_t hdr[2] = { fmt.tag, TO_BE32(SAVEGAME_VERSION << 16) };
_sl.sf->Write((uint8_t*)hdr, sizeof(hdr));
_sl.sf = fmt->init_write(_sl.sf, compression);
_sl.sf = fmt.init_write(_sl.sf, compression);
_sl.dumper->Flush(_sl.sf);
ClearSaveLoadState();

View File

@ -285,14 +285,6 @@ char (&ArraySizeHelper(T (&array)[N]))[N];
*/
#define lengthof(array) (sizeof(ArraySizeHelper(array)))
/**
* Get the last element of an fixed size array.
*
* @param x The pointer to the first element of the array
* @return The pointer to the last element of the array
*/
#define lastof(x) (&x[lengthof(x) - 1])
/**
* Gets the size of a variable within a class.
* @param base The class the variable is in.

View File

@ -1420,7 +1420,7 @@ public:
uint spacer_i = 0;
uint button_i = 0;
/* Index into the arrangement indices. The macro lastof cannot be used here! */
/* Index into the arrangement indices. */
const WidgetID *slotp = rtl ? &arrangement[arrangable_count - 1] : arrangement;
for (uint i = 0; i < arrangable_count; i++) {
uint slot = lookup[*slotp];