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.dev/sumo
3 // Copyright (C) 2001-2024 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 <config.h>
22 #include <vector>
23 #include <algorithm>
24 
25 
26 // ===========================================================================
27 // class definitions
28 // ===========================================================================
33 template<typename T>
34 class InstancePool {
35 public:
40  InstancePool(bool deleteOnQuit) : myDeleteOnQuit(deleteOnQuit) { }
41 
42 
45  typedef typename std::vector<T*>::iterator It;
46  if (myDeleteOnQuit) {
47  for (It i = myFreeInstances.begin(); i != myFreeInstances.end(); i++) {
48  delete *i;
49  }
50  }
51  }
52 
53 
62  if (myFreeInstances.size() == 0) {
63  return 0;
64  } else {
65  T* instance = myFreeInstances.back();
66  myFreeInstances.pop_back();
67  return instance;
68  }
69  }
70 
71 
76  void addFreeInstance(T* instance) {
77  myFreeInstances.push_back(instance);
78  }
79 
80 
85  void addFreeInstances(const std::vector<T*> instances) {
86  std::copy(instances.begin(), instances.end(),
87  std::back_inserter(myFreeInstances));
88  }
89 
90 
91 private:
93  std::vector<T*> myFreeInstances;
94 
97 
98 
99 };
A pool of resuable instances.
Definition: InstancePool.h:34
void addFreeInstances(const std::vector< T * > instances)
Adds some free, reusable instances.
Definition: InstancePool.h:85
T * getFreeInstance()
Returns a free instance or 0 if no such exists.
Definition: InstancePool.h:61
bool myDeleteOnQuit
Information whether the stored instances shall be deleted.
Definition: InstancePool.h:96
void addFreeInstance(T *instance)
Adds a free, reusable instance.
Definition: InstancePool.h:76
~InstancePool()
Destructor.
Definition: InstancePool.h:44
std::vector< T * > myFreeInstances
List of reusable instances.
Definition: InstancePool.h:93
InstancePool(bool deleteOnQuit)
Constructor.
Definition: InstancePool.h:40