![]() |
Chemical Data Processing Library C++ API - Version 1.4.0
|
Data structure that caches instances of type T up to a user specified amount.
More...
#include <ObjectPool.hpp>
Inheritance diagram for CDPL::Util::ObjectPool< T >:Classes | |
| struct | DefaultConstructor |
Default object factory which creates new instances of ObjectType via new. More... | |
| struct | DefaultDestructor |
Default object destructor which destroys instances of ObjectType via delete. More... | |
Public Types | |
| typedef T | ObjectType |
| The type of the pooled objects. More... | |
| typedef std::shared_ptr< ObjectType > | SharedObjectPointer |
| A smart pointer to a borrowed object that returns the object to the pool on destruction. More... | |
| typedef std::function< ObjectType *()> | ConstructorFunction |
Generic wrapper for functions creating new instances of ObjectType. More... | |
| typedef std::function< void(ObjectType *)> | DestructorFunction |
Generic wrapper for functions destroying instances of ObjectType. More... | |
| typedef std::function< void(ObjectType &)> | ObjectFunction |
Generic wrapper for functions operating on instances of ObjectType. More... | |
Public Member Functions | |
| ObjectPool (const ObjectPool &pool) | |
| Copy constructor. More... | |
| ObjectPool (std::size_t max_size=0) | |
Constructs a default-configured ObjectPool instance using DefaultConstructor and DefaultDestructor. More... | |
| template<typename C , typename D > | |
| ObjectPool (const C &ctor_func, const D &dtor_func, std::size_t max_size=0) | |
Constructs an ObjectPool instance using a user-supplied factory and destructor. More... | |
| ~ObjectPool () | |
| Destructor. Destroys all currently idle pool entries. More... | |
| SharedObjectPointer | get () |
| Returns a smart pointer to a pool-owned object. More... | |
| std::size_t | getSize () const |
| Returns the current number of idle objects in 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 () |
| Destroys all currently idle pool entries and releases their memory. 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 to the pool. More... | |
| ObjectPool & | operator= (const ObjectPool &pool) |
| Copy assignment operator. More... | |
Data structure that caches instances of type T up to a user specified amount.
Instances of type T that are allocated via this pool (see get()) are preferentially taken from an internally maintained list of previously allocated but now unused objects. If there are no free objects a new object instance will be created on the fly. Allocated objects are returned as smart pointers which keep track of all current references to the object. If the reference count drops to zero at some point, the object is automatically returned to the pool it was allocated from.
Warning: Due to this automatic return of unused objects it is necessary that all objects have returned to their source pool before the pool instance gets destroyed!!!!
| typedef T CDPL::Util::ObjectPool< T >::ObjectType |
The type of the pooled objects.
| typedef std::shared_ptr<ObjectType> CDPL::Util::ObjectPool< T >::SharedObjectPointer |
A smart pointer to a borrowed object that returns the object to the pool on destruction.
| typedef std::function<ObjectType*()> CDPL::Util::ObjectPool< T >::ConstructorFunction |
Generic wrapper for functions creating new instances of ObjectType.
| typedef std::function<void(ObjectType*)> CDPL::Util::ObjectPool< T >::DestructorFunction |
Generic wrapper for functions destroying instances of ObjectType.
| typedef std::function<void(ObjectType&)> CDPL::Util::ObjectPool< T >::ObjectFunction |
Generic wrapper for functions operating on instances of ObjectType.
|
inline |
Copy constructor.
| pool | The other ObjectPool instance. |
|
inline |
Constructs a default-configured ObjectPool instance using DefaultConstructor and DefaultDestructor.
| max_size | The maximum number of objects retained in the pool, or 0 for no limit. |
|
inline |
Constructs an ObjectPool instance using a user-supplied factory and destructor.
| ctor_func | The factory used to create new instances. |
| dtor_func | The destructor used to release instances. |
| max_size | The maximum number of objects retained in the pool, or 0 for no limit. |
|
inline |
Destructor. Destroys all currently idle pool entries.
|
inline |
Returns a smart pointer to a pool-owned object.
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 object is automatically returned to the pool when the last reference to the smart pointer is released.
|
inline |
Returns the current number of idle objects in the pool.
|
inline |
Returns the currently configured maximum pool size.
|
inline |
Sets the maximum pool size.
Excess idle objects above the new pool size are destroyed.
| max_size | The new maximum pool size, or 0 for no limit. |
|
inline |
Destroys all currently idle pool entries and releases their memory.
|
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 |
Sets the function to be invoked on an object when it is returned to the pool.
| func | The function to invoke on each returned object. |
|
inline |
Copy assignment operator.
| pool | The other ObjectPool instance. |