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-2020 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 <map>
24 #include <string>
25 #include <vector>
26 #include <algorithm>
27 
28 
29 // ===========================================================================
30 // class definitions
31 // ===========================================================================
39 template<class T>
41 public:
43  typedef std::map< std::string, T > IDMap;
44 
46  virtual ~NamedObjectCont() {
47  // iterate over all elements to delete it
48  for (auto i : myMap) {
49  delete i.second;
50  }
51  }
52 
62  bool add(const std::string& id, T item) {
63  if (myMap.find(id) != myMap.end()) {
64  return false;
65  }
66  myMap.insert(std::make_pair(id, item));
67  return true;
68  }
69 
75  bool remove(const std::string& id, const bool del = true) {
76  auto it = myMap.find(id);
77  if (it == myMap.end()) {
78  return false;
79  } else {
80  if (del) {
81  delete it->second;
82  }
83  myMap.erase(it);
84  return true;
85  }
86  }
87 
95  T get(const std::string& id) const {
96  auto it = myMap.find(id);
97  if (it == myMap.end()) {
98  return 0;
99  } else {
100  return it->second;
101  }
102  }
103 
105  void clear() {
106  for (auto i : myMap) {
107  delete i.second;
108  }
109  myMap.clear();
110  }
111 
113  int size() const {
114  return (int) myMap.size();
115  }
116 
117  /* @brief Fills the given vector with the stored objects' ids
118  * @param[in] into The container to fill
119  */
120  void insertIDs(std::vector<std::string>& into) const {
121  for (auto i : myMap) {
122  into.push_back(i.first);
123  }
124  }
125 
127  bool changeID(const std::string& oldId, const std::string& newId) {
128  auto i = myMap.find(oldId);
129  if (i == myMap.end()) {
130  return false;
131  } else {
132  // save Item, remove it from Map, and insert it again with the new ID
133  T item = i->second;
134  myMap.erase(i);
135  myMap.insert(std::make_pair(newId, item));
136  return true;
137  }
138  }
139 
141  typename IDMap::const_iterator begin() const {
142  return myMap.begin();
143  }
144 
146  typename IDMap::const_iterator end() const {
147  return myMap.end();
148  }
149 
150 
151 private:
154 };
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.