59 virtual void CleanPool() = 0;
66 PoolBase(
const PoolBase &other);
80 template <
class Titem,
typename Tindex,
size_t Tgrowth_step,
size_t Tmax_size, PoolType Tpool_type = PT_NORMAL,
bool Tcache = false,
bool Tzero = true>
83 assert_compile((uint64)(Tmax_size - 1) >> 8 *
sizeof(Tindex) == 0);
100 Pool(
const char *name);
109 inline Titem *
Get(
size_t index)
111 assert(index < this->first_unused);
112 return this->data[index];
122 return index < this->first_unused && this->
Get(index) !=
nullptr;
132 bool ret = this->items <= Tmax_size - n;
134 this->checked = ret ? n : 0;
145 typedef T value_type;
147 typedef T& reference;
148 typedef size_t difference_type;
149 typedef std::forward_iterator_tag iterator_category;
153 this->ValidateIndex();
156 bool operator==(
const PoolIterator &other)
const {
return this->index == other.index; }
157 bool operator!=(
const PoolIterator &other)
const {
return !(*
this == other); }
158 T * operator*()
const {
return T::Get(this->index); }
159 PoolIterator & operator++() { this->index++; this->ValidateIndex();
return *
this; }
163 void ValidateIndex() {
while (this->index < T::GetPoolSize() && !(T::IsValidID(this->index))) this->index++; }
176 bool empty() {
return this->begin() == this->end(); }
183 template <
class T,
class F>
185 typedef T value_type;
187 typedef T& reference;
188 typedef size_t difference_type;
189 typedef std::forward_iterator_tag iterator_category;
193 this->ValidateIndex();
198 T * operator*()
const {
return T::Get(this->index); }
204 void ValidateIndex() {
while (this->index < T::GetPoolSize() && !(T::IsValidID(this->index) && this->filter(this->index))) this->index++; }
211 template <
class T,
class F>
218 bool empty() {
return this->begin() == this->end(); }
225 template <struct Pool<Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero> *Tpool>
230 typedef struct Pool<Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero>
Pool;
238 inline void *
operator new(
size_t size)
240 return Tpool->GetNew(size);
248 inline void operator delete(
void *p)
250 if (p ==
nullptr)
return;
251 Titem *pn = (Titem *)p;
252 assert(pn == Tpool->Get(pn->index));
253 Tpool->FreeItem(pn->index);
264 inline void *
operator new(
size_t size,
size_t index)
266 return Tpool->GetNew(size, index);
277 inline void *
operator new(
size_t size,
void *ptr)
279 for (
size_t i = 0; i < Tpool->first_unused; i++) {
286 assert(ptr != Tpool->data[i]);
301 return Tpool->CanAllocate(n);
310 return Tpool->cleaning;
320 return Tpool->IsValidID(index);
329 static inline Titem *
Get(
size_t index)
331 return Tpool->Get(index);
342 return index < Tpool->first_unused ? Tpool->Get(index) :
nullptr;
352 return Tpool->first_unused;
396 void *AllocateItem(
size_t size,
size_t index);
397 void ResizeFor(
size_t index);
398 size_t FindFirstFree();
400 void *GetNew(
size_t size);
401 void *GetNew(
size_t size,
size_t index);
403 void FreeItem(
size_t index);
size_t first_unused
This and all higher indexes are free (doesn't say anything about first_unused-1 !) ...
DECLARE_ENUM_AS_BIT_SET(GenderEthnicity) enum CompanyManagerFaceVariable
Bitgroups of the CompanyManagerFace variable.
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Simple vector class that allows allocating an item without the need to copy this->data needlessly...
static Titem * Get(size_t index)
Returns Titem with given index.
static PoolVector * GetPools()
Function used to access the vector of all pools.
bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Base class for base of all pools.
Tindex index
Index of this pool item.
Iterator to iterate all valid T of a pool.
AllocCache * alloc_cache
Cache of freed pointers.
Normal pool containing game objects.
Type (helpers) for enums.
static const size_t MAX_SIZE
Make template parameter accessible from outside.
static void PostDestructor(size_t index)
Dummy function called after destructor of each member.
bool operator!=(const MultiMapIterator< Tmap_iter1, Tlist_iter1, Tkey, Tvalue1, Tcompare > &iter1, const MultiMapIterator< Tmap_iter2, Tlist_iter2, Tkey, Tvalue2, Tcompare > &iter2)
Inverse of operator==().
static size_t GetPoolSize()
Returns first unused index.
Titem * Get(size_t index)
Returns Titem with given index.
static const size_t NO_FREE_ITEM
Constant to indicate we can't allocate any more items.
PoolBase(PoolType pt)
Constructor registers this object in the pool vector.
Helper struct to cache 'freed' PoolItems so we do not need to allocate them again.
bool operator==(const MultiMapIterator< Tmap_iter1, Tlist_iter1, Tkey, Tvalue1, Tcompare > &iter1, const MultiMapIterator< Tmap_iter2, Tlist_iter2, Tkey, Tvalue2, Tcompare > &iter2)
Compare two MultiMap iterators.
bool cleaning
True if cleaning pool (deleting all items)
NewGRF or other data, that is not reset together with normal pools.
Titem ** data
Pointer to array of pointers to Titem.
size_t items
Number of used indexes (non-nullptr)
#define MAX_UVALUE(type)
The largest value that can be entered in a variable.
AllocCache * next
The next in our 'cache'.
Base class for all PoolItems.
Base class for all pools.
size_t first_free
No item with index lower than this is free (doesn't say anything about this one!) ...
static bool CleaningPool()
Returns current state of pool cleaning - yes or no.
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Iterator to iterate all valid T of a pool.
PoolType
Various types of a pool.
static size_t GetNumItems()
Returns number of valid items in the pool.
std::vector< struct PoolBase * > PoolVector
Vector of pointers to PoolBase.
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function() ...
bool CanAllocate(size_t n=1)
Tests whether we can allocate 'n' items.
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
size_t size
Current allocated size.
const char *const name
Name of this pool.
const PoolType type
Type of this pool.
virtual void CleanPool()
Virtual method that deletes all items in the pool.