From f66baa444ff5575b2b40e3bfd514cdb463f6f560 Mon Sep 17 00:00:00 2001 From: Patric Stout Date: Fri, 25 Dec 2020 19:38:18 +0100 Subject: Codechange: use C++11 constructs for for-each loops (#8432) --- src/script/api/script_controller.cpp | 12 +++++------ src/script/script_config.cpp | 42 ++++++++++++++++++------------------ src/script/script_info.cpp | 32 +++++++++++++-------------- src/script/script_scanner.cpp | 25 ++++++++++----------- 4 files changed, 54 insertions(+), 57 deletions(-) (limited to 'src/script') diff --git a/src/script/api/script_controller.cpp b/src/script/api/script_controller.cpp index adfcf2acd..e74f27e4a 100644 --- a/src/script/api/script_controller.cpp +++ b/src/script/api/script_controller.cpp @@ -77,9 +77,9 @@ ScriptController::ScriptController(CompanyID company) : ScriptController::~ScriptController() { - for (LoadedLibraryList::iterator iter = this->loaded_library.begin(); iter != this->loaded_library.end(); iter++) { - free((*iter).second); - free((*iter).first); + for (const auto &item : this->loaded_library) { + free(item.second); + free(item.first); } this->loaded_library.clear(); @@ -129,9 +129,9 @@ ScriptController::~ScriptController() char fake_class[1024]; - LoadedLibraryList::iterator iter = controller->loaded_library.find(library_name); - if (iter != controller->loaded_library.end()) { - strecpy(fake_class, (*iter).second, lastof(fake_class)); + LoadedLibraryList::iterator it = controller->loaded_library.find(library_name); + if (it != controller->loaded_library.end()) { + strecpy(fake_class, (*it).second, lastof(fake_class)); } else { int next_number = ++controller->loaded_library_count; diff --git a/src/script/script_config.cpp b/src/script/script_config.cpp index 4ebd04916..8de141e66 100644 --- a/src/script/script_config.cpp +++ b/src/script/script_config.cpp @@ -32,9 +32,9 @@ void ScriptConfig::Change(const char *name, int version, bool force_exact_match, if (_game_mode == GM_NORMAL && this->info != nullptr) { /* If we're in an existing game and the Script is changed, set all settings * for the Script that have the random flag to a random value. */ - for (ScriptConfigItemList::const_iterator it = this->info->GetConfigList()->begin(); it != this->info->GetConfigList()->end(); it++) { - if ((*it).flags & SCRIPTCONFIG_RANDOM) { - this->SetSetting((*it).name, InteractiveRandomRange((*it).max_value + 1 - (*it).min_value) + (*it).min_value); + for (const auto &item : *this->info->GetConfigList()) { + if (item.flags & SCRIPTCONFIG_RANDOM) { + this->SetSetting(item.name, InteractiveRandomRange(item.max_value + 1 - item.min_value) + item.min_value); } } this->AddRandomDeviation(); @@ -49,8 +49,8 @@ ScriptConfig::ScriptConfig(const ScriptConfig *config) this->config_list = nullptr; this->is_random = config->is_random; - for (SettingValueList::const_iterator it = config->settings.begin(); it != config->settings.end(); it++) { - this->settings[stredup((*it).first)] = (*it).second; + for (const auto &item : config->settings) { + this->settings[stredup(item.first)] = item.second; } this->AddRandomDeviation(); } @@ -79,24 +79,24 @@ const ScriptConfigItemList *ScriptConfig::GetConfigList() void ScriptConfig::ClearConfigList() { - for (SettingValueList::iterator it = this->settings.begin(); it != this->settings.end(); it++) { - free((*it).first); + for (const auto &item : this->settings) { + free(item.first); } this->settings.clear(); } void ScriptConfig::AnchorUnchangeableSettings() { - for (ScriptConfigItemList::const_iterator it = this->GetConfigList()->begin(); it != this->GetConfigList()->end(); it++) { - if (((*it).flags & SCRIPTCONFIG_INGAME) == 0) { - this->SetSetting((*it).name, this->GetSetting((*it).name)); + for (const auto &item : *this->GetConfigList()) { + if ((item.flags & SCRIPTCONFIG_INGAME) == 0) { + this->SetSetting(item.name, this->GetSetting(item.name)); } } } int ScriptConfig::GetSetting(const char *name) const { - SettingValueList::const_iterator it = this->settings.find(name); + const auto it = this->settings.find(name); if (it == this->settings.end()) return this->info->GetSettingDefaultValue(name); return (*it).second; } @@ -111,7 +111,7 @@ void ScriptConfig::SetSetting(const char *name, int value) value = Clamp(value, config_item->min_value, config_item->max_value); - SettingValueList::iterator it = this->settings.find(name); + const auto it = this->settings.find(name); if (it != this->settings.end()) { (*it).second = value; } else { @@ -121,17 +121,17 @@ void ScriptConfig::SetSetting(const char *name, int value) void ScriptConfig::ResetSettings() { - for (SettingValueList::iterator it = this->settings.begin(); it != this->settings.end(); it++) { - free((*it).first); + for (const auto &item : this->settings) { + free(item.first); } this->settings.clear(); } void ScriptConfig::AddRandomDeviation() { - for (ScriptConfigItemList::const_iterator it = this->GetConfigList()->begin(); it != this->GetConfigList()->end(); it++) { - if ((*it).random_deviation != 0) { - this->SetSetting((*it).name, InteractiveRandomRange((*it).random_deviation * 2 + 1) - (*it).random_deviation + this->GetSetting((*it).name)); + for (const auto &item : *this->GetConfigList()) { + if (item.random_deviation != 0) { + this->SetSetting(item.name, InteractiveRandomRange(item.random_deviation * 2 + 1) - item.random_deviation + this->GetSetting(item.name)); } } } @@ -186,16 +186,16 @@ void ScriptConfig::SettingsToString(char *string, const char *last) const { char *s = string; *s = '\0'; - for (SettingValueList::const_iterator it = this->settings.begin(); it != this->settings.end(); it++) { + for (const auto &item : this->settings) { char no[10]; - seprintf(no, lastof(no), "%d", (*it).second); + seprintf(no, lastof(no), "%d", item.second); /* Check if the string would fit in the destination */ - size_t needed_size = strlen((*it).first) + 1 + strlen(no); + size_t needed_size = strlen(item.first) + 1 + strlen(no); /* If it doesn't fit, skip the next settings */ if (string + needed_size > last) break; - s = strecat(s, (*it).first, last); + s = strecat(s, item.first, last); s = strecat(s, "=", last); s = strecat(s, no, last); s = strecat(s, ",", last); diff --git a/src/script/script_info.cpp b/src/script/script_info.cpp index 53a8fb30e..4eebd548d 100644 --- a/src/script/script_info.cpp +++ b/src/script/script_info.cpp @@ -20,14 +20,14 @@ ScriptInfo::~ScriptInfo() { /* Free all allocated strings */ - for (ScriptConfigItemList::iterator it = this->config_list.begin(); it != this->config_list.end(); it++) { - free((*it).name); - free((*it).description); - if (it->labels != nullptr) { - for (auto &lbl_map : *(*it).labels) { + for (const auto &item : this->config_list) { + free(item.name); + free(item.description); + if (item.labels != nullptr) { + for (auto &lbl_map : *item.labels) { free(lbl_map.second); } - delete it->labels; + delete item.labels; } } this->config_list.clear(); @@ -232,8 +232,8 @@ SQInteger ScriptInfo::AddLabels(HSQUIRRELVM vm) ValidateString(setting_name); ScriptConfigItem *config = nullptr; - for (ScriptConfigItemList::iterator it = this->config_list.begin(); it != this->config_list.end(); it++) { - if (strcmp((*it).name, setting_name) == 0) config = &(*it); + for (auto &item : this->config_list) { + if (strcmp(item.name, setting_name) == 0) config = &item; } if (config == nullptr) { @@ -284,22 +284,22 @@ const ScriptConfigItemList *ScriptInfo::GetConfigList() const const ScriptConfigItem *ScriptInfo::GetConfigItem(const char *name) const { - for (ScriptConfigItemList::const_iterator it = this->config_list.begin(); it != this->config_list.end(); it++) { - if (strcmp((*it).name, name) == 0) return &(*it); + for (const auto &item : this->config_list) { + if (strcmp(item.name, name) == 0) return &item; } return nullptr; } int ScriptInfo::GetSettingDefaultValue(const char *name) const { - for (ScriptConfigItemList::const_iterator it = this->config_list.begin(); it != this->config_list.end(); it++) { - if (strcmp((*it).name, name) != 0) continue; + for (const auto &item : this->config_list) { + if (strcmp(item.name, name) != 0) continue; /* The default value depends on the difficulty level */ switch (GetGameSettings().script.settings_profile) { - case SP_EASY: return (*it).easy_value; - case SP_MEDIUM: return (*it).medium_value; - case SP_HARD: return (*it).hard_value; - case SP_CUSTOM: return (*it).custom_value; + case SP_EASY: return item.easy_value; + case SP_MEDIUM: return item.medium_value; + case SP_HARD: return item.hard_value; + case SP_CUSTOM: return item.custom_value; default: NOT_REACHED(); } } diff --git a/src/script/script_scanner.cpp b/src/script/script_scanner.cpp index e33f233f7..dc3feb58c 100644 --- a/src/script/script_scanner.cpp +++ b/src/script/script_scanner.cpp @@ -103,14 +103,12 @@ void ScriptScanner::RescanDir() void ScriptScanner::Reset() { - ScriptInfoList::iterator it = this->info_list.begin(); - for (; it != this->info_list.end(); it++) { - free((*it).first); - delete (*it).second; + for (const auto &item : this->info_list) { + free(item.first); + delete item.second; } - it = this->info_single_list.begin(); - for (; it != this->info_single_list.end(); it++) { - free((*it).first); + for (const auto &item : this->info_single_list) { + free(item.first); } this->info_list.clear(); @@ -171,9 +169,8 @@ char *ScriptScanner::GetConsoleList(char *p, const char *last, bool newest_only) { p += seprintf(p, last, "List of %s:\n", this->GetScannerName()); const ScriptInfoList &list = newest_only ? this->info_single_list : this->info_list; - ScriptInfoList::const_iterator it = list.begin(); - for (; it != list.end(); it++) { - ScriptInfo *i = (*it).second; + for (const auto &item : list) { + ScriptInfo *i = item.second; p += seprintf(p, last, "%10s (v%d): %s\n", i->GetName(), i->GetVersion(), i->GetDescription()); } p += seprintf(p, last, "\n"); @@ -273,16 +270,16 @@ static bool IsSameScript(const ContentInfo *ci, bool md5sum, ScriptInfo *info, S bool ScriptScanner::HasScript(const ContentInfo *ci, bool md5sum) { - for (ScriptInfoList::iterator it = this->info_list.begin(); it != this->info_list.end(); it++) { - if (IsSameScript(ci, md5sum, (*it).second, this->GetDirectory())) return true; + for (const auto &item : this->info_list) { + if (IsSameScript(ci, md5sum, item.second, this->GetDirectory())) return true; } return false; } const char *ScriptScanner::FindMainScript(const ContentInfo *ci, bool md5sum) { - for (ScriptInfoList::iterator it = this->info_list.begin(); it != this->info_list.end(); it++) { - if (IsSameScript(ci, md5sum, (*it).second, this->GetDirectory())) return (*it).second->GetMainScript(); + for (const auto &item : this->info_list) { + if (IsSameScript(ci, md5sum, item.second, this->GetDirectory())) return item.second->GetMainScript(); } return nullptr; } -- cgit v1.2.3-70-g09d2