From 3fcfb9b248bd228d5885974858a14724205626ad Mon Sep 17 00:00:00 2001 From: rubidium Date: Sat, 18 Jul 2009 18:39:17 +0000 Subject: (svn r16876) -Codechange: add helper functions to cast to Station/Waypoint with some extra checks. --- src/station.cpp | 2 +- src/station_base.h | 71 +++++++++++++++++++++++++++++++++++++++++++----------- src/station_type.h | 1 + src/waypoint.h | 4 +-- 4 files changed, 61 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/station.cpp b/src/station.cpp index ec57d6e04..91605fda2 100644 --- a/src/station.cpp +++ b/src/station.cpp @@ -38,7 +38,7 @@ BaseStation::~BaseStation() } Station::Station(TileIndex tile) : - BaseStation(tile), + SpecializedStation(tile), train_tile(INVALID_TILE), airport_tile(INVALID_TILE), dock_tile(INVALID_TILE), diff --git a/src/station_base.h b/src/station_base.h index 9d092f057..b68858a59 100644 --- a/src/station_base.h +++ b/src/station_base.h @@ -90,8 +90,8 @@ struct BaseStation { OwnerByte owner; ///< The owner of this station StationFacilityByte facilities; ///< The facilities that this station has - uint8 num_specs; ///< NOSAVE: Number of specs in the speclist - StationSpecList *speclist; ///< NOSAVE: List of station specs of this station + uint8 num_specs; ///< Number of specs in the speclist + StationSpecList *speclist; ///< List of station specs of this station Date build_date; ///< Date of construction @@ -99,7 +99,8 @@ struct BaseStation { byte waiting_triggers; ///< Waiting triggers (NewGRF) for this station uint8 cached_anim_triggers; ///< NOSAVE: Combined animation trigger bitmask, used to determine if trigger processing should happen. - BaseStation(TileIndex tile = INVALID_TILE) : xy(tile) { } + BaseStation(TileIndex tile) : xy(tile) { } + virtual ~BaseStation(); /** @@ -132,10 +133,62 @@ struct BaseStation { static BaseStation *GetByTile(TileIndex tile); }; +/** + * Class defining several overloaded accessors so we don't + * have to cast base stations that often + */ +template +struct SpecializedStation : public BaseStation { + static const StationFacility EXPECTED_FACIL = Tis_waypoint ? FACIL_WAYPOINT : FACIL_NONE; ///< Specialized type + + /** + * Set station type correctly + * @param tile The base tile of the station. + */ + FORCEINLINE SpecializedStation(TileIndex tile) : + BaseStation(tile) + { + this->facilities = EXPECTED_FACIL; + } + + /** + * Helper for checking whether the given station is of this type. + * @param st the station to check. + * @return true if the station is the type we expect it to be. + */ + static FORCEINLINE bool IsExpected(const BaseStation *st) + { + return (st->facilities & FACIL_WAYPOINT) == EXPECTED_FACIL; + } + + /** + * Converts a BaseStation to SpecializedStation with type checking. + * @param st BaseStation pointer + * @return pointer to SpecializedStation + */ + static FORCEINLINE T *From(BaseStation *st) + { + assert(IsExpected(st)); + return (T *)st; + } + + /** + * Converts a const BaseStation to const SpecializedStation with type checking. + * @param st BaseStation pointer + * @return pointer to SpecializedStation + */ + static FORCEINLINE const T *From(const BaseStation *st) + { + assert(IsExpected(st)); + return (const T *)st; + } +}; + + typedef SmallVector IndustryVector; /** Station data structure */ -struct Station : StationPool::PoolItem<&_station_pool>, BaseStation { +struct Station : StationPool::PoolItem<&_station_pool>, SpecializedStation { public: RoadStop *GetPrimaryRoadStop(RoadStopType type) const { @@ -220,16 +273,6 @@ public: } static void PostDestructor(size_t index); - - static FORCEINLINE Station *From(BaseStation *st) - { - return (Station *)st; - } - - static FORCEINLINE const Station *From(const BaseStation *st) - { - return (const Station *)st; - } }; #define FOR_ALL_STATIONS_FROM(var, start) FOR_ALL_ITEMS_FROM(Station, station_index, var, start) diff --git a/src/station_type.h b/src/station_type.h index 6a65c30d7..f5de2f270 100644 --- a/src/station_type.h +++ b/src/station_type.h @@ -44,6 +44,7 @@ enum StationFacility { FACIL_BUS_STOP = 1 << 2, ///< Station with bus stops FACIL_AIRPORT = 1 << 3, ///< Station with an airport FACIL_DOCK = 1 << 4, ///< Station with a dock + FACIL_WAYPOINT = 1 << 7, ///< Station is a waypoint }; DECLARE_ENUM_AS_BIT_SET(StationFacility); typedef SimpleTinyEnumT StationFacilityByte; diff --git a/src/waypoint.h b/src/waypoint.h index ee3122fe4..c7d151350 100644 --- a/src/waypoint.h +++ b/src/waypoint.h @@ -17,10 +17,10 @@ typedef Pool WaypointPool; extern WaypointPool _waypoint_pool; -struct Waypoint : WaypointPool::PoolItem<&_waypoint_pool>, BaseStation { +struct Waypoint : WaypointPool::PoolItem<&_waypoint_pool>, SpecializedStation { uint16 town_cn; ///< The Nth waypoint for this town (consecutive number) - Waypoint(TileIndex tile = INVALID_TILE) : BaseStation(tile) { } + Waypoint(TileIndex tile = INVALID_TILE) : SpecializedStation(tile) { } ~Waypoint(); void UpdateVirtCoord(); -- cgit v1.2.3-54-g00ecf