Eclipse SUMO - Simulation of Urban MObility
InstancePool.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
14 // A pool of resuable instances
15 /****************************************************************************/
16 #ifndef InstancePool_h
17 #define InstancePool_h
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 
24 #include <vector>
25 #include <algorithm>
26 
27 
28 // ===========================================================================
29 // class definitions
30 // ===========================================================================
35 template<typename T>
36 class InstancePool {
37 public:
42  InstancePool(bool deleteOnQuit) : myDeleteOnQuit(deleteOnQuit) { }
43 
44 
47  typedef typename std::vector<T*>::iterator It;
48  if (myDeleteOnQuit) {
49  for (It i = myFreeInstances.begin(); i != myFreeInstances.end(); i++) {
50  delete *i;
51  }
52  }
53  }
54 
55 
64  if (myFreeInstances.size() == 0) {
65  return 0;
66  } else {
67  T* instance = myFreeInstances.back();
68  myFreeInstances.pop_back();
69  return instance;
70  }
71  }
72 
73 
78  void addFreeInstance(T* instance) {
79  myFreeInstances.push_back(instance);
80  }
81 
82 
87  void addFreeInstances(const std::vector<T*> instances) {
88  std::copy(instances.begin(), instances.end(),
89  std::back_inserter(myFreeInstances));
90  }
91 
92 
93 private:
95  std::vector<T*> myFreeInstances;
96 
99 
100 
101 };
102 
103 
104 #endif
105 
106 /****************************************************************************/
107 
InstancePool::myFreeInstances
std::vector< T * > myFreeInstances
List of reusable instances.
Definition: InstancePool.h:95
InstancePool::addFreeInstance
void addFreeInstance(T *instance)
Adds a free, reusable instance.
Definition: InstancePool.h:78
InstancePool::getFreeInstance
T * getFreeInstance()
Returns a free instance or 0 if no such exists.
Definition: InstancePool.h:63
InstancePool::InstancePool
InstancePool(bool deleteOnQuit)
Constructor.
Definition: InstancePool.h:42
InstancePool::~InstancePool
~InstancePool()
Destructor.
Definition: InstancePool.h:46
InstancePool::addFreeInstances
void addFreeInstances(const std::vector< T * > instances)
Adds some free, reusable instances.
Definition: InstancePool.h:87
InstancePool
A pool of resuable instances.
Definition: InstancePool.h:36
InstancePool::myDeleteOnQuit
bool myDeleteOnQuit
Information whether the stored instances shall be deleted.
Definition: InstancePool.h:98