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.org/sumo
3 // Copyright (C) 2002-2022 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 
47  virtual ~NamedObjectCont() {
48  // iterate over all elements to delete it
49  for (auto i : myMap) {
50  delete i.second;
51  }
52  }
53 
63  bool add(const std::string& id, T item) {
64  if (myMap.find(id) != myMap.end()) {
65  return false;
66  }
67  myMap.insert(std::make_pair(id, item));
68  return true;
69  }
70 
76  bool remove(const std::string& id, const bool del = true) {
77  auto it = myMap.find(id);
78  if (it == myMap.end()) {
79  return false;
80  } else {
81  if (del) {
82  delete it->second;
83  }
84  myMap.erase(it);
85  return true;
86  }
87  }
88 
96  T get(const std::string& id) const {
97  auto it = myMap.find(id);
98  if (it == myMap.end()) {
99  return 0;
100  } else {
101  return it->second;
102  }
103  }
104 
106  void clear() {
107  for (auto i : myMap) {
108  delete i.second;
109  }
110  myMap.clear();
111  }
112 
114  int size() const {
115  return (int) myMap.size();
116  }
117 
118  /* @brief Fills the given vector with the stored objects' ids
119  * @param[in] into The container to fill
120  */
121  void insertIDs(std::vector<std::string>& into) const {
122  for (auto i : myMap) {
123  into.push_back(i.first);
124  }
125  }
126 
128  bool changeID(const std::string& oldId, const std::string& newId) {
129  auto i = myMap.find(oldId);
130  if (i == myMap.end()) {
131  return false;
132  } else {
133  // save Item, remove it from Map, and insert it again with the new ID
134  T item = i->second;
135  myMap.erase(i);
136  myMap.insert(std::make_pair(newId, item));
137  return true;
138  }
139  }
140 
142  typename IDMap::const_iterator begin() const {
143  return myMap.begin();
144  }
145 
147  typename IDMap::const_iterator end() const {
148  return myMap.end();
149  }
150 
151 
152 private:
155 };
A map of named object pointers.
void clear()
Removes all items from the container (deletes them, too)
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.