29 #ifndef CDPL_UTIL_OBJECTPOOL_HPP
30 #define CDPL_UTIL_OBJECTPOOL_HPP
90 maxSize(pool.maxSize), ctorFunc(pool.ctorFunc), dtorFunc(pool.dtorFunc),
91 initFunc(pool.initFunc), cleanFunc(pool.cleanFunc) {}
96 template <
typename C,
typename D>
97 ObjectPool(
const C& ctor_func,
const D& dtor_func, std::size_t max_size = 0):
98 maxSize(max_size), ctorFunc(ctor_func), dtorFunc(dtor_func)
103 std::for_each(pool.begin(), pool.end(), dtorFunc);
118 throw std::bad_alloc();
148 std::for_each(pool.begin(), pool.end(), dtorFunc);
168 maxSize = pool.maxSize;
169 ctorFunc = pool.ctorFunc;
170 dtorFunc = pool.dtorFunc;
171 initFunc = pool.initFunc;
179 void shrinkToMaxSize()
184 while (pool.size() > maxSize) {
185 dtorFunc(pool.back());
206 if (maxSize > 0 && pool.size() >= maxSize) {
222 typedef std::vector<ObjectType*> PooledObjectList;
225 PooledObjectList pool;
A data structure that caches instances of type T up to a user specified amount.
Definition: ObjectPool.hpp:60
std::size_t getSize() const
Definition: ObjectPool.hpp:129
void setCleanupFunction(const ObjectFunction &func)
Definition: ObjectPool.hpp:158
std::shared_ptr< ObjectType > SharedObjectPointer
Definition: ObjectPool.hpp:65
void freeMemory()
Definition: ObjectPool.hpp:146
std::function< void(ObjectType *)> DestructorFunction
Definition: ObjectPool.hpp:68
ObjectPool(const C &ctor_func, const D &dtor_func, std::size_t max_size=0)
Definition: ObjectPool.hpp:97
SharedObjectPointer get()
Definition: ObjectPool.hpp:106
std::function< ObjectType *()> ConstructorFunction
Definition: ObjectPool.hpp:67
void setInitFunction(const ObjectFunction &func)
Definition: ObjectPool.hpp:153
ObjectPool & operator=(const ObjectPool &pool)
Definition: ObjectPool.hpp:163
ObjectPool(const ObjectPool &pool)
Definition: ObjectPool.hpp:89
ObjectPool(std::size_t max_size=0)
Definition: ObjectPool.hpp:93
void setMaxSize(std::size_t max_size)
Definition: ObjectPool.hpp:139
std::size_t getMaxSize() const
Definition: ObjectPool.hpp:134
~ObjectPool()
Definition: ObjectPool.hpp:101
std::function< void(ObjectType &)> ObjectFunction
Definition: ObjectPool.hpp:69
T ObjectType
Definition: ObjectPool.hpp:63
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.
Definition: ObjectPool.hpp:72
T * operator()() const
Definition: ObjectPool.hpp:74
Definition: ObjectPool.hpp:81
void operator()(T *obj) const
Definition: ObjectPool.hpp:83