Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/utils/common/InstancePool.h
169678 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2001-2025 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
/****************************************************************************/
14
/// @file InstancePool.h
15
/// @author Daniel Krajzewicz
16
/// @date Fri, 29.04.2005
17
///
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
// ===========================================================================
29
/**
30
* @class InstancePool
31
* @brief A pool of resuable instances
32
*/
33
template<typename T>
34
class InstancePool {
35
public:
36
/** @brief Constructor
37
*
38
* @param[in] deleteOnQuit Information whether stored instances shall be deleted when this container is deleted
39
*/
40
InstancePool(bool deleteOnQuit) : myDeleteOnQuit(deleteOnQuit) { }
41
42
43
/// @brief Destructor
44
~InstancePool() {
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
54
/** @brief Returns a free instance or 0 if no such exists
55
*
56
* If any free instance is stored, it is returned and removed from
57
* the storage. If no one is stored, 0 is returned.
58
*
59
* @return A free instance or 0 if no such exists
60
*/
61
T* getFreeInstance() {
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
72
/** @brief Adds a free, reusable instance
73
*
74
* @param[in] instance An instance to add
75
*/
76
void addFreeInstance(T* instance) {
77
myFreeInstances.push_back(instance);
78
}
79
80
81
/** @brief Adds some free, reusable instances
82
*
83
* @param[in] instances A vector of instances to add
84
*/
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:
92
/// @brief List of reusable instances
93
std::vector<T*> myFreeInstances;
94
95
/// @brief Information whether the stored instances shall be deleted
96
bool myDeleteOnQuit;
97
98
99
};
100
101