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 /****************************************************************************/
15 // A pool of resuable instances
16 /****************************************************************************/
17 #ifndef InstancePool_h
18 #define InstancePool_h
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 
25 #include <vector>
26 #include <algorithm>
27 
28 
29 // ===========================================================================
30 // class definitions
31 // ===========================================================================
36 template<typename T>
37 class InstancePool {
38 public:
43  InstancePool(bool deleteOnQuit) : myDeleteOnQuit(deleteOnQuit) { }
44 
45 
48  typedef typename std::vector<T*>::iterator It;
49  if (myDeleteOnQuit) {
50  for (It i = myFreeInstances.begin(); i != myFreeInstances.end(); i++) {
51  delete *i;
52  }
53  }
54  }
55 
56 
65  if (myFreeInstances.size() == 0) {
66  return 0;
67  } else {
68  T* instance = myFreeInstances.back();
69  myFreeInstances.pop_back();
70  return instance;
71  }
72  }
73 
74 
79  void addFreeInstance(T* instance) {
80  myFreeInstances.push_back(instance);
81  }
82 
83 
88  void addFreeInstances(const std::vector<T*> instances) {
89  std::copy(instances.begin(), instances.end(),
90  std::back_inserter(myFreeInstances));
91  }
92 
93 
94 private:
96  std::vector<T*> myFreeInstances;
97 
100 
101 
102 };
103 
104 
105 #endif
106 
107 /****************************************************************************/
108 
std::vector< T * > myFreeInstances
List of reusable instances.
Definition: InstancePool.h:96
InstancePool(bool deleteOnQuit)
Constructor.
Definition: InstancePool.h:43
T * getFreeInstance()
Returns a free instance or 0 if no such exists.
Definition: InstancePool.h:64
A pool of resuable instances.
Definition: InstancePool.h:37
~InstancePool()
Destructor.
Definition: InstancePool.h:47
void addFreeInstances(const std::vector< T *> instances)
Adds some free, reusable instances.
Definition: InstancePool.h:88
bool myDeleteOnQuit
Information whether the stored instances shall be deleted.
Definition: InstancePool.h:99
void addFreeInstance(T *instance)
Adds a free, reusable instance.
Definition: InstancePool.h:79