object_pool_pattern
-
namespace object_pool_pattern
Namespace for the Object Pool pattern.
-
class ConcreteReusable : public object_pool_pattern::Reusable
- #include <concrete_reusable.hpp>
Concrete implementation of the Reusable interface.
Represents an object that can be managed by an object pool. Each
ConcreteReusableobject has a unique identifier and maintains an internal state indicating whether it is currently in use.This class provides functionality to “use” the object and reset it to its initial state when returned to the pool.
Public Functions
-
explicit ConcreteReusable(const std::string &id)
Constructs a reusable object with a unique identifier.
This identifier helps distinguish between different instances and is used in messages to indicate which object is being used or reset.
- Parameters:
id – A unique identifier for the reusable object.
-
virtual void use() override
Simulates using the object.
Marks the object as in use and performs its functionality.
This method marks the object as “in use” and prints a message indicating the object is being used. If the object is already in use, it prints an error message to the console.
Overridden from the
Reusableinterface.This method demonstrates how the object is used. If the object is already in use, a message is printed to indicate that it cannot be reused until it is reset.
If the object is not in use, it transitions to the “in use” state and prints a message to indicate it is being used.
If the object is already in use, an error message is printed.
-
virtual void reset() override
Resets the object to its initial state.
This method marks the object as “not in use” and clears any state associated with the previous usage. A message is printed to indicate that the object has been reset and is ready for reuse.
Overridden from the
Reusableinterface.This method clears the “in use” state of the object, making it available for reuse in the object pool. A message is printed to indicate the object has been reset.
-
explicit ConcreteReusable(const std::string &id)
-
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 ConcreteReusable : public object_pool_pattern::Reusable