Cogs.Foundation
Loading...
Searching...
No Matches
Public Member Functions | List of all members
Cogs::Collections::Pool< ElementType > Struct Template Reference

Pool used to store elements of ElementType. More...

#include <Pool.h>

Public Member Functions

 Pool (MemBlockType memType=MemBlockType::Bucket)
 Create a pool with default capacity and page size.
 
 Pool (ElementOffset capacity, ElementOffset pageSize=128, MemBlockType memType=MemBlockType::Bucket)
 Create a pool with the given capacity and page sizes given in number of elements.
 
 Pool (ElementOffset capacity, ElementOffset pageSize, Memory::Allocator *allocator, MemBlockType memType=MemBlockType::Bucket)
 Create a pool with the given capacity and page sizes given in number of elements.
 
 Pool (Pool &&) noexcept=default
 Defaulted move constructor.
 
Pooloperator= (Pool &&) noexcept=default
 Defaulted move assignment operator.
 
 Pool (const Pool &)=delete
 Disallow copying Pool instances.
 
Pooloperator= (const Pool &)=delete
 Disallow copy-assigning pool instances.
 
void resize (size_t capacity)
 Resize the pool to the given capacity.
 
template<typename... ARGS>
ElementType * create (ARGS &&... args)
 Allocate and initialize a new element from the pool passing any arguments to the constructor of the new item as necessary.
 
void destroy (ElementType *element)
 Free and destroy the given element in the pool.
 
size_t size () const
 Get the size of the pool, i.e the number of created elements.
 
size_t getCapacity () const
 Get the capacity of the pool, i.e the number of elements that may be allocated before any additional pages must be created.
 
size_t getPageSize () const
 Get the page size in number of elements.
 
ElementHandle getHandle (ElementType *element) const
 Get a handle for the given element.
 
ElementType * operator[] (ElementHandle handle)
 Lookup an element using the handle given, using constant time.
 
bool isValid (ElementHandle handle) const
 Checks if the given handle is a valid handle to an allocated or free element.
 
bool isAllocated (ElementHandle handle) const
 Checks if the given handle is a valid handle to an allocated element.
 

Detailed Description

template<typename ElementType>
struct Cogs::Collections::Pool< ElementType >

Pool used to store elements of ElementType.

Template Parameters
ElementTypeType of elements to store in the pool. The size requirements for storage are at least the size of a pointer.

Constructor & Destructor Documentation

◆ Pool() [1/5]

template<typename ElementType >
Cogs::Collections::Pool< ElementType >::Pool ( MemBlockType  memType = MemBlockType::Bucket)
inline

Create a pool with default capacity and page size.

◆ Pool() [2/5]

template<typename ElementType >
Cogs::Collections::Pool< ElementType >::Pool ( ElementOffset  capacity,
ElementOffset  pageSize = 128,
MemBlockType  memType = MemBlockType::Bucket 
)
inline

Create a pool with the given capacity and page sizes given in number of elements.

Parameters
capacityTotal number of elements to allocate storage for, divided between capacity / pageSize number of pages.
pageSizeNumber of elements to store per page. Defaults to 128. Allocations will occur whenever the pool needs to grow into additional pages.

◆ Pool() [3/5]

template<typename ElementType >
Cogs::Collections::Pool< ElementType >::Pool ( ElementOffset  capacity,
ElementOffset  pageSize,
Memory::Allocator allocator,
MemBlockType  memType = MemBlockType::Bucket 
)
inline

Create a pool with the given capacity and page sizes given in number of elements.

Parameters
capacityTotal number of elements to allocate storage for, divided between capacity / pageSize number of pages.
pageSizeNumber of elements to store per page. Defaults to 128. Allocations will occur whenever the pool needs to grow into additional pages.
allocatorPointer to an allocator to use when reserving page memory.

◆ Pool() [4/5]

template<typename ElementType >
Cogs::Collections::Pool< ElementType >::Pool ( Pool< ElementType > &&  )
defaultnoexcept

Defaulted move constructor.

◆ Pool() [5/5]

template<typename ElementType >
Cogs::Collections::Pool< ElementType >::Pool ( const Pool< ElementType > &  )
delete

Disallow copying Pool instances.

Member Function Documentation

◆ create()

template<typename ElementType >
template<typename... ARGS>
ElementType * Cogs::Collections::Pool< ElementType >::create ( ARGS &&...  args)
inline

Allocate and initialize a new element from the pool passing any arguments to the constructor of the new item as necessary.

The pool will grow as needed if no free elements are available to use or reuse.

Returns
A pointer to the newly allocated element. The pointer remains valid after resizing the pool.

◆ destroy()

template<typename ElementType >
void Cogs::Collections::Pool< ElementType >::destroy ( ElementType *  element)
inline

Free and destroy the given element in the pool.

The element is returned to the list of free elements maintained by the pool and can be reused the next time an allocation is requested.

Parameters
elementPointer to the element to deallocate.

◆ getCapacity()

template<typename ElementType >
size_t Cogs::Collections::Pool< ElementType >::getCapacity ( ) const
inline

Get the capacity of the pool, i.e the number of elements that may be allocated before any additional pages must be created.

◆ getHandle()

template<typename ElementType >
ElementHandle Cogs::Collections::Pool< ElementType >::getHandle ( ElementType *  element) const
inline

Get a handle for the given element.

Parameters
elementPointer to a previously allocated element in the pool.
Returns
A handle that may later be used with operator[] to retrieve the given pointer.

◆ getPageSize()

template<typename ElementType >
size_t Cogs::Collections::Pool< ElementType >::getPageSize ( ) const
inline

Get the page size in number of elements.

Total page size in bytes will be numElements * sizeof(ElementType)

◆ isAllocated()

template<typename ElementType >
bool Cogs::Collections::Pool< ElementType >::isAllocated ( ElementHandle  handle) const
inline

Checks if the given handle is a valid handle to an allocated element.

NOTE: Has to traverse free list to check. Can be time-consuming.

◆ isValid()

template<typename ElementType >
bool Cogs::Collections::Pool< ElementType >::isValid ( ElementHandle  handle) const
inline

Checks if the given handle is a valid handle to an allocated or free element.

◆ operator=() [1/2]

template<typename ElementType >
Pool & Cogs::Collections::Pool< ElementType >::operator= ( const Pool< ElementType > &  )
delete

Disallow copy-assigning pool instances.

◆ operator=() [2/2]

template<typename ElementType >
Pool & Cogs::Collections::Pool< ElementType >::operator= ( Pool< ElementType > &&  )
defaultnoexcept

Defaulted move assignment operator.

◆ operator[]()

template<typename ElementType >
ElementType * Cogs::Collections::Pool< ElementType >::operator[] ( ElementHandle  handle)
inline

Lookup an element using the handle given, using constant time.

Uses a handle previously fetched with getHandle() to look up and return a raw pointer to the element.

Parameters
handleHandle fetched with getHandle() used to look up the element.
Returns
A pointer to the element.

◆ resize()

template<typename ElementType >
void Cogs::Collections::Pool< ElementType >::resize ( size_t  capacity)
inline

Resize the pool to the given capacity.

◆ size()

template<typename ElementType >
size_t Cogs::Collections::Pool< ElementType >::size ( ) const
inline

Get the size of the pool, i.e the number of created elements.


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