Sparkle 0.0.1
Loading...
Searching...
No Matches
spk::Pool< TType > Class Template Reference

Thread-safe object pool that reuses allocations with custom destruction. More...

#include <spk_pool.hpp>

Collaboration diagram for spk::Pool< TType >:
Collaboration graph

Public Types

using Deleter = std::function<void(TType *)>
 Custom deleter used to return instances to the pool.
using Object = std::unique_ptr<TType, Deleter>
 Unique pointer type that recycles its payload back into the pool.

Public Member Functions

 Pool ()
 Constructs an empty pool with unlimited capacity.
 ~Pool ()
 Marks the pool as closed and prevents further allocations.
 Pool (const Pool &)=delete
Pooloperator= (const Pool &)=delete
void setMaximumAllocationSize (size_t p_size)
 Limits the number of recyclable objects stored by the pool.
size_t size () const
 Retrieves the number of available objects stored in the pool.
template<typename... TArgs>
void allocate (TArgs &&...p_args)
 Pre-allocates a new instance and pushes it into the available set.
template<typename... TArgs>
void resize (size_t p_newSize, TArgs &&...p_args)
 Adjusts the pool size by constructing or discarding objects.
void release ()
 Clears all cached objects without closing the pool.
template<typename... TArgs>
Object obtain (TArgs &&...p_args)
 Provides an object from the pool, constructing one if necessary.

Detailed Description

template<typename TType>
class spk::Pool< TType >

Thread-safe object pool that reuses allocations with custom destruction.

Template Parameters
TTypeStored object type; must be constructible with forwarded arguments.
pool.allocate();
auto item = pool.obtain();
Thread-safe object pool that reuses allocations with custom destruction.
Definition spk_pool.hpp:29
void allocate(TArgs &&...p_args)
Pre-allocates a new instance and pushes it into the available set.
Definition spk_pool.hpp:105
Object obtain(TArgs &&...p_args)
Provides an object from the pool, constructing one if necessary.
Definition spk_pool.hpp:164

Member Function Documentation

◆ allocate()

template<typename TType>
template<typename... TArgs>
void spk::Pool< TType >::allocate ( TArgs &&... p_args)
inline

Pre-allocates a new instance and pushes it into the available set.

Parameters
p_argsConstructor arguments forwarded to TType.

◆ obtain()

template<typename TType>
template<typename... TArgs>
Object spk::Pool< TType >::obtain ( TArgs &&... p_args)
inline

Provides an object from the pool, constructing one if necessary.

Parameters
p_argsConstructor arguments forwarded to TType.
Returns
Unique pointer that returns the instance to the pool on destruction.

◆ resize()

template<typename TType>
template<typename... TArgs>
void spk::Pool< TType >::resize ( size_t p_newSize,
TArgs &&... p_args )
inline

Adjusts the pool size by constructing or discarding objects.

Parameters
p_newSizeDesired number of available objects.
p_argsConstructor arguments forwarded to TType.

◆ setMaximumAllocationSize()

template<typename TType>
void spk::Pool< TType >::setMaximumAllocationSize ( size_t p_size)
inline

Limits the number of recyclable objects stored by the pool.

Parameters
p_sizeMaximum number of cached instances.

◆ size()

template<typename TType>
size_t spk::Pool< TType >::size ( ) const
inline

Retrieves the number of available objects stored in the pool.

Returns
Current cached object count.

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