OpenTTD Source 20250312-master-gcdcc6b491d
Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache > Struct Template Reference

Base class for all pools. More...

#include <pool_type.hpp>

Inheritance diagram for Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >:
PoolBase

Data Structures

struct  AllocCache
 Helper struct to cache 'freed' PoolItems so we do not need to allocate them again. More...
 
struct  IterateWrapper
 
struct  IterateWrapperFiltered
 
struct  PoolItem
 Base class for all PoolItems. More...
 
struct  PoolIterator
 Iterator to iterate all valid T of a pool. More...
 
struct  PoolIteratorFiltered
 Iterator to iterate all valid T of a pool. More...
 

Public Types

using BitmapStorage = size_t
 

Public Member Functions

 Pool (const char *name)
 
void CleanPool () override
 Virtual method that deletes all items in the pool.
 
Titem * Get (size_t index)
 Returns Titem with given index.
 
bool IsValidID (size_t index)
 Tests whether given index can be used to get valid (non-nullptr) Titem.
 
bool CanAllocate (size_t n=1)
 Tests whether we can allocate 'n' items.
 
- Public Member Functions inherited from PoolBase
 PoolBase (PoolType pt)
 Constructor registers this object in the pool vector.
 
virtual ~PoolBase ()
 Destructor removes this object from the pool vector and deletes the vector itself if this was the last item removed.
 

Data Fields

const char *const name = nullptr
 Name of this pool.
 
