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