/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2002-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 NamedObjectCont.h14/// @author Daniel Krajzewicz15/// @author Michael Behrisch16/// @author Jakob Erdmann17/// @date Sept 200218///19// A map of named object pointers20/****************************************************************************/21#pragma once22#include <config.h>23#include <map>24#include <string>25#include <vector>26#include <algorithm>272829// ===========================================================================30// class definitions31// ===========================================================================32/**33* @class NamedObjectCont34* @brief A map of named object pointers35*36* An associative storage (map) for objects (pointers to them to be exact),37* which do have a name.38*/39template<class T>40class NamedObjectCont {41public:42/// @brief Definition of the key to pointer map type43typedef std::map< std::string, T > IDMap;4445///@brief Constructor46NamedObjectCont() {}4748///@brief Destructor49virtual ~NamedObjectCont() {50// iterate over all elements to delete it51for (auto i : myMap) {52delete i.second;53}54}5556/** @brief Adds an item57*58* If another item with the same name is already known, false is reported59* and the item is not added.60*61* @param[in] id The id of the item to add62* @param[in] item The item to add63* @return If the item could be added (no item with the same id was within the container before)64*/65bool add(const std::string& id, T item) {66const auto it = myMap.lower_bound(id);67if (it == myMap.end() || it->first != id) {68myMap.emplace_hint(it, id, item);69return true;70}71return false;72}7374/** @brief Removes an item75* @param[in] id The id of the item to remove76* @param[in] del delete item after removing of container77* @return If the item could be removed (an item with the id was within the container before)78*/79bool remove(const std::string& id, const bool del = true) {80auto it = myMap.find(id);81if (it == myMap.end()) {82return false;83} else {84if (del) {85delete it->second;86}87myMap.erase(it);88return true;89}90}9192/** @brief Retrieves an item93*94* Returns 0 when no item with the given id is stored within the container95*96* @param[in] id The id of the item to retrieve97* @return The item stored under the given id, or 0 if no such item exists98*/99T get(const std::string& id) const {100auto it = myMap.find(id);101if (it == myMap.end()) {102return 0;103} else {104return it->second;105}106}107108/// @brief Removes all items from the container (deletes them, too)109void clear() {110for (auto i : myMap) {111delete i.second;112}113myMap.clear();114}115116/// @brief Returns the number of stored items within the container117int size() const {118return (int) myMap.size();119}120121/* @brief Fills the given vector with the stored objects' ids122* @param[in] into The container to fill123*/124void insertIDs(std::vector<std::string>& into) const {125for (auto i : myMap) {126into.push_back(i.first);127}128}129130/// @brief change ID of a stored object131bool changeID(const std::string& oldId, const std::string& newId) {132auto i = myMap.find(oldId);133if (i == myMap.end()) {134return false;135} else {136// save Item, remove it from Map, and insert it again with the new ID137T item = i->second;138myMap.erase(i);139myMap.insert(std::make_pair(newId, item));140return true;141}142}143144/// @brief Returns a reference to the begin iterator for the internal map145typename IDMap::const_iterator begin() const {146return myMap.begin();147}148149/// @brief Returns a reference to the end iterator for the internal map150typename IDMap::const_iterator end() const {151return myMap.end();152}153154155private:156/// @brief The map from key to object157IDMap myMap;158};159160161