SUMO - Simulation of Urban MObility
NamedObjectCont.h
Go to the documentation of this file.
1 /****************************************************************************/
9 // A map of named object pointers
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2002-2017 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 #ifndef NamedObjectCont_h
23 #define NamedObjectCont_h
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <map>
36 #include <string>
37 #include <vector>
38 #include <algorithm>
39 
40 
41 // ===========================================================================
42 // class definitions
43 // ===========================================================================
53 template<class T>
55 public:
57  typedef std::map< std::string, T > IDMap;
58 
61 
63  virtual ~NamedObjectCont() {
64  // iterate over all elements to delete it
65  for (typename IDMap::iterator i = myMap.begin(); i != myMap.end(); i++) {
66  delete(*i).second;
67  }
68  }
69 
79  virtual bool add(const std::string& id, T item) {
80  if (myMap.find(id) != myMap.end()) {
81  return false;
82  }
83  myMap.insert(std::make_pair(id, item));
84  myHaveChanged = true;
85  return true;
86  }
87 
93  virtual bool remove(const std::string& id) {
94  typename std::map<std::string, T>::iterator it = myMap.find(id);
95  if (it == myMap.end()) {
96  return false;
97  } else {
98  delete it->second;
99  myMap.erase(it);
100  myHaveChanged = true;
101  return true;
102  }
103  }
104 
112  T get(const std::string& id) const {
113  typename std::map<std::string, T>::const_iterator it = myMap.find(id);
114  if (it == myMap.end()) {
115  return 0;
116  } else {
117  return it->second;
118  }
119  }
120 
122  void clear() {
123  for (typename IDMap::iterator i = myMap.begin(); i != myMap.end(); i++) {
124  delete(*i).second;
125  }
126  myMap.clear();
127  myVector.clear();
128  myHaveChanged = true;
129  }
130 
132  int size() const {
133  return (int) myMap.size();
134  }
135 
145  bool erase(const std::string& id) {
146  typename IDMap::iterator i = myMap.find(id);
147  if (i == myMap.end()) {
148  return false;
149  } else {
150  T o = (*i).second;
151  myMap.erase(i);
152  // and from the vector
153  typename ObjectVector::iterator i2 =
154  find(myVector.begin(), myVector.end(), o);
155  myHaveChanged = true;
156  if (i2 != myVector.end()) {
157  myVector.erase(i2);
158  }
159  delete o;
160  return true;
161  }
162  }
163 
164  /* @brief Fills the given vector with the stored objects' ids
165  * @param[in] into The container to fill
166  */
167  void insertIDs(std::vector<std::string>& into) const {
168  typename IDMap::const_iterator i;
169  for (i = myMap.begin(); i != myMap.end(); ++i) {
170  into.push_back((*i).first);
171  }
172  }
173 
175  bool changeID(const std::string& oldId, const std::string& newId) {
176  typename IDMap::iterator i = myMap.find(oldId);
177  if (i == myMap.end()) {
178  return false;
179  } else {
180  // save Item, remove it from Map, and insert it again with the new ID
181  T item = (*i).second;
182  myMap.erase(i);
183  myMap.insert(std::make_pair(newId, item));
184  return true;
185  }
186  }
187 
189  const IDMap& getMyMap() const {
190  return myMap;
191  }
192 
193 
194 private:
196  typedef typename IDMap::iterator myContIt;
197 
199  IDMap myMap;
200 
202  typedef std::vector<T> ObjectVector;
203 
205  mutable ObjectVector myVector;
206 
208  mutable bool myHaveChanged;
209 };
210 
211 
212 #endif
213 
214 /****************************************************************************/
215 
ObjectVector myVector
The stored vector of all known items.
std::vector< T > ObjectVector
Definition objects vector.
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.
bool erase(const std::string &id)
Removes the named item from the container.
virtual bool add(const std::string &id, T item)
Adds an item.
A map of named object pointers.
void clear()
Removes all items from the container (deletes them, too)
IDMap::iterator myContIt
Definition of the container type iterator.
void insertIDs(std::vector< std::string > &into) const
const IDMap & getMyMap() const
Returns a reference to the internal map.
virtual ~NamedObjectCont()
Destructor.
bool myHaveChanged
Information whether the vector is out of sync with the map.
NamedObjectCont()
Constructor.
int size() const
Returns the number of stored items within the container.