Chemical Data Processing Library C++ API - Version 1.0.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 <vector>
33 #include <cstddef>
34 #include <algorithm>
35 #include <new>
36 #include <memory>
37 #include <functional>
38 
40 
41 
42 namespace CDPL
43 {
44 
45  namespace Util
46  {
47 
51  template <typename T>
53  {
54 
55  public:
56  typedef T ObjectType;
57 
58  typedef std::shared_ptr<ObjectType> SharedObjectPointer;
59 
60  typedef std::function<ObjectType*()> ConstructorFunction;
61  typedef std::function<void(ObjectType*)> DestructorFunction;
62  typedef std::function<void(ObjectType&)> ObjectFunction;
63 
65  {
66 
67  T* operator()() const
68  {
69  return new T();
70  }
71  };
72 
74  {
75 
76  void operator()(T* obj) const
77  {
78  delete obj;
79  }
80  };
81 
82  ObjectStack(const ObjectStack& stack):
83  maxSize(stack.maxSize), freeIndex(0), ctorFunc(stack.ctorFunc), dtorFunc(stack.dtorFunc),
84  initFunc(stack.initFunc), cleanFunc(stack.cleanFunc) {}
85 
86  ObjectStack(std::size_t max_pool_size = 0):
87  maxSize(max_pool_size), freeIndex(0), ctorFunc(DefaultConstructor()), dtorFunc(DefaultDestructor()) {}
88 
89  template <typename C, typename D>
90  ObjectStack(const C& ctor_func, const D& dtor_func, std::size_t max_pool_size = 0):
91  maxSize(max_pool_size), freeIndex(0), ctorFunc(ctor_func), dtorFunc(dtor_func)
92  {}
93 
95 
97  {
98  return get().get();
99  }
100 
102  {
103  if (freeIndex == allocObjects.size()) {
104  SharedObjectPointer obj_ptr(ctorFunc(), dtorFunc);
105 
106  if (!obj_ptr)
107  throw std::bad_alloc();
108 
109  allocObjects.push_back(obj_ptr);
110  }
111 
112  const SharedObjectPointer& obj_ptr = allocObjects[freeIndex++];
113 
114  if (initFunc)
115  initFunc(*obj_ptr);
116 
117  return obj_ptr;
118  }
119 
120  void put()
121  {
122  if (freeIndex > 0) {
123  freeIndex--;
124 
125  if (cleanFunc)
126  cleanFunc(*allocObjects[freeIndex]);
127  }
128 
129  if (maxSize > 0 && freeIndex <= maxSize && allocObjects.size() > maxSize)
130  allocObjects.resize(maxSize);
131  }
132 
133  void putAll()
134  {
135  if (cleanFunc)
136  std::for_each(allocObjects.begin(), allocObjects.begin() + freeIndex,
137  std::bind(cleanFunc, std::bind(Util::Dereferencer<SharedObjectPointer, ObjectType&>(),
138  std::placeholders::_1)));
139  freeIndex = 0;
140 
141  if (maxSize > 0 && allocObjects.size() > maxSize)
142  allocObjects.resize(maxSize);
143  }
144 
145  std::size_t getMaxPoolSize() const
146  {
147  return maxSize;
148  }
149 
150  void setMaxPoolSize(std::size_t max_size)
151  {
152  maxSize = max_size;
153 
154  if (maxSize > 0 && allocObjects.size() > maxSize)
155  allocObjects.resize(std::max(freeIndex, maxSize));
156  }
157 
158  void freeMemory(bool unused_only = true)
159  {
160  if (unused_only) {
161  allocObjects.resize(freeIndex);
162  return;
163  }
164 
165  freeIndex = 0;
166  allocObjects.clear();
167  }
168 
169  void setInitFunction(const ObjectFunction& func)
170  {
171  initFunc = func;
172  }
173 
175  {
176  cleanFunc = func;
177  }
178 
180  {
181  if (this == &stack)
182  return *this;
183 
184  maxSize = stack.maxSize;
185  ctorFunc = stack.ctorFunc;
186  dtorFunc = stack.dtorFunc;
187  initFunc = stack.initFunc;
188 
189  if (maxSize > 0 && allocObjects.size() > maxSize)
190  allocObjects.resize(std::max(freeIndex, maxSize));
191 
192  return *this;
193  }
194 
195  private:
196  typedef std::vector<SharedObjectPointer> AllocObjectList;
197 
198  std::size_t maxSize;
199  std::size_t freeIndex;
200  AllocObjectList allocObjects;
201  ConstructorFunction ctorFunc;
202  DestructorFunction dtorFunc;
203  ObjectFunction initFunc;
204  ObjectFunction cleanFunc;
205  };
206  } // namespace Util
207 } // namespace CDPL
208 
209 #endif // CDPL_UTIL_OBJECTSTACK_HPP
CDPL::Util::ObjectStack::ObjectStack
ObjectStack(const C &ctor_func, const D &dtor_func, std::size_t max_pool_size=0)
Definition: ObjectStack.hpp:90
CDPL::Util::ObjectStack::setCleanupFunction
void setCleanupFunction(const ObjectFunction &func)
Definition: ObjectStack.hpp:174
CDPL::Util::ObjectStack::put
void put()
Definition: ObjectStack.hpp:120
CDPL::Util::ObjectStack::DefaultConstructor::operator()
T * operator()() const
Definition: ObjectStack.hpp:67
CDPL::Util::ObjectStack::freeMemory
void freeMemory(bool unused_only=true)
Definition: ObjectStack.hpp:158
CDPL::Util::ObjectStack::ObjectStack
ObjectStack(const ObjectStack &stack)
Definition: ObjectStack.hpp:82
CDPL::Util::ObjectStack::~ObjectStack
~ObjectStack()
Definition: ObjectStack.hpp:94
CDPL::Util::ObjectStack::get
const SharedObjectPointer & get()
Definition: ObjectStack.hpp:101
CDPL::Util::Dereferencer
An unary functor for the dereferenciation of pointers without null pointer checking.
Definition: Dereferencer.hpp:52
CDPL::Util::ObjectStack::DestructorFunction
std::function< void(ObjectType *)> DestructorFunction
Definition: ObjectStack.hpp:61
CDPL::Util::ObjectStack::setMaxPoolSize
void setMaxPoolSize(std::size_t max_size)
Definition: ObjectStack.hpp:150
CDPL::Util::ObjectStack::putAll
void putAll()
Definition: ObjectStack.hpp:133
CDPL::Util::ObjectStack::getRaw
ObjectType * getRaw()
Definition: ObjectStack.hpp:96
CDPL::Util::ObjectStack::DefaultDestructor::operator()
void operator()(T *obj) const
Definition: ObjectStack.hpp:76
CDPL::Util::ObjectStack::SharedObjectPointer
std::shared_ptr< ObjectType > SharedObjectPointer
Definition: ObjectStack.hpp:58
CDPL::Util::ObjectStack::DefaultDestructor
Definition: ObjectStack.hpp:74
CDPL::Util::ObjectStack::operator=
ObjectStack & operator=(const ObjectStack &stack)
Definition: ObjectStack.hpp:179
CDPL::Chem::AtomType::T
const unsigned int T
Specifies Hydrogen (Tritium).
Definition: AtomType.hpp:67
CDPL
The namespace of the Chemical Data Processing Library.
CDPL::Util::ObjectStack::ObjectType
T ObjectType
Definition: ObjectStack.hpp:56
CDPL::Util::ObjectStack::DefaultConstructor
Definition: ObjectStack.hpp:65
CDPL::Util::ObjectStack::ObjectFunction
std::function< void(ObjectType &)> ObjectFunction
Definition: ObjectStack.hpp:62
Dereferencer.hpp
Definition of the classes CDPL::Util::Dereferencer and CDPL::Util::NullCheckDereferencer.
CDPL::Chem::AtomType::D
const unsigned int D
Specifies Hydrogen (Deuterium).
Definition: AtomType.hpp:62
CDPL::Util::ObjectStack
ObjectStack.
Definition: ObjectStack.hpp:53
CDPL::Util::ObjectStack::getMaxPoolSize
std::size_t getMaxPoolSize() const
Definition: ObjectStack.hpp:145
CDPL::Util::ObjectStack< State >::ConstructorFunction
std::function< ObjectType *()> ConstructorFunction
Definition: ObjectStack.hpp:60
CDPL::Util::ObjectStack::ObjectStack
ObjectStack(std::size_t max_pool_size=0)
Definition: ObjectStack.hpp:86
CDPL::Chem::AtomType::C
const unsigned int C
Specifies Carbon.
Definition: AtomType.hpp:92
CDPL::Util::ObjectStack::setInitFunction
void setInitFunction(const ObjectFunction &func)
Definition: ObjectStack.hpp:169