SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NamedObjectCont.h
Go to the documentation of this file.
1 /****************************************************************************/
8 // A map of named object pointers
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
11 // Copyright (C) 2001-2014 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
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 // ===========================================================================
52 template<class T>
54 public:
56  typedef std::map< std::string, T > IDMap;
57 
60 
61 
63  virtual ~NamedObjectCont() {
64  for (typename IDMap::iterator i = myMap.begin(); i != myMap.end(); i++) {
65  delete(*i).second;
66  }
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 
88 
93  virtual bool remove(const std::string& id) {
94  if (myMap.find(id) == myMap.end()) {
95  return false;
96  }
97  typename std::map<std::string, T>::iterator i = myMap.find(id);
98  delete i->second;
99  myMap.erase(i);
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 i = myMap.find(id);
114  if (i == myMap.end()) {
115  return 0;
116  }
117  return (*i).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 
131 
136  unsigned int size() const {
137  return (unsigned int) myMap.size();
138  }
139 
140 
150  bool erase(const std::string& id) {
151  typename IDMap::iterator i = myMap.find(id);
152  if (i == myMap.end()) {
153  return false;
154  }
155  T o = (*i).second;
156  myMap.erase(i);
157  // and from the vector
158  typename ObjectVector::iterator i2 =
159  find(myVector.begin(), myVector.end(), o);
160  myHaveChanged = true;
161  if (i2 != myVector.end()) {
162  myVector.erase(i2);
163  }
164  delete o;
165  return true;
166  }
167 
168 
169  /* @brief Returns the reference to a vector that contains all objects.
170  *
171  * This method returns the reference to a vector which is stored within
172  * this class and contains all known objects stored within the map.
173  * This vector is rebuild in prior if "myHaveChanged" indicates
174  * a change has taken place.
175  *
176  * @return Reference to a saved vector of objects within the map
177  */
178  const std::vector<T>& buildAndGetStaticVector() const {
179  if (myHaveChanged) {
180  myVector.clear();
181  typename IDMap::const_iterator i;
182  for (i = myMap.begin(); i != myMap.end(); ++i) {
183  myVector.push_back((*i).second);
184  }
185  myHaveChanged = false;
186  }
187  return myVector;
188  }
189 
190 
191  /* @brief Returns a vector that contains all objects.
192  *
193  * This method builds and returns a vector which contains all known
194  * objects stored within the map.
195  *
196  * @return A vector of objects within the map
197  */
198  std::vector<T> getTempVector() const {
199  std::vector<T> ret;
200  typename IDMap::const_iterator i;
201  for (i = myMap.begin(); i != myMap.end(); ++i) {
202  ret.push_back((*i).second);
203  }
204  return ret;
205  }
206 
207 
208  /* @brief Fills the given vector with the stored objects' ids
209  * @param[in] into The container to fill
210  */
211  void insertIDs(std::vector<std::string>& into) const {
212  typename IDMap::const_iterator i;
213  for (i = myMap.begin(); i != myMap.end(); ++i) {
214  into.push_back((*i).first);
215  }
216  }
217 
218 
219  /* @brief Returns a reference to the internal map
220  *
221  * @return A reference to the internal map
222  */
223  const IDMap& getMyMap() const {
224  return myMap;
225  }
226 
227 
228 private:
230  typedef typename IDMap::iterator myContIt;
231 
234 
236  typedef std::vector<T> ObjectVector;
237 
240 
242  mutable bool myHaveChanged;
243 
244 };
245 
246 
247 #endif
248 
249 /****************************************************************************/
250 
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.