Chemical Data Processing Library C++ API - Version 1.2.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 
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
A data structure that caches instances of type T up to a user specified amount.
Definition: ObjectPool.hpp:60
std::size_t getSize() const
Definition: ObjectPool.hpp:129
void setCleanupFunction(const ObjectFunction &func)
Definition: ObjectPool.hpp:158
std::shared_ptr< ObjectType > SharedObjectPointer
Definition: ObjectPool.hpp:65
void freeMemory()
Definition: ObjectPool.hpp:146
std::function< void(ObjectType *)> DestructorFunction
Definition: ObjectPool.hpp:68
ObjectPool(const C &ctor_func, const D &dtor_func, std::size_t max_size=0)
Definition: ObjectPool.hpp:97
SharedObjectPointer get()
Definition: ObjectPool.hpp:106
std::function< ObjectType *()> ConstructorFunction
Definition: ObjectPool.hpp:67
void setInitFunction(const ObjectFunction &func)
Definition: ObjectPool.hpp:153
ObjectPool & operator=(const ObjectPool &pool)
Definition: ObjectPool.hpp:163
ObjectPool(const ObjectPool &pool)
Definition: ObjectPool.hpp:89
ObjectPool(std::size_t max_size=0)
Definition: ObjectPool.hpp:93
void setMaxSize(std::size_t max_size)
Definition: ObjectPool.hpp:139
std::size_t getMaxSize() const
Definition: ObjectPool.hpp:134
~ObjectPool()
Definition: ObjectPool.hpp:101
std::function< void(ObjectType &)> ObjectFunction
Definition: ObjectPool.hpp:69
T ObjectType
Definition: ObjectPool.hpp:63
constexpr unsigned int D
Specifies Hydrogen (Deuterium).
Definition: AtomType.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.
Definition: ObjectPool.hpp:72
T * operator()() const
Definition: ObjectPool.hpp:74
Definition: ObjectPool.hpp:81
void operator()(T *obj) const
Definition: ObjectPool.hpp:83