From 77d13eae6194387ca6ce2b0eb835b7f97f67d085 Mon Sep 17 00:00:00 2001 From: smatz Date: Thu, 16 Jul 2009 20:40:06 +0000 Subject: (svn r16852) -Codechange: use FOR_ALL_CARGOSPECS for iterating over all valid CargoSpecs --- src/cargotype.h | 43 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-) (limited to 'src/cargotype.h') diff --git a/src/cargotype.h b/src/cargotype.h index d7e1b507c..b6649658f 100644 --- a/src/cargotype.h +++ b/src/cargotype.h @@ -50,28 +50,49 @@ struct CargoSpec { const struct GRFFile *grffile; ///< NewGRF where 'group' belongs to const struct SpriteGroup *group; - bool IsValid() const + /** + * Determines index of this cargospec + * @return index (in the CargoSpec::array array) + */ + FORCEINLINE CargoID Index() const + { + return this - CargoSpec::array; + } + + /** + * Tests for validity of this cargospec + * @return is this cargospec valid? + * @note assert(cs->IsValid()) can be triggered when GRF config is modified + */ + FORCEINLINE bool IsValid() const { return this->bitnum != INVALID_CARGO; } + /** + * Total number of subsidies, both valid and invalid + * @return length of Subsidy::array + */ + static FORCEINLINE size_t GetArraySize() + { + return lengthof(CargoSpec::array); + } + /** * Retrieve cargo details for the given cargo ID - * @param c ID of cargo - * @pre c is a valid cargo ID + * @param index ID of cargo + * @pre index is a valid cargo ID */ - static CargoSpec *Get(CargoID c) + static FORCEINLINE CargoSpec *Get(size_t index) { - assert(c < lengthof(CargoSpec::cargo)); - return &CargoSpec::cargo[c]; + assert(index < lengthof(CargoSpec::array)); + return &CargoSpec::array[index]; } private: - static CargoSpec cargo[NUM_CARGO]; + static CargoSpec array[NUM_CARGO]; friend void SetupCargoForClimate(LandscapeID l); - friend CargoID GetCargoIDByLabel(CargoLabel cl); - friend CargoID GetCargoIDByBitnum(uint8 bitnum); }; extern uint32 _cargo_mask; @@ -89,4 +110,8 @@ static inline bool IsCargoInClass(CargoID c, uint16 cc) return (CargoSpec::Get(c)->classes & cc) != 0; } +#define FOR_ALL_CARGOSPECS_FROM(var, start) for (size_t cargospec_index = start; var = NULL, cargospec_index < CargoSpec::GetArraySize(); cargospec_index++) \ + if ((var = CargoSpec::Get(cargospec_index))->IsValid()) +#define FOR_ALL_CARGOSPECS(var) FOR_ALL_CARGOSPECS_FROM(var, 0) + #endif /* CARGOTYPE_H */ -- cgit v1.2.3-54-g00ecf