object_pool_pattern::ObjectPool

class ObjectPool

Generic Object Pool for managing reusable objects.

This class provides a mechanism to borrow and return objects of type Reusable. By reusing objects, the Object Pool reduces the overhead of frequent object creation and destruction, especially in performance-critical systems.

Public Functions

std::unique_ptr<Reusable> borrowObject()

Borrow an object from the pool.

Retrieves an object from the pool for use. If no objects are available, the method throws a std::runtime_error.

Throws:

std::runtime_error – if the pool is empty and no objects are available.

Returns:

A unique pointer to a Reusable object.

void returnObject(std::unique_ptr<Reusable> obj)

Return an object to the pool.

Returns a previously borrowed object back to the pool. The object is reset to its initial state before being made available for reuse.

Parameters:

obj – A unique pointer to the reusable object being returned.

void addObject(std::unique_ptr<Reusable> obj)

Add a new object to the pool.

Allows new objects to be added to the pool for reuse. This is useful for dynamically expanding the pool size as needed.

Parameters:

obj – A unique pointer to the reusable object being added.