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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // A map of named object pointers
20 /****************************************************************************/
21 #ifndef NamedObjectCont_h
22 #define NamedObjectCont_h
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <map>
35 #include <string>
36 #include <vector>
37 #include <algorithm>
38 
39 
40 // ===========================================================================
41 // class definitions
42 // ===========================================================================
50 template<class T>
52 public:
54  typedef std::map< std::string, T > IDMap;
55 
57  virtual ~NamedObjectCont() {
58  // iterate over all elements to delete it
59  for (auto i : myMap) {
60  delete i.second;
61  }
62  }
63 
73  bool add(const std::string& id, T item) {
74  if (myMap.find(id) != myMap.end()) {
75  return false;
76  }
77  myMap.insert(std::make_pair(id, item));
78  return true;
79  }
80 
86  bool remove(const std::string& id, const bool del = true) {
87  auto it = myMap.find(id);
88  if (it == myMap.end()) {
89  return false;
90  } else {
91  if (del) {
92  delete it->second;
93  }
94  myMap.erase(it);
95  return true;
96  }
97  }
98 
106  T get(const std::string& id) const {
107  auto it = myMap.find(id);
108  if (it == myMap.end()) {
109  return 0;
110  } else {
111  return it->second;
112  }
113  }
114 
116  void clear() {
117  for (auto i : myMap) {
118  delete i.second;
119  }
120  myMap.clear();
121  }
122 
124  int size() const {
125  return (int) myMap.size();
126  }
127 
128  /* @brief Fills the given vector with the stored objects' ids
129  * @param[in] into The container to fill
130  */
131  void insertIDs(std::vector<std::string>& into) const {
132  for (auto i : myMap) {
133  into.push_back(i.first);
134  }
135  }
136 
138  bool changeID(const std::string& oldId, const std::string& newId) {
139  auto i = myMap.find(oldId);
140  if (i == myMap.end()) {
141  return false;
142  } else {
143  // save Item, remove it from Map, and insert it again with the new ID
144  T item = i->second;
145  myMap.erase(i);
146  myMap.insert(std::make_pair(newId, item));
147  return true;
148  }
149  }
150 
152  typename IDMap::const_iterator begin() const {
153  return myMap.begin();
154  }
155 
157  typename IDMap::const_iterator end() const {
158  return myMap.end();
159  }
160 
161 
162 private:
164  IDMap myMap;
165 };
166 
167 
168 #endif
169 
170 /****************************************************************************/
171 
IDMap myMap
The map from key to object.
bool changeID(const std::string &oldId, const std::string &newId)
change ID of a stored object
std::map< std::string, T > IDMap
Definition of the key to pointer map type.
int size() const
Returns the number of stored items within the container.
A map of named object pointers.
void insertIDs(std::vector< std::string > &into) const
bool add(const std::string &id, T item)
Adds an item.
void clear()
Removes all items from the container (deletes them, too)
virtual ~NamedObjectCont()
Destructor.
IDMap::const_iterator end() const
Returns a reference to the end iterator for the internal map.
IDMap::const_iterator begin() const
Returns a reference to the begin iterator for the internal map.