OpenTTD Source 20241224-master-gf74b0cf984
Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero > Struct Template Reference

Base class for all pools. More...

#include <pool_type.hpp>

Inheritance diagram for Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >:
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
 Name of this pool.
 
size_t size
 Current allocated size.
 
size_t first_free
 No item with index lower than this is free (doesn't say anything about this one!)
 
size_t first_unused
 This and all higher indexes are free (doesn't say anything about first_unused-1 !)
 
size_t items
 Number of used indexes (non-nullptr)
 
bool cleaning
 True if cleaning pool (deleting all items)
 
Titem ** data
 Pointer to array of 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 = Tmax_size
 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 index)
 

Private Attributes

AllocCachealloc_cache
 Cache of freed pointers.
 

Static Private Attributes

static const size_t NO_FREE_ITEM = MAX_UVALUE(size_t)
 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 (PoolType)
 Clean all pools of given type.
 

Detailed Description

template<class Titem, typename Tindex, size_t Tgrowth_step, size_t Tmax_size, PoolType Tpool_type = PT_NORMAL, bool Tcache = false, bool Tzero = true>
struct Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >

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
Tmax_sizeMaximum size of the pool
Tpool_typeType of this pool
TcacheWhether to perform 'alloc' caching, i.e. don't actually free/malloc just reuse the memory
TzeroWhether to zero the memory
Warning
when Tcache is enabled all instances of this pool's item must be of the same size.

Definition at line 80 of file pool_type.hpp.

Member Typedef Documentation

◆ BitmapStorage

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

Definition at line 86 of file pool_type.hpp.

Member Function Documentation

◆ CanAllocate()

template<class Titem , typename Tindex , size_t Tgrowth_step, size_t Tmax_size, PoolType Tpool_type = PT_NORMAL, bool Tcache = false, bool Tzero = true>
bool Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::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 133 of file pool_type.hpp.

◆ CleanPool()

template<class Titem , typename Tindex , size_t Tgrowth_step, size_t Tmax_size, PoolType Tpool_type = PT_NORMAL, bool Tcache = false, bool Tzero = true>
void Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::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, size_t Tmax_size, PoolType Tpool_type = PT_NORMAL, bool Tcache = false, bool Tzero = true>
Titem * Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::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 112 of file pool_type.hpp.

References Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::first_unused.

Referenced by StoryBookWindow::GetSelPage(), and Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::IsValidID().

◆ IsValidID()

template<class Titem , typename Tindex , size_t Tgrowth_step, size_t Tmax_size, PoolType Tpool_type = PT_NORMAL, bool Tcache = false, bool Tzero = true>
bool Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::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 123 of file pool_type.hpp.

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

Referenced by StoryBookWindow::GetSelPage(), StoryBookWindow::IsFirstPageSelected(), StoryBookWindow::IsLastPageSelected(), StoryBookWindow::OnInvalidateData(), StoryBookWindow::SelectNextPage(), and StoryBookWindow::SelectPrevPage().

Field Documentation

◆ alloc_cache

template<class Titem , typename Tindex , size_t Tgrowth_step, size_t Tmax_size, PoolType Tpool_type = PT_NORMAL, bool Tcache = false, bool Tzero = true>
AllocCache* Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::alloc_cache
private

Cache of freed pointers.

Definition at line 404 of file pool_type.hpp.

◆ BITMAP_SIZE

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

Definition at line 87 of file pool_type.hpp.

◆ cleaning

template<class Titem , typename Tindex , size_t Tgrowth_step, size_t Tmax_size, PoolType Tpool_type = PT_NORMAL, bool Tcache = false, bool Tzero = true>
bool Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::cleaning

True if cleaning pool (deleting all items)

Definition at line 98 of file pool_type.hpp.

◆ data

template<class Titem , typename Tindex , size_t Tgrowth_step, size_t Tmax_size, PoolType Tpool_type = PT_NORMAL, bool Tcache = false, bool Tzero = true>
Titem** Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::data

Pointer to array of pointers to Titem.

Definition at line 100 of file pool_type.hpp.

◆ first_free

template<class Titem , typename Tindex , size_t Tgrowth_step, size_t Tmax_size, PoolType Tpool_type = PT_NORMAL, bool Tcache = false, bool Tzero = true>
size_t Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::first_free

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

Definition at line 92 of file pool_type.hpp.

◆ first_unused

template<class Titem , typename Tindex , size_t Tgrowth_step, size_t Tmax_size, PoolType Tpool_type = PT_NORMAL, bool Tcache = false, bool Tzero = true>
size_t Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::first_unused

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

Definition at line 93 of file pool_type.hpp.

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

◆ items

template<class Titem , typename Tindex , size_t Tgrowth_step, size_t Tmax_size, PoolType Tpool_type = PT_NORMAL, bool Tcache = false, bool Tzero = true>
size_t Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::items

Number of used indexes (non-nullptr)

Definition at line 94 of file pool_type.hpp.

Referenced by CmdCreateStoryPage(), and CmdCreateStoryPageElement().

◆ MAX_SIZE

template<class Titem , typename Tindex , size_t Tgrowth_step, size_t Tmax_size, PoolType Tpool_type = PT_NORMAL, bool Tcache = false, bool Tzero = true>
constexpr size_t Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::MAX_SIZE = Tmax_size
staticconstexpr

◆ name

template<class Titem , typename Tindex , size_t Tgrowth_step, size_t Tmax_size, PoolType Tpool_type = PT_NORMAL, bool Tcache = false, bool Tzero = true>
const char* const Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::name

Name of this pool.

Definition at line 89 of file pool_type.hpp.

◆ NO_FREE_ITEM

template<class Titem , typename Tindex , size_t Tgrowth_step, size_t Tmax_size, PoolType Tpool_type = PT_NORMAL, bool Tcache = false, bool Tzero = true>
const size_t Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::NO_FREE_ITEM = MAX_UVALUE(size_t)
staticprivate

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

Definition at line 392 of file pool_type.hpp.

◆ size

template<class Titem , typename Tindex , size_t Tgrowth_step, size_t Tmax_size, PoolType Tpool_type = PT_NORMAL, bool Tcache = false, bool Tzero = true>
size_t Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::size

◆ used_bitmap

template<class Titem , typename Tindex , size_t Tgrowth_step, size_t Tmax_size, PoolType Tpool_type = PT_NORMAL, bool Tcache = false, bool Tzero = true>
std::vector<BitmapStorage> Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::used_bitmap

Bitmap of used indices.

Definition at line 101 of file pool_type.hpp.


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