|
Chemical Data Processing Library C++ API - Version 1.0.0
|
Go to the documentation of this file.
29 #ifndef CDPL_UTIL_OBJECTPOOL_HPP
30 #define CDPL_UTIL_OBJECTPOOL_HPP
89 maxSize(pool.maxSize), ctorFunc(pool.ctorFunc), dtorFunc(pool.dtorFunc),
90 initFunc(pool.initFunc), cleanFunc(pool.cleanFunc) {}
93 maxSize(max_size), ctorFunc(DefaultConstructor()), dtorFunc(DefaultDestructor()) {}
95 template <
typename C,
typename D>
96 ObjectPool(
const C& ctor_func,
const D& dtor_func, std::size_t max_size = 0):
97 maxSize(max_size), ctorFunc(ctor_func), dtorFunc(dtor_func)
102 std::for_each(pool.begin(), pool.end(), dtorFunc);
117 throw std::bad_alloc();
147 std::for_each(pool.begin(), pool.end(), dtorFunc);
167 maxSize = pool.maxSize;
168 ctorFunc = pool.ctorFunc;
169 dtorFunc = pool.dtorFunc;
170 initFunc = pool.initFunc;
178 void shrinkToMaxSize()
183 while (pool.size() > maxSize) {
184 dtorFunc(pool.back());
205 if (maxSize > 0 && pool.size() >= maxSize) {
221 typedef std::vector<ObjectType*> PooledObjectList;
224 PooledObjectList pool;
233 #endif // CDPL_UTIL_OBJECTPOOL_HPP
ObjectPool(const C &ctor_func, const D &dtor_func, std::size_t max_size=0)
Definition: ObjectPool.hpp:96
std::size_t getSize() const
Definition: ObjectPool.hpp:128
ObjectPool & operator=(const ObjectPool &pool)
Definition: ObjectPool.hpp:162
~ObjectPool()
Definition: ObjectPool.hpp:100
void operator()(T *obj) const
Definition: ObjectPool.hpp:82
SharedObjectPointer get()
Definition: ObjectPool.hpp:105
void setMaxSize(std::size_t max_size)
Definition: ObjectPool.hpp:138
T * operator()() const
Definition: ObjectPool.hpp:73
std::shared_ptr< ObjectType > SharedObjectPointer
Definition: ObjectPool.hpp:64
ObjectPool(std::size_t max_size=0)
Definition: ObjectPool.hpp:92
std::size_t getMaxSize() const
Definition: ObjectPool.hpp:133
Definition: ObjectPool.hpp:71
std::function< void(ObjectType *)> DestructorFunction
Definition: ObjectPool.hpp:67
std::function< void(ObjectType &)> ObjectFunction
Definition: ObjectPool.hpp:68
A data structure that caches instances of type T up to a user specified amount.
Definition: ObjectPool.hpp:59
ObjectPool(const ObjectPool &pool)
Definition: ObjectPool.hpp:88
const unsigned int T
Specifies Hydrogen (Tritium).
Definition: AtomType.hpp:67
Definition: ObjectPool.hpp:80
T ObjectType
Definition: ObjectPool.hpp:62
The namespace of the Chemical Data Processing Library.
void setInitFunction(const ObjectFunction &func)
Definition: ObjectPool.hpp:152
void freeMemory()
Definition: ObjectPool.hpp:145
const unsigned int D
Specifies Hydrogen (Deuterium).
Definition: AtomType.hpp:62
void setCleanupFunction(const ObjectFunction &func)
Definition: ObjectPool.hpp:157
std::function< ObjectType *()> ConstructorFunction
Definition: ObjectPool.hpp:66
const unsigned int C
Specifies Carbon.
Definition: AtomType.hpp:92