Chemical Data Processing Library C++ API - Version 1.4.0
ObjectStack.hpp
Go to the documentation of this file.
1 /*
2  * ObjectStack.hpp
3  *
4  * This file is part of the Chemical Data Processing Toolkit
5  *
6  * Copyright (C) 2003 Thomas Seidel <thomas.seidel@univie.ac.at>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; see the file COPYING. If not, write to
20  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 
29 #ifndef CDPL_UTIL_OBJECTSTACK_HPP
30 #define CDPL_UTIL_OBJECTSTACK_HPP
31 
32 #include <cstddef>
33 #include <algorithm>
34 
35 #include <boost/ptr_container/ptr_vector.hpp>
36 
37 
38 namespace CDPL
39 {
40 
41  namespace Util
42  {
43 
54  template <typename T>
56  {
57 
58  public:
62  typedef T ObjectType;
63 
67  typedef std::function<ObjectType*()> ConstructorFunction;
68 
72  typedef std::function<void(ObjectType&)> ObjectFunction;
73 
78  {
79 
80  T* operator()() const
81  {
82  return new T();
83  }
84  };
85 
92  ObjectStack(const ObjectStack& stack):
93  maxSize(stack.maxSize), freeIndex(0), ctorFunc(stack.ctorFunc),
94  initFunc(stack.initFunc), cleanFunc(stack.cleanFunc) {}
95 
100  ObjectStack(std::size_t max_pool_size = 0):
101  maxSize(max_pool_size), freeIndex(0), ctorFunc(DefaultConstructor()) {}
102 
108  template <typename C>
109  ObjectStack(const C& ctor_func, std::size_t max_pool_size):
110  maxSize(max_pool_size), freeIndex(0), ctorFunc(ctor_func)
111  {}
112 
117 
129  {
130  if (freeIndex == allocObjects.size())
131  allocObjects.push_back(ctorFunc());
132 
133  ObjectType& obj = allocObjects[freeIndex++];
134 
135  if (initFunc)
136  initFunc(obj);
137 
138  return &obj;
139  }
140 
147  void put()
148  {
149  if (freeIndex > 0) {
150  freeIndex--;
151 
152  if (cleanFunc)
153  cleanFunc(allocObjects[freeIndex]);
154  }
155 
156  if (maxSize > 0 && freeIndex <= maxSize && allocObjects.size() > maxSize)
157  allocObjects.erase(allocObjects.begin() + maxSize, allocObjects.end());
158  }
159 
166  void putAll()
167  {
168  if (cleanFunc)
169  std::for_each(allocObjects.begin(), allocObjects.begin() + freeIndex,
170  [this](ObjectType& obj) { cleanFunc(obj); });
171  freeIndex = 0;
172 
173  if (maxSize > 0 && allocObjects.size() > maxSize)
174  allocObjects.erase(allocObjects.begin() + maxSize, allocObjects.end());
175  }
176 
181  std::size_t getMaxSize() const
182  {
183  return maxSize;
184  }
185 
194  void setMaxSize(std::size_t max_size)
195  {
196  maxSize = max_size;
197 
198  if (maxSize > 0 && allocObjects.size() > maxSize)
199  allocObjects.erase(allocObjects.begin() + std::max(freeIndex, maxSize), allocObjects.end());
200  }
201 
211  void freeMemory(bool unused_only = true)
212  {
213  if (unused_only) {
214  allocObjects.erase(allocObjects.begin() + freeIndex, allocObjects.end());
215  return;
216  }
217 
218  freeIndex = 0;
219  allocObjects.clear();
220  }
221 
226  void setInitFunction(const ObjectFunction& func)
227  {
228  initFunc = func;
229  }
230 
236  {
237  cleanFunc = func;
238  }
239 
247  {
248  if (this == &stack)
249  return *this;
250 
251  maxSize = stack.maxSize;
252  ctorFunc = stack.ctorFunc;
253  initFunc = stack.initFunc;
254 
255  if (maxSize > 0 && allocObjects.size() > maxSize)
256  allocObjects.erase(allocObjects.begin() + std::max(freeIndex, maxSize), allocObjects.end());
257 
258  return *this;
259  }
260 
261  private:
262  typedef boost::ptr_vector<ObjectType> AllocObjectList;
263 
264  std::size_t maxSize;
265  std::size_t freeIndex;
266  AllocObjectList allocObjects;
267  ConstructorFunction ctorFunc;
268  ObjectFunction initFunc;
269  ObjectFunction cleanFunc;
270  };
271  } // namespace Util
272 } // namespace CDPL
273 
274 #endif // CDPL_UTIL_OBJECTSTACK_HPP
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