diff options
author | smatz <smatz@openttd.org> | 2009-06-06 16:54:22 +0000 |
---|---|---|
committer | smatz <smatz@openttd.org> | 2009-06-06 16:54:22 +0000 |
commit | 0c10006907b7d149f91d277a0c28a79b40eaa4c0 (patch) | |
tree | dc8cb6a7a9315a10688af00187db94be8911acef /src/vehicle_base.h | |
parent | c90819ff6d8d49ac25aa9194bdf04e8dfd7149ea (diff) | |
download | openttd-0c10006907b7d149f91d277a0c28a79b40eaa4c0.tar.xz |
(svn r16527) -Codechange: use static member functions instead of simple casts when converting Vehicle to specialised vehicle types. Includes safety check
Diffstat (limited to 'src/vehicle_base.h')
-rw-r--r-- | src/vehicle_base.h | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/vehicle_base.h b/src/vehicle_base.h index b6bcbb4b6..b5e7a16f5 100644 --- a/src/vehicle_base.h +++ b/src/vehicle_base.h @@ -573,6 +573,28 @@ struct SpecializedVehicle : public Vehicle { { return IsValidID(index) ? Get(index) : NULL ; } + + /** + * Converts a Vehicle to SpecializedVehicle with type checking. + * @param v Vehicle pointer + * @return pointer to SpecializedVehicle + */ + static FORCEINLINE T *From(Vehicle *v) + { + assert(v->type == Type); + return (T *)v; + } + + /** + * Converts a const Vehicle to const SpecializedVehicle with type checking. + * @param v Vehicle pointer + * @return pointer to SpecializedVehicle + */ + static FORCEINLINE const T *From(const Vehicle *v) + { + assert(v->type == Type); + return (const T *)v; + } }; #define FOR_ALL_VEHICLES_OF_TYPE(name, var) FOR_ALL_ITEMS_FROM(name, vehicle_index, var, 0) if (var->type == name::EXPECTED_TYPE) |