Chemical Data Processing Library C++ API - Version 1.4.0
Classes | Public Types | Public Member Functions | List of all members
CDPL::Util::ObjectPool< T > Class Template Reference

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< ObjectTypeSharedObjectPointer
 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...
 
ObjectPooloperator= (const ObjectPool &pool)
 Copy assignment operator. More...
 

Detailed Description

template<typename T>
class CDPL::Util::ObjectPool< T >

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!!!!

Member Typedef Documentation

◆ ObjectType

template<typename T >
typedef T CDPL::Util::ObjectPool< T >::ObjectType

The type of the pooled objects.

◆ SharedObjectPointer

template<typename T >
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.

◆ ConstructorFunction

template<typename T >
typedef std::function<ObjectType*()> CDPL::Util::ObjectPool< T >::ConstructorFunction

Generic wrapper for functions creating new instances of ObjectType.

◆ DestructorFunction

template<typename T >
typedef std::function<void(ObjectType*)> CDPL::Util::ObjectPool< T >::DestructorFunction

Generic wrapper for functions destroying instances of ObjectType.

◆ ObjectFunction

template<typename T >
typedef std::function<void(ObjectType&)> CDPL::Util::ObjectPool< T >::ObjectFunction

Generic wrapper for functions operating on instances of ObjectType.

Constructor & Destructor Documentation

◆ ObjectPool() [1/3]

template<typename T >
CDPL::Util::ObjectPool< T >::ObjectPool ( const ObjectPool< T > &  pool)
inline

Copy constructor.

Parameters
poolThe other ObjectPool instance.
Note
The pool of allocated objects of pool is not copied; the new instance starts with an empty pool.

◆ ObjectPool() [2/3]

template<typename T >
CDPL::Util::ObjectPool< T >::ObjectPool ( std::size_t  max_size = 0)
inline

Constructs a default-configured ObjectPool instance using DefaultConstructor and DefaultDestructor.

Parameters
max_sizeThe maximum number of objects retained in the pool, or 0 for no limit.

◆ ObjectPool() [3/3]

template<typename T >
template<typename C , typename D >
CDPL::Util::ObjectPool< T >::ObjectPool ( const C &  ctor_func,
const D &  dtor_func,
std::size_t  max_size = 0 
)
inline

Constructs an ObjectPool instance using a user-supplied factory and destructor.

Parameters
ctor_funcThe factory used to create new instances.
dtor_funcThe destructor used to release instances.
max_sizeThe maximum number of objects retained in the pool, or 0 for no limit.

◆ ~ObjectPool()

template<typename T >
CDPL::Util::ObjectPool< T >::~ObjectPool ( )
inline

Destructor. Destroys all currently idle pool entries.

Warning
All previously handed-out smart pointers must have been released before destruction; otherwise the deferred put() will operate on a destroyed pool.

Member Function Documentation

◆ get()

template<typename T >
SharedObjectPointer CDPL::Util::ObjectPool< T >::get ( )
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.

Returns
A smart pointer to a borrowed object.

◆ getSize()

template<typename T >
std::size_t CDPL::Util::ObjectPool< T >::getSize ( ) const
inline

Returns the current number of idle objects in the pool.

Returns
The current pool size.

◆ getMaxSize()

template<typename T >
std::size_t CDPL::Util::ObjectPool< T >::getMaxSize ( ) const
inline

Returns the currently configured maximum pool size.

Returns
The maximum number of objects retained in the pool, or 0 for no limit.

◆ setMaxSize()

template<typename T >
void CDPL::Util::ObjectPool< T >::setMaxSize ( std::size_t  max_size)
inline

Sets the maximum pool size.

Excess idle objects above the new pool size are destroyed.

Parameters
max_sizeThe new maximum pool size, or 0 for no limit.

◆ freeMemory()

template<typename T >
void CDPL::Util::ObjectPool< T >::freeMemory ( )
inline

Destroys all currently idle pool entries and releases their memory.

◆ setInitFunction()

template<typename T >
void CDPL::Util::ObjectPool< T >::setInitFunction ( const ObjectFunction func)
inline

Sets the function to be invoked on an object when it is handed out by get().

Parameters
funcThe function to invoke on each newly handed-out object.

◆ setCleanupFunction()

template<typename T >
void CDPL::Util::ObjectPool< T >::setCleanupFunction ( const ObjectFunction func)
inline

Sets the function to be invoked on an object when it is returned to the pool.

Parameters
funcThe function to invoke on each returned object.

◆ operator=()

template<typename T >
ObjectPool& CDPL::Util::ObjectPool< T >::operator= ( const ObjectPool< T > &  pool)
inline

Copy assignment operator.

Parameters
poolThe other ObjectPool instance.
Returns
A reference to itself.
Note
The pool of allocated objects of pool is not copied; only the configuration (max size, factory, destructor, cleanup and init function) is taken over.

The documentation for this class was generated from the following file: