libosmscout  1.1.1
ObjectPool.h
Go to the documentation of this file.
1 #ifndef OSMSCOUT_OBJECTPOOL_H
2 #define OSMSCOUT_OBJECTPOOL_H
3 
4 /*
5  This source is part of the libosmscout library
6  Copyright (C) 2020 Lukas Karas
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.1 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
19  License along with this library; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22 
24 
25 #include <vector>
26 #include <memory>
27 #include <mutex>
28 #include <functional>
29 
30 namespace osmscout {
31 
32  template<typename T>
33  class ObjectPool {
34  private:
35  std::vector<T*> pool;
36  size_t maxSize;
37  std::mutex mutex;
38 
39  private:
40  void Return(T* o){
41  std::scoped_lock<std::mutex> guard(mutex);
42  if (!IsValid(o) || pool.size()==maxSize){
43  Destroy(o);
44  } else {
45  pool.push_back(o);
46  }
47  }
48 
49  public:
50  using Ptr = std::unique_ptr<T, std::function<void(T*)>>;
51 
52  public:
53  explicit ObjectPool(size_t maxSize):
54  maxSize(maxSize)
55  {
56  pool.reserve(std::min((size_t)100, maxSize));
57  }
58 
59  virtual ~ObjectPool(){
60  Clear();
61  }
62 
66  virtual T* MakeNew() noexcept = 0;
67 
68  virtual void Destroy(T* o) noexcept
69  {
70  delete o;
71  }
72 
73  virtual bool IsValid(T*) noexcept
74  {
75  return true;
76  }
77 
78  virtual Ptr Borrow()
79  {
80  std::scoped_lock<std::mutex> guard(mutex);
81  T* o;
82  if (!pool.empty()){
83  o=pool.back();
84  pool.pop_back();
85  } else {
86  o=MakeNew();
87  if (o == nullptr){
88  return std::unique_ptr<T>(nullptr);
89  }
90  }
91  return Ptr(o, [this](T* o){ Return(o); });
92  }
93 
94  size_t Size()
95  {
96  std::scoped_lock<std::mutex> guard(mutex);
97  return pool.size();
98  }
99 
100  void Clear()
101  {
102  std::scoped_lock<std::mutex> guard(mutex);
103  for (T* o:pool){
104  Destroy(o);
105  }
106  pool.clear();
107  }
108  };
109 
110 }
111 
112 #endif //OSMSCOUT_OBJECTPOOL_H
Definition: ObjectPool.h:33
std::unique_ptr< FileScanner, std::function< void(FileScanner *)> > Ptr
Definition: ObjectPool.h:50
Definition: Area.h:38
virtual void Destroy(T *o) noexcept
Definition: ObjectPool.h:68
virtual T * MakeNew() noexcept=0
ObjectPool(size_t maxSize)
Definition: ObjectPool.h:53
size_t Size()
Definition: ObjectPool.h:94
virtual ~ObjectPool()
Definition: ObjectPool.h:59
void Clear()
Definition: ObjectPool.h:100
virtual Ptr Borrow()
Definition: ObjectPool.h:78
virtual bool IsValid(T *) noexcept
Definition: ObjectPool.h:73