diff --git a/src/autoreplace_base.h b/src/autoreplace_base.h index 895449845b..2abbe47548 100644 --- a/src/autoreplace_base.h +++ b/src/autoreplace_base.h @@ -22,7 +22,7 @@ using EngineRenewID = PoolID; * placed here so the only exception to this rule, the saveload code, can use * it. */ -using EngineRenewPool = Pool; +using EngineRenewPool = Pool; extern EngineRenewPool _enginerenew_pool; /** diff --git a/src/base_station_base.h b/src/base_station_base.h index c4b3a12119..69e0af39c6 100644 --- a/src/base_station_base.h +++ b/src/base_station_base.h @@ -16,7 +16,7 @@ #include "station_map.h" #include "timer/timer_game_calendar.h" -typedef Pool StationPool; +typedef Pool StationPool; extern StationPool _station_pool; template diff --git a/src/cargopacket.h b/src/cargopacket.h index c1e85febbc..59093dc39e 100644 --- a/src/cargopacket.h +++ b/src/cargopacket.h @@ -25,7 +25,7 @@ using CargoPacketID = PoolID; +using CargoPacketPool = Pool; /** The actual pool with cargo packets. */ extern CargoPacketPool _cargopacket_pool; diff --git a/src/company_base.h b/src/company_base.h index cc0a33f423..2975725fd6 100644 --- a/src/company_base.h +++ b/src/company_base.h @@ -62,7 +62,7 @@ private: std::vector used_bitmap; }; -typedef Pool CompanyPool; +typedef Pool CompanyPool; extern CompanyPool _company_pool; /** Statically loadable part of Company pool item */ diff --git a/src/core/pool_func.hpp b/src/core/pool_func.hpp index e11473dc66..9d33916f33 100644 --- a/src/core/pool_func.hpp +++ b/src/core/pool_func.hpp @@ -23,8 +23,9 @@ * @param type The return type of the method. */ #define DEFINE_POOL_METHOD(type) \ - template \ - type Pool + template \ + requires std::is_base_of_v \ + type Pool /** * Create a clean pool. @@ -47,15 +48,15 @@ DEFINE_POOL_METHOD(inline)::Pool(const char *name) : * Resizes the pool so 'index' can be addressed * @param index index we will allocate later * @pre index >= this->size - * @pre index < Tmax_size + * @pre index < MAX_SIZE */ DEFINE_POOL_METHOD(inline void)::ResizeFor(size_t index) { assert(index >= this->data.size()); - assert(index < Tmax_size); + assert(index < MAX_SIZE); size_t old_size = this->data.size(); - size_t new_size = std::min(Tmax_size, Align(index + 1, Tgrowth_step)); + size_t new_size = std::min(MAX_SIZE, Align(index + 1, Tgrowth_step)); this->data.resize(new_size); this->used_bitmap.resize(Align(new_size, BITMAP_SIZE) / BITMAP_SIZE); @@ -83,12 +84,12 @@ DEFINE_POOL_METHOD(inline size_t)::FindFirstFree() assert(this->first_unused == this->data.size()); - if (this->first_unused < Tmax_size) { + if (this->first_unused < MAX_SIZE) { this->ResizeFor(this->first_unused); return this->first_unused; } - assert(this->first_unused == Tmax_size); + assert(this->first_unused == MAX_SIZE); return NO_FREE_ITEM; } @@ -124,12 +125,8 @@ 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); - if constexpr (std::is_base_of_v) { - /* 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)); - } else { - item->index = static_cast(index); - } + /* 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; } @@ -164,8 +161,8 @@ DEFINE_POOL_METHOD(void *)::GetNew(size_t size) */ DEFINE_POOL_METHOD(void *)::GetNew(size_t size, size_t index) { - if (index >= Tmax_size) { - SlErrorCorruptFmt("{} index {} out of range ({})", this->name, index, Tmax_size); + if (index >= MAX_SIZE) { + SlErrorCorruptFmt("{} index {} out of range ({})", this->name, index, MAX_SIZE); } if (index >= this->data.size()) this->ResizeFor(index); diff --git a/src/core/pool_type.hpp b/src/core/pool_type.hpp index dfe922fc74..a258cc1b10 100644 --- a/src/core/pool_type.hpp +++ b/src/core/pool_type.hpp @@ -124,27 +124,16 @@ private: * @tparam Titem Type of the class/struct that is going to be pooled * @tparam Tindex Type of the index for this pool * @tparam Tgrowth_step Size of growths; if the pool is full increase the size by this amount - * @tparam Tmax_size Maximum size of the pool * @tparam Tpool_type Type of this pool * @tparam Tcache Whether to perform 'alloc' caching, i.e. don't actually free/malloc just reuse the memory * @tparam Tzero Whether to zero the memory * @warning when Tcache is enabled *all* instances of this pool's item must be of the same size. */ -template +template +requires std::is_base_of_v struct Pool : PoolBase { -private: - /** Some helper functions to get the maximum value of the provided index. */ - template - static constexpr size_t GetMaxIndexValue(T) { return std::numeric_limits::max(); } - template requires std::is_enum_v - static constexpr size_t GetMaxIndexValue(T) { return std::numeric_limits>::max(); } - template requires std::is_base_of_v - static constexpr size_t GetMaxIndexValue(T) { return std::numeric_limits::max(); } public: - /* Ensure the highest possible index, i.e. Tmax_size -1, is within the bounds of Tindex. */ - static_assert(Tmax_size - 1 <= GetMaxIndexValue(Tindex{})); - - static constexpr size_t MAX_SIZE = Tmax_size; ///< Make template parameter accessible from outside + static constexpr size_t MAX_SIZE = Tindex::End().base(); ///< Make template parameter accessible from outside using BitmapStorage = size_t; static constexpr size_t BITMAP_SIZE = std::numeric_limits::digits; @@ -194,7 +183,7 @@ public: */ inline bool CanAllocate(size_t n = 1) { - bool ret = this->items <= Tmax_size - n; + bool ret = this->items <= MAX_SIZE - n; #ifdef WITH_ASSERT this->checked = ret ? n : 0; #endif /* WITH_ASSERT */ @@ -293,12 +282,12 @@ public: * Base class for all PoolItems * @tparam Tpool The pool this item is going to be part of */ - template *Tpool> + template *Tpool> struct PoolItem { Tindex index; ///< Index of this pool item /** Type of the pool this item is going to be part of */ - typedef struct Pool Pool; + typedef struct Pool Pool; /** * Allocates space for new Titem @@ -472,7 +461,6 @@ private: void FreeItem(size_t index); - /* Temporary helper functions to get the raw index from either strongly and non-strongly typed pool items. */ static constexpr size_t GetRawIndex(size_t index) { return index; } template requires std::is_base_of_v static constexpr size_t GetRawIndex(const T &index) { return index.base(); } diff --git a/src/depot_base.h b/src/depot_base.h index 3998f4456b..db375f893c 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/economy_base.h b/src/economy_base.h index 8f19b2788e..13ce2744b9 100644 --- a/src/economy_base.h +++ b/src/economy_base.h @@ -14,7 +14,7 @@ #include "company_type.h" /** Type of pool to store cargo payments in; little over 1 million. */ -using CargoPaymentPool = Pool; +using CargoPaymentPool = Pool; /** The actual pool to store cargo payments in. */ extern CargoPaymentPool _cargo_payment_pool; diff --git a/src/engine_base.h b/src/engine_base.h index 15b28ee59a..b662de7507 100644 --- a/src/engine_base.h +++ b/src/engine_base.h @@ -32,7 +32,7 @@ enum class EngineDisplayFlag : uint8_t { using EngineDisplayFlags = EnumBitSet; -typedef Pool EnginePool; +typedef Pool EnginePool; extern EnginePool _engine_pool; struct Engine : EnginePool::PoolItem<&_engine_pool> { diff --git a/src/goal_base.h b/src/goal_base.h index 816b238e4d..8423bcb8b9 100644 --- a/src/goal_base.h +++ b/src/goal_base.h @@ -14,7 +14,7 @@ #include "goal_type.h" #include "core/pool_type.hpp" -using GoalPool = Pool; +using GoalPool = Pool; extern GoalPool _goal_pool; /** Struct about goals, current and completed */ diff --git a/src/group.h b/src/group.h index 32d1132b01..41ae9a3236 100644 --- a/src/group.h +++ b/src/group.h @@ -17,7 +17,7 @@ #include "engine_type.h" #include "livery.h" -using GroupPool = Pool; +using GroupPool = Pool; extern GroupPool _group_pool; ///< Pool of groups. /** Statistics and caches on the vehicles in a group. */ diff --git a/src/industry.h b/src/industry.h index 4ddcd5ed31..f3a4707bf8 100644 --- a/src/industry.h +++ b/src/industry.h @@ -20,7 +20,7 @@ #include "timer/timer_game_economy.h" -typedef Pool IndustryPool; +typedef Pool IndustryPool; extern IndustryPool _industry_pool; static const TimerGameEconomy::Year PROCESSING_INDUSTRY_ABANDONMENT_YEARS{5}; ///< If a processing industry doesn't produce for this many consecutive economy years, it may close. diff --git a/src/league_base.h b/src/league_base.h index 27c354962c..3dd3180208 100644 --- a/src/league_base.h +++ b/src/league_base.h @@ -17,10 +17,10 @@ bool IsValidLink(Link link); -using LeagueTableElementPool = Pool; +using LeagueTableElementPool = Pool; extern LeagueTableElementPool _league_table_element_pool; -using LeagueTablePool = Pool; +using LeagueTablePool = Pool; extern LeagueTablePool _league_table_pool; diff --git a/src/linkgraph/linkgraph.h b/src/linkgraph/linkgraph.h index c4cb1ba5a5..e89259fd0a 100644 --- a/src/linkgraph/linkgraph.h +++ b/src/linkgraph/linkgraph.h @@ -24,7 +24,7 @@ class LinkGraph; * Type of the pool for link graph components. Each station can be in at up to * 32 link graphs. So we allow for plenty of them to be created. */ -using LinkGraphPool = Pool; +using LinkGraphPool = Pool; /** The actual pool with link graphs. */ extern LinkGraphPool _link_graph_pool; diff --git a/src/linkgraph/linkgraphjob.h b/src/linkgraph/linkgraphjob.h index ce383da970..867f324f6d 100644 --- a/src/linkgraph/linkgraphjob.h +++ b/src/linkgraph/linkgraphjob.h @@ -19,7 +19,7 @@ class Path; typedef std::list PathList; /** Type of the pool for link graph jobs. */ -using LinkGraphJobPool = Pool; +using LinkGraphJobPool = Pool; /** The actual pool with link graph jobs. */ extern LinkGraphJobPool _link_graph_job_pool; diff --git a/src/network/network_admin.h b/src/network/network_admin.h index 9d16b8e373..b6193bede9 100644 --- a/src/network/network_admin.h +++ b/src/network/network_admin.h @@ -18,7 +18,7 @@ extern AdminID _redirect_console_to_admin; class ServerNetworkAdminSocketHandler; /** Pool with all admin connections. */ -using NetworkAdminSocketPool = Pool; +using NetworkAdminSocketPool = Pool; extern NetworkAdminSocketPool _networkadminsocket_pool; /** Class for handling the server side of the game connection. */ diff --git a/src/network/network_base.h b/src/network/network_base.h index ce3768237b..3fd5de124f 100644 --- a/src/network/network_base.h +++ b/src/network/network_base.h @@ -17,7 +17,7 @@ #include "../timer/timer_game_economy.h" /** Type for the pool with client information. */ -using NetworkClientInfoPool = Pool; +using NetworkClientInfoPool = Pool; extern NetworkClientInfoPool _networkclientinfo_pool; /** Container for all information known about a client. */ diff --git a/src/network/network_server.h b/src/network/network_server.h index 13226fc775..6826cbad55 100644 --- a/src/network/network_server.h +++ b/src/network/network_server.h @@ -17,7 +17,7 @@ class ServerNetworkGameSocketHandler; /** Make the code look slightly nicer/simpler. */ typedef ServerNetworkGameSocketHandler NetworkClientSocket; /** Pool with all client sockets. */ -using NetworkClientSocketPool = Pool; +using NetworkClientSocketPool = Pool; extern NetworkClientSocketPool _networkclientsocket_pool; /** Class for handling the server side of the game connection. */ diff --git a/src/newgrf_spritegroup.h b/src/newgrf_spritegroup.h index 6b49d17040..03b24c0860 100644 --- a/src/newgrf_spritegroup.h +++ b/src/newgrf_spritegroup.h @@ -50,7 +50,7 @@ struct ResolverObject; * Adding an 'extra' margin would be assuming 64 sprite groups per real * sprite. 64 = 2^6, so 2^30 should be enough (for now) */ using SpriteGroupID = PoolID; -using SpriteGroupPool = Pool; +using SpriteGroupPool = Pool; extern SpriteGroupPool _spritegroup_pool; /* Common wrapper for all the different sprite group types */ diff --git a/src/newgrf_storage.h b/src/newgrf_storage.h index 0f962d6681..ea7ec3c286 100644 --- a/src/newgrf_storage.h +++ b/src/newgrf_storage.h @@ -189,7 +189,7 @@ typedef PersistentStorageArray OldPersistentStorage; using PersistentStorageID = PoolID; struct PersistentStorage; -using PersistentStoragePool = Pool; +using PersistentStoragePool = Pool; extern PersistentStoragePool _persistent_storage_pool; diff --git a/src/object_base.h b/src/object_base.h index 192e371da4..544fc9ffec 100644 --- a/src/object_base.h +++ b/src/object_base.h @@ -16,7 +16,7 @@ #include "town_type.h" #include "timer/timer_game_calendar.h" -using ObjectPool = Pool; +using ObjectPool = Pool; extern ObjectPool _object_pool; /** An object, such as transmitter, on the map. */ diff --git a/src/order_backup.h b/src/order_backup.h index 45f0ddcea3..b445ca9c0b 100644 --- a/src/order_backup.h +++ b/src/order_backup.h @@ -22,7 +22,7 @@ using OrderBackupID = PoolID; struct OrderBackup; /** The pool type for order backups. */ -using OrderBackupPool = Pool; +using OrderBackupPool = Pool; /** The pool with order backups. */ extern OrderBackupPool _order_backup_pool; diff --git a/src/order_base.h b/src/order_base.h index 1ca7c7293b..ca7050623a 100644 --- a/src/order_base.h +++ b/src/order_base.h @@ -20,8 +20,8 @@ #include "timer/timer_game_tick.h" #include "saveload/saveload.h" -using OrderPool = Pool; -using OrderListPool = Pool; +using OrderPool = Pool; +using OrderListPool = Pool; extern OrderPool _order_pool; extern OrderListPool _orderlist_pool; diff --git a/src/roadstop_base.h b/src/roadstop_base.h index af95fee4f8..88091cb40d 100644 --- a/src/roadstop_base.h +++ b/src/roadstop_base.h @@ -15,7 +15,7 @@ #include "core/bitmath_func.hpp" #include "vehicle_type.h" -using RoadStopPool = Pool; +using RoadStopPool = Pool; extern RoadStopPool _roadstop_pool; /** A Stop for a Road Vehicle */ diff --git a/src/signs_base.h b/src/signs_base.h index b208b21704..2d02817b0b 100644 --- a/src/signs_base.h +++ b/src/signs_base.h @@ -15,7 +15,7 @@ #include "core/pool_type.hpp" #include "company_type.h" -typedef Pool SignPool; +typedef Pool SignPool; extern SignPool _sign_pool; struct Sign : SignPool::PoolItem<&_sign_pool> { diff --git a/src/story_base.h b/src/story_base.h index 235115f378..c784944055 100644 --- a/src/story_base.h +++ b/src/story_base.h @@ -17,8 +17,8 @@ #include "vehicle_type.h" #include "core/pool_type.hpp" -using StoryPageElementPool = Pool; -using StoryPagePool = Pool; +using StoryPageElementPool = Pool; +using StoryPagePool = Pool; extern StoryPageElementPool _story_page_element_pool; extern StoryPagePool _story_page_pool; extern uint32_t _story_page_element_next_sort_value; diff --git a/src/subsidy_base.h b/src/subsidy_base.h index 07e526aec7..cd3020090e 100644 --- a/src/subsidy_base.h +++ b/src/subsidy_base.h @@ -16,7 +16,7 @@ #include "subsidy_type.h" #include "core/pool_type.hpp" -using SubsidyPool = Pool; +using SubsidyPool = Pool; extern SubsidyPool _subsidy_pool; /** Struct about subsidies, offered and awarded */ diff --git a/src/town.h b/src/town.h index 2cbf9325b8..e2ba175359 100644 --- a/src/town.h +++ b/src/town.h @@ -33,7 +33,7 @@ static const uint TOWN_GROWTH_DESERT = 0xFFFFFFFF; ///< The town needs the cargo static const uint16_t TOWN_GROWTH_RATE_NONE = 0xFFFF; ///< Special value for Town::growth_rate to disable town growth. static const uint16_t MAX_TOWN_GROWTH_TICKS = 930; ///< Max amount of original town ticks that still fit into uint16_t, about equal to UINT16_MAX / TOWN_GROWTH_TICKS but slightly less to simplify calculations -typedef Pool TownPool; +typedef Pool TownPool; extern TownPool _town_pool; /** Data structure with cached data of towns. */ diff --git a/src/vehicle_base.h b/src/vehicle_base.h index 1555ceaa3c..6c2ea473d2 100644 --- a/src/vehicle_base.h +++ b/src/vehicle_base.h @@ -196,7 +196,7 @@ struct MutableSpriteCache { }; /** A vehicle pool for a little over 1 million vehicles. */ -typedef Pool VehiclePool; +typedef Pool VehiclePool; extern VehiclePool _vehicle_pool; /* Some declarations of functions, so we can make them friendly */