intnslib 0.1
A library to hold common functionality used across multiple projects.
Loading...
Searching...
No Matches
intns::memory::ObjectPool< T, AcquirePolicy, ReleasePolicy > Class Template Reference

A thread-safe object pool for managing reusable objects of type T. More...

#include <ObjectPool.hpp>

Public Types

using queue_type = std::deque< T >
 
using size_type = size_t
 
using value_type = T
 

Public Member Functions

 ObjectPool (queue_type &&src)
 Constructs an ObjectPool by moving the given queue of objects.
 
 ObjectPool (size_type init_size, std::optional< size_type > limit=std::nullopt)
 Creates an ObjectPool with a set initial size and optional max limit. Objects are default-constructed via value_type and processed by ReleasePolicy::on_release upon creation. If creation fails, the queue resets and the exception propagates.
 
value_type take ()
 Removes and returns an object from the pool.
 
std::optional< value_type > try_take ()
 Attempts to take an object from the container.
 
void add (value_type &&back)
 Adds a new object to the pool.
 
bool try_add (value_type &&back)
 Attempts to add a new object to the container.
 
void reserve (size_type target_size)
 Reserves space for a minimum number of objects in the pool.
 
bool try_reserve (size_type target_size)
 Reserves space for a minimum number of objects in the pool without throwing exceptions.
 
void shrink_to_fit ()
 Reduces the memory usage of the internal object storage to fit its current size. This may free unused memory and optimize resource usage.
 
size_type capacity () const
 Returns the pool's maximum capacity without reallocation.
 
size_type size () const
 Returns the number of objects currently managed.
 
bool empty () const
 Checks if the object pool is empty.
 
std::optional< size_type > size_limit () const noexcept
 Returns the maximum allowed size for the container.
 
void set_size_limit (std::optional< size_type > limit) noexcept
 Sets the max pool size; nullopt makes it unlimited.
 

Detailed Description

template<typename T, typename AcquirePolicy = NoOpPoolPolicy<T>, typename ReleasePolicy = AcquirePolicy>
requires AcquireHook<AcquirePolicy, T> && ReleaseHook<ReleasePolicy, T>
class intns::memory::ObjectPool< T, AcquirePolicy, ReleasePolicy >

A thread-safe object pool for managing reusable objects of type T.

ObjectPool efficiently manages reusable objects, reducing allocation costs. It supports size limits, customizable policies, and ensures exception safety.

Template Parameters
TThe type of objects managed (default and move constructible).
AcquirePolicyClass with static on_acquire(T&) called on acquisition.
ReleasePolicyClass with static on_release(T&) called on release.
Note
All public methods are thread-safe.

Usage

Use take() or try_take() to acquire objects. Use add() or try_add() to return objects. Construct the pool with an initial size and optional size limit.

Safety

Construction throws std::runtime_error if initial size exceeds limit. Methods throw std::runtime_error if pool is empty or size limit is exceeded. Exceptions during object creation or policy methods are propagated.

Constructor & Destructor Documentation

◆ ObjectPool() [1/2]

template<typename T , typename AcquirePolicy = NoOpPoolPolicy<T>, typename ReleasePolicy = AcquirePolicy>
intns::memory::ObjectPool< T, AcquirePolicy, ReleasePolicy >::ObjectPool ( queue_type &&  src)
inline

Constructs an ObjectPool by moving the given queue of objects.

Parameters
srcThe source queue containing objects to be managed by the pool. Ownership of the objects is transferred to the pool.

◆ ObjectPool() [2/2]

template<typename T , typename AcquirePolicy = NoOpPoolPolicy<T>, typename ReleasePolicy = AcquirePolicy>
intns::memory::ObjectPool< T, AcquirePolicy, ReleasePolicy >::ObjectPool ( size_type  init_size,
std::optional< size_type >  limit = std::nullopt 
)

Creates an ObjectPool with a set initial size and optional max limit. Objects are default-constructed via value_type and processed by ReleasePolicy::on_release upon creation. If creation fails, the queue resets and the exception propagates.

Parameters
init_sizeThe number of objects to initially create in the pool.
limitThe maximum number of objects allowed in the pool (optional, default is 0 for unlimited).
Exceptions
std::runtime_errorIf init_size is greater than the specified size limit.
Anyexception thrown during object creation or by ReleasePolicy::on_release.

Member Function Documentation

◆ add()

