From 1f6b3a37f9c39adb22eb61d3153b0c62bfa20551 Mon Sep 17 00:00:00 2001 From: glx Date: Sun, 15 Dec 2019 22:45:18 +0100 Subject: [PATCH] Codechange: Replace FOR_ALL_ENGINES with range-based for loops --- src/autoreplace_gui.cpp | 3 +-- src/build_vehicle_gui.cpp | 12 ++++-------- src/company_gui.cpp | 5 ++--- src/depot_gui.cpp | 6 ++---- src/elrail.cpp | 3 +-- src/engine.cpp | 22 +++++++--------------- src/engine_base.h | 16 +++++++++++----- src/newgrf.cpp | 16 +++++----------- src/newgrf_engine.cpp | 3 +-- src/rail.cpp | 6 ++---- src/road.cpp | 12 ++++-------- src/saveload/afterload.cpp | 6 ++---- src/saveload/engine_sl.cpp | 6 ++---- src/saveload/vehicle_sl.cpp | 9 ++++----- src/script/api/script_enginelist.cpp | 3 +-- src/vehicle.cpp | 3 +-- 16 files changed, 50 insertions(+), 81 deletions(-) diff --git a/src/autoreplace_gui.cpp b/src/autoreplace_gui.cpp index 3999597a0f..19df0f1d80 100644 --- a/src/autoreplace_gui.cpp +++ b/src/autoreplace_gui.cpp @@ -124,8 +124,7 @@ class ReplaceVehicleWindow : public Window { GUIEngineList *list = &this->engines[side]; list->clear(); - const Engine *e; - FOR_ALL_ENGINES_OF_TYPE(e, type) { + for (const Engine *e : Engine::IterateType(type)) { if (!draw_left && !this->show_hidden_engines && e->IsHidden(_local_company)) continue; EngineID eid = e->index; switch (type) { diff --git a/src/build_vehicle_gui.cpp b/src/build_vehicle_gui.cpp index a1290532af..c457b0f195 100644 --- a/src/build_vehicle_gui.cpp +++ b/src/build_vehicle_gui.cpp @@ -1260,8 +1260,7 @@ struct BuildVehicleWindow : Window { * Also check to see if the previously selected engine is still available, * and if not, reset selection to INVALID_ENGINE. This could be the case * when engines become obsolete and are removed */ - const Engine *e; - FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) { + for (const Engine *e : Engine::IterateType(VEH_TRAIN)) { if (!this->show_hidden_engines && e->IsHidden(_local_company)) continue; EngineID eid = e->index; const RailVehicleInfo *rvi = &e->u.rail; @@ -1304,8 +1303,7 @@ struct BuildVehicleWindow : Window { this->eng_list.clear(); - const Engine *e; - FOR_ALL_ENGINES_OF_TYPE(e, VEH_ROAD) { + for (const Engine *e : Engine::IterateType(VEH_ROAD)) { if (!this->show_hidden_engines && e->IsHidden(_local_company)) continue; EngineID eid = e->index; if (!IsEngineBuildable(eid, VEH_ROAD, _local_company)) continue; @@ -1324,8 +1322,7 @@ struct BuildVehicleWindow : Window { EngineID sel_id = INVALID_ENGINE; this->eng_list.clear(); - const Engine *e; - FOR_ALL_ENGINES_OF_TYPE(e, VEH_SHIP) { + for (const Engine *e : Engine::IterateType(VEH_SHIP)) { if (!this->show_hidden_engines && e->IsHidden(_local_company)) continue; EngineID eid = e->index; if (!IsEngineBuildable(eid, VEH_SHIP, _local_company)) continue; @@ -1349,8 +1346,7 @@ struct BuildVehicleWindow : Window { * Also check to see if the previously selected plane is still available, * and if not, reset selection to INVALID_ENGINE. This could be the case * when planes become obsolete and are removed */ - const Engine *e; - FOR_ALL_ENGINES_OF_TYPE(e, VEH_AIRCRAFT) { + for (const Engine *e : Engine::IterateType(VEH_AIRCRAFT)) { if (!this->show_hidden_engines && e->IsHidden(_local_company)) continue; EngineID eid = e->index; if (!IsEngineBuildable(eid, VEH_AIRCRAFT, _local_company)) continue; diff --git a/src/company_gui.cpp b/src/company_gui.cpp index c09dab139f..92c6ccb535 100644 --- a/src/company_gui.cpp +++ b/src/company_gui.cpp @@ -1825,8 +1825,7 @@ struct CompanyInfrastructureWindow : Window this->roadtypes = ROADTYPES_NONE; /* Find the used railtypes. */ - Engine *e; - FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) { + for (const Engine *e : Engine::IterateType(VEH_TRAIN)) { if (!HasBit(e->info.climates, _settings_game.game_creation.landscape)) continue; this->railtypes |= GetRailTypeInfo(e->u.rail.railtype)->introduces_railtypes; @@ -1836,7 +1835,7 @@ struct CompanyInfrastructureWindow : Window this->railtypes = AddDateIntroducedRailTypes(this->railtypes, MAX_DAY); /* Find the used roadtypes. */ - FOR_ALL_ENGINES_OF_TYPE(e, VEH_ROAD) { + for (const Engine *e : Engine::IterateType(VEH_ROAD)) { if (!HasBit(e->info.climates, _settings_game.game_creation.landscape)) continue; this->roadtypes |= GetRoadTypeInfo(e->u.road.roadtype)->introduces_roadtypes; diff --git a/src/depot_gui.cpp b/src/depot_gui.cpp index 430d023814..f525e714c5 100644 --- a/src/depot_gui.cpp +++ b/src/depot_gui.cpp @@ -170,8 +170,7 @@ static void InitBlocksizeForVehicles(VehicleType type, EngineImageType image_typ int max_extend_right = 0; uint max_height = 0; - const Engine *e; - FOR_ALL_ENGINES_OF_TYPE(e, type) { + for (const Engine *e : Engine::IterateType(type)) { if (!e->IsEnabled()) continue; EngineID eid = e->index; @@ -222,8 +221,7 @@ void InitDepotWindowBlockSizes() _consistent_train_width = TRAININFO_DEFAULT_VEHICLE_WIDTH; bool first = true; - const Engine *e; - FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) { + for (const Engine *e : Engine::IterateType(VEH_TRAIN)) { if (!e->IsEnabled()) continue; uint w = TRAININFO_DEFAULT_VEHICLE_WIDTH; diff --git a/src/elrail.cpp b/src/elrail.cpp index b169479da3..eacb21f246 100644 --- a/src/elrail.cpp +++ b/src/elrail.cpp @@ -599,8 +599,7 @@ bool SettingsDisableElrail(int32 p1) const RailType new_railtype = disable ? RAILTYPE_RAIL : RAILTYPE_ELECTRIC; /* walk through all train engines */ - Engine *e; - FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) { + for (Engine *e : Engine::IterateType(VEH_TRAIN)) { RailVehicleInfo *rv_info = &e->u.rail; /* if it is an electric rail engine and its railtype is the wrong one */ if (rv_info->engclass == 2 && rv_info->railtype == old_railtype) { diff --git a/src/engine.cpp b/src/engine.cpp index 4baf906e5b..d0333b8f44 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -618,8 +618,7 @@ void SetYearEngineAgingStops() /* Determine last engine aging year, default to 2050 as previously. */ _year_engine_aging_stops = 2050; - const Engine *e; - FOR_ALL_ENGINES(e) { + for (const Engine *e : Engine::Iterate()) { const EngineInfo *ei = &e->info; /* Exclude certain engines */ @@ -694,11 +693,10 @@ void StartupOneEngine(Engine *e, Date aging_date) */ void StartupEngines() { - Engine *e; /* Aging of vehicles stops, so account for that when starting late */ const Date aging_date = min(_date, ConvertYMDToDate(_year_engine_aging_stops, 0, 1)); - FOR_ALL_ENGINES(e) { + for (Engine *e : Engine::Iterate()) { StartupOneEngine(e, aging_date); } @@ -811,8 +809,7 @@ void EnginesDailyLoop() if (_cur_year >= _year_engine_aging_stops) return; - Engine *e; - FOR_ALL_ENGINES(e) { + for (Engine *e : Engine::Iterate()) { EngineID i = e->index; if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) { if (e->preview_company != INVALID_COMPANY) { @@ -848,8 +845,7 @@ void EnginesDailyLoop() */ void ClearEnginesHiddenFlagOfCompany(CompanyID cid) { - Engine *e; - FOR_ALL_ENGINES(e) { + for (Engine *e : Engine::Iterate()) { SB(e->company_hidden, cid, 1, 0); } } @@ -973,8 +969,7 @@ static void NewVehicleAvailable(Engine *e) void EnginesMonthlyLoop() { if (_cur_year < _year_engine_aging_stops) { - Engine *e; - FOR_ALL_ENGINES(e) { + for (Engine *e : Engine::Iterate()) { /* Age the vehicle */ if ((e->flags & ENGINE_AVAILABLE) && e->age != MAX_DAY) { e->age++; @@ -1015,9 +1010,7 @@ void EnginesMonthlyLoop() */ static bool IsUniqueEngineName(const char *name) { - const Engine *e; - - FOR_ALL_ENGINES(e) { + for (const Engine *e : Engine::Iterate()) { if (e->name != nullptr && strcmp(e->name, name) == 0) return false; } @@ -1138,10 +1131,9 @@ bool IsEngineRefittable(EngineID engine) */ void CheckEngines() { - const Engine *e; Date min_date = INT32_MAX; - FOR_ALL_ENGINES(e) { + for (const Engine *e : Engine::Iterate()) { if (!e->IsEnabled()) continue; /* We have an available engine... yay! */ diff --git a/src/engine_base.h b/src/engine_base.h index df43f3f781..1328f66a18 100644 --- a/src/engine_base.h +++ b/src/engine_base.h @@ -141,6 +141,17 @@ struct Engine : EnginePool::PoolItem<&_engine_pool> { } uint32 GetGRFID() const; + + /** + * Returns an iterable ensemble of all valid engines of the given type + * @param vt the VehicleType for engines to be valid + * @param from index of the first engine to consider + * @return an iterable ensemble of all valid engines of the given type + */ + static Pool::IterateWrapper IterateType(VehicleType vt, size_t from = 0) + { + return Pool::IterateWrapper(from, [vt](size_t index) { return Engine::Get(index)->type == vt; }); + } }; struct EngineIDMapping { @@ -165,11 +176,6 @@ struct EngineOverrideManager : std::vector { extern EngineOverrideManager _engine_mngr; -#define FOR_ALL_ENGINES_FROM(var, start) FOR_ALL_ITEMS_FROM(Engine, engine_index, var, start) -#define FOR_ALL_ENGINES(var) FOR_ALL_ENGINES_FROM(var, 0) - -#define FOR_ALL_ENGINES_OF_TYPE(e, engine_type) FOR_ALL_ENGINES(e) if (e->type == engine_type) - static inline const EngineInfo *EngInfo(EngineID e) { return &Engine::Get(e)->info; diff --git a/src/newgrf.cpp b/src/newgrf.cpp index ce1b2babe6..a6a48b1ca1 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -8555,8 +8555,7 @@ void ResetNewGRFData() _gted = CallocT(Engine::GetPoolSize()); /* Fill rail type label temporary data for default trains */ - Engine *e; - FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) { + for (const Engine *e : Engine::IterateType(VEH_TRAIN)) { _gted[e->index].railtypelabel = GetRailTypeInfo(e->u.rail.railtype)->label; } @@ -8772,9 +8771,7 @@ static const CargoLabel * const _default_refitmasks[] = { */ static void CalculateRefitMasks() { - Engine *e; - - FOR_ALL_ENGINES(e) { + for (Engine *e : Engine::Iterate()) { EngineID engine = e->index; EngineInfo *ei = &e->info; bool only_defaultcargo; ///< Set if the vehicle shall carry only the default cargo @@ -8888,9 +8885,7 @@ static void FinaliseCanals() /** Check for invalid engines */ static void FinaliseEngineArray() { - Engine *e; - - FOR_ALL_ENGINES(e) { + for (Engine *e : Engine::Iterate()) { if (e->GetGRF() == nullptr) { const EngineIDMapping &eid = _engine_mngr[e->index]; if (eid.grfid != INVALID_GRFID || eid.internal_id != eid.substitute_id) { @@ -9684,8 +9679,7 @@ static void AfterLoadGRFs() InitRailTypes(); InitRoadTypes(); - Engine *e; - FOR_ALL_ENGINES_OF_TYPE(e, VEH_ROAD) { + for (Engine *e : Engine::IterateType(VEH_ROAD)) { if (_gted[e->index].rv_max_speed != 0) { /* Set RV maximum speed from the mph/0.8 unit value */ e->u.road.max_speed = _gted[e->index].rv_max_speed * 4; @@ -9717,7 +9711,7 @@ static void AfterLoadGRFs() e->info.climates = 0; } - FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) { + for (Engine *e : Engine::IterateType(VEH_TRAIN)) { RailType railtype = GetRailTypeByLabel(_gted[e->index].railtypelabel); if (railtype == INVALID_RAILTYPE) { /* Rail type is not available, so disable this engine */ diff --git a/src/newgrf_engine.cpp b/src/newgrf_engine.cpp index dba05ebc4b..dc0e1cfe01 100644 --- a/src/newgrf_engine.cpp +++ b/src/newgrf_engine.cpp @@ -1236,8 +1236,7 @@ void CommitVehicleListOrderChanges() { /* Pre-sort engines by scope-grfid and local index */ std::vector ordering; - Engine *e; - FOR_ALL_ENGINES(e) { + for (const Engine *e : Engine::Iterate()) { ordering.push_back(e->index); } std::sort(ordering.begin(), ordering.end(), EnginePreSort); diff --git a/src/rail.cpp b/src/rail.cpp index 260404dbd5..f3c2c3dcce 100644 --- a/src/rail.cpp +++ b/src/rail.cpp @@ -267,8 +267,7 @@ RailTypes GetCompanyRailtypes(CompanyID company, bool introduces) { RailTypes rts = RAILTYPES_NONE; - const Engine *e; - FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) { + for (const Engine *e : Engine::IterateType(VEH_TRAIN)) { const EngineInfo *ei = &e->info; if (HasBit(ei->climates, _settings_game.game_creation.landscape) && @@ -299,8 +298,7 @@ RailTypes GetRailTypes(bool introduces) { RailTypes rts = RAILTYPES_NONE; - const Engine *e; - FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) { + for (const Engine *e : Engine::IterateType(VEH_TRAIN)) { const EngineInfo *ei = &e->info; if (!HasBit(ei->climates, _settings_game.game_creation.landscape)) continue; diff --git a/src/road.cpp b/src/road.cpp index 3b3f89272e..aa9935ad41 100644 --- a/src/road.cpp +++ b/src/road.cpp @@ -189,8 +189,7 @@ RoadTypes GetCompanyRoadTypes(CompanyID company, bool introduces) { RoadTypes rts = ROADTYPES_NONE; - const Engine *e; - FOR_ALL_ENGINES_OF_TYPE(e, VEH_ROAD) { + for (const Engine *e : Engine::IterateType(VEH_ROAD)) { const EngineInfo *ei = &e->info; if (HasBit(ei->climates, _settings_game.game_creation.landscape) && @@ -218,8 +217,7 @@ RoadTypes GetRoadTypes(bool introduces) { RoadTypes rts = ROADTYPES_NONE; - const Engine *e; - FOR_ALL_ENGINES_OF_TYPE(e, VEH_ROAD) { + for (const Engine *e : Engine::IterateType(VEH_ROAD)) { const EngineInfo *ei = &e->info; if (!HasBit(ei->climates, _settings_game.game_creation.landscape)) continue; @@ -282,8 +280,7 @@ RoadTypes ExistingRoadTypes(CompanyID c) RoadTypes known_roadtypes = ROADTYPES_NONE; /* Find used roadtypes */ - Engine *e; - FOR_ALL_ENGINES_OF_TYPE(e, VEH_ROAD) { + for (Engine *e : Engine::IterateType(VEH_ROAD)) { /* Check if the roadtype can be used in the current climate */ if (!HasBit(e->info.climates, _settings_game.game_creation.landscape)) continue; @@ -319,8 +316,7 @@ bool CanBuildRoadTypeInfrastructure(RoadType roadtype, CompanyID company) * and if we can build new ones */ if (_settings_game.vehicle.max_roadveh > 0 && HasBit(roadtypes, roadtype)) { /* Can we actually build the vehicle type? */ - const Engine *e; - FOR_ALL_ENGINES_OF_TYPE(e, VEH_ROAD) { + for (const Engine *e : Engine::IterateType(VEH_ROAD)) { if (!HasBit(e->company_avail, company)) continue; if (HasPowerOnRoad(e->u.road.roadtype, roadtype) || HasPowerOnRoad(roadtype, e->u.road.roadtype)) return true; } diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp index eb438515b6..fb3b1ecf28 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -1413,7 +1413,6 @@ bool AfterLoadGame() /* Time starts at 0 instead of 1920. * Account for this in older games by adding an offset */ if (IsSavegameVersionBefore(SLV_31)) { - Engine *e; Industry *i; Vehicle *v; @@ -1422,7 +1421,7 @@ bool AfterLoadGame() for (Station *st : Station::Iterate()) st->build_date += DAYS_TILL_ORIGINAL_BASE_YEAR; for (Waypoint *wp : Waypoint::Iterate()) wp->build_date += DAYS_TILL_ORIGINAL_BASE_YEAR; - FOR_ALL_ENGINES(e) e->intro_date += DAYS_TILL_ORIGINAL_BASE_YEAR; + for (Engine *e : Engine::Iterate()) e->intro_date += DAYS_TILL_ORIGINAL_BASE_YEAR; for (Company *c : Company::Iterate()) c->inaugurated_year += ORIGINAL_BASE_YEAR; FOR_ALL_INDUSTRIES(i) i->last_prod_year += ORIGINAL_BASE_YEAR; @@ -2044,8 +2043,7 @@ bool AfterLoadGame() if (c->bankrupt_asked == 0xFF) c->bankrupt_asked = 0xFFFF; } - Engine *e; - FOR_ALL_ENGINES(e) { + for (Engine *e : Engine::Iterate()) { if (e->company_avail == 0xFF) e->company_avail = 0xFFFF; } diff --git a/src/saveload/engine_sl.cpp b/src/saveload/engine_sl.cpp index 98cb4d0a3c..4ba38d04bb 100644 --- a/src/saveload/engine_sl.cpp +++ b/src/saveload/engine_sl.cpp @@ -86,8 +86,7 @@ Engine *GetTempDataEngine(EngineID index) static void Save_ENGN() { - Engine *e; - FOR_ALL_ENGINES(e) { + for (Engine *e : Engine::Iterate()) { SlSetArrayIndex(e->index); SlObject(e, _engine_desc); } @@ -118,8 +117,7 @@ static void Load_ENGN() */ void CopyTempEngineData() { - Engine *e; - FOR_ALL_ENGINES(e) { + for (Engine *e : Engine::Iterate()) { if (e->index >= _temp_engine.size()) break; const Engine *se = GetTempDataEngine(e->index); diff --git a/src/saveload/vehicle_sl.cpp b/src/saveload/vehicle_sl.cpp index 3529c84eb2..74a01616bb 100644 --- a/src/saveload/vehicle_sl.cpp +++ b/src/saveload/vehicle_sl.cpp @@ -215,11 +215,10 @@ static void CheckValidVehicles() size_t total_engines = Engine::GetPoolSize(); EngineID first_engine[4] = { INVALID_ENGINE, INVALID_ENGINE, INVALID_ENGINE, INVALID_ENGINE }; - Engine *e; - FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) { first_engine[VEH_TRAIN] = e->index; break; } - FOR_ALL_ENGINES_OF_TYPE(e, VEH_ROAD) { first_engine[VEH_ROAD] = e->index; break; } - FOR_ALL_ENGINES_OF_TYPE(e, VEH_SHIP) { first_engine[VEH_SHIP] = e->index; break; } - FOR_ALL_ENGINES_OF_TYPE(e, VEH_AIRCRAFT) { first_engine[VEH_AIRCRAFT] = e->index; break; } + for (const Engine *e : Engine::IterateType(VEH_TRAIN)) { first_engine[VEH_TRAIN] = e->index; break; } + for (const Engine *e : Engine::IterateType(VEH_ROAD)) { first_engine[VEH_ROAD] = e->index; break; } + for (const Engine *e : Engine::IterateType(VEH_SHIP)) { first_engine[VEH_SHIP] = e->index; break; } + for (const Engine *e : Engine::IterateType(VEH_AIRCRAFT)) { first_engine[VEH_AIRCRAFT] = e->index; break; } Vehicle *v; FOR_ALL_VEHICLES(v) { diff --git a/src/script/api/script_enginelist.cpp b/src/script/api/script_enginelist.cpp index 3f4b5d3ff2..c6f594cc7e 100644 --- a/src/script/api/script_enginelist.cpp +++ b/src/script/api/script_enginelist.cpp @@ -15,8 +15,7 @@ ScriptEngineList::ScriptEngineList(ScriptVehicle::VehicleType vehicle_type) { - Engine *e; - FOR_ALL_ENGINES_OF_TYPE(e, (::VehicleType)vehicle_type) { + for (const Engine *e : Engine::IterateType((::VehicleType)vehicle_type)) { if (ScriptObject::GetCompany() == OWNER_DEITY || HasBit(e->company_avail, ScriptObject::GetCompany())) this->AddItem(e->index); } } diff --git a/src/vehicle.cpp b/src/vehicle.cpp index 3e14c982e7..57e00418ac 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -1779,8 +1779,7 @@ bool CanBuildVehicleInfrastructure(VehicleType type, byte subtype) /* We can build vehicle infrastructure when we may build the vehicle type */ if (max > 0) { /* Can we actually build the vehicle type? */ - const Engine *e; - FOR_ALL_ENGINES_OF_TYPE(e, type) { + for (const Engine *e : Engine::IterateType(type)) { if (type == VEH_ROAD && GetRoadTramType(e->u.road.roadtype) != (RoadTramType)subtype) continue; if (HasBit(e->company_avail, _local_company)) return true; }