Object Pool Header
-
namespace object_pool_pattern
Namespace for the Object Pool pattern.
-
class ObjectPool
- #include <object_pool.hpp>
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
Reusableobject.
-
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.
-
std::unique_ptr<Reusable> borrowObject()
-
class Reusable
- #include <object_pool.hpp>
Abstract interface for reusable objects in the Object Pool Pattern.
Classes implementing this interface represent objects that can be managed by an object pool. These objects must define how they are used and reset to their initial state.
Subclassed by object_pool_pattern::ConcreteReusable
Public Functions
-
virtual ~Reusable() = default
Virtual destructor for proper cleanup in derived classes.
-
virtual void use() = 0
Use the reusable object for its intended purpose.
Derived classes should provide concrete implementations of this method to define how the object is used.
-
virtual void reset() = 0
Reset the reusable object to its initial state.
This method ensures that objects can be safely reused by the pool, clearing any state or data from previous usage.
-
virtual ~Reusable() = default
-
class ObjectPool