From 126669e10d9de26ae4c6d44958a4e7f9e8abeed5 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Tue, 9 Sep 2025 18:00:54 +0100 Subject: [PATCH] Codechange: Make GetRail/RoadTypeInfoIndex() a member method. This follows a similar pattern used for cargoes, houses and objects. Also fixes incorrect documentation of GetRoadTypeInfoIndex(). --- src/newgrf_railtype.cpp | 2 +- src/newgrf_roadtype.cpp | 2 +- src/rail.cpp | 12 ++++++++++++ src/rail.h | 15 ++------------- src/road.cpp | 12 ++++++++++++ src/road.h | 15 ++------------- 6 files changed, 30 insertions(+), 28 deletions(-) diff --git a/src/newgrf_railtype.cpp b/src/newgrf_railtype.cpp index 0d215971b8..67754addb2 100644 --- a/src/newgrf_railtype.cpp +++ b/src/newgrf_railtype.cpp @@ -35,7 +35,7 @@ case 0x43: return TimerGameCalendar::date.base(); case 0x44: return to_underlying(HouseZone::TownEdge); case 0x45: { - auto rt = GetRailTypeInfoIndex(this->rti); + RailType rt = this->rti->Index(); uint8_t local = GetReverseRailTypeTranslation(rt, this->ro.grffile); if (local == 0xFF) local = 0xFE; return 0xFFFF | local << 16; diff --git a/src/newgrf_roadtype.cpp b/src/newgrf_roadtype.cpp index e48a67b11a..6b40a1e0d8 100644 --- a/src/newgrf_roadtype.cpp +++ b/src/newgrf_roadtype.cpp @@ -69,7 +69,7 @@ uint32_t GetTrackTypes(TileIndex tile, const GRFFile *grffile) case 0x43: return TimerGameCalendar::date.base(); case 0x44: return to_underlying(HouseZone::TownEdge); case 0x45: { - auto rt = GetRoadTypeInfoIndex(this->rti); + RoadType rt = this->rti->Index(); uint8_t local = GetReverseRoadTypeTranslation(rt, this->ro.grffile); if (local == 0xFF) local = 0xFE; if (RoadTypeIsRoad(rt)) { diff --git a/src/rail.cpp b/src/rail.cpp index d9e41087e3..590676aa87 100644 --- a/src/rail.cpp +++ b/src/rail.cpp @@ -19,6 +19,18 @@ #include "safeguards.h" +/** + * Get the RailType for this RailTypeInfo. + * @return RailType in static RailTypeInfo definitions. + */ +RailType RailTypeInfo::Index() const +{ + extern RailTypeInfo _railtypes[RAILTYPE_END]; + size_t index = this - _railtypes; + assert(index < RAILTYPE_END); + return static_cast(index); +} + /** * Return the rail type of tile, or INVALID_RAILTYPE if this is no rail tile. */ diff --git a/src/rail.h b/src/rail.h index 4a294e213d..631a7989d0 100644 --- a/src/rail.h +++ b/src/rail.h @@ -286,6 +286,8 @@ public: { return 82 * this->fallback_railtype; } + + RailType Index() const; }; @@ -301,19 +303,6 @@ inline const RailTypeInfo *GetRailTypeInfo(RailType railtype) return &_railtypes[railtype]; } -/** - * Returns the railtype for a Railtype information. - * @param rti Pointer to static RailTypeInfo - * @return Railtype in static railtype definitions - */ -inline RailType GetRailTypeInfoIndex(const RailTypeInfo *rti) -{ - extern RailTypeInfo _railtypes[RAILTYPE_END]; - size_t index = rti - _railtypes; - assert(index < RAILTYPE_END && rti == _railtypes + index); - return static_cast(index); -} - /** * Returns all compatible railtypes for a set of railtypes. * @param railtypes Set of railtypes to get the compatible railtypes from. diff --git a/src/road.cpp b/src/road.cpp index 5b796f12ea..51bdf1900d 100644 --- a/src/road.cpp +++ b/src/road.cpp @@ -23,6 +23,18 @@ #include "safeguards.h" +/** + * Get the RoadType for this RoadTypeInfo. + * @return RoadType in static RoadTypeInfo definitions. + */ +RoadType RoadTypeInfo::Index() const +{ + extern RoadTypeInfo _roadtypes[ROADTYPE_END]; + size_t index = this - _roadtypes; + assert(index < ROADTYPE_END); + return static_cast(index); +} + /** * Return if the tile is a valid tile for a crossing. * diff --git a/src/road.h b/src/road.h index 76355d216e..a3c689419f 100644 --- a/src/road.h +++ b/src/road.h @@ -185,6 +185,8 @@ public: { return this->group[ROTSG_GROUND] != nullptr; } + + RoadType Index() const; }; /** @@ -231,19 +233,6 @@ inline const RoadTypeInfo *GetRoadTypeInfo(RoadType roadtype) return &_roadtypes[roadtype]; } -/** - * Returns the railtype for a Railtype information. - * @param rti Pointer to static RailTypeInfo - * @return Railtype in static railtype definitions - */ -inline RoadType GetRoadTypeInfoIndex(const RoadTypeInfo *rti) -{ - extern RoadTypeInfo _roadtypes[ROADTYPE_END]; - size_t index = rti - _roadtypes; - assert(index < ROADTYPE_END && rti == _roadtypes + index); - return static_cast(index); -} - /** * Checks if an engine of the given RoadType got power on a tile with a given * RoadType. This would normally just be an equality check, but for electrified