29 #ifndef CDPL_UTIL_OBJECTPOOL_HPP
30 #define CDPL_UTIL_OBJECTPOOL_HPP
127 maxSize(pool.maxSize), ctorFunc(pool.ctorFunc), dtorFunc(pool.dtorFunc),
128 initFunc(pool.initFunc), cleanFunc(pool.cleanFunc) {}
143 template <
typename C,
typename D>
144 ObjectPool(
const C& ctor_func,
const D& dtor_func, std::size_t max_size = 0):
145 maxSize(max_size), ctorFunc(ctor_func), dtorFunc(dtor_func)
155 std::for_each(pool.begin(), pool.end(), dtorFunc);
180 throw std::bad_alloc();
228 std::for_each(pool.begin(), pool.end(), dtorFunc);
263 maxSize = pool.maxSize;
264 ctorFunc = pool.ctorFunc;
265 dtorFunc = pool.dtorFunc;
266 initFunc = pool.initFunc;
267 cleanFunc = pool.cleanFunc;
275 void shrinkToMaxSize()
280 while (pool.size() > maxSize) {
281 dtorFunc(pool.back());
302 if (maxSize > 0 && pool.size() >= maxSize) {
318 typedef std::vector<ObjectType*> PooledObjectList;
321 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:195
void setCleanupFunction(const ObjectFunction &func)
Sets the function to be invoked on an object when it is returned to the pool.
Definition: ObjectPool.hpp:246
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:226
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:144
SharedObjectPointer get()
Returns a smart pointer to a pool-owned object.
Definition: ObjectPool.hpp:168
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:237
ObjectPool & operator=(const ObjectPool &pool)
Copy assignment operator.
Definition: ObjectPool.hpp:258
ObjectPool(const ObjectPool &pool)
Copy constructor.
Definition: ObjectPool.hpp:126
ObjectPool(std::size_t max_size=0)
Constructs a default-configured ObjectPool instance using DefaultConstructor and DefaultDestructor.
Definition: ObjectPool.hpp:134
void setMaxSize(std::size_t max_size)
Sets the maximum pool size.
Definition: ObjectPool.hpp:216
std::size_t getMaxSize() const
Returns the currently configured maximum pool size.
Definition: ObjectPool.hpp:204
~ObjectPool()
Destructor. Destroys all currently idle pool entries.
Definition: ObjectPool.hpp:153
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
Creates a new instance of ObjectType via new.
Definition: ObjectPool.hpp:98
Default object destructor which destroys instances of ObjectType via delete.
Definition: ObjectPool.hpp:108
void operator()(T *obj) const
Destroys the instance pointed to by obj via delete.
Definition: ObjectPool.hpp:114