diff --git a/src/aircraft.h b/src/aircraft.h index 624750050c..ee3c814c5d 100644 --- a/src/aircraft.h +++ b/src/aircraft.h @@ -82,8 +82,7 @@ struct Aircraft final : public SpecializedVehicle { AircraftCache acache{}; - /** We don't want GCC to zero our struct! It already is zeroed and has an index! */ - Aircraft() : SpecializedVehicleBase() {} + Aircraft(VehicleID index) : SpecializedVehicleBase(index) {} /** We want to 'destruct' the right class. */ ~Aircraft() override { this->PreDestructor(); } diff --git a/src/autoreplace_base.h b/src/autoreplace_base.h index 50bdc9c8b9..28e92678ae 100644 --- a/src/autoreplace_base.h +++ b/src/autoreplace_base.h @@ -37,9 +37,9 @@ struct EngineRenew : EngineRenewPool::PoolItem<&_enginerenew_pool> { GroupID group_id = GroupID::Invalid(); bool replace_when_old = false; ///< Do replacement only when vehicle is old. - EngineRenew() {} - EngineRenew(EngineID from, EngineID to, GroupID group_id, bool replace_when_old, EngineRenew *next) : - from(from), to(to), next(next), group_id(group_id), replace_when_old(replace_when_old) {} + EngineRenew(EngineRenewID index) : EngineRenewPool::PoolItem<&_enginerenew_pool>(index) {} + EngineRenew(EngineRenewID index, EngineID from, EngineID to, GroupID group_id, bool replace_when_old, EngineRenew *next) : + EngineRenewPool::PoolItem<&_enginerenew_pool>(index), from(from), to(to), next(next), group_id(group_id), replace_when_old(replace_when_old) {} ~EngineRenew() {} }; diff --git a/src/base_station_base.h b/src/base_station_base.h index b19607ca50..a01b6a8179 100644 --- a/src/base_station_base.h +++ b/src/base_station_base.h @@ -87,9 +87,10 @@ struct BaseStation : StationPool::PoolItem<&_station_pool> { /** * Initialize the base station. + * @param index The index of the station within the pool. * @param tile The location of the station sign */ - BaseStation(TileIndex tile) : xy(tile) {} + BaseStation(StationID index, TileIndex tile) : StationPool::PoolItem<&_station_pool>(index), xy(tile) {} virtual ~BaseStation(); @@ -213,10 +214,11 @@ struct SpecializedStation : public BaseStation { /** * Set station type correctly + * @param index The index within the station pool. * @param tile The base tile of the station. */ - inline SpecializedStation(TileIndex tile) : - BaseStation(tile) + inline SpecializedStation(StationID index, TileIndex tile) : + BaseStation(index, tile) { this->facilities = EXPECTED_FACIL; } diff --git a/src/cargopacket.cpp b/src/cargopacket.cpp index f2bf4253cf..99f4c43449 100644 --- a/src/cargopacket.cpp +++ b/src/cargopacket.cpp @@ -23,20 +23,23 @@ INSTANTIATE_POOL_METHODS(CargoPacket) /** * Create a new packet for savegame loading. + * @param index Index into the cargo packet pool. */ -CargoPacket::CargoPacket() +CargoPacket::CargoPacket(CargoPacketID index) : CargoPacketPool::PoolItem<&_cargopacket_pool>(index) { } /** * Creates a new cargo packet. * + * @param index Index into the cargo packet pool. * @param first_station Source station of the packet. * @param count Number of cargo entities to put in this packet. * @param source Source of the packet (for subsidies). * @pre count != 0 */ -CargoPacket::CargoPacket(StationID first_station,uint16_t count, Source source) : +CargoPacket::CargoPacket(CargoPacketID index, StationID first_station,uint16_t count, Source source) : + CargoPacketPool::PoolItem<&_cargopacket_pool>(index), count(count), source(source), first_station(first_station) @@ -47,13 +50,15 @@ CargoPacket::CargoPacket(StationID first_station,uint16_t count, Source source) /** * Create a new cargo packet. Used for older savegames to load in their partial data. * + * @param index Index into the cargo packet pool. * @param count Number of cargo entities to put in this packet. * @param periods_in_transit Number of cargo aging periods the cargo has been in transit. * @param first_station Station the cargo was initially loaded. * @param source_xy Station location the cargo was initially loaded. * @param feeder_share Feeder share the packet has already accumulated. */ -CargoPacket::CargoPacket(uint16_t count, uint16_t periods_in_transit, StationID first_station, TileIndex source_xy, Money feeder_share) : +CargoPacket::CargoPacket(CargoPacketID index, uint16_t count, uint16_t periods_in_transit, StationID first_station, TileIndex source_xy, Money feeder_share) : + CargoPacketPool::PoolItem<&_cargopacket_pool>(index), count(count), periods_in_transit(periods_in_transit), feeder_share(feeder_share), @@ -66,11 +71,13 @@ CargoPacket::CargoPacket(uint16_t count, uint16_t periods_in_transit, StationID /** * Creates a new cargo packet. Used when loading or splitting packets. * + * @param index Index into the cargo packet pool. * @param count Number of cargo entities to put in this packet. * @param feeder_share Feeder share the packet has already accumulated. * @param original The original packet we are splitting. */ -CargoPacket::CargoPacket(uint16_t count, Money feeder_share, CargoPacket &original) : +CargoPacket::CargoPacket(CargoPacketID index, uint16_t count, Money feeder_share, CargoPacket &original) : + CargoPacketPool::PoolItem<&_cargopacket_pool>(index), count(count), periods_in_transit(original.periods_in_transit), feeder_share(feeder_share), diff --git a/src/cargopacket.h b/src/cargopacket.h index 39174cdb95..f625a7cf02 100644 --- a/src/cargopacket.h +++ b/src/cargopacket.h @@ -73,10 +73,10 @@ public: /** Maximum number of items in a single cargo packet. */ static const uint16_t MAX_COUNT = UINT16_MAX; - CargoPacket(); - CargoPacket(StationID first_station, uint16_t count, Source source); - CargoPacket(uint16_t count, uint16_t periods_in_transit, StationID first_station, TileIndex source_xy, Money feeder_share); - CargoPacket(uint16_t count, Money feeder_share, CargoPacket &original); + CargoPacket(CargoPacketID index); + CargoPacket(CargoPacketID index, StationID first_station, uint16_t count, Source source); + CargoPacket(CargoPacketID index, uint16_t count, uint16_t periods_in_transit, StationID first_station, TileIndex source_xy, Money feeder_share); + CargoPacket(CargoPacketID index, uint16_t count, Money feeder_share, CargoPacket &original); /** Destroy the packet. */ ~CargoPacket() { } diff --git a/src/company_base.h b/src/company_base.h index 2896c8be3a..91b1dbf5b5 100644 --- a/src/company_base.h +++ b/src/company_base.h @@ -125,7 +125,7 @@ struct CompanyProperties { }; struct Company : CompanyProperties, CompanyPool::PoolItem<&_company_pool> { - Company(StringID name_1 = {}, bool is_ai = false); + Company(CompanyID index, StringID name_1 = {}, bool is_ai = false); ~Company(); RailTypes avail_railtypes{}; ///< Rail types available to this company. diff --git a/src/company_cmd.cpp b/src/company_cmd.cpp index cc4265456a..027569532d 100644 --- a/src/company_cmd.cpp +++ b/src/company_cmd.cpp @@ -66,7 +66,7 @@ INSTANTIATE_POOL_METHODS(Company) * @param name_1 Name of the company. * @param is_ai A computer program is running for this company. */ -Company::Company(StringID name_1, bool is_ai) +Company::Company(CompanyID index, StringID name_1, bool is_ai) : CompanyPool::PoolItem<&_company_pool>(index) { this->name_1 = name_1; this->is_ai = is_ai; diff --git a/src/core/pool_func.hpp b/src/core/pool_func.hpp index 9f6720110e..8140dde4ef 100644 --- a/src/core/pool_func.hpp +++ b/src/core/pool_func.hpp @@ -83,7 +83,7 @@ DEFINE_POOL_METHOD(inline size_t)::FindFirstFree() * @pre index < this->size * @pre this->Get(index) == nullptr */ -DEFINE_POOL_METHOD(inline void *)::AllocateItem(size_t size, size_t index) +DEFINE_POOL_METHOD(inline AllocationResult)::AllocateItem(size_t size, size_t index) { assert(this->data[index] == nullptr); @@ -101,17 +101,16 @@ DEFINE_POOL_METHOD(inline void *)::AllocateItem(size_t size, size_t index) this->data[index] = item; SetBit(this->used_bitmap[index / BITMAP_SIZE], index % BITMAP_SIZE); /* MSVC complains about casting to narrower type, so first cast to the base type... then to the strong type. */ - item->index = static_cast(static_cast(index)); - return item; + return {item, static_cast(static_cast(index))}; } /** * Allocates new item * @param size size of item - * @return pointer to allocated item + * @return pointer to allocated item and the index of said item. * @note FatalError() on failure! (no free item) */ -DEFINE_POOL_METHOD(void *)::GetNew(size_t size) +DEFINE_POOL_METHOD(AllocationResult)::GetNew(size_t size) { size_t index = this->FindFirstFree(); @@ -134,7 +133,7 @@ DEFINE_POOL_METHOD(void *)::GetNew(size_t size) * @return pointer to allocated item * @note SlErrorCorruptFmt() on failure! (index out of range or already used) */ -DEFINE_POOL_METHOD(void *)::GetNew(size_t size, size_t index) +DEFINE_POOL_METHOD(AllocationResult)::GetNew(size_t size, size_t index) { if (index >= MAX_SIZE) { SlErrorCorruptFmt("{} index {} out of range ({})", this->name, index, MAX_SIZE); @@ -208,8 +207,8 @@ DEFINE_POOL_METHOD(void)::CleanPool() * forcefully instantiated. */ #define INSTANTIATE_POOL_METHODS(name) \ - template void * name ## Pool::GetNew(size_t size); \ - template void * name ## Pool::GetNew(size_t size, size_t index); \ + template AllocationResult name ## Pool::GetNew(size_t size); \ + template AllocationResult name ## Pool::GetNew(size_t size, size_t index); \ template void name ## Pool::FreeItem(size_t size, size_t index); \ template void name ## Pool::CleanPool(); diff --git a/src/core/pool_type.hpp b/src/core/pool_type.hpp index ef24f2b8f3..cfd9a713c1 100644 --- a/src/core/pool_type.hpp +++ b/src/core/pool_type.hpp @@ -24,6 +24,10 @@ static constexpr PoolTypes PT_ALL = {PoolType::Normal, PoolType::NetworkClient, typedef std::vector PoolVector; ///< Vector of pointers to PoolBase +template +using AllocationResult = std::pair; + + /** Non-templated base for #PoolID for use with type trait queries. */ struct PoolIDBase {}; @@ -134,6 +138,7 @@ struct Pool : PoolBase { public: static constexpr size_t MAX_SIZE = Tindex::End().base(); ///< Make template parameter accessible from outside + using IndexType = Tindex; using BitmapStorage = size_t; static constexpr size_t BITMAP_SIZE = std::numeric_limits::digits; @@ -283,7 +288,13 @@ public: */ template *Tpool> struct PoolItem { - Tindex index; ///< Index of this pool item + const Tindex index; ///< Index of this pool item + + /** + * Construct the item. + * @param index The index of this PoolItem in the pool. + */ + PoolItem(Tindex index) : index(index) {} /** Type of the pool this item is going to be part of */ typedef struct Pool Pool; @@ -338,8 +349,8 @@ public: requires std::is_base_of_v static inline T *Create(Targs &&... args) { - void *data = Tpool->GetNew(sizeof(T)); - return ::new (data) T(std::forward(args)...); + auto [data, index] = Tpool->GetNew(sizeof(T)); + return ::new (data) T(index, std::forward(args)...); } /** @@ -352,8 +363,8 @@ public: requires std::is_base_of_v static inline T *CreateAtIndex(Tindex index, Targs &&... args) { - void *data = Tpool->GetNew(sizeof(T), index.base()); - return ::new (data) T(std::forward(args)...); + auto [data, _] = Tpool->GetNew(sizeof(T), index.base()); + return ::new (data) T(index, std::forward(args)...); } /** Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function() */ @@ -461,12 +472,12 @@ private: AllocCache *alloc_cache = nullptr; std::allocator allocator{}; - void *AllocateItem(size_t size, size_t index); + AllocationResult AllocateItem(size_t size, size_t index); void ResizeFor(size_t index); size_t FindFirstFree(); - void *GetNew(size_t size); - void *GetNew(size_t size, size_t index); + AllocationResult GetNew(size_t size); + AllocationResult GetNew(size_t size, size_t index); void FreeItem(size_t size, size_t index); diff --git a/src/depot_base.h b/src/depot_base.h index cdeef1f4e6..80877eeac8 100644 --- a/src/depot_base.h +++ b/src/depot_base.h @@ -25,8 +25,7 @@ struct Depot : DepotPool::PoolItem<&_depot_pool> { std::string name{}; TimerGameCalendar::Date build_date{}; ///< Date of construction - Depot() {} - Depot(TileIndex xy) : xy(xy), build_date(TimerGameCalendar::date) {} + Depot(DepotID index, TileIndex xy = INVALID_TILE) : DepotPool::PoolItem<&_depot_pool>(index), xy(xy), build_date(TimerGameCalendar::date) {} ~Depot(); static inline Depot *GetByTile(TileIndex tile) diff --git a/src/disaster_vehicle.cpp b/src/disaster_vehicle.cpp index 6bbb453860..d2ccb9044c 100644 --- a/src/disaster_vehicle.cpp +++ b/src/disaster_vehicle.cpp @@ -118,14 +118,15 @@ void DisasterVehicle::UpdateImage() /** * Construct the disaster vehicle. + * @param index The index within the vehicle pool. * @param x The X coordinate. * @param y The Y coordinate. * @param direction The direction the vehicle is facing. * @param subtype The sub type of vehicle. * @param big_ufo_destroyer_target The target for the UFO destroyer. */ -DisasterVehicle::DisasterVehicle(int x, int y, Direction direction, DisasterSubType subtype, VehicleID big_ufo_destroyer_target) : - SpecializedVehicleBase(), big_ufo_destroyer_target(big_ufo_destroyer_target) +DisasterVehicle::DisasterVehicle(VehicleID index, int x, int y, Direction direction, DisasterSubType subtype, VehicleID big_ufo_destroyer_target) : + SpecializedVehicleBase(index), big_ufo_destroyer_target(big_ufo_destroyer_target) { this->vehstatus = VehState::Unclickable; diff --git a/src/disaster_vehicle.h b/src/disaster_vehicle.h index d178f05982..70a991363e 100644 --- a/src/disaster_vehicle.h +++ b/src/disaster_vehicle.h @@ -41,8 +41,8 @@ struct DisasterVehicle final : public SpecializedVehicle(index), current_station(front->last_station_visited), front(front) { diff --git a/src/economy_base.h b/src/economy_base.h index 7d29634cfc..a7f7fc1d63 100644 --- a/src/economy_base.h +++ b/src/economy_base.h @@ -29,9 +29,8 @@ struct CargoPayment : CargoPaymentPool::PoolItem<&_cargo_payment_pool> { Money visual_profit = 0; ///< The visual profit to show Money visual_transfer = 0; ///< The transfer credits to be shown - /** Constructor for pool saveload */ - CargoPayment() {} - CargoPayment(Vehicle *front); + CargoPayment(CargoPaymentID index) : CargoPaymentPool::PoolItem<&_cargo_payment_pool>(index) {} + CargoPayment(CargoPaymentID index, Vehicle *front); ~CargoPayment(); Money PayTransfer(CargoType cargo, const CargoPacket *cp, uint count, TileIndex current_tile); diff --git a/src/effectvehicle_base.h b/src/effectvehicle_base.h index db2bb9e53b..932f677d1a 100644 --- a/src/effectvehicle_base.h +++ b/src/effectvehicle_base.h @@ -25,8 +25,7 @@ struct EffectVehicle final : public SpecializedVehicle(index) { this->type = type; this->grf_prop.local_id = local_id; diff --git a/src/engine_base.h b/src/engine_base.h index a213785367..4a1999e0b1 100644 --- a/src/engine_base.h +++ b/src/engine_base.h @@ -77,8 +77,8 @@ private: std::variant vehicle_info{}; public: - Engine() {} - Engine(VehicleType type, uint16_t local_id); + Engine(EngineID index) : EnginePool::PoolItem<&_engine_pool>(index) {} + Engine(EngineID index, VehicleType type, uint16_t local_id); bool IsEnabled() const; /** diff --git a/src/goal_base.h b/src/goal_base.h index 97e10e50e5..0ab33cbe61 100644 --- a/src/goal_base.h +++ b/src/goal_base.h @@ -27,11 +27,8 @@ struct Goal : GoalPool::PoolItem<&_goal_pool> { EncodedString progress{}; ///< Progress text of the goal. bool completed = false; ///< Is the goal completed or not? - /** - * We need an (empty) constructor so struct isn't zeroed (as C++ standard states) - */ - Goal() { } - Goal(GoalType type, GoalTypeID dst, CompanyID company, const EncodedString &text) : company(company), type(type), dst(dst), text(text) {} + Goal(GoalID index, GoalType type = GT_NONE, GoalTypeID dst = 0, CompanyID company = CompanyID::Invalid(), const EncodedString &text = {}) : + GoalPool::PoolItem<&_goal_pool>(index), company(company), type(type), dst(dst), text(text) {} /** * (Empty) destructor has to be defined else operator delete might be called with nullptr parameter diff --git a/src/ground_vehicle.hpp b/src/ground_vehicle.hpp index 92464db2d4..9d3e93fa14 100644 --- a/src/ground_vehicle.hpp +++ b/src/ground_vehicle.hpp @@ -87,8 +87,9 @@ struct GroundVehicle : public SpecializedVehicle { /** * The constructor at SpecializedVehicle must be called. + * @param index The index into the vehicle pool. */ - GroundVehicle() : SpecializedVehicle() {} + GroundVehicle(VehicleID index) : SpecializedVehicle(index) {} void PowerChanged(); void CargoChanged(); diff --git a/src/group.h b/src/group.h index 3200fbada0..c748e6cba8 100644 --- a/src/group.h +++ b/src/group.h @@ -85,8 +85,8 @@ struct Group : GroupPool::PoolItem<&_group_pool> { GroupID parent = GroupID::Invalid(); ///< Parent group uint16_t number = 0; ///< Per-company group number. - Group() {} - Group(CompanyID owner, VehicleType vehicle_type) : owner(owner), vehicle_type(vehicle_type) {} + Group(GroupID index, CompanyID owner = INVALID_OWNER, VehicleType vehicle_type = VEH_INVALID) : + GroupPool::PoolItem<&_group_pool>(index), owner(owner), vehicle_type(vehicle_type) {} }; diff --git a/src/industry.h b/src/industry.h index c3c316e38a..2234a3984f 100644 --- a/src/industry.h +++ b/src/industry.h @@ -135,7 +135,7 @@ struct Industry : IndustryPool::PoolItem<&_industry_pool> { PersistentStorage *psa = nullptr; ///< Persistent storage for NewGRF industries. - Industry(TileIndex tile = INVALID_TILE) : location(tile, 0, 0) {} + Industry(IndustryID index, TileIndex tile = INVALID_TILE) : IndustryPool::PoolItem<&_industry_pool>(index), location(tile, 0, 0) {} ~Industry(); void RecomputeProductionMultipliers(); diff --git a/src/league_base.h b/src/league_base.h index 85894f6367..1217731929 100644 --- a/src/league_base.h +++ b/src/league_base.h @@ -37,12 +37,9 @@ struct LeagueTableElement : LeagueTableElementPool::PoolItem<&_league_table_elem EncodedString score{}; ///< String representation of the score associated with the element Link link{}; ///< What opens when element is clicked - /** - * We need an (empty) constructor so struct isn't zeroed (as C++ standard states) - */ - LeagueTableElement() { } - LeagueTableElement(LeagueTableID table, int64_t rating, CompanyID company, const EncodedString &text, const EncodedString &score, const Link &link) : - table(table), rating(rating), company(company), text(text), score(score), link(link) {} + LeagueTableElement(LeagueTableElementID index) : LeagueTableElementPool::PoolItem<&_league_table_element_pool>(index) { } + LeagueTableElement(LeagueTableElementID index, LeagueTableID table, int64_t rating, CompanyID company, const EncodedString &text, const EncodedString &score, const Link &link) : + LeagueTableElementPool::PoolItem<&_league_table_element_pool>(index), table(table), rating(rating), company(company), text(text), score(score), link(link) {} /** * (Empty) destructor has to be defined else operator delete might be called with nullptr parameter @@ -57,11 +54,8 @@ struct LeagueTable : LeagueTablePool::PoolItem<&_league_table_pool> { EncodedString header{}; ///< Text to show above the table EncodedString footer{}; ///< Text to show below the table - /** - * We need an (empty) constructor so struct isn't zeroed (as C++ standard states) - */ - LeagueTable() { } - LeagueTable(const EncodedString &title, const EncodedString &header, const EncodedString &footer) : title(title), header(header), footer(footer) { } + LeagueTable(LeagueTableID index, const EncodedString &title = {}, const EncodedString &header = {}, const EncodedString &footer = {}) : + LeagueTablePool::PoolItem<&_league_table_pool>(index), title(title), header(header), footer(footer) { } /** * (Empty) destructor has to be defined else operator delete might be called with nullptr parameter diff --git a/src/linkgraph/linkgraph.h b/src/linkgraph/linkgraph.h index 706d8d8cf5..00b7719e08 100644 --- a/src/linkgraph/linkgraph.h +++ b/src/linkgraph/linkgraph.h @@ -188,13 +188,12 @@ public: return val > 0 ? std::max(1U, val * target_age.base() / orig_age.base()) : 0; } - /** Bare constructor, only for save/load. */ - LinkGraph() {} /** * Real constructor. * @param cargo Cargo the link graph is about. */ - LinkGraph(CargoType cargo) : cargo(cargo), last_compression(TimerGameEconomy::date) {} + LinkGraph(LinkGraphID index, CargoType cargo = INVALID_CARGO) : + LinkGraphPool::PoolItem<&_link_graph_pool>(index), cargo(cargo), last_compression(TimerGameEconomy::date) {} void Init(uint size); void ShiftDates(TimerGameEconomy::Date interval); diff --git a/src/linkgraph/linkgraphjob.cpp b/src/linkgraph/linkgraphjob.cpp index 365b0358a5..54190121df 100644 --- a/src/linkgraph/linkgraphjob.cpp +++ b/src/linkgraph/linkgraphjob.cpp @@ -30,9 +30,11 @@ INSTANTIATE_POOL_METHODS(LinkGraphJob) * Create a link graph job from a link graph. The link graph will be copied so * that the calculations don't interfere with the normal operations on the * original. The job is immediately started. + * @param index Index into the LinkGraphJob pool. * @param orig Original LinkGraph to be copied. */ -LinkGraphJob::LinkGraphJob(const LinkGraph &orig) : +LinkGraphJob::LinkGraphJob(LinkGraphJobID index, const LinkGraph &orig) : + LinkGraphJobPool::PoolItem<&_link_graph_job_pool>(index), /* Copying the link graph here also copies its index member. * This is on purpose. */ link_graph(orig), diff --git a/src/linkgraph/linkgraphjob.h b/src/linkgraph/linkgraphjob.h index b308211154..519fa63f3a 100644 --- a/src/linkgraph/linkgraphjob.h +++ b/src/linkgraph/linkgraphjob.h @@ -26,7 +26,7 @@ extern LinkGraphJobPool _link_graph_job_pool; /** * Class for calculation jobs to be run on link graphs. */ -class LinkGraphJob : public LinkGraphJobPool::PoolItem<&_link_graph_job_pool>{ +class LinkGraphJob : public LinkGraphJobPool::PoolItem<&_link_graph_job_pool> { public: /** * Demand between two nodes. @@ -176,9 +176,9 @@ public: * Bare constructor, only for save/load. link_graph, join_date and actually * settings have to be brutally const-casted in order to populate them. */ - LinkGraphJob() : settings(_settings_game.linkgraph) {} + LinkGraphJob(LinkGraphJobID index) : LinkGraphJobPool::PoolItem<&_link_graph_job_pool>(index), link_graph(LinkGraphID::Invalid()), settings(_settings_game.linkgraph) {} - LinkGraphJob(const LinkGraph &orig); + LinkGraphJob(LinkGraphJobID index, const LinkGraph &orig); ~LinkGraphJob(); void Init(); diff --git a/src/network/network_admin.cpp b/src/network/network_admin.cpp index 045088d0a3..5eb57208cf 100644 --- a/src/network/network_admin.cpp +++ b/src/network/network_admin.cpp @@ -62,9 +62,11 @@ static_assert(lengthof(_admin_update_type_frequencies) == ADMIN_UPDATE_END); /** * Create a new socket for the server side of the admin network. + * @param index The index in the admin pool. * @param s The socket to connect with. */ -ServerNetworkAdminSocketHandler::ServerNetworkAdminSocketHandler(SOCKET s) : NetworkAdminSocketHandler(s) +ServerNetworkAdminSocketHandler::ServerNetworkAdminSocketHandler(AdminID index, SOCKET s) : + NetworkAdminSocketPool::PoolItem<&_networkadminsocket_pool>(index), NetworkAdminSocketHandler(s) { this->status = ADMIN_STATUS_INACTIVE; this->connect_time = std::chrono::steady_clock::now(); diff --git a/src/network/network_admin.h b/src/network/network_admin.h index 05fe6e6e66..9b5ac9e44a 100644 --- a/src/network/network_admin.h +++ b/src/network/network_admin.h @@ -47,7 +47,7 @@ public: std::chrono::steady_clock::time_point connect_time{}; ///< Time of connection. NetworkAddress address{}; ///< Address of the admin. - ServerNetworkAdminSocketHandler(SOCKET s); + ServerNetworkAdminSocketHandler(AdminID index, SOCKET s); ~ServerNetworkAdminSocketHandler() override; NetworkRecvStatus SendError(NetworkErrorCode error); diff --git a/src/network/network_base.h b/src/network/network_base.h index dc62fd9c86..c49c80d26a 100644 --- a/src/network/network_base.h +++ b/src/network/network_base.h @@ -30,9 +30,10 @@ struct NetworkClientInfo : NetworkClientInfoPool::PoolItem<&_networkclientinfo_p /** * Create a new client. + * @param index The index into the client info pool. * @param client_id The unique identifier of the client. */ - NetworkClientInfo(ClientID client_id = INVALID_CLIENT_ID) : client_id(client_id) {} + NetworkClientInfo(ClientPoolID index, ClientID client_id = INVALID_CLIENT_ID) : NetworkClientInfoPool::PoolItem<&_networkclientinfo_pool>(index), client_id(client_id) {} ~NetworkClientInfo(); static NetworkClientInfo *GetByClientID(ClientID client_id); diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp index bd60630df3..8fe830dc97 100644 --- a/src/network/network_server.cpp +++ b/src/network/network_server.cpp @@ -186,9 +186,11 @@ struct PacketWriter : SaveFilter { /** * Create a new socket for the server side of the game connection. + * @param index The index into the client pool. * @param s The socket to connect with. */ -ServerNetworkGameSocketHandler::ServerNetworkGameSocketHandler(SOCKET s) : NetworkGameSocketHandler(s) +ServerNetworkGameSocketHandler::ServerNetworkGameSocketHandler(ClientPoolID index, SOCKET s) : + NetworkClientSocketPool::PoolItem<&_networkclientsocket_pool>(index), NetworkGameSocketHandler(s) { this->client_id = _network_client_id++; this->receive_limit = _settings_client.network.bytes_per_frame_burst; diff --git a/src/network/network_server.h b/src/network/network_server.h index 35b39ae7ae..75f13d5d99 100644 --- a/src/network/network_server.h +++ b/src/network/network_server.h @@ -74,7 +74,7 @@ public: std::shared_ptr savegame = nullptr; ///< Writer used to write the savegame. NetworkAddress client_address{}; ///< IP-address of the client (so they can be banned) - ServerNetworkGameSocketHandler(SOCKET s); + ServerNetworkGameSocketHandler(ClientPoolID index, SOCKET s); ~ServerNetworkGameSocketHandler() override; std::unique_ptr ReceivePacket() override; diff --git a/src/newgrf_industries.cpp b/src/newgrf_industries.cpp index b3b87a0468..42d9c8a882 100644 --- a/src/newgrf_industries.cpp +++ b/src/newgrf_industries.cpp @@ -546,8 +546,7 @@ CommandCost CheckIfCallBackAllowsCreation(TileIndex tile, IndustryType type, siz { const IndustrySpec *indspec = GetIndustrySpec(type); - Industry ind; - ind.index = IndustryID::Invalid(); + Industry ind(IndustryID::Invalid()); ind.location.tile = tile; ind.location.w = 0; // important to mark the industry invalid ind.type = type; diff --git a/src/newgrf_industrytiles.cpp b/src/newgrf_industrytiles.cpp index ab8df73bf9..ee7365cc9f 100644 --- a/src/newgrf_industrytiles.cpp +++ b/src/newgrf_industrytiles.cpp @@ -231,8 +231,7 @@ extern bool IsSlopeRefused(Slope current, Slope refused); */ CommandCost PerformIndustryTileSlopeCheck(TileIndex ind_base_tile, TileIndex ind_tile, const IndustryTileSpec *its, IndustryType type, IndustryGfx gfx, size_t layout_index, uint16_t initial_random_bits, Owner founder, IndustryAvailabilityCallType creation_type) { - Industry ind; - ind.index = IndustryID::Invalid(); + Industry ind(IndustryID::Invalid()); ind.location.tile = ind_base_tile; ind.location.w = 0; ind.type = type; diff --git a/src/newgrf_spritegroup.h b/src/newgrf_spritegroup.h index 3cfc692e9e..5da16e1152 100644 --- a/src/newgrf_spritegroup.h +++ b/src/newgrf_spritegroup.h @@ -46,7 +46,7 @@ extern SpriteGroupPool _spritegroup_pool; /* Common wrapper for all the different sprite group types */ struct SpriteGroup : SpriteGroupPool::PoolItem<&_spritegroup_pool> { protected: - SpriteGroup() {} // Not `= default` as that resets PoolItem->index. + SpriteGroup(SpriteGroupID index) : SpriteGroupPool::PoolItem<&_spritegroup_pool>(index) {} /** Base sprite group resolver */ virtual ResolverResult Resolve(ResolverObject &object) const = 0; @@ -64,7 +64,7 @@ public: */ template struct SpecializedSpriteGroup : public SpriteGroup { - inline SpecializedSpriteGroup() : SpriteGroup() {} + inline SpecializedSpriteGroup(SpriteGroupID index) : SpriteGroup(index) {} /** * Creates a new T-object in the SpriteGroup pool. @@ -82,7 +82,7 @@ struct SpecializedSpriteGroup : public SpriteGroup { /* 'Real' sprite groups contain a list of other result or callback sprite * groups. */ struct RealSpriteGroup : SpecializedSpriteGroup { - RealSpriteGroup() : SpecializedSpriteGroup() {} + RealSpriteGroup(SpriteGroupID index) : SpecializedSpriteGroup(index) {} /* Loaded = in motion, loading = not moving * Each group contains several spritesets, for various loading stages */ @@ -177,7 +177,7 @@ struct DeterministicSpriteGroupRange { struct DeterministicSpriteGroup : SpecializedSpriteGroup { - DeterministicSpriteGroup() : SpecializedSpriteGroup() {} + DeterministicSpriteGroup(SpriteGroupID index) : SpecializedSpriteGroup(index) {} VarSpriteGroupScope var_scope{}; DeterministicSpriteGroupSize size{}; @@ -199,7 +199,7 @@ enum RandomizedSpriteGroupCompareMode : uint8_t { }; struct RandomizedSpriteGroup : SpecializedSpriteGroup { - RandomizedSpriteGroup() : SpecializedSpriteGroup() {} + RandomizedSpriteGroup(SpriteGroupID index) : SpecializedSpriteGroup(index) {} VarSpriteGroupScope var_scope{}; ///< Take this object: @@ -223,7 +223,7 @@ struct CallbackResultSpriteGroup : SpecializedSpriteGroup(), result(value) {} + CallbackResultSpriteGroup(SpriteGroupID index, CallbackResult value) : SpecializedSpriteGroup(index), result(value) {} CallbackResult result = 0; @@ -241,7 +241,7 @@ struct ResultSpriteGroup : SpecializedSpriteGroup { * @param num_sprites The number of sprites per set. * @return A spritegroup representing the sprite number result. */ - ResultSpriteGroup(SpriteID sprite, uint8_t num_sprites) : SpecializedSpriteGroup(), num_sprites(num_sprites), sprite(sprite) {} + ResultSpriteGroup(SpriteGroupID index, SpriteID sprite, uint8_t num_sprites) : SpecializedSpriteGroup(index), num_sprites(num_sprites), sprite(sprite) {} uint8_t num_sprites = 0; SpriteID sprite = 0; @@ -254,7 +254,7 @@ protected: * Action 2 sprite layout for houses, industry tiles, objects and airport tiles. */ struct TileLayoutSpriteGroup : SpecializedSpriteGroup { - TileLayoutSpriteGroup() : SpecializedSpriteGroup() {} + TileLayoutSpriteGroup(SpriteGroupID index) : SpecializedSpriteGroup(index) {} NewGRFSpriteLayout dts{}; @@ -265,7 +265,7 @@ protected: }; struct IndustryProductionSpriteGroup : SpecializedSpriteGroup { - IndustryProductionSpriteGroup() : SpecializedSpriteGroup() {} + IndustryProductionSpriteGroup(SpriteGroupID index) : SpecializedSpriteGroup(index) {} uint8_t version = 0; ///< Production callback version used, or 0xFF if marked invalid uint8_t num_input = 0; ///< How many subtract_input values are valid diff --git a/src/newgrf_storage.h b/src/newgrf_storage.h index c582409aca..3947d1bf79 100644 --- a/src/newgrf_storage.h +++ b/src/newgrf_storage.h @@ -197,8 +197,7 @@ extern PersistentStoragePool _persistent_storage_pool; * Class for pooled persistent storage of data. */ struct PersistentStorage : PersistentStorageArray, PersistentStoragePool::PoolItem<&_persistent_storage_pool> { - /** We don't want GCC to zero our struct! It already is zeroed and has an index! */ - PersistentStorage(const uint32_t new_grfid, GrfSpecFeature feature, TileIndex tile) + PersistentStorage(PersistentStorageID index, const uint32_t new_grfid, GrfSpecFeature feature, TileIndex tile) : PersistentStoragePool::PoolItem<&_persistent_storage_pool>(index) { this->grfid = new_grfid; this->feature = feature; diff --git a/src/object_base.h b/src/object_base.h index d51e29e63b..327b722710 100644 --- a/src/object_base.h +++ b/src/object_base.h @@ -28,10 +28,9 @@ struct Object : ObjectPool::PoolItem<&_object_pool> { uint8_t colour = 0; ///< Colour of the object, for display purpose uint8_t view = 0; ///< The view setting for this object - /** Make sure the object isn't zeroed. */ - Object() {} - Object(ObjectType type, Town *town, TileArea location, TimerGameCalendar::Date build_date, uint8_t view) : - type(type), town(town), location(location), build_date(build_date), view(view) {} + Object(ObjectID index) : ObjectPool::PoolItem<&_object_pool>(index) {} + Object(ObjectID index, ObjectType type, Town *town, TileArea location, TimerGameCalendar::Date build_date, uint8_t view) : + ObjectPool::PoolItem<&_object_pool>(index), type(type), town(town), location(location), build_date(build_date), view(view) {} /** Make sure the right destructor is called as well! */ ~Object() {} diff --git a/src/order_backup.cpp b/src/order_backup.cpp index 8de1cbe661..6b4dd9fab9 100644 --- a/src/order_backup.cpp +++ b/src/order_backup.cpp @@ -29,12 +29,23 @@ INSTANTIATE_POOL_METHODS(OrderBackup) OrderBackup::~OrderBackup() = default; +/** + * Create an order backup for savegame loading. + * @param index The index of the order backup pool. + */ +OrderBackup::OrderBackup(OrderBackupID index) : + OrderBackupPool::PoolItem<&_order_backup_pool>(index) +{ +} + /** * Create an order backup for the given vehicle. + * @param index The index of the order backup pool. * @param v The vehicle to make a backup of. * @param user The user that is requesting the backup. */ -OrderBackup::OrderBackup(const Vehicle *v, uint32_t user) : user(user), tile(v->tile), group(v->group_id) +OrderBackup::OrderBackup(OrderBackupID index, const Vehicle *v, uint32_t user) : + OrderBackupPool::PoolItem<&_order_backup_pool>(index), user(user), tile(v->tile), group(v->group_id) { this->CopyConsistPropertiesFrom(v); diff --git a/src/order_backup.h b/src/order_backup.h index 24360719f3..333b9e5803 100644 --- a/src/order_backup.h +++ b/src/order_backup.h @@ -48,9 +48,8 @@ private: void DoRestore(Vehicle *v); friend OrderBackupPool::PoolItem<&_order_backup_pool>; - /** Creation for savegame restoration. */ - OrderBackup() = default; - OrderBackup(const Vehicle *v, uint32_t user); + OrderBackup(OrderBackupID index); + OrderBackup(OrderBackupID index, const Vehicle *v, uint32_t user); public: ~OrderBackup(); diff --git a/src/order_base.h b/src/order_base.h index 8b67756200..d2d06e778b 100644 --- a/src/order_base.h +++ b/src/order_base.h @@ -290,26 +290,27 @@ private: public: /** Default constructor producing an invalid order list. */ - OrderList() {} + OrderList(OrderListID index) : OrderListPool::PoolItem<&_orderlist_pool>(index) {} /** * Create an order list with the given order chain for the given vehicle. - * @param chain pointer to the first order of the order chain - * @param v any vehicle using this orderlist + * @param index index of the list within the order list pool + * @param chain pointer to the first order of the order chain + * @param v any vehicle using this orderlist */ - OrderList(Order &&order, Vehicle *v) + OrderList(OrderListID index, Order &&order, Vehicle *v) : OrderListPool::PoolItem<&_orderlist_pool>(index) { this->orders.emplace_back(std::move(order)); this->Initialize(v); } - OrderList(std::vector &&orders, Vehicle *v) + OrderList(OrderListID index, std::vector &&orders, Vehicle *v) : OrderListPool::PoolItem<&_orderlist_pool>(index) { this->orders = std::move(orders); this->Initialize(v); } - OrderList(Vehicle *v) + OrderList(OrderListID index, Vehicle *v) : OrderListPool::PoolItem<&_orderlist_pool>(index) { this->Initialize(v); } diff --git a/src/roadstop_base.h b/src/roadstop_base.h index b4e359d4be..897216ff1c 100644 --- a/src/roadstop_base.h +++ b/src/roadstop_base.h @@ -71,7 +71,7 @@ struct RoadStop : RoadStopPool::PoolItem<&_roadstop_pool> { RoadStop *next = nullptr; ///< Next stop of the given type at this station /** Initializes a RoadStop */ - inline RoadStop(TileIndex tile = INVALID_TILE) : xy(tile) { } + inline RoadStop(RoadStopID index, TileIndex tile = INVALID_TILE) : RoadStopPool::PoolItem<&_roadstop_pool>(index), xy(tile) { } ~RoadStop(); diff --git a/src/roadveh.h b/src/roadveh.h index 71e56dcc0c..3eea464d5c 100644 --- a/src/roadveh.h +++ b/src/roadveh.h @@ -109,8 +109,7 @@ struct RoadVehicle final : public GroundVehicle { VehicleID disaster_vehicle = VehicleID::Invalid(); ///< NOSAVE: Disaster vehicle targetting this vehicle. RoadTypes compatible_roadtypes{}; ///< NOSAVE: Roadtypes this consist is powered on. - /** We don't want GCC to zero our struct! It already is zeroed and has an index! */ - RoadVehicle() : GroundVehicleBase() {} + RoadVehicle(VehicleID index) : GroundVehicleBase(index) {} /** We want to 'destruct' the right class. */ ~RoadVehicle() override { this->PreDestructor(); } diff --git a/src/saveload/engine_sl.cpp b/src/saveload/engine_sl.cpp index 7b99111649..ecb89953b6 100644 --- a/src/saveload/engine_sl.cpp +++ b/src/saveload/engine_sl.cpp @@ -48,7 +48,7 @@ Engine *GetTempDataEngine(EngineID index) if (index < _temp_engine.size()) { return &_temp_engine[index]; } else if (index == _temp_engine.size()) { - return &_temp_engine.emplace_back(); + return &_temp_engine.emplace_back(index); } else { NOT_REACHED(); } diff --git a/src/saveload/oldloader_sl.cpp b/src/saveload/oldloader_sl.cpp index df6c964f88..94fe4c51ea 100644 --- a/src/saveload/oldloader_sl.cpp +++ b/src/saveload/oldloader_sl.cpp @@ -379,10 +379,10 @@ static bool FixTTOEngines() /* Load the default engine set. Many of them will be overridden later */ { EngineID j = EngineID::Begin(); - for (uint16_t i = 0; i < lengthof(_orig_rail_vehicle_info); ++i, ++j) new (GetTempDataEngine(j)) Engine(VEH_TRAIN, i); - for (uint16_t i = 0; i < lengthof(_orig_road_vehicle_info); ++i, ++j) new (GetTempDataEngine(j)) Engine(VEH_ROAD, i); - for (uint16_t i = 0; i < lengthof(_orig_ship_vehicle_info); ++i, ++j) new (GetTempDataEngine(j)) Engine(VEH_SHIP, i); - for (uint16_t i = 0; i < lengthof(_orig_aircraft_vehicle_info); ++i, ++j) new (GetTempDataEngine(j)) Engine(VEH_AIRCRAFT, i); + for (uint16_t i = 0; i < lengthof(_orig_rail_vehicle_info); ++i, ++j) new (GetTempDataEngine(j)) Engine(j, VEH_TRAIN, i); + for (uint16_t i = 0; i < lengthof(_orig_road_vehicle_info); ++i, ++j) new (GetTempDataEngine(j)) Engine(j, VEH_ROAD, i); + for (uint16_t i = 0; i < lengthof(_orig_ship_vehicle_info); ++i, ++j) new (GetTempDataEngine(j)) Engine(j, VEH_SHIP, i); + for (uint16_t i = 0; i < lengthof(_orig_aircraft_vehicle_info); ++i, ++j) new (GetTempDataEngine(j)) Engine(j, VEH_AIRCRAFT, i); } TimerGameCalendar::Date aging_date = std::min(TimerGameCalendar::date + CalendarTime::DAYS_TILL_ORIGINAL_BASE_YEAR, TimerGameCalendar::ConvertYMDToDate(TimerGameCalendar::Year{2050}, 0, 1)); diff --git a/src/ship.h b/src/ship.h index 4b155baa68..164372cd24 100644 --- a/src/ship.h +++ b/src/ship.h @@ -36,8 +36,7 @@ struct Ship final : public SpecializedVehicle { int16_t rotation_x_pos = 0; ///< NOSAVE: X Position before rotation. int16_t rotation_y_pos = 0; ///< NOSAVE: Y Position before rotation. - /** We don't want GCC to zero our struct! It already is zeroed and has an index! */ - Ship() : SpecializedVehicleBase() {} + Ship(VehicleID index) : SpecializedVehicleBase(index) {} /** We want to 'destruct' the right class. */ ~Ship() override { this->PreDestructor(); } diff --git a/src/signs_base.h b/src/signs_base.h index 195d56225b..b183ea5588 100644 --- a/src/signs_base.h +++ b/src/signs_base.h @@ -27,8 +27,9 @@ struct Sign : SignPool::PoolItem<&_sign_pool> { Owner owner = INVALID_OWNER; // Placed by this company. Anyone can delete them though. OWNER_NONE for gray signs from old games. Colours text_colour = COLOUR_WHITE; // Colour of the sign's text. Only relevant for OWNER_DEITY. - Sign() {} - Sign(Owner owner, int32_t x, int32_t y, int32_t z, const std::string &name) : name(name), x(x), y(y), z(z), owner(owner) {} + Sign(SignID index) : SignPool::PoolItem<&_sign_pool>(index) {} + Sign(SignID index, Owner owner, int32_t x, int32_t y, int32_t z, const std::string &name) : + SignPool::PoolItem<&_sign_pool>(index), name(name), x(x), y(y), z(z), owner(owner) {} ~Sign(); void UpdateVirtCoord(); diff --git a/src/station.cpp b/src/station.cpp index cf990434e4..1afa57a40b 100644 --- a/src/station.cpp +++ b/src/station.cpp @@ -61,8 +61,8 @@ BaseStation::~BaseStation() this->sign.MarkDirty(); } -Station::Station(TileIndex tile) : - SpecializedStation(tile), +Station::Station(StationID index, TileIndex tile) : + SpecializedStation(index, tile), bus_station(INVALID_TILE, 0, 0), truck_station(INVALID_TILE, 0, 0), ship_station(INVALID_TILE, 0, 0), diff --git a/src/station_base.h b/src/station_base.h index 0d38330ce0..51e2fca070 100644 --- a/src/station_base.h +++ b/src/station_base.h @@ -552,7 +552,7 @@ public: IndustryList industries_near{}; ///< Cached list of industries near the station that can accept cargo, @see DeliverGoodsToIndustry() Industry *industry = nullptr; ///< NOSAVE: Associated industry for neutral stations. (Rebuilt on load from Industry->st) - Station(TileIndex tile = INVALID_TILE); + Station(StationID index, TileIndex tile = INVALID_TILE); ~Station() override; void AddFacility(StationFacility new_facility_bit, TileIndex facil_xy); diff --git a/src/story_base.h b/src/story_base.h index 0ba352f979..8e1a83bd9f 100644 --- a/src/story_base.h +++ b/src/story_base.h @@ -150,12 +150,9 @@ struct StoryPageElement : StoryPageElementPool::PoolItem<&_story_page_element_po uint32_t referenced_id = 0; ///< Id of referenced object (location, goal etc.) EncodedString text{}; ///< Static content text of page element - /** - * We need an (empty) constructor so struct isn't zeroed (as C++ standard states) - */ - StoryPageElement() { } - StoryPageElement(uint32_t sort_value, StoryPageElementType type, StoryPageID page) : - sort_value(sort_value), page(page), type(type) { } + StoryPageElement(StoryPageElementID index) : StoryPageElementPool::PoolItem<&_story_page_element_pool>(index) {} + StoryPageElement(StoryPageElementID index, uint32_t sort_value, StoryPageElementType type, StoryPageID page) : + StoryPageElementPool::PoolItem<&_story_page_element_pool>(index), sort_value(sort_value), page(page), type(type) {} /** * (Empty) destructor has to be defined else operator delete might be called with nullptr parameter @@ -171,12 +168,9 @@ struct StoryPage : StoryPagePool::PoolItem<&_story_page_pool> { EncodedString title; ///< Title of story page - /** - * We need an (empty) constructor so struct isn't zeroed (as C++ standard states) - */ - StoryPage() { } - StoryPage(uint32_t sort_value, TimerGameCalendar::Date date, CompanyID company, const EncodedString &title) : - sort_value(sort_value), date(date), company(company), title(title) {} + StoryPage(StoryPageID index) : StoryPagePool::PoolItem<&_story_page_pool>(index) {} + StoryPage(StoryPageID index, uint32_t sort_value, TimerGameCalendar::Date date, CompanyID company, const EncodedString &title) : + StoryPagePool::PoolItem<&_story_page_pool>(index), sort_value(sort_value), date(date), company(company), title(title) {} ~StoryPage(); }; diff --git a/src/subsidy_base.h b/src/subsidy_base.h index d219f9c615..baf9d1b63c 100644 --- a/src/subsidy_base.h +++ b/src/subsidy_base.h @@ -27,11 +27,8 @@ struct Subsidy : SubsidyPool::PoolItem<&_subsidy_pool> { Source src{}; ///< Source of subsidised path Source dst{}; ///< Destination of subsidised path - /** - * We need an (empty) constructor so struct isn't zeroed (as C++ standard states) - */ - Subsidy() { } - Subsidy(CargoType cargo_type, Source src, Source dst, uint16_t remaining) : cargo_type(cargo_type), remaining(remaining), src(src), dst(dst) {} + Subsidy(SubsidyID index, CargoType cargo_type = INVALID_CARGO, Source src = {}, Source dst = {}, uint16_t remaining = 0) : + SubsidyPool::PoolItem<&_subsidy_pool>(index), cargo_type(cargo_type), remaining(remaining), src(src), dst(dst) {} /** * (Empty) destructor has to be defined else operator delete might be called with nullptr parameter diff --git a/src/town.h b/src/town.h index 0b644dd691..97361b6b8b 100644 --- a/src/town.h +++ b/src/town.h @@ -156,9 +156,10 @@ struct Town : TownPool::PoolItem<&_town_pool> { /** * Creates a new town. + * @param index the index within the town pool * @param tile center tile of the town */ - Town(TileIndex tile = INVALID_TILE) : xy(tile) { } + Town(TownID index, TileIndex tile = INVALID_TILE) : TownPool::PoolItem<&_town_pool>(index), xy(tile) { } /** Destroy the town. */ ~Town(); diff --git a/src/train.h b/src/train.h index 24b33d9fa9..3b1222ae77 100644 --- a/src/train.h +++ b/src/train.h @@ -104,8 +104,7 @@ struct Train final : public GroundVehicle { TrackBits track{}; TrainForceProceeding force_proceed{}; - /** We don't want GCC to zero our struct! It already is zeroed and has an index! */ - Train() : GroundVehicleBase() {} + Train(VehicleID index) : GroundVehicleBase(index) {} /** We want to 'destruct' the right class. */ ~Train() override { this->PreDestructor(); } diff --git a/src/vehicle.cpp b/src/vehicle.cpp index 7094569d93..2b70a2a6a1 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -364,9 +364,10 @@ void VehicleLengthChanged(const Vehicle *u) /** * Vehicle constructor. + * @param index The index within the vehicle pool. * @param type Type of the new vehicle. */ -Vehicle::Vehicle(VehicleType type) +Vehicle::Vehicle(VehicleID index, VehicleType type) : VehiclePool::PoolItem<&_vehicle_pool>(index) { this->type = type; this->coord.left = INVALID_COORD; diff --git a/src/vehicle_base.h b/src/vehicle_base.h index a693cd71d0..fcb5d8e98d 100644 --- a/src/vehicle_base.h +++ b/src/vehicle_base.h @@ -331,7 +331,7 @@ public: return 0; } - Vehicle(VehicleType type = VEH_INVALID); + Vehicle(VehicleID index, VehicleType type = VEH_INVALID); void PreDestructor(); /** We want to 'destruct' the right class. */ @@ -1018,8 +1018,9 @@ struct SpecializedVehicle : public Vehicle { /** * Set vehicle type correctly + * @param index The index into the vehicle pool. */ - inline SpecializedVehicle() : Vehicle(Type) + inline SpecializedVehicle(VehicleID index) : Vehicle(index, Type) { this->sprite_cache.sprite_seq.count = 1; } diff --git a/src/waypoint_base.h b/src/waypoint_base.h index 5e56c6de39..8b77d18819 100644 --- a/src/waypoint_base.h +++ b/src/waypoint_base.h @@ -27,9 +27,10 @@ struct Waypoint final : SpecializedStation { /** * Create a waypoint at the given tile. + * @param index The index within the station pool. * @param tile The location of the waypoint. */ - Waypoint(TileIndex tile = INVALID_TILE) : SpecializedStation(tile) { } + Waypoint(StationID index, TileIndex tile = INVALID_TILE) : SpecializedStation(index, tile) { } ~Waypoint() override; void UpdateVirtCoord() override;