29 #ifndef CDPL_UTIL_OBJECTSTACK_HPP
30 #define CDPL_UTIL_OBJECTSTACK_HPP
35 #include <boost/ptr_container/ptr_vector.hpp>
93 maxSize(stack.maxSize), freeIndex(0), ctorFunc(stack.ctorFunc),
94 initFunc(stack.initFunc), cleanFunc(stack.cleanFunc) {}
108 template <
typename C>
110 maxSize(max_pool_size), freeIndex(0), ctorFunc(ctor_func)
130 if (freeIndex == allocObjects.size())
131 allocObjects.push_back(ctorFunc());
153 cleanFunc(allocObjects[freeIndex]);
156 if (maxSize > 0 && freeIndex <= maxSize && allocObjects.size() > maxSize)
157 allocObjects.erase(allocObjects.begin() + maxSize, allocObjects.end());
169 std::for_each(allocObjects.begin(), allocObjects.begin() + freeIndex,
173 if (maxSize > 0 && allocObjects.size() > maxSize)
174 allocObjects.erase(allocObjects.begin() + maxSize, allocObjects.end());
198 if (maxSize > 0 && allocObjects.size() > maxSize)
199 allocObjects.erase(allocObjects.begin() + std::max(freeIndex, maxSize), allocObjects.end());
214 allocObjects.erase(allocObjects.begin() + freeIndex, allocObjects.end());
219 allocObjects.clear();
251 maxSize = stack.maxSize;
252 ctorFunc = stack.ctorFunc;
253 initFunc = stack.initFunc;
255 if (maxSize > 0 && allocObjects.size() > maxSize)
256 allocObjects.erase(allocObjects.begin() + std::max(freeIndex, maxSize), allocObjects.end());
262 typedef boost::ptr_vector<ObjectType> AllocObjectList;
265 std::size_t freeIndex;
266 AllocObjectList allocObjects;
A reusable object pool with stack-like borrow/return semantics.
Definition: ObjectStack.hpp:56
void put()
Returns the most recently borrowed object back to the pool.
Definition: ObjectStack.hpp:147
std::function< ObjectType *()> ConstructorFunction
A generic wrapper for functions creating new instances of ObjectType.
Definition: ObjectStack.hpp:67
void setCleanupFunction(const ObjectFunction &func)
Sets the function to be invoked on an object when it is returned by put() or putAll().
Definition: ObjectStack.hpp:235
void putAll()
Returns all currently borrowed objects back to the pool.
Definition: ObjectStack.hpp:166
ObjectStack & operator=(const ObjectStack &stack)
Copy assignment operator.
Definition: ObjectStack.hpp:246
void setMaxSize(std::size_t max_size)
Sets the maximum pool size.
Definition: ObjectStack.hpp:194
ObjectType * get()
Returns a pointer to an object handed out from the pool.
Definition: ObjectStack.hpp:128
void freeMemory(bool unused_only=true)
Releases memory held by the pool.
Definition: ObjectStack.hpp:211
std::function< void(ObjectType &)> ObjectFunction
A generic wrapper for functions operating on instances of ObjectType.
Definition: ObjectStack.hpp:72
std::size_t getMaxSize() const
Returns the currently configured maximum pool size.
Definition: ObjectStack.hpp:181
ObjectStack(const C &ctor_func, std::size_t max_pool_size)
Constructs a ObjectStack instance using a user-supplied object factory.
Definition: ObjectStack.hpp:109
void setInitFunction(const ObjectFunction &func)
Sets the function to be invoked on an object when it is handed out by get().
Definition: ObjectStack.hpp:226
ObjectStack(std::size_t max_pool_size=0)
Constructs a default-configured ObjectStack instance using DefaultConstructor as the object factory.
Definition: ObjectStack.hpp:100
ObjectStack(const ObjectStack &stack)
Copy constructor.
Definition: ObjectStack.hpp:92
~ObjectStack()
Destructor.
Definition: ObjectStack.hpp:116
T ObjectType
The type of the pooled objects.
Definition: ObjectStack.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: ObjectStack.hpp:78
T * operator()() const
Definition: ObjectStack.hpp:80