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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
17 // A pool of resuable instances
18 /****************************************************************************/
19 #ifndef InstancePool_h
20 #define InstancePool_h
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <vector>
33 #include <algorithm>
34 
35 
36 // ===========================================================================
37 // class definitions
38 // ===========================================================================
43 template<typename T>
44 class InstancePool {
45 public:
50  InstancePool(bool deleteOnQuit) : myDeleteOnQuit(deleteOnQuit) { }
51 
52 
55  typedef typename std::vector<T*>::iterator It;
56  if (myDeleteOnQuit) {
57  for (It i = myFreeInstances.begin(); i != myFreeInstances.end(); i++) {
58  delete *i;
59  }
60  }
61  }
62 
63 
72  if (myFreeInstances.size() == 0) {
73  return 0;
74  } else {
75  T* instance = myFreeInstances.back();
76  myFreeInstances.pop_back();
77  return instance;
78  }
79  }
80 
81 
86  void addFreeInstance(T* instance) {
87  myFreeInstances.push_back(instance);
88  }
89 
90 
95  void addFreeInstances(const std::vector<T*> instances) {
96  std::copy(instances.begin(), instances.end(),
97  std::back_inserter(myFreeInstances));
98  }
99 
100 
101 private:
103  std::vector<T*> myFreeInstances;
104 
107 
108 
109 };
110 
111 
112 #endif
113 
114 /****************************************************************************/
115 
std::vector< T * > myFreeInstances
List of reusable instances.
Definition: InstancePool.h:103
InstancePool(bool deleteOnQuit)
Constructor.
Definition: InstancePool.h:50
T * getFreeInstance()
Returns a free instance or 0 if no such exists.
Definition: InstancePool.h:71
void addFreeInstances(const std::vector< T * > instances)
Adds some free, reusable instances.
Definition: InstancePool.h:95
A pool of resuable instances.
Definition: InstancePool.h:44
~InstancePool()
Destructor.
Definition: InstancePool.h:54
bool myDeleteOnQuit
Information whether the stored instances shall be deleted.
Definition: InstancePool.h:106
void addFreeInstance(T *instance)
Adds a free, reusable instance.
Definition: InstancePool.h:86