size_t first_free = 0
 No item with index lower than this is free (doesn't say anything about this one!)
 
size_t first_unused = 0
 This and all higher indexes are free (doesn't say anything about first_unused-1 !)
 
size_t items = 0
 Number of used indexes (non-nullptr)
 
bool cleaning = false
 True if cleaning pool (deleting all items)
 
std::vector< Titem * > data {}
 Pointers to Titem.
 
std::vector< BitmapStorage > used_bitmap {}
 Bitmap of used indices.
 
- Data Fields inherited from PoolBase
const PoolType type
 Type of this pool.
 

Static Public Attributes

static constexpr size_t MAX_SIZE = Tindex::End().base()
 Make template parameter accessible from outside.
 
static constexpr size_t BITMAP_SIZE = std::numeric_limits<BitmapStorage>::digits
 

Private Member Functions

void * 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)
 
void FreeItem (size_t size, size_t index)
 

Static Private Member Functions

static constexpr size_t GetRawIndex (size_t index)
 
template<typename T >
requires std::is_base_of_v<PoolIDBase, T>
static constexpr size_t GetRawIndex (const T &index)
 

Private Attributes

AllocCachealloc_cache = nullptr
 Cache of freed pointers.
 
std::allocator< uint8_t > allocator {}
 

Static Private Attributes

static const size_t NO_FREE_ITEM = std::numeric_limits<size_t>::max()
 Constant to indicate we can't allocate any more items.
 

Additional Inherited Members

- Static Public Member Functions inherited from PoolBase
static PoolVectorGetPools ()
 Function used to access the vector of all pools.
 
static void Clean (PoolTypes)
 Clean all pools of given type.
 

Detailed Description

template<class Titem, typename Tindex, size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
requires std::is_base_of_v<PoolIDBase, Tindex>
struct Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >

Base class for all pools.

Template Parameters
TitemType of the class/struct that is going to be pooled
TindexType of the index for this pool
Tgrowth_stepSize of growths; if the pool is full increase the size by this amount
Tpool_typeType of this pool
TcacheWhether to perform 'alloc' caching, i.e. don't actually deallocated/allocate just reuse the memory
Warning
when Tcache is enabled all instances of this pool's item must be of the same size.

Definition at line 133 of file pool_type.hpp.

Member Typedef Documentation

◆ BitmapStorage

template<class Titem , typename Tindex , size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
using Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::BitmapStorage = size_t

Definition at line 137 of file pool_type.hpp.

Constructor & Destructor Documentation

◆ Pool()

template<class Titem , typename Tindex , size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::Pool ( const char *  name)
inline

Definition at line 153 of file pool_type.hpp.

Member Function Documentation

◆ CanAllocate()

template<class Titem , typename Tindex , size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
bool Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::CanAllocate ( size_t  n = 1)
inline

Tests whether we can allocate 'n' items.

Parameters
nnumber of items we want to allocate
Returns
true if 'n' items can be allocated

Definition at line 183 of file pool_type.hpp.

References Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::MAX_SIZE.

◆ CleanPool()

template<class Titem , typename Tindex , size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
void Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::CleanPool ( )
overridevirtual

Virtual method that deletes all items in the pool.

Implements PoolBase.

Referenced by AfterLoadStoryBook(), ResetNewGRFData(), and SetupEngines().

◆ Get()

template<class Titem , typename Tindex , size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
Titem * Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::Get ( size_t  index)
inline

Returns Titem with given index.

Parameters
indexof item to get
Returns
pointer to Titem
Precondition
index < this->first_unused

Definition at line 162 of file pool_type.hpp.

References Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::data, and Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::first_unused.

Referenced by Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::IsValidID().

◆ GetRawIndex() [1/2]

template<class Titem , typename Tindex , size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
template<typename T >
requires std::is_base_of_v<PoolIDBase, T>
static constexpr size_t Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::GetRawIndex ( const T &  index)
inlinestaticconstexprprivate

Definition at line 466 of file pool_type.hpp.

◆ GetRawIndex() [2/2]

template<class Titem , typename Tindex , size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
static constexpr size_t Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::GetRawIndex ( size_t  index)
inlinestaticconstexprprivate

Definition at line 464 of file pool_type.hpp.

◆ IsValidID()

template<class Titem , typename Tindex , size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
bool Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::IsValidID ( size_t  index)
inline

Tests whether given index can be used to get valid (non-nullptr) Titem.

Parameters
indexindex to examine
Returns
true if PoolItem::Get(index) will return non-nullptr pointer

Definition at line 173 of file pool_type.hpp.

References Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::Get().

Field Documentation

◆ alloc_cache

template<class Titem , typename Tindex , size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
AllocCache* Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::alloc_cache = nullptr
private

Cache of freed pointers.

Definition at line 452 of file pool_type.hpp.

◆ allocator

template<class Titem , typename Tindex , size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
std::allocator<uint8_t> Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::allocator {}
private

Definition at line 453 of file pool_type.hpp.

◆ BITMAP_SIZE

template<class Titem , typename Tindex , size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
constexpr size_t Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::BITMAP_SIZE = std::numeric_limits<BitmapStorage>::digits
staticconstexpr

Definition at line 138 of file pool_type.hpp.

◆ cleaning

template<class Titem , typename Tindex , size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
bool Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::cleaning = false

True if cleaning pool (deleting all items)

Definition at line 148 of file pool_type.hpp.

◆ data

template<class Titem , typename Tindex , size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
std::vector<Titem *> Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::data {}

Pointers to Titem.

Definition at line 150 of file pool_type.hpp.

Referenced by Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::Get().

◆ first_free

template<class Titem , typename Tindex , size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
size_t Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::first_free = 0

No item with index lower than this is free (doesn't say anything about this one!)

Definition at line 142 of file pool_type.hpp.

◆ first_unused

template<class Titem , typename Tindex , size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
size_t Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::first_unused = 0

This and all higher indexes are free (doesn't say anything about first_unused-1 !)

Definition at line 143 of file pool_type.hpp.

Referenced by Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::Get().

◆ items

template<class Titem , typename Tindex , size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
size_t Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::items = 0

Number of used indexes (non-nullptr)

Definition at line 144 of file pool_type.hpp.

◆ MAX_SIZE

template<class Titem , typename Tindex , size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
constexpr size_t Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::MAX_SIZE = Tindex::End().base()
staticconstexpr

◆ name

template<class Titem , typename Tindex , size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
const char* const Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::name = nullptr

Name of this pool.

Definition at line 140 of file pool_type.hpp.

◆ NO_FREE_ITEM

template<class Titem , typename Tindex , size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
const size_t Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::NO_FREE_ITEM = std::numeric_limits<size_t>::max()
staticprivate

Constant to indicate we can't allocate any more items.

Definition at line 440 of file pool_type.hpp.

◆ used_bitmap

template<class Titem , typename Tindex , size_t Tgrowth_step, PoolType Tpool_type = PoolType::Normal, bool Tcache = false>
std::vector<BitmapStorage> Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::used_bitmap {}

Bitmap of used indices.

Definition at line 151 of file pool_type.hpp.


The documentation for this struct was generated from the following file: