summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2009-07-18 18:39:17 +0000
committerrubidium <rubidium@openttd.org>2009-07-18 18:39:17 +0000
commit3fcfb9b248bd228d5885974858a14724205626ad (patch)
tree6b35f962b628ddcf39d6c80e26497cf856a5aa82 /src
parent73453e61d678d106be04149fc549716ecc5eed58 (diff)
downloadopenttd-3fcfb9b248bd228d5885974858a14724205626ad.tar.xz
(svn r16876) -Codechange: add helper functions to cast to Station/Waypoint with some extra checks.
Diffstat (limited to 'src')
-rw-r--r--src/station.cpp2
-rw-r--r--src/station_base.h71
-rw-r--r--src/station_type.h1
-rw-r--r--src/waypoint.h4
4 files changed, 61 insertions, 17 deletions
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<Station, false>(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 <class T, bool Tis_waypoint>
+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<T, Tis_waypoint>(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<Industry *, 2> IndustryVector;
/** Station data structure */
-struct Station : StationPool::PoolItem<&_station_pool>, BaseStation {
+struct Station : StationPool::PoolItem<&_station_pool>, SpecializedStation<Station, false> {
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<StationFacility, byte> 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<Waypoint, WaypointID, 32, 64000> WaypointPool;
extern WaypointPool _waypoint_pool;
-struct Waypoint : WaypointPool::PoolItem<&_waypoint_pool>, BaseStation {
+struct Waypoint : WaypointPool::PoolItem<&_waypoint_pool>, SpecializedStation<Waypoint, true> {
uint16 town_cn; ///< The Nth waypoint for this town (consecutive number)
- Waypoint(TileIndex tile = INVALID_TILE) : BaseStation(tile) { }
+ Waypoint(TileIndex tile = INVALID_TILE) : SpecializedStation<Waypoint, true>(tile) { }
~Waypoint();
void UpdateVirtCoord();