Chemical Data Processing Library C++ API - Version 1.1.1
ObjectPool.hpp
Go to the documentation of this file.
1 /*
2  * ObjectPool.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_OBJECTPOOL_HPP
30 #define CDPL_UTIL_OBJECTPOOL_HPP
31 
32 #include <vector>
33 #include <cstddef>
34 #include <algorithm>
35 #include <new>
36 #include <memory>
37 #include <functional>
38 
39 
40 namespace CDPL
41 {
42 
43  namespace Util
44  {
45 
58  template <typename T>
59  class ObjectPool
60  {
61 
62  public:
63  typedef T ObjectType;
64 
65  typedef std::shared_ptr<ObjectType> SharedObjectPointer;
66 
67  typedef std::function<ObjectType*()> ConstructorFunction;
68  typedef std::function<void(ObjectType*)> DestructorFunction;
69  typedef std::function<void(ObjectType&)> ObjectFunction;
70 
72  {
73 
74  T* operator()() const
75  {
76  return new T();
77  }
78  };
79 
81  {
82 
83  void operator()(T* obj) const
84  {
85  delete obj;
86  }
87  };
88 
89  ObjectPool(const ObjectPool& pool):
90  maxSize(pool.maxSize), ctorFunc(pool.ctorFunc), dtorFunc(pool.dtorFunc),
91  initFunc(pool.initFunc), cleanFunc(pool.cleanFunc) {}
92 
93  ObjectPool(std::size_t max_size = 0):
94  maxSize(max_size), ctorFunc(DefaultConstructor()), dtorFunc(DefaultDestructor()) {}
95 
96  template <typename C, typename D>
97  ObjectPool(const C& ctor_func, const D& dtor_func, std::size_t max_size = 0):
98  maxSize(max_size), ctorFunc(ctor_func), dtorFunc(dtor_func)
99  {}
100 
102  {
103  std::for_each(pool.begin(), pool.end(), dtorFunc);
104  }
105 
107  {
108  ObjectType* obj;
109 
110  if (!pool.empty()) {
111  obj = pool.back();
112  pool.pop_back();
113 
114  } else {
115  obj = ctorFunc();
116 
117  if (!obj)
118  throw std::bad_alloc();
119  }
120 
121  SharedObjectPointer obj_ptr(obj, PutObject(*this));
122 
123  if (initFunc)
124  initFunc(*obj);
125 
126  return obj_ptr;
127  }
128 
129  std::size_t getSize() const
130  {
131  return pool.size();
132  }
133 
134  std::size_t getMaxSize() const
135  {
136  return maxSize;
137  }
138 
139  void setMaxSize(std::size_t max_size)
140  {
141  maxSize = max_size;
142 
143  shrinkToMaxSize();
144  }
145 
146  void freeMemory()
147  {
148  std::for_each(pool.begin(), pool.end(), dtorFunc);
149 
150  pool.clear();
151  }
152 
153  void setInitFunction(const ObjectFunction& func)
154  {
155  initFunc = func;
156  }
157 
159  {
160  cleanFunc = func;
161  }
162 
164  {
165  if (this == &pool)
166  return *this;
167 
168  maxSize = pool.maxSize;
169  ctorFunc = pool.ctorFunc;
170  dtorFunc = pool.dtorFunc;
171  initFunc = pool.initFunc;
172 
173  shrinkToMaxSize();
174 
175  return *this;
176  }
177 
178  private:
179  void shrinkToMaxSize()
180  {
181  if (maxSize == 0)
182  return;
183 
184  while (pool.size() > maxSize) {
185  dtorFunc(pool.back());
186  pool.pop_back();
187  }
188  }
189 
190  struct PutObject
191  {
192 
193  PutObject(ObjectPool& pool):
194  pool(pool) {}
195 
196  void operator()(ObjectType* obj) const
197  {
198  pool.put(obj);
199  }
200 
201  ObjectPool& pool;
202  };
203 
204  void put(ObjectType* obj)
205  {
206  if (maxSize > 0 && pool.size() >= maxSize) {
207  dtorFunc(obj);
208  return;
209  }
210 
211  try {
212  if (cleanFunc)
213  cleanFunc(*obj);
214 
215  pool.push_back(obj);
216 
217  } catch (...) {
218  dtorFunc(obj);
219  }
220  }
221 
222  typedef std::vector<ObjectType*> PooledObjectList;
223 
224  std::size_t maxSize;
225  PooledObjectList pool;
226  ConstructorFunction ctorFunc;
227  DestructorFunction dtorFunc;
228  ObjectFunction initFunc;
229  ObjectFunction cleanFunc;
230  };
231  } // namespace Util
232 } // namespace CDPL
233 
234 #endif // CDPL_UTIL_OBJECTPOOL_HPP
CDPL::Util::ObjectPool::ObjectPool
ObjectPool(const C &ctor_func, const D &dtor_func, std::size_t max_size=0)
Definition: ObjectPool.hpp:97
CDPL::Util::ObjectPool::getSize
std::size_t getSize() const
Definition: ObjectPool.hpp:129
CDPL::Util::ObjectPool::operator=
ObjectPool & operator=(const ObjectPool &pool)
Definition: ObjectPool.hpp:163
CDPL::Util::ObjectPool::~ObjectPool
~ObjectPool()
Definition: ObjectPool.hpp:101
CDPL::Util::ObjectPool::DefaultDestructor::operator()
void operator()(T *obj) const
Definition: ObjectPool.hpp:83
CDPL::Util::ObjectPool::get
SharedObjectPointer get()
Definition: ObjectPool.hpp:106
CDPL::Util::ObjectPool::setMaxSize
void setMaxSize(std::size_t max_size)
Definition: ObjectPool.hpp:139
VectorArray< Vector3D >
CDPL::Util::ObjectPool::DefaultConstructor::operator()
T * operator()() const
Definition: ObjectPool.hpp:74
CDPL::Util::ObjectPool::SharedObjectPointer
std::shared_ptr< ObjectType > SharedObjectPointer
Definition: ObjectPool.hpp:65
CDPL::Util::ObjectPool::ObjectPool
ObjectPool(std::size_t max_size=0)
Definition: ObjectPool.hpp:93
CDPL::Util::ObjectPool::getMaxSize
std::size_t getMaxSize() const
Definition: ObjectPool.hpp:134
CDPL::Util::ObjectPool::DefaultConstructor
Definition: ObjectPool.hpp:72
CDPL::Util::ObjectPool::DestructorFunction
std::function< void(ObjectType *)> DestructorFunction
Definition: ObjectPool.hpp:68
CDPL::Util::ObjectPool::ObjectFunction
std::function< void(ObjectType &)> ObjectFunction
Definition: ObjectPool.hpp:69
CDPL::Util::ObjectPool
A data structure that caches instances of type T up to a user specified amount.
Definition: ObjectPool.hpp:60
CDPL::Util::ObjectPool::ObjectPool
ObjectPool(const ObjectPool &pool)
Definition: ObjectPool.hpp:89
CDPL::Chem::AtomType::T
const unsigned int T
Specifies Hydrogen (Tritium).
Definition: AtomType.hpp:67
CDPL::Util::ObjectPool::DefaultDestructor
Definition: ObjectPool.hpp:81
CDPL::Util::ObjectPool::ObjectType
T ObjectType
Definition: ObjectPool.hpp:63
CDPL
The namespace of the Chemical Data Processing Library.
CDPL::Util::ObjectPool::setInitFunction
void setInitFunction(const ObjectFunction &func)
Definition: ObjectPool.hpp:153
CDPL::Util::ObjectPool::freeMemory
void freeMemory()
Definition: ObjectPool.hpp:146
CDPL::Chem::AtomType::D
const unsigned int D
Specifies Hydrogen (Deuterium).
Definition: AtomType.hpp:62
CDPL::Util::ObjectPool::setCleanupFunction
void setCleanupFunction(const ObjectFunction &func)
Definition: ObjectPool.hpp:158
CDPL::Util::ObjectPool< Math::Vector3DArray >::ConstructorFunction
std::function< ObjectType *()> ConstructorFunction
Definition: ObjectPool.hpp:67
CDPL::Chem::AtomType::C
const unsigned int C
Specifies Carbon.
Definition: AtomType.hpp:92