Codechange: Replace FOR_ALL_ENGINES with range-based for loops

pull/128/head
glx 4 years ago committed by Niels Martin Hansen
parent 1c92ba8ebe
commit 1f6b3a37f9

@ -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) {

@ -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;

@ -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;

@ -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;

@ -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) {

@ -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! */

@ -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<Engine> IterateType(VehicleType vt, size_t from = 0)
{
return Pool::IterateWrapper<Engine>(from, [vt](size_t index) { return Engine::Get(index)->type == vt; });
}
};
struct EngineIDMapping {
@ -165,11 +176,6 @@ struct EngineOverrideManager : std::vector<EngineIDMapping> {
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;

@ -8555,8 +8555,7 @@ void ResetNewGRFData()
_gted = CallocT<GRFTempEngineData>(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 */

@ -1236,8 +1236,7 @@ void CommitVehicleListOrderChanges()
{
/* Pre-sort engines by scope-grfid and local index */
std::vector<EngineID> 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);

@ -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;

@ -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;
}

@ -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;
}

@ -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);

@ -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) {

@ -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);
}
}

@ -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;
}

Loading…
Cancel
Save