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-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
18 // A pool of resuable instances
19 /****************************************************************************/
20 #pragma once
21 #include <vector>
22 #include <algorithm>
23 
24 
25 // ===========================================================================
26 // class definitions
27 // ===========================================================================
32 template<typename T>
33 class InstancePool {
34 public:
39  InstancePool(bool deleteOnQuit) : myDeleteOnQuit(deleteOnQuit) { }
40 
41 
44  typedef typename std::vector<T*>::iterator It;
45  if (myDeleteOnQuit) {
46  for (It i = myFreeInstances.begin(); i != myFreeInstances.end(); i++) {
47  delete *i;
48  }
49  }
50  }
51 
52 
61  if (myFreeInstances.size() == 0) {
62  return 0;
63  } else {
64  T* instance = myFreeInstances.back();
65  myFreeInstances.pop_back();
66  return instance;
67  }
68  }
69 
70 
75  void addFreeInstance(T* instance) {
76  myFreeInstances.push_back(instance);
77  }
78 
79 
84  void addFreeInstances(const std::vector<T*> instances) {
85  std::copy(instances.begin(), instances.end(),
86  std::back_inserter(myFreeInstances));
87  }
88 
89 
90 private:
92  std::vector<T*> myFreeInstances;
93 
96 
97 
98 };
A pool of resuable instances.
Definition: InstancePool.h:33
void addFreeInstances(const std::vector< T * > instances)
Adds some free, reusable instances.
Definition: InstancePool.h:84
T * getFreeInstance()
Returns a free instance or 0 if no such exists.
Definition: InstancePool.h:60
bool myDeleteOnQuit
Information whether the stored instances shall be deleted.
Definition: InstancePool.h:95
void addFreeInstance(T *instance)
Adds a free, reusable instance.
Definition: InstancePool.h:75
~InstancePool()
Destructor.
Definition: InstancePool.h:43
std::vector< T * > myFreeInstances
List of reusable instances.
Definition: InstancePool.h:92
InstancePool(bool deleteOnQuit)
Constructor.
Definition: InstancePool.h:39