Changeset - r25310:e304033a69f3
[Not reviewed]
master
0 8 0
glx22 - 4 years ago 2021-04-29 15:51:05
glx@openttd.org
Codechange: Replace FOR_ALL_CARGOSPECS with range-based for loops
8 files changed with 53 insertions and 25 deletions:
0 comments (0 inline, 0 general)
src/cargotype.cpp
Show inline comments
 
@@ -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 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);
 
	}
 

	
src/cargotype.h
Show inline comments
 
@@ -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(CargoI
 
	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)
 

	
 
/**
src/economy.cpp
Show inline comments
 
@@ -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;
 
	}
 

	
src/newgrf.cpp
Show inline comments
 
@@ -5332,8 +5332,7 @@ static CargoID TranslateCargo(uint8 feat
 
			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());
 
				}
src/newgrf_station.cpp
Show inline comments
 
@@ -572,8 +572,7 @@ StationResolverObject::StationResolverOb
 
	} 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();
src/script/api/script_cargolist.cpp
Show inline comments
 
@@ -19,8 +19,7 @@
 

	
 
ScriptCargoList::ScriptCargoList()
 
{
 
	const CargoSpec *cs;
 
	FOR_ALL_CARGOSPECS(cs) {
 
	for (const CargoSpec *cs : CargoSpec::Iterate()) {
 
		this->AddItem(cs->Index());
 
	}
 
}
src/station_cmd.cpp
Show inline comments
 
@@ -3467,8 +3467,7 @@ static void UpdateStationRating(Station 
 
	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
src/town_cmd.cpp
Show inline comments
 
@@ -2779,8 +2779,7 @@ CommandCost CmdRenameTown(TileIndex tile
 
 */
 
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;
0 comments (0 inline, 0 general)