|
| 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.
|
|
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
-
T | The type of objects managed (default and move constructible). |
AcquirePolicy | Class with static on_acquire(T&) called on acquisition. |
ReleasePolicy | Class 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.