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

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...
 
ObjectTypeget ()
 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...
 
ObjectStackoperator= (const ObjectStack &stack)
 Copy assignment operator. More...
 

Detailed Description

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

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.

Template Parameters
TThe type of the pooled objects.

Member Typedef Documentation

◆ ObjectType

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

The type of the pooled objects.

◆ ConstructorFunction

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

A generic wrapper for functions creating new instances of ObjectType.

◆ ObjectFunction

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

A generic wrapper for functions operating on instances of ObjectType.

Constructor & Destructor Documentation

◆ ObjectStack() [1/3]

template<typename T >
CDPL::Util::ObjectStack< T >::ObjectStack ( const ObjectStack< T > &  stack)
inline

Copy constructor.

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

◆ ObjectStack() [2/3]

template<typename T >
CDPL::Util::ObjectStack< T >::ObjectStack ( std::size_t  max_pool_size = 0)
inline

Constructs a default-configured ObjectStack instance using DefaultConstructor as the object factory.

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

◆ ObjectStack() [3/3]

template<typename T >
template<typename C >
CDPL::Util::ObjectStack< T >::ObjectStack ( const C &  ctor_func,
std::size_t  max_pool_size 
)
inline

Constructs a ObjectStack instance using a user-supplied object factory.

Parameters
ctor_funcThe object factory to use for the creation of new instances.
max_pool_sizeThe maximum number of objects retained in the pool, or 0 for no limit.

◆ ~ObjectStack()

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

Destructor.

Member Function Documentation

◆ get()

template<typename T >
ObjectType* CDPL::Util::ObjectStack< T >::get ( )
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.

Returns
A pointer to a borrowed object.

◆ put()

template<typename T >
void CDPL::Util::ObjectStack< T >::put ( )
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.

◆ putAll()

template<typename T >
void CDPL::Util::ObjectStack< T >::putAll ( )
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.

◆ getMaxSize()

template<typename T >
std::size_t CDPL::Util::ObjectStack< 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::ObjectStack< T >::setMaxSize ( std::size_t  max_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.

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

◆ freeMemory()

template<typename T >
void CDPL::Util::ObjectStack< T >::freeMemory ( bool  unused_only = true)
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.

Parameters
unused_onlyIf true, only idle objects are destroyed.

◆ setInitFunction()

template<typename T >
void CDPL::Util::ObjectStack< 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::ObjectStack< T >::setCleanupFunction ( const ObjectFunction func)
inline

Sets the function to be invoked on an object when it is returned by put() or putAll().

Parameters
funcThe function to invoke on each returned object.

◆ operator=()

template<typename T >
ObjectStack& CDPL::Util::ObjectStack< T >::operator= ( const ObjectStack< T > &  stack)
inline

Copy assignment operator.

Parameters
stackThe other ObjectStack instance.
Returns
A reference to itself.
Note
The pool of allocated objects of stack is not copied; the pool of *this is left intact.

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