diff --git a/src/cargotype.cpp b/src/cargotype.cpp index 81818d5f9d..bf9561dda8 100644 --- a/src/cargotype.cpp +++ b/src/cargotype.cpp @@ -84,8 +84,7 @@ void SetupCargoForClimate(LandscapeID l) */ CargoID GetCargoIDByLabel(CargoLabel cl) { - const CargoSpec *cs; - FOR_ALL_CARGOSPECS(cs) { + for (const CargoSpec *cs : CargoSpec::Iterate()) { if (cs->label == cl) return cs->Index(); } @@ -103,8 +102,7 @@ CargoID GetCargoIDByBitnum(uint8 bitnum) { if (bitnum == INVALID_CARGO) return CT_INVALID; - const CargoSpec *cs; - FOR_ALL_CARGOSPECS(cs) { + for (const CargoSpec *cs : CargoSpec::Iterate()) { if (cs->bitnum == bitnum) return cs->Index(); } @@ -132,7 +130,6 @@ SpriteID CargoSpec::GetCargoIcon() const std::vector _sorted_cargo_specs; ///< Cargo specifications sorted alphabetically by name. uint8 _sorted_standard_cargo_specs_size; ///< Number of standard cargo specifications stored in the _sorted_cargo_specs array. - /** Sort cargo specifications by their name. */ static bool CargoSpecNameSorter(const CargoSpec * const &a, const CargoSpec * const &b) { @@ -169,9 +166,8 @@ static bool CargoSpecClassSorter(const CargoSpec * const &a, const CargoSpec * c void InitializeSortedCargoSpecs() { _sorted_cargo_specs.clear(); - const CargoSpec *cargo; /* Add each cargo spec to the list. */ - FOR_ALL_CARGOSPECS(cargo) { + for (const CargoSpec *cargo : CargoSpec::Iterate()) { _sorted_cargo_specs.push_back(cargo); } diff --git a/src/cargotype.h b/src/cargotype.h index 4b27beea02..e56e3c5955 100644 --- a/src/cargotype.h +++ b/src/cargotype.h @@ -122,6 +122,49 @@ struct CargoSpec { SpriteID GetCargoIcon() const; + /** + * Iterator to iterate all valid CargoSpec + */ + struct Iterator { + typedef CargoSpec value_type; + typedef CargoSpec *pointer; + typedef CargoSpec &reference; + typedef size_t difference_type; + typedef std::forward_iterator_tag iterator_category; + + explicit Iterator(size_t index) : index(index) + { + this->ValidateIndex(); + }; + + bool operator==(const Iterator &other) const { return this->index == other.index; } + bool operator!=(const Iterator &other) const { return !(*this == other); } + CargoSpec * operator*() const { return CargoSpec::Get(this->index); } + Iterator & operator++() { this->index++; this->ValidateIndex(); return *this; } + + private: + size_t index; + void ValidateIndex() { while (this->index < CargoSpec::GetArraySize() && !(CargoSpec::Get(this->index)->IsValid())) this->index++; } + }; + + /* + * Iterable ensemble of all valid CargoSpec + */ + struct IterateWrapper { + size_t from; + IterateWrapper(size_t from = 0) : from(from) {} + Iterator begin() { return Iterator(this->from); } + Iterator end() { return Iterator(CargoSpec::GetArraySize()); } + bool empty() { return this->begin() == this->end(); } + }; + + /** + * Returns an iterable ensemble of all valid CargoSpec + * @param from index of the first CargoSpec to consider + * @return an iterable ensemble of all valid CargoSpec + */ + static IterateWrapper Iterate(size_t from = 0) { return IterateWrapper(from); } + private: static CargoSpec array[NUM_CARGO]; ///< Array holding all CargoSpecs @@ -153,10 +196,6 @@ static inline bool IsCargoInClass(CargoID c, CargoClass cc) return (CargoSpec::Get(c)->classes & cc) != 0; } -#define FOR_ALL_CARGOSPECS_FROM(var, start) for (size_t cargospec_index = start; var = nullptr, cargospec_index < CargoSpec::GetArraySize(); cargospec_index++) \ - if ((var = CargoSpec::Get(cargospec_index))->IsValid()) -#define FOR_ALL_CARGOSPECS(var) FOR_ALL_CARGOSPECS_FROM(var, 0) - #define FOR_EACH_SET_CARGO_ID(var, cargo_bits) FOR_EACH_SET_BIT_EX(CargoID, var, CargoTypes, cargo_bits) /** diff --git a/src/economy.cpp b/src/economy.cpp index c4cf4a990a..88425f7ee3 100644 --- a/src/economy.cpp +++ b/src/economy.cpp @@ -848,8 +848,7 @@ void RecomputePrices() } /* Setup cargo payment */ - CargoSpec *cs; - FOR_ALL_CARGOSPECS(cs) { + for (CargoSpec *cs : CargoSpec::Iterate()) { cs->current_payment = ((int64)cs->initial_payment * _economy.inflation_payment) >> 16; } diff --git a/src/newgrf.cpp b/src/newgrf.cpp index 9b158391c8..080b2566d8 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -5477,8 +5477,7 @@ static CargoID TranslateCargo(uint8 feature, uint8 ctype) return CT_INVALID; } - const CargoSpec *cs; - FOR_ALL_CARGOSPECS(cs) { + for (const CargoSpec *cs : CargoSpec::Iterate()) { if (cs->bitnum == ctype) { grfmsg(6, "TranslateCargo: Cargo bitnum %d mapped to cargo type %d.", ctype, cs->Index()); return cs->Index(); @@ -9434,8 +9433,7 @@ static void CalculateRefitMasks() if (_gted[engine].cargo_allowed != 0) { /* Build up the list of cargo types from the set cargo classes. */ - const CargoSpec *cs; - FOR_ALL_CARGOSPECS(cs) { + for (const CargoSpec *cs : CargoSpec::Iterate()) { if (_gted[engine].cargo_allowed & cs->classes) SetBit(mask, cs->Index()); if (_gted[engine].cargo_disallowed & cs->classes) SetBit(not_mask, cs->Index()); } diff --git a/src/newgrf_station.cpp b/src/newgrf_station.cpp index 2576b83a23..e6dce42e2c 100644 --- a/src/newgrf_station.cpp +++ b/src/newgrf_station.cpp @@ -573,8 +573,7 @@ StationResolverObject::StationResolverObject(const StationSpec *statspec, BaseSt } else if (Station::IsExpected(this->station_scope.st)) { const Station *st = Station::From(this->station_scope.st); /* Pick the first cargo that we have waiting */ - const CargoSpec *cs; - FOR_ALL_CARGOSPECS(cs) { + for (const CargoSpec *cs : CargoSpec::Iterate()) { if (this->station_scope.statspec->grf_prop.spritegroup[cs->Index()] != nullptr && st->goods[cs->Index()].cargo.TotalCount() > 0) { ctype = cs->Index(); diff --git a/src/script/api/script_cargolist.cpp b/src/script/api/script_cargolist.cpp index fbd150c6cf..0019dc5e91 100644 --- a/src/script/api/script_cargolist.cpp +++ b/src/script/api/script_cargolist.cpp @@ -19,8 +19,7 @@ ScriptCargoList::ScriptCargoList() { - const CargoSpec *cs; - FOR_ALL_CARGOSPECS(cs) { + for (const CargoSpec *cs : CargoSpec::Iterate()) { this->AddItem(cs->Index()); } } diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp index 6f1b433be3..3178246462 100644 --- a/src/station_cmd.cpp +++ b/src/station_cmd.cpp @@ -3774,8 +3774,7 @@ static void UpdateStationRating(Station *st) byte_inc_sat(&st->time_since_load); byte_inc_sat(&st->time_since_unload); - const CargoSpec *cs; - FOR_ALL_CARGOSPECS(cs) { + for (const CargoSpec *cs : CargoSpec::Iterate()) { GoodsEntry *ge = &st->goods[cs->Index()]; /* Slowly increase the rating back to his original level in the case we * didn't deliver cargo yet to this station. This happens when a bribe diff --git a/src/town_cmd.cpp b/src/town_cmd.cpp index 833dd1311a..90b06802cb 100644 --- a/src/town_cmd.cpp +++ b/src/town_cmd.cpp @@ -3102,8 +3102,7 @@ CommandCost CmdRenameTownNonAdmin(TileIndex tile, DoCommandFlag flags, uint32 p1 */ const CargoSpec *FindFirstCargoWithTownEffect(TownEffect effect) { - const CargoSpec *cs; - FOR_ALL_CARGOSPECS(cs) { + for (const CargoSpec *cs : CargoSpec::Iterate()) { if (cs->town_effect == effect) return cs; } return nullptr;