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-2016 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 
62 
64  virtual ~NamedObjectCont() {
65  for (typename IDMap::iterator i = myMap.begin(); i != myMap.end(); i++) {
66  delete(*i).second;
67  }
68  }
69 
70 
80  virtual bool add(const std::string& id, T item) {
81  if (myMap.find(id) != myMap.end()) {
82  return false;
83  }
84  myMap.insert(std::make_pair(id, item));
85  myHaveChanged = true;
86  return true;
87  }
88 
89 
94  virtual bool remove(const std::string& id) {
95  if (myMap.find(id) == myMap.end()) {
96  return false;
97  }
98  typename std::map<std::string, T>::iterator i = myMap.find(id);
99  delete i->second;
100  myMap.erase(i);
101  myHaveChanged = true;
102  return true;
103  }
104 
105 
113  T get(const std::string& id) const {
114  typename std::map<std::string, T>::const_iterator i = myMap.find(id);
115  if (i == myMap.end()) {
116  return 0;
117  }
118  return (*i).second;
119  }
120 
121 
123  void clear() {
124  for (typename IDMap::iterator i = myMap.begin(); i != myMap.end(); i++) {
125  delete(*i).second;
126  }
127  myMap.clear();
128  myVector.clear();
129  myHaveChanged = true;
130  }
131 
132 
137  unsigned int size() const {
138  return (unsigned int) myMap.size();
139  }
140 
141 
151  bool erase(const std::string& id) {
152  typename IDMap::iterator i = myMap.find(id);
153  if (i == myMap.end()) {
154  return false;
155  }
156  T o = (*i).second;
157  myMap.erase(i);
158  // and from the vector
159  typename ObjectVector::iterator i2 =
160  find(myVector.begin(), myVector.end(), o);
161  myHaveChanged = true;
162  if (i2 != myVector.end()) {
163  myVector.erase(i2);
164  }
165  delete o;
166  return true;
167  }
168 
169 
170  /* @brief Returns the reference to a vector that contains all objects.
171  *
172  * This method returns the reference to a vector which is stored within
173  * this class and contains all known objects stored within the map.
174  * This vector is rebuild in prior if "myHaveChanged" indicates
175  * a change has taken place.
176  *
177  * @return Reference to a saved vector of objects within the map
178  */
179  const std::vector<T>& buildAndGetStaticVector() const {
180  if (myHaveChanged) {
181  myVector.clear();
182  typename IDMap::const_iterator i;
183  for (i = myMap.begin(); i != myMap.end(); ++i) {
184  myVector.push_back((*i).second);
185  }
186  myHaveChanged = false;
187  }
188  return myVector;
189  }
190 
191 
192  /* @brief Returns a vector that contains all objects.
193  *
194  * This method builds and returns a vector which contains all known
195  * objects stored within the map.
196  *
197  * @return A vector of objects within the map
198  */
199  std::vector<T> getTempVector() const {
200  std::vector<T> ret;
201  typename IDMap::const_iterator i;
202  for (i = myMap.begin(); i != myMap.end(); ++i) {
203  ret.push_back((*i).second);
204  }
205  return ret;
206  }
207 
208 
209  /* @brief Fills the given vector with the stored objects' ids
210  * @param[in] into The container to fill
211  */
212  void insertIDs(std::vector<std::string>& into) const {
213  typename IDMap::const_iterator i;
214  for (i = myMap.begin(); i != myMap.end(); ++i) {
215  into.push_back((*i).first);
216  }
217  }
218 
219 
220  /* @brief Returns a reference to the internal map
221  *
222  * @return A reference to the internal map
223  */
224  const IDMap& getMyMap() const {
225  return myMap;
226  }
227 
228 
229 private:
231  typedef typename IDMap::iterator myContIt;
232 
234  IDMap myMap;
235 
237  typedef std::vector<T> ObjectVector;
238 
240  mutable ObjectVector myVector;
241 
243  mutable bool myHaveChanged;
244 
245 };
246 
247 
248 #endif
249 
250 /****************************************************************************/
251 
ObjectVector myVector
The stored vector of all known items.
std::vector< T > ObjectVector
Definition objects vector.
IDMap myMap
The map from key to 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
unsigned int size() const
Returns the number of items within the container.
std::vector< T > getTempVector() const
const IDMap & getMyMap() const
virtual ~NamedObjectCont()
Destructor.
const std::vector< T > & buildAndGetStaticVector() const
bool myHaveChanged
Information whether the vector is out of sync with the map.
NamedObjectCont()
Constructor.