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