mirror of https://github.com/OpenTTD/OpenTTD.git
Codechange: Replace FOR_ALL_CARGOSPECS with range-based for loops
This commit is contained in:
parent
14e92bd8e2
commit
9a8756d7ed
|
@ -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<const CargoSpec *> _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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
@ -150,10 +193,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)
|
||||
|
||||
/**
|
||||
|
|
|
@ -798,8 +798,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;
|
||||
}
|
||||
|
||||
|
|
|
@ -5332,8 +5332,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();
|
||||
|
@ -8811,8 +8810,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());
|
||||
}
|
||||
|
|
|
@ -572,8 +572,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();
|
||||
|
|
|
@ -19,8 +19,7 @@
|
|||
|
||||
ScriptCargoList::ScriptCargoList()
|
||||
{
|
||||
const CargoSpec *cs;
|
||||
FOR_ALL_CARGOSPECS(cs) {
|
||||
for (const CargoSpec *cs : CargoSpec::Iterate()) {
|
||||
this->AddItem(cs->Index());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3467,8 +3467,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
|
||||
|
|
|
@ -2779,8 +2779,7 @@ CommandCost CmdRenameTown(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32
|
|||
*/
|
||||
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;
|
||||
|
|
Loading…
Reference in New Issue