/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2001-2025 German Aerospace Center (DLR) and others.3// This program and the accompanying materials are made available under the4// terms of the Eclipse Public License 2.0 which is available at5// https://www.eclipse.org/legal/epl-2.0/6// This Source Code may also be made available under the following Secondary7// Licenses when the conditions for such availability set forth in the Eclipse8// Public License 2.0 are satisfied: GNU General Public License, version 29// or later which is available at10// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later12/****************************************************************************/13/// @file InstancePool.h14/// @author Daniel Krajzewicz15/// @date Fri, 29.04.200516///17// A pool of resuable instances18/****************************************************************************/19#pragma once20#include <config.h>21#include <vector>22#include <algorithm>232425// ===========================================================================26// class definitions27// ===========================================================================28/**29* @class InstancePool30* @brief A pool of resuable instances31*/32template<typename T>33class InstancePool {34public:35/** @brief Constructor36*37* @param[in] deleteOnQuit Information whether stored instances shall be deleted when this container is deleted38*/39InstancePool(bool deleteOnQuit) : myDeleteOnQuit(deleteOnQuit) { }404142/// @brief Destructor43~InstancePool() {44typedef typename std::vector<T*>::iterator It;45if (myDeleteOnQuit) {46for (It i = myFreeInstances.begin(); i != myFreeInstances.end(); i++) {47delete *i;48}49}50}515253/** @brief Returns a free instance or 0 if no such exists54*55* If any free instance is stored, it is returned and removed from56* the storage. If no one is stored, 0 is returned.57*58* @return A free instance or 0 if no such exists59*/60T* getFreeInstance() {61if (myFreeInstances.size() == 0) {62return 0;63} else {64T* instance = myFreeInstances.back();65myFreeInstances.pop_back();66return instance;67}68}697071/** @brief Adds a free, reusable instance72*73* @param[in] instance An instance to add74*/75void addFreeInstance(T* instance) {76myFreeInstances.push_back(instance);77}787980/** @brief Adds some free, reusable instances81*82* @param[in] instances A vector of instances to add83*/84void addFreeInstances(const std::vector<T*> instances) {85std::copy(instances.begin(), instances.end(),86std::back_inserter(myFreeInstances));87}888990private:91/// @brief List of reusable instances92std::vector<T*> myFreeInstances;9394/// @brief Information whether the stored instances shall be deleted95bool myDeleteOnQuit;969798};99100101