OpenTTD Source
12.0-beta2
|
Go to the documentation of this file.
59 virtual void CleanPool() = 0;
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 static_assert((uint64)(Tmax_size - 1) >> 8 *
sizeof(Tindex) == 0);
109 inline Titem *
Get(
size_t index)
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; }
165 while (this->index < T::GetPoolSize() && !(T::IsValidID(this->index))) this->index++;
166 if (this->index >= T::GetPoolSize()) this->index = T::Pool::MAX_SIZE;
180 bool empty() {
return this->begin() == this->end(); }
187 template <
class T,
class F>
189 typedef T value_type;
191 typedef T& reference;
192 typedef size_t difference_type;
193 typedef std::forward_iterator_tag iterator_category;
197 this->ValidateIndex();
202 T * operator*()
const {
return T::Get(this->index); }
210 while (this->index < T::GetPoolSize() && !(T::IsValidID(this->index) && this->filter(this->index))) this->index++;
211 if (this->index >= T::GetPoolSize()) this->index = T::Pool::MAX_SIZE;
219 template <
class T,
class F>
226 bool empty() {
return this->begin() == this->end(); }
233 template <struct Pool<Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero> *Tpool>
238 typedef struct Pool<Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero>
Pool;
246 inline void *
operator new(
size_t size)
248 return Tpool->GetNew(
size);
256 inline void operator delete(
void *p)
258 if (p ==
nullptr)
return;
259 Titem *pn = (Titem *)p;
260 assert(pn == Tpool->Get(pn->index));
261 Tpool->FreeItem(pn->index);
285 inline void *
operator new(
size_t size,
void *ptr)
287 for (
size_t i = 0; i < Tpool->first_unused; i++) {
294 assert(ptr != Tpool->data[i]);
309 return Tpool->CanAllocate(n);
318 return Tpool->cleaning;
328 return Tpool->IsValidID(
index);
339 return Tpool->Get(
index);
350 return index < Tpool->first_unused ? Tpool->Get(
index) :
nullptr;
360 return Tpool->first_unused;
404 void *AllocateItem(
size_t size,
size_t index);
405 void ResizeFor(
size_t index);
406 size_t FindFirstFree();
408 void *GetNew(
size_t size);
409 void *GetNew(
size_t size,
size_t index);
411 void FreeItem(
size_t index);
bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
std::vector< struct PoolBase * > PoolVector
Vector of pointers to PoolBase.
static Titem * Get(size_t index)
Returns Titem with given index.
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
virtual void CleanPool()
Virtual method that deletes all items in the pool.
Helper struct to cache 'freed' PoolItems so we do not need to allocate them again.
Tindex index
Index of this pool item.
Iterator to iterate all valid T of a pool.
static void PostDestructor(size_t index)
Dummy function called after destructor of each member.
static constexpr size_t MAX_SIZE
Make template parameter accessible from outside.
Base class for base of all pools.
static size_t GetPoolSize()
Returns first unused index.
AllocCache * alloc_cache
Cache of freed pointers.
size_t items
Number of used indexes (non-nullptr)
Titem ** data
Pointer to array of pointers to Titem.
PoolType
Various types of a pool.
bool cleaning
True if cleaning pool (deleting all items)
@ PT_NADMIN
Network admin pool.
@ PT_NONE
No pool is selected.
Iterator to iterate all valid T of a pool.
static const size_t NO_FREE_ITEM
Constant to indicate we can't allocate any more items.
Titem * Get(size_t index)
Returns Titem with given index.
@ PT_NCLIENT
Network client pools.
size_t first_free
No item with index lower than this is free (doesn't say anything about this one!)
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
@ PT_NORMAL
Normal pool containing game objects.
Base class for all pools.
size_t size
Current allocated size.
@ PT_DATA
NewGRF or other data, that is not reset together with normal pools.
static size_t GetNumItems()
Returns number of valid items in the pool.
AllocCache * next
The next in our 'cache'.
static bool CleaningPool()
Returns current state of pool cleaning - yes or no.
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
#define MAX_UVALUE(type)
The largest value that can be entered in a variable.
DECLARE_ENUM_AS_BIT_SET(GenderEthnicity) enum CompanyManagerFaceVariable
Bitgroups of the CompanyManagerFace variable.
PoolBase(PoolType pt)
Constructor registers this object in the pool vector.
const char *const name
Name of this pool.
size_t first_unused
This and all higher indexes are free (doesn't say anything about first_unused-1 !)
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
const PoolType type
Type of this pool.
Base class for all PoolItems.
static PoolVector * GetPools()
Function used to access the vector of all pools.
bool CanAllocate(size_t n=1)
Tests whether we can allocate 'n' items.