summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatric Stout <truebrain@openttd.org>2020-12-25 19:38:18 +0100
committerGitHub <noreply@github.com>2020-12-25 19:38:18 +0100
commitf66baa444ff5575b2b40e3bfd514cdb463f6f560 (patch)
treef0977f8443106be749dac7a84fb01c2eebaedd45
parent9add62796c91c8eb7dd971cb21c8cdeba49cadfa (diff)
downloadopenttd-f66baa444ff5575b2b40e3bfd514cdb463f6f560.tar.xz
Codechange: use C++11 constructs for for-each loops (#8432)
-rw-r--r--src/ai/ai_gui.cpp25
-rw-r--r--src/ai/ai_scanner.cpp17
-rw-r--r--src/game/game_scanner.cpp13
-rw-r--r--src/script/api/script_controller.cpp12
-rw-r--r--src/script/script_config.cpp42
-rw-r--r--src/script/script_info.cpp32
-rw-r--r--src/script/script_scanner.cpp25
7 files changed, 81 insertions, 85 deletions
diff --git a/src/ai/ai_gui.cpp b/src/ai/ai_gui.cpp
index d61cfea0d..078bde4e1 100644
--- a/src/ai/ai_gui.cpp
+++ b/src/ai/ai_gui.cpp
@@ -87,8 +87,8 @@ struct AIListWindow : public Window {
if (GetConfig(slot)->HasScript()) {
ScriptInfo *info = GetConfig(slot)->GetInfo();
int i = 0;
- for (ScriptInfoList::const_iterator it = this->info_list->begin(); it != this->info_list->end(); it++, i++) {
- if ((*it).second == info) {
+ for (const auto &item : *this->info_list) {
+ if (item.second == info) {
this->selected = i;
break;
}
@@ -127,10 +127,11 @@ struct AIListWindow : public Window {
DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_LEFT, y + WD_MATRIX_TOP, this->slot == OWNER_DEITY ? STR_AI_CONFIG_NONE : STR_AI_CONFIG_RANDOM_AI, this->selected == -1 ? TC_WHITE : TC_ORANGE);
y += this->line_height;
}
- ScriptInfoList::const_iterator it = this->info_list->begin();
- for (int i = 1; it != this->info_list->end(); i++, it++) {
+ int i = 1;
+ for (const auto &item : *this->info_list) {
+ i++;
if (this->vscroll->IsVisible(i)) {
- DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_RIGHT, y + WD_MATRIX_TOP, (*it).second->GetName(), (this->selected == i - 1) ? TC_WHITE : TC_ORANGE);
+ DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_RIGHT, y + WD_MATRIX_TOP, item.second->GetName(), (this->selected == i - 1) ? TC_WHITE : TC_ORANGE);
y += this->line_height;
}
}
@@ -138,9 +139,10 @@ struct AIListWindow : public Window {
}
case WID_AIL_INFO_BG: {
AIInfo *selected_info = nullptr;
- ScriptInfoList::const_iterator it = this->info_list->begin();
- for (int i = 1; selected_info == nullptr && it != this->info_list->end(); i++, it++) {
- if (this->selected == i - 1) selected_info = static_cast<AIInfo *>((*it).second);
+ int i = 1;
+ for (const auto &item : *this->info_list) {
+ i++;
+ if (this->selected == i - 1) selected_info = static_cast<AIInfo *>(item.second);
}
/* Some info about the currently selected AI. */
if (selected_info != nullptr) {
@@ -334,11 +336,10 @@ struct AISettingsWindow : public Window {
{
visible_settings.clear();
- ScriptConfigItemList::const_iterator it = this->ai_config->GetConfigList()->begin();
- for (; it != this->ai_config->GetConfigList()->end(); it++) {
- bool no_hide = (it->flags & SCRIPTCONFIG_DEVELOPER) == 0;
+ for (const auto &item : *this->ai_config->GetConfigList()) {
+ bool no_hide = (item.flags & SCRIPTCONFIG_DEVELOPER) == 0;
if (no_hide || _settings_client.gui.ai_developer_tools) {
- visible_settings.push_back(&(*it));
+ visible_settings.push_back(&item);
}
}
diff --git a/src/ai/ai_scanner.cpp b/src/ai/ai_scanner.cpp
index aeb5b50fa..1431a2820 100644
--- a/src/ai/ai_scanner.cpp
+++ b/src/ai/ai_scanner.cpp
@@ -61,8 +61,8 @@ void AIScannerInfo::RegisterAPI(class Squirrel *engine)
AIInfo *AIScannerInfo::SelectRandomAI() const
{
uint num_random_ais = 0;
- for (ScriptInfoList::const_iterator it = this->info_single_list.begin(); it != this->info_single_list.end(); it++) {
- AIInfo *i = static_cast<AIInfo *>((*it).second);
+ for (const auto &item : info_single_list) {
+ AIInfo *i = static_cast<AIInfo *>(item.second);
if (i->UseAsRandomAI()) num_random_ais++;
}
@@ -121,11 +121,10 @@ AIInfo *AIScannerInfo::FindInfo(const char *nameParam, int versionParam, bool fo
/* See if there is a compatible AI which goes by that name, with the highest
* version which allows loading the requested version */
- ScriptInfoList::iterator it = this->info_list.begin();
- for (; it != this->info_list.end(); it++) {
- AIInfo *i = static_cast<AIInfo *>((*it).second);
+ for (const auto &item : this->info_list) {
+ AIInfo *i = static_cast<AIInfo *>(item.second);
if (strcasecmp(ai_name, i->GetName()) == 0 && i->CanLoadFromVersion(versionParam) && (version == -1 || i->GetVersion() > version)) {
- version = (*it).second->GetVersion();
+ version = item.second->GetVersion();
info = i;
}
}
@@ -158,8 +157,8 @@ AILibrary *AIScannerLibrary::FindLibrary(const char *library, int version)
strtolower(library_name);
/* Check if the library + version exists */
- ScriptInfoList::iterator iter = this->info_list.find(library_name);
- if (iter == this->info_list.end()) return nullptr;
+ ScriptInfoList::iterator it = this->info_list.find(library_name);
+ if (it == this->info_list.end()) return nullptr;
- return static_cast<AILibrary *>((*iter).second);
+ return static_cast<AILibrary *>((*it).second);
}
diff --git a/src/game/game_scanner.cpp b/src/game/game_scanner.cpp
index 6bcba4455..1935b7813 100644
--- a/src/game/game_scanner.cpp
+++ b/src/game/game_scanner.cpp
@@ -60,11 +60,10 @@ GameInfo *GameScannerInfo::FindInfo(const char *nameParam, int versionParam, boo
/* See if there is a compatible Game script which goes by that name, with the highest
* version which allows loading the requested version */
- ScriptInfoList::iterator it = this->info_list.begin();
- for (; it != this->info_list.end(); it++) {
- GameInfo *i = static_cast<GameInfo *>((*it).second);
+ for (const auto &item : this->info_list) {
+ GameInfo *i = static_cast<GameInfo *>(item.second);
if (strcasecmp(game_name, i->GetName()) == 0 && i->CanLoadFromVersion(versionParam) && (version == -1 || i->GetVersion() > version)) {
- version = (*it).second->GetVersion();
+ version = item.second->GetVersion();
info = i;
}
}
@@ -97,8 +96,8 @@ GameLibrary *GameScannerLibrary::FindLibrary(const char *library, int version)
strtolower(library_name);
/* Check if the library + version exists */
- ScriptInfoList::iterator iter = this->info_list.find(library_name);
- if (iter == this->info_list.end()) return nullptr;
+ ScriptInfoList::iterator it = this->info_list.find(library_name);
+ if (it == this->info_list.end()) return nullptr;
- return static_cast<GameLibrary *>((*iter).second);
+ return static_cast<GameLibrary *>((*it).second);
}
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;
}