78template <
class Titem,
typename Tindex,
size_t Tgrowth_step,
size_t Tmax_size, PoolType Tpool_type = PoolType::Normal,
bool Tcache = false,
bool Tzero = true>
84 template <
typename T>
requires std::is_enum_v<T>
85 static constexpr size_t GetMaxIndexValue(T) {
return std::numeric_limits<std::underlying_type_t<T>>::max(); }
92 using BitmapStorage = size_t;
93 static constexpr size_t BITMAP_SIZE = std::numeric_limits<BitmapStorage>::digits;
117 inline Titem *
Get(
size_t index)
120 return this->data[index];
130 return index < this->first_unused && this->
Get(index) !=
nullptr;
140 bool ret = this->items <= Tmax_size - n;
142 this->checked = ret ? n : 0;
153 typedef T value_type;
155 typedef T &reference;
156 typedef size_t difference_type;
157 typedef std::forward_iterator_tag iterator_category;
161 this->ValidateIndex();
164 bool operator==(
const PoolIterator &other)
const {
return this->index == other.index; }
165 bool operator!=(
const PoolIterator &other)
const {
return !(*
this == other); }
166 T * operator*()
const {
return T::Get(this->index); }
167 PoolIterator & operator++() { this->index++; this->ValidateIndex();
return *
this; }
173 while (this->index < T::GetPoolSize() && !(T::IsValidID(this->index))) this->index++;
174 if (this->index >= T::GetPoolSize()) this->index = T::Pool::MAX_SIZE;
188 bool empty() {
return this->begin() == this->end(); }
195 template <
class T,
class F>
197 typedef T value_type;
199 typedef T &reference;
200 typedef size_t difference_type;
201 typedef std::forward_iterator_tag iterator_category;
205 this->ValidateIndex();
210 T * operator*()
const {
return T::Get(this->index); }
218 while (this->index < T::GetPoolSize() && !(T::IsValidID(this->index) && this->filter(this->index))) this->index++;
219 if (this->index >= T::GetPoolSize()) this->index = T::Pool::MAX_SIZE;
227 template <
class T,
class F>
234 bool empty() {
return this->begin() == this->end(); }
241 template <struct Pool<Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero> *Tpool>
246 typedef struct Pool<Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero>
Pool;
254 inline void *
operator new(
size_t size)
256 return Tpool->GetNew(size);
264 inline void operator delete(
void *p)
266 if (p ==
nullptr)
return;
267 Titem *pn =
static_cast<Titem *
>(p);
268 assert(pn == Tpool->Get(pn->index));
269 Tpool->FreeItem(pn->index);
280 inline void *
operator new(
size_t size,
size_t index)
282 return Tpool->GetNew(size,
index);
292 inline void *
operator new(size_t,
void *ptr)
294 for (
size_t i = 0; i < Tpool->first_unused; i++) {
301 assert(ptr != Tpool->data[i]);
316 return Tpool->CanAllocate(n);
325 return Tpool->cleaning;
335 return Tpool->IsValidID(
index);
346 return Tpool->Get(
index);
357 return index < Tpool->first_unused ? Tpool->Get(
index) :
nullptr;
367 return Tpool->first_unused;
411 void *AllocateItem(
size_t size,
size_t index);
412 void ResizeFor(
size_t index);
413 size_t FindFirstFree();
415 void *GetNew(
size_t size);
416 void *GetNew(
size_t size,
size_t index);
418 void FreeItem(
size_t index);
Type (helpers) for enums.
PoolType
Various types of a pool.
@ NetworkClient
Network client pools.
@ NetworkAdmin
Network admin pool.
@ Normal
Normal pool containing game objects.
@ Data
NewGRF or other data, that is not reset together with normal pools.
std::vector< struct PoolBase * > PoolVector
Vector of pointers to PoolBase.
Base class for base of all pools.
const PoolType type
Type of this pool.
virtual void CleanPool()=0
Virtual method that deletes all items in the pool.
static PoolVector * GetPools()
Function used to access the vector of all pools.
PoolBase(PoolType pt)
Constructor registers this object in the pool vector.
static void Clean(PoolTypes)
Clean all pools of given type.
virtual ~PoolBase()
Destructor removes this object from the pool vector and deletes the vector itself if this was the las...
PoolBase(const PoolBase &other)
Dummy private copy constructor to prevent compilers from copying the structure, which fails due to Ge...
Helper struct to cache 'freed' PoolItems so we do not need to allocate them again.
AllocCache * next
The next in our 'cache'.
Base class for all PoolItems.
static size_t GetPoolSize()
Returns first unused index.
Tindex index
Index of this pool item.
static void PostDestructor(size_t index)
Dummy function called after destructor of each member.
static size_t GetNumItems()
Returns number of valid items in the pool.
static bool CleaningPool()
Returns current state of pool cleaning - yes or no.
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
static Titem * Get(size_t index)
Returns Titem with given index.
Iterator to iterate all valid T of a pool.
Iterator to iterate all valid T of a pool.
Base class for all pools.
void CleanPool() override
Virtual method that deletes all items in the pool.
size_t first_free
No item with index lower than this is free (doesn't say anything about this one!)
const char *const name
Name of this pool.
std::vector< BitmapStorage > used_bitmap
Bitmap of used indices.
size_t first_unused
This and all higher indexes are free (doesn't say anything about first_unused-1 !)
std::vector< Titem * > data
Pointers to Titem.
bool cleaning
True if cleaning pool (deleting all items)
size_t items
Number of used indexes (non-nullptr)
Titem * Get(size_t index)
Returns Titem with given index.
static constexpr size_t MAX_SIZE
Make template parameter accessible from outside.
AllocCache * alloc_cache
Cache of freed pointers.
bool CanAllocate(size_t n=1)
Tests whether we can allocate 'n' items.
static constexpr size_t GetMaxIndexValue(T)
Some helper functions to get the maximum value of the provided index.
bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
static const size_t NO_FREE_ITEM
Constant to indicate we can't allocate any more items.