From 798e9f229c0f91dd41301d391ddba7171e5cfeb2 Mon Sep 17 00:00:00 2001 From: Rubidium Date: Fri, 7 Feb 2025 16:45:45 +0100 Subject: [PATCH] Codechange: strongly type DepotID --- src/depot_base.h | 2 +- src/depot_map.h | 2 +- src/depot_type.h | 6 ++++-- src/order_type.h | 1 + src/rail_map.h | 2 +- src/road_map.h | 2 +- src/saveload/afterload.cpp | 4 ++-- src/water_map.h | 2 +- 8 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/depot_base.h b/src/depot_base.h index 1d8330fc74..3998f4456b 100644 --- a/src/depot_base.h +++ b/src/depot_base.h @@ -14,7 +14,7 @@ #include "core/pool_type.hpp" #include "timer/timer_game_calendar.h" -typedef Pool DepotPool; +typedef Pool DepotPool; extern DepotPool _depot_pool; struct Depot : DepotPool::PoolItem<&_depot_pool> { diff --git a/src/depot_map.h b/src/depot_map.h index cb38fbaf97..793047ada2 100644 --- a/src/depot_map.h +++ b/src/depot_map.h @@ -54,7 +54,7 @@ inline DepotID GetDepotIndex(Tile t) { /* Hangars don't have a Depot class, thus store no DepotID. */ assert(IsRailDepotTile(t) || IsRoadDepotTile(t) || IsShipDepotTile(t)); - return t.m2(); + return DepotID{t.m2()}; } /** diff --git a/src/depot_type.h b/src/depot_type.h index 4e61c1bcbd..d76693aa26 100644 --- a/src/depot_type.h +++ b/src/depot_type.h @@ -10,10 +10,12 @@ #ifndef DEPOT_TYPE_H #define DEPOT_TYPE_H -typedef uint16_t DepotID; ///< Type for the unique identifier of depots. +#include "core/pool_type.hpp" + +using DepotID = PoolID; ///< Type for the unique identifier of depots. struct Depot; -static const DepotID INVALID_DEPOT = UINT16_MAX; +static constexpr DepotID INVALID_DEPOT = DepotID::Invalid(); static const uint MAX_LENGTH_DEPOT_NAME_CHARS = 32; ///< The maximum length of a depot name in characters including '\0' diff --git a/src/order_type.h b/src/order_type.h index 915d0b242f..cf7f880297 100644 --- a/src/order_type.h +++ b/src/order_type.h @@ -25,6 +25,7 @@ struct DestinationID { explicit DestinationID() = default; constexpr DestinationID(size_t index) : value(static_cast(index)) {} + constexpr DestinationID(DepotID depot) : value(depot.base()) {} constexpr DepotID ToDepotID() const noexcept { return static_cast(this->value); } constexpr StationID ToStationID() const noexcept { return static_cast(this->value); } diff --git a/src/rail_map.h b/src/rail_map.h index 88ea36af7f..17e2988dc0 100644 --- a/src/rail_map.h +++ b/src/rail_map.h @@ -554,7 +554,7 @@ inline void MakeRailDepot(Tile tile, Owner owner, DepotID depot_id, DiagDirectio SetTileType(tile, MP_RAILWAY); SetTileOwner(tile, owner); SetDockingTile(tile, false); - tile.m2() = depot_id; + tile.m2() = depot_id.base(); tile.m3() = 0; tile.m4() = 0; tile.m5() = RAIL_TILE_DEPOT << 6 | dir; diff --git a/src/road_map.h b/src/road_map.h index e40ee33857..a99e94be1a 100644 --- a/src/road_map.h +++ b/src/road_map.h @@ -695,7 +695,7 @@ inline void MakeRoadDepot(Tile tile, Owner owner, DepotID depot_id, DiagDirectio { SetTileType(tile, MP_ROAD); SetTileOwner(tile, owner); - tile.m2() = depot_id; + tile.m2() = depot_id.base(); tile.m3() = 0; tile.m4() = INVALID_ROADTYPE; tile.m5() = ROAD_TILE_DEPOT << 6 | dir; diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp index bfeb97c42b..572e739882 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -2355,8 +2355,8 @@ bool AfterLoadGame() d = nullptr; continue; } - tile.m2() = d->index; - if (IsTileType(tile, MP_WATER)) Tile(GetOtherShipDepotTile(tile)).m2() = d->index; + tile.m2() = d->index.base(); + if (IsTileType(tile, MP_WATER)) Tile(GetOtherShipDepotTile(tile)).m2() = d->index.base(); } } diff --git a/src/water_map.h b/src/water_map.h index 82d532de88..b3a7a335f3 100644 --- a/src/water_map.h +++ b/src/water_map.h @@ -461,7 +461,7 @@ inline void MakeShipDepot(Tile t, Owner o, DepotID did, DepotPart part, Axis a, SetTileOwner(t, o); SetWaterClass(t, original_water_class); SetDockingTile(t, false); - t.m2() = did; + t.m2() = did.base(); t.m3() = 0; t.m4() = 0; t.m5() = part << WBL_DEPOT_PART | a << WBL_DEPOT_AXIS;