From ee7a8eebca774666cd3625431a86dd05113b5e88 Mon Sep 17 00:00:00 2001 From: glx Date: Tue, 17 Dec 2019 22:04:09 +0100 Subject: Codechange: Replace FOR_ALL_TOWNS with range-based for loops --- src/economy.cpp | 4 +--- src/network/network_chat_gui.cpp | 4 +--- src/newgrf_house.cpp | 3 +-- src/openttd.cpp | 5 ++--- src/saveload/afterload.cpp | 33 ++++++++++----------------------- src/saveload/oldloader_sl.cpp | 4 +--- src/saveload/town_sl.cpp | 14 +++++--------- src/script/api/script_townlist.cpp | 3 +-- src/smallmap_gui.cpp | 3 +-- src/station.cpp | 3 +-- src/station_cmd.cpp | 8 +++----- src/subsidy.cpp | 3 +-- src/town.h | 3 --- src/town_cmd.cpp | 25 +++++++------------------ src/town_gui.cpp | 6 ++---- src/townname.cpp | 3 +-- src/viewport.cpp | 3 +-- 17 files changed, 39 insertions(+), 88 deletions(-) (limited to 'src') diff --git a/src/economy.cpp b/src/economy.cpp index 49fcddf26..256e94d5c 100644 --- a/src/economy.cpp +++ b/src/economy.cpp @@ -302,8 +302,6 @@ void ChangeOwnershipOfCompanyItems(Owner old_owner, Owner new_owner) assert(old_owner != _local_company); } - Town *t; - assert(old_owner != new_owner); { @@ -357,7 +355,7 @@ void ChangeOwnershipOfCompanyItems(Owner old_owner, Owner new_owner) if (new_owner == INVALID_OWNER) RebuildSubsidisedSourceAndDestinationCache(); /* Take care of rating and transport rights in towns */ - FOR_ALL_TOWNS(t) { + for (Town *t : Town::Iterate()) { /* If a company takes over, give the ratings to that company. */ if (new_owner != INVALID_OWNER) { if (HasBit(t->have_ratings, old_owner)) { diff --git a/src/network/network_chat_gui.cpp b/src/network/network_chat_gui.cpp index adaf6978c..07348c048 100644 --- a/src/network/network_chat_gui.cpp +++ b/src/network/network_chat_gui.cpp @@ -347,9 +347,7 @@ struct NetworkChatWindow : public Window { * Not that the following assumes all town indices are adjacent, ie no * towns have been deleted. */ if (*item < (uint)MAX_CLIENT_SLOTS + Town::GetPoolSize()) { - const Town *t; - - FOR_ALL_TOWNS_FROM(t, *item - MAX_CLIENT_SLOTS) { + for (const Town *t : Town::Iterate(*item - MAX_CLIENT_SLOTS)) { /* Get the town-name via the string-system */ SetDParam(0, t->index); GetString(chat_tab_temp_buffer, STR_TOWN_NAME, lastof(chat_tab_temp_buffer)); diff --git a/src/newgrf_house.cpp b/src/newgrf_house.cpp index 967b3cb83..b7d8048c9 100644 --- a/src/newgrf_house.cpp +++ b/src/newgrf_house.cpp @@ -83,8 +83,7 @@ void InitializeBuildingCounts() { memset(&_building_counts, 0, sizeof(_building_counts)); - Town *t; - FOR_ALL_TOWNS(t) { + for (Town *t : Town::Iterate()) { memset(&t->cache.building_counts, 0, sizeof(t->cache.building_counts)); } } diff --git a/src/openttd.cpp b/src/openttd.cpp index c7af56be1..48d889aef 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -1188,8 +1188,7 @@ static void CheckCaches() /* Check the town caches. */ std::vector old_town_caches; - Town *t; - FOR_ALL_TOWNS(t) { + for (const Town *t : Town::Iterate()) { old_town_caches.push_back(t->cache); } @@ -1198,7 +1197,7 @@ static void CheckCaches() RebuildSubsidisedSourceAndDestinationCache(); uint i = 0; - FOR_ALL_TOWNS(t) { + for (Town *t : Town::Iterate()) { if (MemCmpT(old_town_caches.data() + i, &t->cache) != 0) { DEBUG(desync, 2, "town cache mismatch: town %i", (int)t->index); } diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp index 519e4d46e..dd9fcd534 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -169,9 +169,7 @@ static void ConvertTownOwner() /* since savegame version 4.1, exclusive transport rights are stored at towns */ static void UpdateExclusiveRights() { - Town *t; - - FOR_ALL_TOWNS(t) { + for (Town *t : Town::Iterate()) { t->exclusivity = INVALID_COMPANY; } @@ -270,8 +268,7 @@ static void InitializeWindowsAndCaches() s->airport.psa->tile = s->airport.tile; } } - Town *t; - FOR_ALL_TOWNS(t) { + for (Town *t : Town::Iterate()) { for (std::list::iterator it = t->psa_list.begin(); it != t->psa_list.end(); ++it) { (*it)->feature = GSF_FAKE_TOWNS; (*it)->tile = t->xy; @@ -651,8 +648,7 @@ bool AfterLoadGame() if (st->name != nullptr) st->string_id = STR_SV_STNAME_FALLBACK; } - Town *t; - FOR_ALL_TOWNS(t) { + for (Town *t : Town::Iterate()) { t->name = CopyFromOldName(t->townnametype); if (t->name != nullptr) t->townnametype = SPECSTR_TOWNNAME_START + _settings_game.game_creation.town_name; } @@ -980,8 +976,7 @@ bool AfterLoadGame() /* From version 9.0, we update the max passengers of a town (was sometimes negative * before that. */ if (IsSavegameVersionBefore(SLV_9)) { - Town *t; - FOR_ALL_TOWNS(t) UpdateTownMaxPass(t); + for (Town *t : Town::Iterate()) UpdateTownMaxPass(t); } /* From version 16.0, we included autorenew on engines, which are now saved, but @@ -1570,9 +1565,7 @@ bool AfterLoadGame() * fast was added in version 54. From version 56 this is now saved in the * town as cities can be built specifically in the scenario editor. */ if (IsSavegameVersionBefore(SLV_56)) { - Town *t; - - FOR_ALL_TOWNS(t) { + for (Town *t : Town::Iterate()) { if (_settings_game.economy.larger_towns != 0 && (t->index % _settings_game.economy.larger_towns) == 0) { t->larger_town = true; } @@ -2018,8 +2011,7 @@ bool AfterLoadGame() if (e->company_avail == 0xFF) e->company_avail = 0xFFFF; } - Town *t; - FOR_ALL_TOWNS(t) { + for (Town *t : Town::Iterate()) { if (t->have_ratings == 0xFF) t->have_ratings = 0xFFFF; for (uint i = 8; i != MAX_COMPANIES; i++) t->ratings[i] = RATING_INITIAL; } @@ -2108,8 +2100,7 @@ bool AfterLoadGame() /* Initialize layout of all towns. Older versions were using different * generator for random town layout, use it if needed. */ - Town *t; - FOR_ALL_TOWNS(t) { + for (Town *t : Town::Iterate()) { if (_settings_game.economy.town_layout != TL_RANDOM) { t->layout = _settings_game.economy.town_layout; continue; @@ -2791,9 +2782,7 @@ bool AfterLoadGame() if (IsSavegameVersionBefore(SLV_164)) FixupTrainLengths(); if (IsSavegameVersionBefore(SLV_165)) { - Town *t; - - FOR_ALL_TOWNS(t) { + for (Town *t : Town::Iterate()) { /* Set the default cargo requirement for town growth */ switch (_settings_game.game_creation.landscape) { case LT_ARCTIC: @@ -2827,8 +2816,7 @@ bool AfterLoadGame() Town::Get(GetTownIndex(t))->cargo_accepted.Add(t); } - Town *town; - FOR_ALL_TOWNS(town) { + for (Town *town : Town::Iterate()) { UpdateTownCargoes(town); } } @@ -2984,8 +2972,7 @@ bool AfterLoadGame() if (IsSavegameVersionBefore(SLV_198)) { /* Convert towns growth_rate and grow_counter to ticks */ - Town *t; - FOR_ALL_TOWNS(t) { + for (Town *t : Town::Iterate()) { /* 0x8000 = TOWN_GROWTH_RATE_CUSTOM previously */ if (t->growth_rate & 0x8000) SetBit(t->flags, TOWN_CUSTOM_GROWTH); if (t->growth_rate != TOWN_GROWTH_RATE_NONE) { diff --git a/src/saveload/oldloader_sl.cpp b/src/saveload/oldloader_sl.cpp index 830d300b6..659216c0c 100644 --- a/src/saveload/oldloader_sl.cpp +++ b/src/saveload/oldloader_sl.cpp @@ -152,10 +152,8 @@ static uint32 RemapOldTownName(uint32 townnameparts, byte old_town_name_type) static void FixOldTowns() { - Town *town; - /* Convert town-names if needed */ - FOR_ALL_TOWNS(town) { + for (Town *town : Town::Iterate()) { if (IsInsideMM(town->townnametype, 0x20C1, 0x20C3)) { town->townnametype = SPECSTR_TOWNNAME_ENGLISH + _settings_game.game_creation.town_name; town->townnameparts = RemapOldTownName(town->townnameparts, _settings_game.game_creation.town_name); diff --git a/src/saveload/town_sl.cpp b/src/saveload/town_sl.cpp index 549a86cad..6fe1439b4 100644 --- a/src/saveload/town_sl.cpp +++ b/src/saveload/town_sl.cpp @@ -24,12 +24,11 @@ */ void RebuildTownCaches() { - Town *town; InitializeBuildingCounts(); RebuildTownKdtree(); /* Reset town population and num_houses */ - FOR_ALL_TOWNS(town) { + for (Town *town : Town::Iterate()) { town->cache.population = 0; town->cache.num_houses = 0; } @@ -38,7 +37,7 @@ void RebuildTownCaches() if (!IsTileType(t, MP_HOUSE)) continue; HouseID house_id = GetHouseType(t); - town = Town::GetByTile(t); + Town *town = Town::GetByTile(t); IncreaseBuildingCount(town, house_id); if (IsHouseCompleted(t)) town->cache.population += HouseSpec::Get(house_id)->population; @@ -47,7 +46,7 @@ void RebuildTownCaches() } /* Update the population and num_house dependent values */ - FOR_ALL_TOWNS(town) { + for (Town *town : Town::Iterate()) { UpdateTownRadius(town); UpdateTownCargoes(town); } @@ -263,9 +262,7 @@ static void RealSave_Town(Town *t) static void Save_TOWN() { - Town *t; - - FOR_ALL_TOWNS(t) { + for (Town *t : Town::Iterate()) { SlSetArrayIndex(t->index); SlAutolength((AutolengthProc*)RealSave_Town, t); } @@ -311,8 +308,7 @@ static void Ptrs_TOWN() /* Don't run when savegame version lower than 161. */ if (IsSavegameVersionBefore(SLV_161)) return; - Town *t; - FOR_ALL_TOWNS(t) { + for (Town *t : Town::Iterate()) { SlObject(t, _town_desc); } } diff --git a/src/script/api/script_townlist.cpp b/src/script/api/script_townlist.cpp index 8b544f29b..b5af62b5f 100644 --- a/src/script/api/script_townlist.cpp +++ b/src/script/api/script_townlist.cpp @@ -15,8 +15,7 @@ ScriptTownList::ScriptTownList() { - Town *t; - FOR_ALL_TOWNS(t) { + for (const Town *t : Town::Iterate()) { this->AddItem(t->index); } } diff --git a/src/smallmap_gui.cpp b/src/smallmap_gui.cpp index d8816265d..00f682f63 100644 --- a/src/smallmap_gui.cpp +++ b/src/smallmap_gui.cpp @@ -914,8 +914,7 @@ void SmallMapWindow::DrawVehicles(const DrawPixelInfo *dpi, Blitter *blitter) co */ void SmallMapWindow::DrawTowns(const DrawPixelInfo *dpi) const { - const Town *t; - FOR_ALL_TOWNS(t) { + for (const Town *t : Town::Iterate()) { /* Remap the town coordinate */ Point pt = this->RemapTile(TileX(t->xy), TileY(t->xy)); int x = pt.x - this->subscroll - (t->cache.sign.width_small >> 1); diff --git a/src/station.cpp b/src/station.cpp index 3e00bdb14..61c53bc56 100644 --- a/src/station.cpp +++ b/src/station.cpp @@ -381,8 +381,7 @@ static void AddIndustryToDeliver(Industry *ind, Station *st) */ void Station::RemoveFromAllNearbyLists() { - Town *t; - FOR_ALL_TOWNS(t) { t->stations_near.erase(this); } + for (Town *t : Town::Iterate()) { t->stations_near.erase(this); } for (Industry *i : Industry::Iterate()) { i->stations_near.erase(this); } } diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp index 86b3aedae..deed5161b 100644 --- a/src/station_cmd.cpp +++ b/src/station_cmd.cpp @@ -2185,10 +2185,10 @@ uint8 GetAirportNoiseLevelForDistance(const AirportSpec *as, uint distance) */ Town *AirportGetNearestTown(const AirportSpec *as, const TileIterator &it, uint &mindist) { - Town *t, *nearest = nullptr; + Town *nearest = nullptr; uint add = as->size_x + as->size_y - 2; // GetMinimalAirportDistanceToTile can differ from DistanceManhattan by this much mindist = UINT_MAX - add; // prevent overflow - FOR_ALL_TOWNS(t) { + for (Town *t : Town::Iterate()) { if (DistanceManhattan(t->xy, it) < mindist + add) { // avoid calling GetMinimalAirportDistanceToTile too often TileIterator *copy = it.Clone(); uint dist = GetMinimalAirportDistanceToTile(*copy, t->xy); @@ -2207,9 +2207,7 @@ Town *AirportGetNearestTown(const AirportSpec *as, const TileIterator &it, uint /** Recalculate the noise generated by the airports of each town */ void UpdateAirportsNoise() { - Town *t; - - FOR_ALL_TOWNS(t) t->noise_reached = 0; + for (Town *t : Town::Iterate()) t->noise_reached = 0; for (const Station *st : Station::Iterate()) { if (st->airport.tile != INVALID_TILE && st->airport.type != AT_OILRIG) { diff --git a/src/subsidy.cpp b/src/subsidy.cpp index 32c8f0a1b..e3913a289 100644 --- a/src/subsidy.cpp +++ b/src/subsidy.cpp @@ -130,8 +130,7 @@ static inline void SetPartOfSubsidyFlag(SourceType type, SourceID index, PartOfS /** Perform a full rebuild of the subsidies cache. */ void RebuildSubsidisedSourceAndDestinationCache() { - Town *t; - FOR_ALL_TOWNS(t) t->cache.part_of_subsidy = POS_NONE; + for (Town *t : Town::Iterate()) t->cache.part_of_subsidy = POS_NONE; for (Industry *i : Industry::Iterate()) i->part_of_subsidy = POS_NONE; diff --git a/src/town.h b/src/town.h index d07f9cc23..c8903d4ff 100644 --- a/src/town.h +++ b/src/town.h @@ -185,9 +185,6 @@ TileIndexDiff GetHouseNorthPart(HouseID &house); Town *CalcClosestTownFromTile(TileIndex tile, uint threshold = UINT_MAX); -#define FOR_ALL_TOWNS_FROM(var, start) FOR_ALL_ITEMS_FROM(Town, town_index, var, start) -#define FOR_ALL_TOWNS(var) FOR_ALL_TOWNS_FROM(var, 0) - void ResetHouses(); void ClearTownHouse(Town *t, TileIndex tile); diff --git a/src/town_cmd.cpp b/src/town_cmd.cpp index 3fbf26e1d..7b1d5c90f 100644 --- a/src/town_cmd.cpp +++ b/src/town_cmd.cpp @@ -67,8 +67,7 @@ TownKdtree _town_kdtree(&Kdtree_TownXYFunc); void RebuildTownKdtree() { std::vector townids; - Town *town; - FOR_ALL_TOWNS(town) { + for (const Town *town : Town::Iterate()) { townids.push_back(town->index); } _town_kdtree.Build(townids.begin(), townids.end()); @@ -408,9 +407,7 @@ void Town::UpdateVirtCoord() /** Update the virtual coords needed to draw the town sign for all towns. */ void UpdateAllTownVirtCoords() { - Town *t; - - FOR_ALL_TOWNS(t) { + for (Town *t : Town::Iterate()) { t->UpdateVirtCoord(); } } @@ -437,9 +434,7 @@ static void ChangePopulation(Town *t, int mod) uint32 GetWorldPopulation() { uint32 pop = 0; - const Town *t; - - FOR_ALL_TOWNS(t) pop += t->cache.population; + for (const Town *t : Town::Iterate()) pop += t->cache.population; return pop; } @@ -844,10 +839,9 @@ void UpdateTownCargoes(Town *t) /** Updates the bitmap of all cargoes accepted by houses. */ void UpdateTownCargoBitmap() { - Town *town; _town_cargoes_accepted = 0; - FOR_ALL_TOWNS(town) { + for (const Town *town : Town::Iterate()) { _town_cargoes_accepted |= town->cargo_accepted_total; } } @@ -874,8 +868,7 @@ void OnTick_Town() { if (_game_mode == GM_EDITOR) return; - Town *t; - FOR_ALL_TOWNS(t) { + for (Town *t : Town::Iterate()) { TownTickHandler(t); } } @@ -1842,9 +1835,7 @@ static CommandCost TownCanBePlacedHere(TileIndex tile) */ static bool IsUniqueTownName(const char *name) { - const Town *t; - - FOR_ALL_TOWNS(t) { + for (const Town *t : Town::Iterate()) { if (t->name != nullptr && strcmp(t->name, name) == 0) return false; } @@ -3642,9 +3633,7 @@ CommandCost CheckforTownRating(DoCommandFlag flags, Town *t, TownRatingCheckType void TownsMonthlyLoop() { - Town *t; - - FOR_ALL_TOWNS(t) { + for (Town *t : Town::Iterate()) { if (t->road_build_months != 0) t->road_build_months--; if (t->exclusive_counter != 0) { diff --git a/src/town_gui.cpp b/src/town_gui.cpp index 2f5a72a36..632080379 100644 --- a/src/town_gui.cpp +++ b/src/town_gui.cpp @@ -689,8 +689,7 @@ private: if (this->towns.NeedRebuild()) { this->towns.clear(); - const Town *t; - FOR_ALL_TOWNS(t) { + for (const Town *t : Town::Iterate()) { this->towns.push_back(t); } @@ -991,8 +990,7 @@ public: } else { this->towns.clear(); - const Town *t; - FOR_ALL_TOWNS(t) { + for (const Town *t : Town::Iterate()) { this->string_filter.ResetState(); SetDParam(0, t->index); diff --git a/src/townname.cpp b/src/townname.cpp index ba5992844..5cc414794 100644 --- a/src/townname.cpp +++ b/src/townname.cpp @@ -94,8 +94,7 @@ bool VerifyTownName(uint32 r, const TownNameParams *par, TownNames *town_names) if (town_names->find(buf1) != town_names->end()) return false; town_names->insert(buf1); } else { - const Town *t; - FOR_ALL_TOWNS(t) { + for (const Town *t : Town::Iterate()) { /* We can't just compare the numbers since * several numbers may map to a single name. */ const char *buf = t->name; diff --git a/src/viewport.cpp b/src/viewport.cpp index fa590354b..06f502379 100644 --- a/src/viewport.cpp +++ b/src/viewport.cpp @@ -2255,8 +2255,7 @@ void RebuildViewportKdtree() if (wp->sign.kdtree_valid) items.push_back(ViewportSignKdtreeItem::MakeWaypoint(wp->index)); } - const Town *town; - FOR_ALL_TOWNS(town) { + for (const Town *town : Town::Iterate()) { if (town->cache.sign.kdtree_valid) items.push_back(ViewportSignKdtreeItem::MakeTown(town->index)); } -- cgit v1.2.3-54-g00ecf