29 #ifndef CDPL_UTIL_OBJECTPOOL_HPP
30 #define CDPL_UTIL_OBJECTPOOL_HPP
119 maxSize(pool.maxSize), ctorFunc(pool.ctorFunc), dtorFunc(pool.dtorFunc),
120 initFunc(pool.initFunc), cleanFunc(pool.cleanFunc) {}
135 template <
typename C,
typename D>
136 ObjectPool(
const C& ctor_func,
const D& dtor_func, std::size_t max_size = 0):
137 maxSize(max_size), ctorFunc(ctor_func), dtorFunc(dtor_func)
147 std::for_each(pool.begin(), pool.end(), dtorFunc);
172 throw std::bad_alloc();
220 std::for_each(pool.begin(), pool.end(), dtorFunc);
255 maxSize = pool.maxSize;
256 ctorFunc = pool.ctorFunc;
257 dtorFunc = pool.dtorFunc;
258 initFunc = pool.initFunc;
259 cleanFunc = pool.cleanFunc;
267 void shrinkToMaxSize()
272 while (pool.size() > maxSize) {
273 dtorFunc(pool.back());
294 if (maxSize > 0 && pool.size() >= maxSize) {
310 typedef std::vector<ObjectType*> PooledObjectList;
313 PooledObjectList pool;
Data structure that caches instances of type T up to a user specified amount.
Definition: ObjectPool.hpp:60
std::size_t getSize() const
Returns the current number of idle objects in the pool.
Definition: ObjectPool.hpp:187
void setCleanupFunction(const ObjectFunction &func)
Sets the function to be invoked on an object when it is returned to the pool.
Definition: ObjectPool.hpp:238
std::shared_ptr< ObjectType > SharedObjectPointer
A smart pointer to a borrowed object that returns the object to the pool on destruction.
Definition: ObjectPool.hpp:71
void freeMemory()
Destroys all currently idle pool entries and releases their memory.
Definition: ObjectPool.hpp:218
std::function< void(ObjectType *)> DestructorFunction
Generic wrapper for functions destroying instances of ObjectType.
Definition: ObjectPool.hpp:81
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.
Definition: ObjectPool.hpp:136
SharedObjectPointer get()
Returns a smart pointer to a pool-owned object.
Definition: ObjectPool.hpp:160
std::function< ObjectType *()> ConstructorFunction
Generic wrapper for functions creating new instances of ObjectType.
Definition: ObjectPool.hpp:76
void setInitFunction(const ObjectFunction &func)
Sets the function to be invoked on an object when it is handed out by get().
Definition: ObjectPool.hpp:229
ObjectPool & operator=(const ObjectPool &pool)
Copy assignment operator.
Definition: ObjectPool.hpp:250
ObjectPool(const ObjectPool &pool)
Copy constructor.
Definition: ObjectPool.hpp:118
ObjectPool(std::size_t max_size=0)
Constructs a default-configured ObjectPool instance using DefaultConstructor and DefaultDestructor.
Definition: ObjectPool.hpp:126
void setMaxSize(std::size_t max_size)
Sets the maximum pool size.
Definition: ObjectPool.hpp:208
std::size_t getMaxSize() const
Returns the currently configured maximum pool size.
Definition: ObjectPool.hpp:196
~ObjectPool()
Destructor. Destroys all currently idle pool entries.
Definition: ObjectPool.hpp:145
std::function< void(ObjectType &)> ObjectFunction
Generic wrapper for functions operating on instances of ObjectType.
Definition: ObjectPool.hpp:86
T ObjectType
The type of the pooled objects.
Definition: ObjectPool.hpp:66
constexpr unsigned int D
Specifies Hydrogen (Deuterium).
Definition: AtomType.hpp:62
constexpr unsigned int T
Specifies Hydrogen (Tritium).
Definition: AtomType.hpp:67
constexpr unsigned int C
Specifies Carbon.
Definition: AtomType.hpp:92
The namespace of the Chemical Data Processing Library.
Default object factory which creates new instances of ObjectType via new.
Definition: ObjectPool.hpp:92
T * operator()() const
Definition: ObjectPool.hpp:94
Default object destructor which destroys instances of ObjectType via delete.
Definition: ObjectPool.hpp:104
void operator()(T *obj) const
Definition: ObjectPool.hpp:106