From 8bd06807e4299f867563fefaf0b0e13073a4a357 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Tue, 10 Oct 2023 19:25:59 +0100 Subject: [PATCH] Codechange: Pass initializer list instead of null-terminated list of group types. --- src/ini.cpp | 4 ++-- src/ini_load.cpp | 24 +++++------------------- src/ini_type.h | 10 ++++++---- src/settings.cpp | 3 +-- src/settingsgen/settingsgen.cpp | 10 +++++----- 5 files changed, 19 insertions(+), 32 deletions(-) diff --git a/src/ini.cpp b/src/ini.cpp index b1eb776502..a0e0009f46 100644 --- a/src/ini.cpp +++ b/src/ini.cpp @@ -32,9 +32,9 @@ /** * Create a new ini file with given group names. - * @param list_group_names A \c nullptr terminated list with group names that should be loaded as lists instead of variables. @see IGT_LIST + * @param list_group_names A list with group names that should be loaded as lists instead of variables. @see IGT_LIST */ -IniFile::IniFile(const char * const *list_group_names) : IniLoadFile(list_group_names) +IniFile::IniFile(const IniGroupNameList &list_group_names) : IniLoadFile(list_group_names) { } diff --git a/src/ini_load.cpp b/src/ini_load.cpp index 305529b32b..10a265e5a0 100644 --- a/src/ini_load.cpp +++ b/src/ini_load.cpp @@ -56,22 +56,8 @@ IniGroup::IniGroup(IniLoadFile *parent, const std::string &name) : next(nullptr) *parent->last_group = this; parent->last_group = &this->next; - if (parent->list_group_names != nullptr) { - for (uint i = 0; parent->list_group_names[i] != nullptr; i++) { - if (this->name == parent->list_group_names[i]) { - this->type = IGT_LIST; - return; - } - } - } - if (parent->seq_group_names != nullptr) { - for (uint i = 0; parent->seq_group_names[i] != nullptr; i++) { - if (this->name == parent->seq_group_names[i]) { - this->type = IGT_SEQUENCE; - return; - } - } - } + if (std::find(parent->list_group_names.begin(), parent->list_group_names.end(), name) != parent->list_group_names.end()) this->type = IGT_LIST; + if (std::find(parent->seq_group_names.begin(), parent->seq_group_names.end(), name) != parent->seq_group_names.end()) this->type = IGT_SEQUENCE; } /** Free everything we loaded. */ @@ -156,10 +142,10 @@ void IniGroup::Clear() /** * Construct a new in-memory Ini file representation. - * @param list_group_names A \c nullptr terminated list with group names that should be loaded as lists instead of variables. @see IGT_LIST - * @param seq_group_names A \c nullptr terminated list with group names that should be loaded as lists of names. @see IGT_SEQUENCE + * @param list_group_names A list with group names that should be loaded as lists instead of variables. @see IGT_LIST + * @param seq_group_names A list with group names that should be loaded as lists of names. @see IGT_SEQUENCE */ -IniLoadFile::IniLoadFile(const char * const *list_group_names, const char * const *seq_group_names) : +IniLoadFile::IniLoadFile(const IniGroupNameList &list_group_names, const IniGroupNameList &seq_group_names) : group(nullptr), list_group_names(list_group_names), seq_group_names(seq_group_names) diff --git a/src/ini_type.h b/src/ini_type.h index 56852b256c..cdafeafee8 100644 --- a/src/ini_type.h +++ b/src/ini_type.h @@ -53,13 +53,15 @@ struct IniGroup { /** Ini file that only supports loading. */ struct IniLoadFile { + using IniGroupNameList = std::initializer_list; + IniGroup *group; ///< the first group in the ini IniGroup **last_group; ///< the last group in the ini std::string comment; ///< last comment in file - const char * const *list_group_names; ///< nullptr terminated list with group names that are lists - const char * const *seq_group_names; ///< nullptr terminated list with group names that are sequences. + const IniGroupNameList list_group_names; ///< list of group names that are lists + const IniGroupNameList seq_group_names; ///< list of group names that are sequences. - IniLoadFile(const char * const *list_group_names = nullptr, const char * const *seq_group_names = nullptr); + IniLoadFile(const IniGroupNameList &list_group_names = {}, const IniGroupNameList &seq_group_names = {}); virtual ~IniLoadFile(); IniGroup *GetGroup(const std::string &name) const; @@ -89,7 +91,7 @@ struct IniLoadFile { /** Ini file that supports both loading and saving. */ struct IniFile : IniLoadFile { - IniFile(const char * const *list_group_names = nullptr); + IniFile(const IniGroupNameList &list_group_names = {}); bool SaveToDisk(const std::string &filename); diff --git a/src/settings.cpp b/src/settings.cpp index 563791b72b..9e8e38228c 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -131,12 +131,11 @@ static bool IsSignedVarMemType(VarType vt) */ class ConfigIniFile : public IniFile { private: - inline static const char * const list_group_names[] = { + inline static const IniGroupNameList list_group_names = { "bans", "newgrf", "servers", "server_bind_addresses", - nullptr, }; public: diff --git a/src/settingsgen/settingsgen.cpp b/src/settingsgen/settingsgen.cpp index e25f2ded8f..84e48a2c54 100644 --- a/src/settingsgen/settingsgen.cpp +++ b/src/settingsgen/settingsgen.cpp @@ -151,10 +151,10 @@ private: struct SettingsIniFile : IniLoadFile { /** * Construct a new ini loader. - * @param list_group_names A \c nullptr terminated list with group names that should be loaded as lists instead of variables. @see IGT_LIST - * @param seq_group_names A \c nullptr terminated list with group names that should be loaded as lists of names. @see IGT_SEQUENCE + * @param list_group_names A list with group names that should be loaded as lists instead of variables. @see IGT_LIST + * @param seq_group_names A list with group names that should be loaded as lists of names. @see IGT_SEQUENCE */ - SettingsIniFile(const char * const *list_group_names = nullptr, const char * const *seq_group_names = nullptr) : + SettingsIniFile(const IniGroupNameList &list_group_names = {}, const IniGroupNameList &seq_group_names = {}) : IniLoadFile(list_group_names, seq_group_names) { } @@ -416,9 +416,9 @@ static const OptionData _opts[] = { */ static void ProcessIniFile(const char *fname) { - static const char * const seq_groups[] = {PREAMBLE_GROUP_NAME, POSTAMBLE_GROUP_NAME, nullptr}; + static const IniLoadFile::IniGroupNameList seq_groups = {PREAMBLE_GROUP_NAME, POSTAMBLE_GROUP_NAME}; - SettingsIniFile ini{nullptr, seq_groups}; + SettingsIniFile ini{{}, seq_groups}; ini.LoadFromDisk(fname, NO_DIRECTORY); DumpGroup(ini, PREAMBLE_GROUP_NAME);