![]() |
Chemical Data Processing Library C++ API - Version 1.4.0
|
A reusable object pool with stack-like borrow/return semantics. More...
#include <ObjectStack.hpp>
Inheritance diagram for CDPL::Util::ObjectStack< T >:Classes | |
| struct | DefaultConstructor |
Default object factory which creates new instances of ObjectType via new. More... | |
Public Types | |
| typedef T | ObjectType |
| The type of the pooled objects. More... | |
| typedef std::function< ObjectType *()> | ConstructorFunction |
A generic wrapper for functions creating new instances of ObjectType. More... | |
| typedef std::function< void(ObjectType &)> | ObjectFunction |
A generic wrapper for functions operating on instances of ObjectType. More... | |
Public Member Functions | |
| ObjectStack (const ObjectStack &stack) | |
| Copy constructor. More... | |
| ObjectStack (std::size_t max_pool_size=0) | |
Constructs a default-configured ObjectStack instance using DefaultConstructor as the object factory. More... | |
| template<typename C > | |
| ObjectStack (const C &ctor_func, std::size_t max_pool_size) | |
Constructs a ObjectStack instance using a user-supplied object factory. More... | |
| ~ObjectStack () | |
| Destructor. More... | |
| ObjectType * | get () |
| Returns a pointer to an object handed out from the pool. More... | |
| void | put () |
| Returns the most recently borrowed object back to the pool. More... | |
| void | putAll () |
| Returns all currently borrowed objects back to the pool. More... | |
| std::size_t | getMaxSize () const |
| Returns the currently configured maximum pool size. More... | |
| void | setMaxSize (std::size_t max_size) |
| Sets the maximum pool size. More... | |
| void | freeMemory (bool unused_only=true) |
| Releases memory held by the pool. More... | |
| void | setInitFunction (const ObjectFunction &func) |
| Sets the function to be invoked on an object when it is handed out by get(). More... | |
| void | setCleanupFunction (const ObjectFunction &func) |
| Sets the function to be invoked on an object when it is returned by put() or putAll(). More... | |
| ObjectStack & | operator= (const ObjectStack &stack) |
| Copy assignment operator. More... | |
A reusable object pool with stack-like borrow/return semantics.
ObjectStack allocates objects of type T lazily on demand and recycles them across subsequent get() calls instead of destroying them on put(). An optional pool size limit controls the maximum number of objects retained between requests; user-supplied init and cleanup functions can be installed to be invoked when objects are handed out or returned.
| T | The type of the pooled objects. |
| typedef T CDPL::Util::ObjectStack< T >::ObjectType |
The type of the pooled objects.
| typedef std::function<ObjectType*()> CDPL::Util::ObjectStack< T >::ConstructorFunction |
A generic wrapper for functions creating new instances of ObjectType.
| typedef std::function<void(ObjectType&)> CDPL::Util::ObjectStack< T >::ObjectFunction |
A generic wrapper for functions operating on instances of ObjectType.
|
inline |
Copy constructor.
| stack | The other ObjectStack instance. |
|
inline |
Constructs a default-configured ObjectStack instance using DefaultConstructor as the object factory.
| max_pool_size | The maximum number of objects retained in the pool, or 0 for no limit. |
|
inline |
Constructs a ObjectStack instance using a user-supplied object factory.
| ctor_func | The object factory to use for the creation of new instances. |
| max_pool_size | The maximum number of objects retained in the pool, or 0 for no limit. |
|
inline |
Destructor.
|
inline |
Returns a pointer to an object handed out from the pool.
If no idle object is available a new one is created via the configured object factory. If an init function has been set (see setInitFunction()) it is invoked on the object before it is returned. The pool retains ownership of the object; it must be returned via put() (or putAll()) before the ObjectStack is destroyed.
|
inline |
Returns the most recently borrowed object back to the pool.
If a cleanup function has been set (see setCleanupFunction()) it is invoked on the returned object. Excess objects above the configured maximum pool size are destroyed.
|
inline |
Returns all currently borrowed objects back to the pool.
If a cleanup function has been set (see setCleanupFunction()) it is invoked on each returned object. Excess objects above the configured maximum pool size are destroyed.
|
inline |
Returns the currently configured maximum pool size.
|
inline |
Sets the maximum pool size.
Excess idle objects above the new pool size are destroyed. Currently borrowed objects are never destroyed by this method.
| max_size | The new maximum pool size, or 0 for no limit. |
|
inline |
Releases memory held by the pool.
If unused_only is true, only idle (currently not borrowed) objects are destroyed. If unused_only is false, all objects (including currently borrowed ones) are destroyed and any pointers obtained via get() are invalidated.
| unused_only | If true, only idle objects are destroyed. |
|
inline |
Sets the function to be invoked on an object when it is handed out by get().
| func | The function to invoke on each newly handed-out object. |
|
inline |
|
inline |
Copy assignment operator.
| stack | The other ObjectStack instance. |
*this is left intact.