summaryrefslogtreecommitdiff
path: root/src/cargotype.h
diff options
context:
space:
mode:
authorsmatz <smatz@openttd.org>2009-07-16 20:40:06 +0000
committersmatz <smatz@openttd.org>2009-07-16 20:40:06 +0000
commit77d13eae6194387ca6ce2b0eb835b7f97f67d085 (patch)
treea15b28b9384b9590cf11866a877097709db4d89d /src/cargotype.h
parent665fa7f9c170774f6a640ecf381f714b50b6b174 (diff)
downloadopenttd-77d13eae6194387ca6ce2b0eb835b7f97f67d085.tar.xz
(svn r16852) -Codechange: use FOR_ALL_CARGOSPECS for iterating over all valid CargoSpecs
Diffstat (limited to 'src/cargotype.h')
-rw-r--r--src/cargotype.h43
1 files changed, 34 insertions, 9 deletions
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 */