Eclipse SUMO - Simulation of Urban MObility
NamedObjectCont.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 // Copyright (C) 2002-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 /****************************************************************************/
20 // A map of named object pointers
21 /****************************************************************************/
22 #pragma once
23 #include <config.h>
24 #include <map>
25 #include <string>
26 #include <vector>
27 #include <algorithm>
28 
29 
30 // ===========================================================================
31 // class definitions
32 // ===========================================================================
40 template<class T>
42 public:
44  typedef std::map< std::string, T > IDMap;
45 
48 
50  virtual ~NamedObjectCont() {
51  // iterate over all elements to delete it
52  for (auto i : myMap) {
53  delete i.second;
54  }
55  }
56 
66  bool add(const std::string& id, T item) {
67  const auto it = myMap.lower_bound(id);
68  if (it == myMap.end() || it->first != id) {
69  myMap.emplace_hint(it, id, item);
70  return true;
71  }
72  return false;
73  }
74 
80  bool remove(const std::string& id, const bool del = true) {
81  auto it = myMap.find(id);
82  if (it == myMap.end()) {
83  return false;
84  } else {
85  if (del) {
86  delete it->second;
87  }
88  myMap.erase(it);
89  return true;
90  }
91  }
92 
100  T get(const std::string& id) const {
101  auto it = myMap.find(id);
102  if (it == myMap.end()) {
103  return 0;
104  } else {
105  return it->second;
106  }
107  }
108 
110  void clear() {
111  for (auto i : myMap) {
112  delete i.second;
113  }
114  myMap.clear();
115  }
116 
118  int size() const {
119  return (int) myMap.size();
120  }
121 
122  /* @brief Fills the given vector with the stored objects' ids
123  * @param[in] into The container to fill
124  */
125  void insertIDs(std::vector<std::string>& into) const {
126  for (auto i : myMap) {
127  into.push_back(i.first);
128  }
129  }
130 
132  bool changeID(const std::string& oldId, const std::string& newId) {
133  auto i = myMap.find(oldId);
134  if (i == myMap.end()) {
135  return false;
136  } else {
137  // save Item, remove it from Map, and insert it again with the new ID
138  T item = i->second;
139  myMap.erase(i);
140  myMap.insert(std::make_pair(newId, item));
141  return true;
142  }
143  }
144 
146  typename IDMap::const_iterator begin() const {
147  return myMap.begin();
148  }
149 
151  typename IDMap::const_iterator end() const {
152  return myMap.end();
153  }
154 
155 
156 private:
159 };
A map of named object pointers.
void clear()
Removes all items from the container (deletes them, too)
NamedObjectCont()
Constructor.
IDMap myMap
The map from key to object.
std::map< std::string, T > IDMap
Definition of the key to pointer map type.
T get(const std::string &id) const
Retrieves an item.
void insertIDs(std::vector< std::string > &into) const
IDMap::const_iterator begin() const
Returns a reference to the begin iterator for the internal map.
IDMap::const_iterator end() const
Returns a reference to the end iterator for the internal map.
int size() const
Returns the number of stored items within the container.
bool changeID(const std::string &oldId, const std::string &newId)
change ID of a stored object
virtual ~NamedObjectCont()
Destructor.
bool remove(const std::string &id, const bool del=true)
Removes an item.
bool add(const std::string &id, T item)
Adds an item.