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 
84  T* operator()() const
85  {
86  return new T();
87  }
88  };
89 
96  ObjectStack(const ObjectStack& stack):
97  maxSize(stack.maxSize), freeIndex(0), ctorFunc(stack.ctorFunc),
98  initFunc(stack.initFunc), cleanFunc(stack.cleanFunc) {}
99 
104  ObjectStack(std::size_t max_pool_size = 0):
105  maxSize(max_pool_size), freeIndex(0), ctorFunc(DefaultConstructor()) {}
106 
112  template <typename C>
113  ObjectStack(const C& ctor_func, std::size_t max_pool_size):
114  maxSize(max_pool_size), freeIndex(0), ctorFunc(ctor_func)
115  {}
116 
121 
133  {
134  if (freeIndex == allocObjects.size())
135  allocObjects.push_back(ctorFunc());
136 
137  ObjectType& obj = allocObjects[freeIndex++];
138 
139  if (initFunc)
140  initFunc(obj);
141 
142  return &obj;
143  }
144 
151  void put()
152  {
153  if (freeIndex > 0) {
154  freeIndex--;
155 
156  if (cleanFunc)
157  cleanFunc(allocObjects[freeIndex]);
158  }
159 
160  if (maxSize > 0 && freeIndex <= maxSize && allocObjects.size() > maxSize)
161  allocObjects.erase(allocObjects.begin() + maxSize, allocObjects.end());
162  }
163 
170  void putAll()
171  {
172  if (cleanFunc)
173  std::for_each(allocObjects.begin(), allocObjects.begin() + freeIndex,
174  [this](ObjectType& obj) { cleanFunc(obj); });
175  freeIndex = 0;
176 
177  if (maxSize > 0 && allocObjects.size() > maxSize)
178  allocObjects.erase(allocObjects.begin() + maxSize, allocObjects.end());
179  }
180 
185  std::size_t getMaxSize() const
186  {
187  return maxSize;
188  }
189 
198  void setMaxSize(std::size_t max_size)
199  {
200  maxSize = max_size;
201 
202  if (maxSize > 0 && allocObjects.size() > maxSize)
203  allocObjects.erase(allocObjects.begin() + std::max(freeIndex, maxSize), allocObjects.end());
204  }
205 
215  void freeMemory(bool unused_only = true)
216  {
217  if (unused_only) {
218  allocObjects.erase(allocObjects.begin() + freeIndex, allocObjects.end());
219  return;
220  }
221 
222  freeIndex = 0;
223  allocObjects.clear();
224  }
225 
230  void setInitFunction(const ObjectFunction& func)
231  {
232  initFunc = func;
233  }
234 
240  {
241  cleanFunc = func;
242  }
243 
251  {
252  if (this == &stack)
253  return *this;
254 
255  maxSize = stack.maxSize;
256  ctorFunc = stack.ctorFunc;
257  initFunc = stack.initFunc;
258 
259  if (maxSize > 0 && allocObjects.size() > maxSize)
260  allocObjects.erase(allocObjects.begin() + std::max(freeIndex, maxSize), allocObjects.end());
261 
262  return *this;
263  }
264 
265  private:
266  typedef boost::ptr_vector<ObjectType> AllocObjectList;
267 
268  std::size_t maxSize;
269  std::size_t freeIndex;
270  AllocObjectList allocObjects;
271  ConstructorFunction ctorFunc;
272  ObjectFunction initFunc;
273  ObjectFunction cleanFunc;
274  };
275  } // namespace Util
276 } // namespace CDPL
277 
278 #endif // CDPL_UTIL_OBJECTSTACK_HPP
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