Chemical Data Processing Library C++ API - Version 1.1.1
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 
47  template <typename T>
49  {
50 
51  public:
52  typedef T ObjectType;
53 
54  typedef std::function<ObjectType*()> ConstructorFunction;
55  typedef std::function<void(ObjectType&)> ObjectFunction;
56 
58  {
59 
60  T* operator()() const
61  {
62  return new T();
63  }
64  };
65 
66  ObjectStack(const ObjectStack& stack):
67  maxSize(stack.maxSize), freeIndex(0), ctorFunc(stack.ctorFunc),
68  initFunc(stack.initFunc), cleanFunc(stack.cleanFunc) {}
69 
70  ObjectStack(std::size_t max_pool_size = 0):
71  maxSize(max_pool_size), freeIndex(0), ctorFunc(DefaultConstructor()) {}
72 
73  template <typename C>
74  ObjectStack(const C& ctor_func, std::size_t max_pool_size):
75  maxSize(max_pool_size), freeIndex(0), ctorFunc(ctor_func)
76  {}
77 
79 
81  {
82  if (freeIndex == allocObjects.size())
83  allocObjects.push_back(ctorFunc());
84 
85  ObjectType& obj = allocObjects[freeIndex++];
86 
87  if (initFunc)
88  initFunc(obj);
89 
90  return &obj;
91  }
92 
93  void put()
94  {
95  if (freeIndex > 0) {
96  freeIndex--;
97 
98  if (cleanFunc)
99  cleanFunc(allocObjects[freeIndex]);
100  }
101 
102  if (maxSize > 0 && freeIndex <= maxSize && allocObjects.size() > maxSize)
103  allocObjects.erase(allocObjects.begin() + maxSize, allocObjects.end());
104  }
105 
106  void putAll()
107  {
108  if (cleanFunc)
109  std::for_each(allocObjects.begin(), allocObjects.begin() + freeIndex,
110  [this](ObjectType& obj) { cleanFunc(obj); });
111  freeIndex = 0;
112 
113  if (maxSize > 0 && allocObjects.size() > maxSize)
114  allocObjects.erase(allocObjects.begin() + maxSize, allocObjects.end());
115  }
116 
117  std::size_t getMaxSize() const
118  {
119  return maxSize;
120  }
121 
122  void setMaxSize(std::size_t max_size)
123  {
124  maxSize = max_size;
125 
126  if (maxSize > 0 && allocObjects.size() > maxSize)
127  allocObjects.erase(allocObjects.begin() + std::max(freeIndex, maxSize), allocObjects.end());
128  }
129 
130  void freeMemory(bool unused_only = true)
131  {
132  if (unused_only) {
133  allocObjects.erase(allocObjects.begin() + freeIndex, allocObjects.end());
134  return;
135  }
136 
137  freeIndex = 0;
138  allocObjects.clear();
139  }
140 
141  void setInitFunction(const ObjectFunction& func)
142  {
143  initFunc = func;
144  }
145 
147  {
148  cleanFunc = func;
149  }
150 
152  {
153  if (this == &stack)
154  return *this;
155 
156  maxSize = stack.maxSize;
157  ctorFunc = stack.ctorFunc;
158  initFunc = stack.initFunc;
159 
160  if (maxSize > 0 && allocObjects.size() > maxSize)
161  allocObjects.erase(allocObjects.begin() + std::max(freeIndex, maxSize), allocObjects.end());
162 
163  return *this;
164  }
165 
166  private:
167  typedef boost::ptr_vector<ObjectType> AllocObjectList;
168 
169  std::size_t maxSize;
170  std::size_t freeIndex;
171  AllocObjectList allocObjects;
172  ConstructorFunction ctorFunc;
173  ObjectFunction initFunc;
174  ObjectFunction cleanFunc;
175  };
176  } // namespace Util
177 } // namespace CDPL
178 
179 #endif // CDPL_UTIL_OBJECTSTACK_HPP
CDPL::Util::ObjectStack::setCleanupFunction
void setCleanupFunction(const ObjectFunction &func)
Definition: ObjectStack.hpp:146
CDPL::Util::ObjectStack::put
void put()
Definition: ObjectStack.hpp:93
CDPL::Util::ObjectStack::DefaultConstructor::operator()
T * operator()() const
Definition: ObjectStack.hpp:60
CDPL::Util::ObjectStack::freeMemory
void freeMemory(bool unused_only=true)
Definition: ObjectStack.hpp:130
CDPL::Util::ObjectStack::ObjectStack
ObjectStack(const ObjectStack &stack)
Definition: ObjectStack.hpp:66
CDPL::Util::ObjectStack::~ObjectStack
~ObjectStack()
Definition: ObjectStack.hpp:78
CDPL::Util::ObjectStack::get
ObjectType * get()
Definition: ObjectStack.hpp:80
CDPL::Util::ObjectStack::putAll
void putAll()
Definition: ObjectStack.hpp:106
CDPL::Util::ObjectStack::ObjectStack
ObjectStack(const C &ctor_func, std::size_t max_pool_size)
Definition: ObjectStack.hpp:74
CDPL::Util::ObjectStack::getMaxSize
std::size_t getMaxSize() const
Definition: ObjectStack.hpp:117
CDPL::Util::ObjectStack::operator=
ObjectStack & operator=(const ObjectStack &stack)
Definition: ObjectStack.hpp:151
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:52
CDPL::Util::ObjectStack::DefaultConstructor
Definition: ObjectStack.hpp:58
CDPL::Util::ObjectStack::ObjectFunction
std::function< void(ObjectType &)> ObjectFunction
Definition: ObjectStack.hpp:55
CDPL::Util::ObjectStack
ObjectStack.
Definition: ObjectStack.hpp:49
CDPL::Util::ObjectStack< State >::ConstructorFunction
std::function< ObjectType *()> ConstructorFunction
Definition: ObjectStack.hpp:54
CDPL::Util::ObjectStack::setMaxSize
void setMaxSize(std::size_t max_size)
Definition: ObjectStack.hpp:122
CDPL::Util::ObjectStack::ObjectStack
ObjectStack(std::size_t max_pool_size=0)
Definition: ObjectStack.hpp:70
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:141