# HG changeset patch # User Patric Stout # Date 2023-08-15 16:12:05 # Node ID ab7d36d1026410a69e991e4a39aa12f49a99b600 # Parent d7f49906d8cf3789218c970a4f713ebb9c0caf21 Codechange: make explicit when a TileIndex is cast to its basetype (#11190) This prevents people accidentially assigning a TileIndex to a Date or any other type they shouldn't. diff --git a/src/build_vehicle_gui.cpp b/src/build_vehicle_gui.cpp --- a/src/build_vehicle_gui.cpp +++ b/src/build_vehicle_gui.cpp @@ -1177,7 +1177,7 @@ struct BuildVehicleWindow : Window { { this->vehicle_type = type; this->listview_mode = tile == INVALID_TILE; - this->window_number = this->listview_mode ? (int)type : (int)tile; + this->window_number = this->listview_mode ? (int)type : static_cast(tile); this->sel_engine = INVALID_ENGINE; @@ -1212,7 +1212,11 @@ struct BuildVehicleWindow : Window { this->details_height = ((this->vehicle_type == VEH_TRAIN) ? 10 : 9); - this->FinishInitNested(tile == INVALID_TILE ? (int)type : (int)tile); + if (tile == INVALID_TILE) { + this->FinishInitNested(type); + } else { + this->FinishInitNested(tile); + } this->querystrings[WID_BV_FILTER] = &this->vehicle_editbox; this->vehicle_editbox.cancel_button = QueryString::ACTION_CLEAR; @@ -1891,7 +1895,7 @@ void ShowBuildVehicleWindow(TileIndex ti * so if tile == INVALID_TILE (Available XXX Window), use 'type' as unique number. * As it always is a low value, it won't collide with any real tile * number. */ - uint num = (tile == INVALID_TILE) ? (int)type : (int)tile; + uint num = (tile == INVALID_TILE) ? (int)type : static_cast(tile); assert(IsCompanyBuildableVehicleType(type)); diff --git a/src/company_cmd.cpp b/src/company_cmd.cpp --- a/src/company_cmd.cpp +++ b/src/company_cmd.cpp @@ -389,7 +389,7 @@ set_name:; SetDParam(1, STR_NEWS_COMPANY_LAUNCH_DESCRIPTION); SetDParamStr(2, cni->company_name); SetDParam(3, t->index); - AddNewsItem(STR_MESSAGE_NEWS_FORMAT, NT_COMPANY_INFO, NF_COMPANY, NR_TILE, c->last_build_coordinate, NR_NONE, UINT32_MAX, cni); + AddNewsItem(STR_MESSAGE_NEWS_FORMAT, NT_COMPANY_INFO, NF_COMPANY, NR_TILE, static_cast(c->last_build_coordinate), NR_NONE, UINT32_MAX, cni); } return; } diff --git a/src/core/strong_typedef_type.hpp b/src/core/strong_typedef_type.hpp --- a/src/core/strong_typedef_type.hpp +++ b/src/core/strong_typedef_type.hpp @@ -133,33 +133,6 @@ namespace StrongType { }; /** - * Mix-in which makes the new Typedef implicitly convertible to its base type. - * - * Be careful: when allowing implicit conversion, you won't notice if this type is assigned to a compatible, but different, type. - * For example: - * - * StrongType::Typedef a = 1; - * StrongType::Typedef b = 2; - * a = b; // OK - */ - struct Implicit { - template - struct mixin { - constexpr operator TBaseType () const { return static_cast(*this).value; } - }; - }; - - /** - * Mix-in which makes the new Typedef explicitly convertible to its base type. - */ - struct Explicit { - template - struct mixin { - explicit constexpr operator TBaseType () const { return static_cast(*this).value; } - }; - }; - - /** * Templated helper to make a type-safe 'typedef' representing a single POD value. * A normal 'typedef' is not distinct from its base type and will be treated as * identical in many contexts. This class provides a distinct type that can still @@ -187,9 +160,10 @@ namespace StrongType { constexpr Typedef &operator =(Typedef &&rhs) { this->value = std::move(rhs.value); return *this; } constexpr Typedef &operator =(const TBaseType &rhs) { this->value = rhs; return *this; } + /* Only allow explicit conversions to BaseType. */ + explicit constexpr operator TBaseType () const { return this->value; } + /* Only allow TProperties classes access to the internal value. Everyone else needs to do an explicit cast. */ - friend struct Explicit; - friend struct Implicit; friend struct Compare; friend struct Integer; template friend struct Compatible; diff --git a/src/disaster_vehicle.cpp b/src/disaster_vehicle.cpp --- a/src/disaster_vehicle.cpp +++ b/src/disaster_vehicle.cpp @@ -358,7 +358,7 @@ static bool DisasterTick_Ufo(DisasterVeh return true; } else { /* Target a vehicle */ - RoadVehicle *u = RoadVehicle::Get(v->dest_tile); + RoadVehicle *u = RoadVehicle::Get(static_cast(v->dest_tile)); assert(u != nullptr && u->type == VEH_ROAD && u->IsFrontEngine()); uint dist = Delta(v->x_pos, u->x_pos) + Delta(v->y_pos, u->y_pos); @@ -437,7 +437,7 @@ static bool DisasterTick_Aircraft(Disast if (v->state == 2) { if (GB(v->tick_counter, 0, 2) == 0) { - Industry *i = Industry::Get(v->dest_tile); // Industry destructor calls ReleaseDisastersTargetingIndustry, so this is valid + Industry *i = Industry::Get(static_cast(v->dest_tile)); // Industry destructor calls ReleaseDisastersTargetingIndustry, so this is valid int x = TileX(i->location.tile) * TILE_SIZE; int y = TileY(i->location.tile) * TILE_SIZE; uint32_t r = Random(); @@ -455,7 +455,7 @@ static bool DisasterTick_Aircraft(Disast v->state = 2; v->age = 0; - Industry *i = Industry::Get(v->dest_tile); // Industry destructor calls ReleaseDisastersTargetingIndustry, so this is valid + Industry *i = Industry::Get(static_cast(v->dest_tile)); // Industry destructor calls ReleaseDisastersTargetingIndustry, so this is valid DestructIndustry(i); SetDParam(0, i->town->index); diff --git a/src/industry_cmd.cpp b/src/industry_cmd.cpp --- a/src/industry_cmd.cpp +++ b/src/industry_cmd.cpp @@ -151,7 +151,7 @@ Industry::~Industry() for (TileIndex tile_cur : this->location) { if (IsTileType(tile_cur, MP_INDUSTRY)) { if (GetIndustryIndex(tile_cur) == this->index) { - DeleteNewGRFInspectWindow(GSF_INDUSTRYTILES, tile_cur); + DeleteNewGRFInspectWindow(GSF_INDUSTRYTILES, static_cast(tile_cur)); /* MakeWaterKeepingClass() can also handle 'land' */ MakeWaterKeepingClass(tile_cur, OWNER_NONE); diff --git a/src/map_func.h b/src/map_func.h --- a/src/map_func.h +++ b/src/map_func.h @@ -77,7 +77,7 @@ public: /** * Implicit conversion to the uint for bounds checking. */ - debug_inline constexpr operator uint() const { return tile; } + debug_inline constexpr operator uint() const { return static_cast(tile); } /** * The type (bits 4..7), bridges (2..3), rainforest/desert (0..1) @@ -88,7 +88,7 @@ public: */ debug_inline byte &type() { - return base_tiles[tile].type; + return base_tiles[static_cast(tile)].type; } /** @@ -100,7 +100,7 @@ public: */ debug_inline byte &height() { - return base_tiles[tile].height; + return base_tiles[static_cast(tile)].height; } /** @@ -112,7 +112,7 @@ public: */ debug_inline byte &m1() { - return base_tiles[tile].m1; + return base_tiles[static_cast(tile)].m1; } /** @@ -124,7 +124,7 @@ public: */ debug_inline uint16_t &m2() { - return base_tiles[tile].m2; + return base_tiles[static_cast(tile)].m2; } /** @@ -136,7 +136,7 @@ public: */ debug_inline byte &m3() { - return base_tiles[tile].m3; + return base_tiles[static_cast(tile)].m3; } /** @@ -148,7 +148,7 @@ public: */ debug_inline byte &m4() { - return base_tiles[tile].m4; + return base_tiles[static_cast(tile)].m4; } /** @@ -160,7 +160,7 @@ public: */ debug_inline byte &m5() { - return base_tiles[tile].m5; + return base_tiles[static_cast(tile)].m5; } /** @@ -172,7 +172,7 @@ public: */ debug_inline byte &m6() { - return extended_tiles[tile].m6; + return extended_tiles[static_cast(tile)].m6; } /** @@ -184,7 +184,7 @@ public: */ debug_inline byte &m7() { - return extended_tiles[tile].m7; + return extended_tiles[static_cast(tile)].m7; } /** @@ -196,7 +196,7 @@ public: */ debug_inline uint16_t &m8() { - return extended_tiles[tile].m8; + return extended_tiles[static_cast(tile)].m8; } }; @@ -314,9 +314,9 @@ public: * It does this by masking the 'high' bits of. * @param tile the tile to 'wrap' */ - static inline TileIndex WrapToMap(uint tile) + static inline TileIndex WrapToMap(TileIndex tile) { - return tile & Map::tile_mask; + return static_cast(tile) & Map::tile_mask; } /** diff --git a/src/misc/dbg_helpers.cpp b/src/misc/dbg_helpers.cpp --- a/src/misc/dbg_helpers.cpp +++ b/src/misc/dbg_helpers.cpp @@ -63,7 +63,7 @@ std::string ValueStr(SignalType t) std::string TileStr(TileIndex tile) { std::stringstream ss; - ss << "0x" << std::setfill('0') << std::setw(4) << std::hex << tile; // 0x%04X + ss << "0x" << std::setfill('0') << std::setw(4) << std::hex << static_cast(tile); // 0x%04X ss << " (" << TileX(tile) << ", " << TileY(tile) << ")"; return ss.str(); } diff --git a/src/misc_gui.cpp b/src/misc_gui.cpp --- a/src/misc_gui.cpp +++ b/src/misc_gui.cpp @@ -207,7 +207,7 @@ public: /* Location */ std::stringstream tile_ss; - tile_ss << "0x" << std::setfill('0') << std::setw(4) << std::hex << std::uppercase << tile; // 0x%.4X + tile_ss << "0x" << std::setfill('0') << std::setw(4) << std::hex << std::uppercase << static_cast(tile); // 0x%.4X SetDParam(0, TileX(tile)); SetDParam(1, TileY(tile)); @@ -331,12 +331,12 @@ public: bool IsNewGRFInspectable() const override { - return ::IsNewGRFInspectable(GetGrfSpecFeature(this->tile), this->tile); + return ::IsNewGRFInspectable(GetGrfSpecFeature(this->tile), static_cast(this->tile)); } void ShowNewGRFInspectWindow() const override { - ::ShowNewGRFInspectWindow(GetGrfSpecFeature(this->tile), this->tile); + ::ShowNewGRFInspectWindow(GetGrfSpecFeature(this->tile), static_cast(this->tile)); } void OnClick(Point pt, int widget, int click_count) override diff --git a/src/newgrf_industries.cpp b/src/newgrf_industries.cpp --- a/src/newgrf_industries.cpp +++ b/src/newgrf_industries.cpp @@ -162,7 +162,7 @@ static uint32_t GetCountAndDistanceOfClo /* Variables available during construction check. */ switch (variable) { - case 0x80: return this->tile; + case 0x80: return static_cast(this->tile); case 0x81: return GB(static_cast(this->tile), 8, 8); /* Pointer to the town the industry is associated with */ @@ -347,7 +347,7 @@ static uint32_t GetCountAndDistanceOfClo case 0x7C: return (this->industry->psa != nullptr) ? this->industry->psa->GetValue(parameter) : 0; /* Industry structure access*/ - case 0x80: return this->industry->location.tile; + case 0x80: return static_cast(this->industry->location.tile); case 0x81: return GB(static_cast(this->industry->location.tile), 8, 8); /* Pointer to the town the industry is associated with */ case 0x82: return this->industry->town->index; diff --git a/src/newgrf_object.cpp b/src/newgrf_object.cpp --- a/src/newgrf_object.cpp +++ b/src/newgrf_object.cpp @@ -305,7 +305,7 @@ static uint32_t GetCountAndDistanceOfClo switch (variable) { /* Relative position. */ case 0x40: { - uint offset = this->tile - this->obj->location.tile; + TileIndex offset = this->tile - this->obj->location.tile; uint offset_x = TileX(offset); uint offset_y = TileY(offset); return offset_y << 20 | offset_x << 16 | offset_y << 8 | offset_x; diff --git a/src/newgrf_station.cpp b/src/newgrf_station.cpp --- a/src/newgrf_station.cpp +++ b/src/newgrf_station.cpp @@ -649,7 +649,7 @@ uint16_t GetStationCallback(CallbackID c */ CommandCost PerformStationTileSlopeCheck(TileIndex north_tile, TileIndex cur_tile, const StationSpec *statspec, Axis axis, byte plat_len, byte numtracks) { - TileIndexDiff diff = cur_tile - north_tile; + TileIndex diff = cur_tile - north_tile; Slope slope = GetTileSlope(cur_tile); StationResolverObject object(statspec, nullptr, cur_tile, CBID_STATION_LAND_SLOPE_CHECK, diff --git a/src/newgrf_town.cpp b/src/newgrf_town.cpp --- a/src/newgrf_town.cpp +++ b/src/newgrf_town.cpp @@ -43,7 +43,7 @@ } /* Town properties */ - case 0x80: return this->t->xy; + case 0x80: return static_cast(this->t->xy); case 0x81: return GB(static_cast(this->t->xy), 8, 8); case 0x82: return ClampTo(this->t->cache.population); case 0x83: return GB(ClampTo(this->t->cache.population), 8, 8); diff --git a/src/news_func.h b/src/news_func.h --- a/src/news_func.h +++ b/src/news_func.h @@ -44,7 +44,7 @@ static inline void AddVehicleAdviceNewsI static inline void AddTileNewsItem(StringID string, NewsType type, TileIndex tile, const NewsAllocatedData *data = nullptr, StationID station = INVALID_STATION) { - AddNewsItem(string, type, NF_NO_TRANSPARENT | NF_SHADE | NF_THIN, NR_TILE, tile, station == INVALID_STATION ? NR_NONE : NR_STATION, station, data); + AddNewsItem(string, type, NF_NO_TRANSPARENT | NF_SHADE | NF_THIN, NR_TILE, static_cast(tile), station == INVALID_STATION ? NR_NONE : NR_STATION, station, data); } static inline void AddIndustryNewsItem(StringID string, NewsType type, IndustryID industry) diff --git a/src/object_cmd.cpp b/src/object_cmd.cpp --- a/src/object_cmd.cpp +++ b/src/object_cmd.cpp @@ -511,7 +511,7 @@ static void ReallyClearObjectTile(Object { Object::DecTypeCount(o->type); for (TileIndex tile_cur : o->location) { - DeleteNewGRFInspectWindow(GSF_OBJECTS, tile_cur); + DeleteNewGRFInspectWindow(GSF_OBJECTS, static_cast(tile_cur)); MakeWaterKeepingClass(tile_cur, GetTileOwner(tile_cur)); } diff --git a/src/pathfinder/yapf/yapf_node_rail.hpp b/src/pathfinder/yapf/yapf_node_rail.hpp --- a/src/pathfinder/yapf/yapf_node_rail.hpp +++ b/src/pathfinder/yapf/yapf_node_rail.hpp @@ -27,7 +27,7 @@ struct CYapfRailSegmentKey inline void Set(const CYapfNodeKeyTrackDir &node_key) { - m_value = (((int)node_key.m_tile) << 4) | node_key.m_td; + m_value = (static_cast(node_key.m_tile) << 4) | node_key.m_td; } inline int32_t CalcHash() const diff --git a/src/rail_cmd.cpp b/src/rail_cmd.cpp --- a/src/rail_cmd.cpp +++ b/src/rail_cmd.cpp @@ -662,7 +662,7 @@ CommandCost CmdRemoveSingleRail(DoComman Company::Get(owner)->infrastructure.rail[GetRailType(tile)] -= LEVELCROSSING_TRACKBIT_FACTOR; DirtyCompanyInfrastructureWindows(owner); MakeRoadNormal(tile, GetCrossingRoadBits(tile), GetRoadTypeRoad(tile), GetRoadTypeTram(tile), GetTownIndex(tile), GetRoadOwner(tile, RTT_ROAD), GetRoadOwner(tile, RTT_TRAM)); - DeleteNewGRFInspectWindow(GSF_RAILTYPES, tile); + DeleteNewGRFInspectWindow(GSF_RAILTYPES, static_cast(tile)); } break; } @@ -720,7 +720,7 @@ CommandCost CmdRemoveSingleRail(DoComman } else { DoClearSquare(tile); } - DeleteNewGRFInspectWindow(GSF_RAILTYPES, tile); + DeleteNewGRFInspectWindow(GSF_RAILTYPES, static_cast(tile)); } else { SetTrackBits(tile, present); SetTrackReservation(tile, GetRailReservationTrackBits(tile) & present); diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -2143,7 +2143,7 @@ bool AfterLoadGame() /* Delete small ufos heading for non-existing vehicles */ for (DisasterVehicle *v : DisasterVehicle::Iterate()) { if (v->subtype == 2 /* ST_SMALL_UFO */ && v->state != 0) { - const Vehicle *u = Vehicle::GetIfValid(v->dest_tile); + const Vehicle *u = Vehicle::GetIfValid(static_cast(v->dest_tile)); if (u == nullptr || u->type != VEH_ROAD || !RoadVehicle::From(u)->IsFrontEngine()) { delete v; } diff --git a/src/saveload/map_sl.cpp b/src/saveload/map_sl.cpp --- a/src/saveload/map_sl.cpp +++ b/src/saveload/map_sl.cpp @@ -72,7 +72,7 @@ struct MAPTChunkHandler : ChunkHandler { void Load() const override { std::array buf; - TileIndex size = Map::Size(); + uint size = Map::Size(); for (TileIndex i = 0; i != size;) { SlCopy(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT8); @@ -83,7 +83,7 @@ struct MAPTChunkHandler : ChunkHandler { void Save() const override { std::array buf; - TileIndex size = Map::Size(); + uint size = Map::Size(); SlSetLength(size); for (TileIndex i = 0; i != size;) { @@ -99,7 +99,7 @@ struct MAPHChunkHandler : ChunkHandler { void Load() const override { std::array buf; - TileIndex size = Map::Size(); + uint size = Map::Size(); for (TileIndex i = 0; i != size;) { SlCopy(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT8); @@ -110,7 +110,7 @@ struct MAPHChunkHandler : ChunkHandler { void Save() const override { std::array buf; - TileIndex size = Map::Size(); + uint size = Map::Size(); SlSetLength(size); for (TileIndex i = 0; i != size;) { @@ -126,7 +126,7 @@ struct MAPOChunkHandler : ChunkHandler { void Load() const override { std::array buf; - TileIndex size = Map::Size(); + uint size = Map::Size(); for (TileIndex i = 0; i != size;) { SlCopy(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT8); @@ -137,7 +137,7 @@ struct MAPOChunkHandler : ChunkHandler { void Save() const override { std::array buf; - TileIndex size = Map::Size(); + uint size = Map::Size(); SlSetLength(size); for (TileIndex i = 0; i != size;) { @@ -153,7 +153,7 @@ struct MAP2ChunkHandler : ChunkHandler { void Load() const override { std::array buf; - TileIndex size = Map::Size(); + uint size = Map::Size(); for (TileIndex i = 0; i != size;) { SlCopy(buf.data(), MAP_SL_BUF_SIZE, @@ -167,7 +167,7 @@ struct MAP2ChunkHandler : ChunkHandler { void Save() const override { std::array buf; - TileIndex size = Map::Size(); + uint size = Map::Size(); SlSetLength(static_cast(size) * sizeof(uint16_t)); for (TileIndex i = 0; i != size;) { @@ -183,7 +183,7 @@ struct M3LOChunkHandler : ChunkHandler { void Load() const override { std::array buf; - TileIndex size = Map::Size(); + uint size = Map::Size(); for (TileIndex i = 0; i != size;) { SlCopy(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT8); @@ -194,7 +194,7 @@ struct M3LOChunkHandler : ChunkHandler { void Save() const override { std::array buf; - TileIndex size = Map::Size(); + uint size = Map::Size(); SlSetLength(size); for (TileIndex i = 0; i != size;) { @@ -210,7 +210,7 @@ struct M3HIChunkHandler : ChunkHandler { void Load() const override { std::array buf; - TileIndex size = Map::Size(); + uint size = Map::Size(); for (TileIndex i = 0; i != size;) { SlCopy(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT8); @@ -221,7 +221,7 @@ struct M3HIChunkHandler : ChunkHandler { void Save() const override { std::array buf; - TileIndex size = Map::Size(); + uint size = Map::Size(); SlSetLength(size); for (TileIndex i = 0; i != size;) { @@ -237,7 +237,7 @@ struct MAP5ChunkHandler : ChunkHandler { void Load() const override { std::array buf; - TileIndex size = Map::Size(); + uint size = Map::Size(); for (TileIndex i = 0; i != size;) { SlCopy(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT8); @@ -248,7 +248,7 @@ struct MAP5ChunkHandler : ChunkHandler { void Save() const override { std::array buf; - TileIndex size = Map::Size(); + uint size = Map::Size(); SlSetLength(size); for (TileIndex i = 0; i != size;) { @@ -264,7 +264,7 @@ struct MAPEChunkHandler : ChunkHandler { void Load() const override { std::array buf; - TileIndex size = Map::Size(); + uint size = Map::Size(); if (IsSavegameVersionBefore(SLV_42)) { for (TileIndex i = 0; i != size;) { @@ -288,7 +288,7 @@ struct MAPEChunkHandler : ChunkHandler { void Save() const override { std::array buf; - TileIndex size = Map::Size(); + uint size = Map::Size(); SlSetLength(size); for (TileIndex i = 0; i != size;) { @@ -304,7 +304,7 @@ struct MAP7ChunkHandler : ChunkHandler { void Load() const override { std::array buf; - TileIndex size = Map::Size(); + uint size = Map::Size(); for (TileIndex i = 0; i != size;) { SlCopy(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT8); @@ -315,7 +315,7 @@ struct MAP7ChunkHandler : ChunkHandler { void Save() const override { std::array buf; - TileIndex size = Map::Size(); + uint size = Map::Size(); SlSetLength(size); for (TileIndex i = 0; i != size;) { @@ -331,7 +331,7 @@ struct MAP8ChunkHandler : ChunkHandler { void Load() const override { std::array buf; - TileIndex size = Map::Size(); + uint size = Map::Size(); for (TileIndex i = 0; i != size;) { SlCopy(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT16); @@ -342,7 +342,7 @@ struct MAP8ChunkHandler : ChunkHandler { void Save() const override { std::array buf; - TileIndex size = Map::Size(); + uint size = Map::Size(); SlSetLength(static_cast(size) * sizeof(uint16_t)); for (TileIndex i = 0; i != size;) { diff --git a/src/script/api/script_bridge.cpp b/src/script/api/script_bridge.cpp --- a/src/script/api/script_bridge.cpp +++ b/src/script/api/script_bridge.cpp @@ -83,8 +83,8 @@ static void _DoCommandReturnBuildBridge1 switch (vehicle_type) { case ScriptVehicle::VT_ROAD: - ScriptObject::SetCallbackVariable(0, start); - ScriptObject::SetCallbackVariable(1, end); + ScriptObject::SetCallbackVariable(0, static_cast(start)); + ScriptObject::SetCallbackVariable(1, static_cast(end)); return ScriptObject::Command::Do(&::_DoCommandReturnBuildBridge1, end, start, TRANSPORT_ROAD, bridge_id, ScriptRoad::GetCurrentRoadType()); case ScriptVehicle::VT_RAIL: return ScriptObject::Command::Do(end, start, TRANSPORT_RAIL, bridge_id, ScriptRail::GetCurrentRailType()); diff --git a/src/script/api/script_depotlist.cpp b/src/script/api/script_depotlist.cpp --- a/src/script/api/script_depotlist.cpp +++ b/src/script/api/script_depotlist.cpp @@ -30,7 +30,7 @@ ScriptDepotList::ScriptDepotList(ScriptT for (const Station *st : Station::Iterate()) { if (st->owner == ScriptObject::GetCompany() || ScriptCompanyMode::IsDeity()) { for (uint i = 0; i < st->airport.GetNumHangars(); i++) { - this->AddItem(st->airport.GetHangarTile(i)); + this->AddItem(static_cast(st->airport.GetHangarTile(i))); } } } @@ -40,6 +40,6 @@ ScriptDepotList::ScriptDepotList(ScriptT /* Handle 'standard' depots. */ for (const Depot *depot : Depot::Iterate()) { - if ((::GetTileOwner(depot->xy) == ScriptObject::GetCompany() || ScriptCompanyMode::IsDeity()) && ::IsTileType(depot->xy, tile_type)) this->AddItem(depot->xy); + if ((::GetTileOwner(depot->xy) == ScriptObject::GetCompany() || ScriptCompanyMode::IsDeity()) && ::IsTileType(depot->xy, tile_type)) this->AddItem(static_cast(depot->xy)); } } diff --git a/src/script/api/script_map.hpp b/src/script/api/script_map.hpp --- a/src/script/api/script_map.hpp +++ b/src/script/api/script_map.hpp @@ -19,7 +19,7 @@ */ class ScriptMap : public ScriptObject { public: - static const int TILE_INVALID = (int)INVALID_TILE; ///< Invalid TileIndex. + static const int TILE_INVALID = static_cast(INVALID_TILE); ///< Invalid TileIndex. /** * Checks whether the given tile is valid. diff --git a/src/script/api/script_road.cpp b/src/script/api/script_road.cpp --- a/src/script/api/script_road.cpp +++ b/src/script/api/script_road.cpp @@ -382,8 +382,8 @@ static bool NormaliseTileOffset(int32_t /* static */ SQInteger ScriptRoad::CanBuildConnectedRoadParts(ScriptTile::Slope slope_, Array<> &&existing, TileIndex start_, TileIndex end_) { ::Slope slope = (::Slope)slope_; - int32_t start = start_; - int32_t end = end_; + int32_t start = static_cast(start_); + int32_t end = static_cast(end_); /* The start tile and end tile cannot be the same tile either. */ if (start == end) return -1; diff --git a/src/script/api/script_tilelist.cpp b/src/script/api/script_tilelist.cpp --- a/src/script/api/script_tilelist.cpp +++ b/src/script/api/script_tilelist.cpp @@ -21,14 +21,14 @@ void ScriptTileList::AddRectangle(TileIn if (!::IsValidTile(t2)) return; TileArea ta(t1, t2); - for (TileIndex t : ta) this->AddItem(t); + for (TileIndex t : ta) this->AddItem(static_cast(t)); } void ScriptTileList::AddTile(TileIndex tile) { if (!::IsValidTile(tile)) return; - this->AddItem(tile); + this->AddItem(static_cast(tile)); } void ScriptTileList::RemoveRectangle(TileIndex t1, TileIndex t2) @@ -37,14 +37,14 @@ void ScriptTileList::RemoveRectangle(Til if (!::IsValidTile(t2)) return; TileArea ta(t1, t2); - for (TileIndex t : ta) this->RemoveItem(t); + for (TileIndex t : ta) this->RemoveItem(static_cast(t)); } void ScriptTileList::RemoveTile(TileIndex tile) { if (!::IsValidTile(tile)) return; - this->RemoveItem(tile); + this->RemoveItem(static_cast(tile)); } /** diff --git a/src/script/api/script_tunnel.cpp b/src/script/api/script_tunnel.cpp --- a/src/script/api/script_tunnel.cpp +++ b/src/script/api/script_tunnel.cpp @@ -93,7 +93,7 @@ static void _DoCommandReturnBuildTunnel1 /* For rail we do nothing special */ return ScriptObject::Command::Do(start, TRANSPORT_RAIL, ScriptRail::GetCurrentRailType()); } else { - ScriptObject::SetCallbackVariable(0, start); + ScriptObject::SetCallbackVariable(0, static_cast(start)); return ScriptObject::Command::Do(&::_DoCommandReturnBuildTunnel1, start, TRANSPORT_ROAD, ScriptRoad::GetCurrentRoadType()); } } diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp --- a/src/station_cmd.cpp +++ b/src/station_cmd.cpp @@ -1651,7 +1651,7 @@ CommandCost RemoveFromRailBaseStation(Ti if (!build_rail && !IsStationTileBlocked(tile)) Company::Get(owner)->infrastructure.rail[rt]--; DoClearSquare(tile); - DeleteNewGRFInspectWindow(GSF_STATIONS, tile); + DeleteNewGRFInspectWindow(GSF_STATIONS, static_cast(tile)); if (build_rail) MakeRailNormal(tile, owner, TrackToTrackBits(track), rt); Company::Get(owner)->infrastructure.station--; DirtyCompanyInfrastructureWindows(owner); @@ -2149,7 +2149,7 @@ static CommandCost RemoveRoadStop(TileIn uint specindex = GetCustomRoadStopSpecIndex(tile); - DeleteNewGRFInspectWindow(GSF_ROADSTOPS, tile); + DeleteNewGRFInspectWindow(GSF_ROADSTOPS, static_cast(tile)); if (IsDriveThroughStopTile(tile)) { /* Clears the tile for us */ @@ -2533,7 +2533,7 @@ static CommandCost RemoveAirport(TileInd if (flags & DC_EXEC) { DeleteAnimatedTile(tile_cur); DoClearSquare(tile_cur); - DeleteNewGRFInspectWindow(GSF_AIRPORTTILES, tile_cur); + DeleteNewGRFInspectWindow(GSF_AIRPORTTILES, static_cast(tile_cur)); } } diff --git a/src/story.cpp b/src/story.cpp --- a/src/story.cpp +++ b/src/story.cpp @@ -99,7 +99,7 @@ static void UpdateElement(StoryPageEleme break; case SPET_LOCATION: pe.text = text; - pe.referenced_id = tile; + pe.referenced_id = static_cast(tile); break; case SPET_GOAL: pe.referenced_id = (GoalID)reference; diff --git a/src/tile_type.h b/src/tile_type.h --- a/src/tile_type.h +++ b/src/tile_type.h @@ -84,7 +84,7 @@ enum TropicZone { * * It is compatible with int32 / int64 for easy math throughout the code. */ -using TileIndex = StrongType::Typedef, StrongType::Compatible>; +using TileIndex = StrongType::Typedef, StrongType::Compatible>; /* Make sure the size is as expected. */ static_assert(sizeof(TileIndex) == 4); diff --git a/src/timer/timer_game_calendar.h b/src/timer/timer_game_calendar.h --- a/src/timer/timer_game_calendar.h +++ b/src/timer/timer_game_calendar.h @@ -78,13 +78,13 @@ public: }; /** The type to store our dates in. */ - using Date = StrongType::Typedef; + using Date = StrongType::Typedef; /** The fraction of a date we're in, i.e. the number of ticks since the last date changeover. */ using DateFract = uint16_t; /** Type for the year, note: 0 based, i.e. starts at the year 0. */ - using Year = StrongType::Typedef; + using Year = StrongType::Typedef; /** Type for the month, note: 0 based, i.e. 0 = January, 11 = December. */ using Month = uint8_t; /** Type for the day of the month, note: 1 based, first day of a month is 1. */ diff --git a/src/town_cmd.cpp b/src/town_cmd.cpp --- a/src/town_cmd.cpp +++ b/src/town_cmd.cpp @@ -2714,7 +2714,7 @@ static void DoClearTownHouseHelper(TileI DoClearSquare(tile); DeleteAnimatedTile(tile); - DeleteNewGRFInspectWindow(GSF_HOUSES, tile); + DeleteNewGRFInspectWindow(GSF_HOUSES, static_cast(tile)); } /** diff --git a/src/window_func.h b/src/window_func.h --- a/src/window_func.h +++ b/src/window_func.h @@ -13,12 +13,19 @@ #include "window_type.h" #include "company_type.h" #include "core/geometry_type.hpp" +#include "core/strong_typedef_type.hpp" Window *FindWindowById(WindowClass cls, WindowNumber number); Window *FindWindowByClass(WindowClass cls); Window *GetMainWindow(); void ChangeWindowOwner(Owner old_owner, Owner new_owner); +template::value, int> = 0> +Window *FindWindowById(WindowClass cls, T number) +{ + return FindWindowById(cls, static_cast(number)); +} + void ResizeWindow(Window *w, int x, int y, bool clamp_to_screen = true); int PositionMainToolbar(Window *w); int PositionStatusbar(Window *w); @@ -37,6 +44,12 @@ void InputLoop(); void InvalidateWindowData(WindowClass cls, WindowNumber number, int data = 0, bool gui_scope = false); void InvalidateWindowClassesData(WindowClass cls, int data = 0, bool gui_scope = false); +template::value, int> = 0> +void InvalidateWindowData(WindowClass cls, T number, int data = 0, bool gui_scope = false) +{ + InvalidateWindowData(cls, static_cast(number), data, gui_scope); +} + void CloseNonVitalWindows(); void CloseAllNonVitalWindows(); void DeleteAllMessages(); @@ -54,9 +67,21 @@ void SetWindowWidgetDirty(WindowClass cl void SetWindowDirty(WindowClass cls, WindowNumber number); void SetWindowClassesDirty(WindowClass cls); +template::value, int> = 0> +void SetWindowDirty(WindowClass cls, T number) +{ + SetWindowDirty(cls, static_cast(number)); +} + void CloseWindowById(WindowClass cls, WindowNumber number, bool force = true); void CloseWindowByClass(WindowClass cls); +template::value, int> = 0> +void CloseWindowById(WindowClass cls, T number, bool force = true) +{ + CloseWindowById(cls, static_cast(number), force); +} + bool EditBoxInGlobalFocus(); bool FocusedWindowIsConsole(); Point GetCaretPosition(); diff --git a/src/window_gui.h b/src/window_gui.h --- a/src/window_gui.h +++ b/src/window_gui.h @@ -284,6 +284,12 @@ public: void CreateNestedTree(bool fill_nested = true); void FinishInitNested(WindowNumber window_number = 0); + template::value, int> = 0> + void FinishInitNested(T number) + { + this->FinishInitNested(static_cast(number)); + } + /** * Set the timeout flag of the window and initiate the timer. */ @@ -880,6 +886,12 @@ public: Window *BringWindowToFrontById(WindowClass cls, WindowNumber number); Window *FindWindowFromPt(int x, int y); +template::value, int> = 0> +Window *BringWindowToFrontById(WindowClass cls, T number) +{ + return BringWindowToFrontById(cls, static_cast(number)); +} + /** * Open a new window. * @tparam Wcls %Window class to use if the window does not exist.