29 #ifndef CDPL_UTIL_OBJECTSTACK_HPP
30 #define CDPL_UTIL_OBJECTSTACK_HPP
35 #include <boost/ptr_container/ptr_vector.hpp>
97 maxSize(stack.maxSize), freeIndex(0), ctorFunc(stack.ctorFunc),
98 initFunc(stack.initFunc), cleanFunc(stack.cleanFunc) {}
112 template <
typename C>
114 maxSize(max_pool_size), freeIndex(0), ctorFunc(ctor_func)
134 if (freeIndex == allocObjects.size())
135 allocObjects.push_back(ctorFunc());
157 cleanFunc(allocObjects[freeIndex]);
160 if (maxSize > 0 && freeIndex <= maxSize && allocObjects.size() > maxSize)
161 allocObjects.erase(allocObjects.begin() + maxSize, allocObjects.end());
173 std::for_each(allocObjects.begin(), allocObjects.begin() + freeIndex,
177 if (maxSize > 0 && allocObjects.size() > maxSize)
178 allocObjects.erase(allocObjects.begin() + maxSize, allocObjects.end());
202 if (maxSize > 0 && allocObjects.size() > maxSize)
203 allocObjects.erase(allocObjects.begin() + std::max(freeIndex, maxSize), allocObjects.end());
218 allocObjects.erase(allocObjects.begin() + freeIndex, allocObjects.end());
223 allocObjects.clear();
255 maxSize = stack.maxSize;
256 ctorFunc = stack.ctorFunc;
257 initFunc = stack.initFunc;
259 if (maxSize > 0 && allocObjects.size() > maxSize)
260 allocObjects.erase(allocObjects.begin() + std::max(freeIndex, maxSize), allocObjects.end());
266 typedef boost::ptr_vector<ObjectType> AllocObjectList;
269 std::size_t freeIndex;
270 AllocObjectList allocObjects;
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:151
std::function< ObjectType *()> ConstructorFunction
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:239
void putAll()
Returns all currently borrowed objects back to the pool.
Definition: ObjectStack.hpp:170
ObjectStack & operator=(const ObjectStack &stack)
Copy assignment operator.
Definition: ObjectStack.hpp:250
void setMaxSize(std::size_t max_size)
Sets the maximum pool size.
Definition: ObjectStack.hpp:198
ObjectType * get()
Returns a pointer to an object handed out from the pool.
Definition: ObjectStack.hpp:132
void freeMemory(bool unused_only=true)
Releases memory held by the pool.
Definition: ObjectStack.hpp:215
std::function< void(ObjectType &)> ObjectFunction
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:185
ObjectStack(const C &ctor_func, std::size_t max_pool_size)
Constructs a ObjectStack instance using a user-supplied object factory.
Definition: ObjectStack.hpp:113
void setInitFunction(const ObjectFunction &func)
Sets the function to be invoked on an object when it is handed out by get().
Definition: ObjectStack.hpp:230
ObjectStack(std::size_t max_pool_size=0)
Constructs a default-configured ObjectStack instance using DefaultConstructor as the object factory.
Definition: ObjectStack.hpp:104
ObjectStack(const ObjectStack &stack)
Copy constructor.
Definition: ObjectStack.hpp:96
~ObjectStack()
Destructor.
Definition: ObjectStack.hpp:120
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
Creates a new instance of ObjectType via new.
Definition: ObjectStack.hpp:84