template<typename T , typename AcquirePolicy = NoOpPoolPolicy<T>, typename ReleasePolicy = AcquirePolicy>
void intns::memory::ObjectPool< T, AcquirePolicy, ReleasePolicy >::add ( value_type &&  back)

Adds a new object to the pool.

Parameters
backThe object to add to the pool (rvalue reference).
Exceptions
std::runtime_errorIf the pool size limit has been reached.

◆ capacity()

template<typename T , typename AcquirePolicy = NoOpPoolPolicy<T>, typename ReleasePolicy = AcquirePolicy>
size_type intns::memory::ObjectPool< T, AcquirePolicy, ReleasePolicy >::capacity ( ) const
inline

Returns the pool's maximum capacity without reallocation.

Returns
Maximum objects before reallocation.

◆ empty()

template<typename T , typename AcquirePolicy = NoOpPoolPolicy<T>, typename ReleasePolicy = AcquirePolicy>
bool intns::memory::ObjectPool< T, AcquirePolicy, ReleasePolicy >::empty ( ) const
inline

Checks if the object pool is empty.

Returns
true if the pool contains no objects, false otherwise.

◆ reserve()

template<typename T , typename AcquirePolicy = NoOpPoolPolicy<T>, typename ReleasePolicy = AcquirePolicy>
void intns::memory::ObjectPool< T, AcquirePolicy, ReleasePolicy >::reserve ( size_type  target_size)
inline

Reserves space for a minimum number of objects in the pool.

Parameters
target_sizeMinimum number of elements to reserve space for.
Exceptions
std::runtime_errorif the target size is zero and ThrowOnError is enabled.

◆ set_size_limit()

template<typename T , typename AcquirePolicy = NoOpPoolPolicy<T>, typename ReleasePolicy = AcquirePolicy>
void intns::memory::ObjectPool< T, AcquirePolicy, ReleasePolicy >::set_size_limit ( std::optional< size_type >  limit)
inlinenoexcept

Sets the max pool size; nullopt makes it unlimited.

Parameters
limitThe new size limit.

◆ size()

template<typename T , typename AcquirePolicy = NoOpPoolPolicy<T>, typename ReleasePolicy = AcquirePolicy>
size_type intns::memory::ObjectPool< T, AcquirePolicy, ReleasePolicy >::size ( ) const
inline

Returns the number of objects currently managed.

Returns
The number of objects managed.

◆ size_limit()

template<typename T , typename AcquirePolicy = NoOpPoolPolicy<T>, typename ReleasePolicy = AcquirePolicy>
std::optional< size_type > intns::memory::ObjectPool< T, AcquirePolicy, ReleasePolicy >::size_limit ( ) const
inlinenoexcept

Returns the maximum allowed size for the container.

Returns
The size limit as a value of type size_type.

◆ take()

template<typename T , typename AcquirePolicy = NoOpPoolPolicy<T>, typename ReleasePolicy = AcquirePolicy>
value_type intns::memory::ObjectPool< T, AcquirePolicy, ReleasePolicy >::take ( )

Removes and returns an object from the pool.

Returns
value_type The acquired object from the pool.
Exceptions
std::runtime_errorIf the pool is empty.

◆ try_add()

template<typename T , typename AcquirePolicy = NoOpPoolPolicy<T>, typename ReleasePolicy = AcquirePolicy>
bool intns::memory::ObjectPool< T, AcquirePolicy, ReleasePolicy >::try_add ( value_type &&  back)

Attempts to add a new object to the container.

Parameters
backThe object to be added, passed as an rvalue reference.
Returns
true if the object was successfully added; false if the size limit was reached.

◆ try_reserve()

template<typename T , typename AcquirePolicy = NoOpPoolPolicy<T>, typename ReleasePolicy = AcquirePolicy>
bool intns::memory::ObjectPool< T, AcquirePolicy, ReleasePolicy >::try_reserve ( size_type  target_size)
inline

Reserves space for a minimum number of objects in the pool without throwing exceptions.

Parameters
target_sizeMinimum number of elements to reserve space for.
Returns
true if the reservation was successful; false otherwise.

◆ try_take()

template<typename T , typename AcquirePolicy = NoOpPoolPolicy<T>, typename ReleasePolicy = AcquirePolicy>
std::optional< value_type > intns::memory::ObjectPool< T, AcquirePolicy, ReleasePolicy >::try_take ( )

Attempts to take an object from the container.

Returns
std::optional<value_type> The acquired object if available, otherwise std::nullopt.

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