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-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 // A map of named object pointers
18 /****************************************************************************/
19 #ifndef NamedObjectCont_h
20 #define NamedObjectCont_h
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
28 #include <map>
29 #include <string>
30 #include <vector>
31 #include <algorithm>
32 
33 
34 // ===========================================================================
35 // class definitions
36 // ===========================================================================
44 template<class T>
46 public:
48  typedef std::map< std::string, T > IDMap;
49 
51  virtual ~NamedObjectCont() {
52  // iterate over all elements to delete it
53  for (auto i : myMap) {
54  delete i.second;
55  }
56  }
57 
67  bool add(const std::string& id, T item) {
68  if (myMap.find(id) != myMap.end()) {
69  return false;
70  }
71  myMap.insert(std::make_pair(id, item));
72  return true;
73  }
74 
80  bool remove(const std::string& id, const bool del = true) {
81  auto it = myMap.find(id);
82  if (it == myMap.end()) {
83  return false;
84  } else {
85  if (del) {
86  delete it->second;
87  }
88  myMap.erase(it);
89  return true;
90  }
91  }
92 
100  T get(const std::string& id) const {
101  auto it = myMap.find(id);
102  if (it == myMap.end()) {
103  return 0;
104  } else {
105  return it->second;
106  }
107  }
108 
110  void clear() {
111  for (auto i : myMap) {
112  delete i.second;
113  }
114  myMap.clear();
115  }
116 
118  int size() const {
119  return (int) myMap.size();
120  }
121 
122  /* @brief Fills the given vector with the stored objects' ids
123  * @param[in] into The container to fill
124  */
125  void insertIDs(std::vector<std::string>& into) const {
126  for (auto i : myMap) {
127  into.push_back(i.first);
128  }
129  }
130 
132  bool changeID(const std::string& oldId, const std::string& newId) {
133  auto i = myMap.find(oldId);
134  if (i == myMap.end()) {
135  return false;
136  } else {
137  // save Item, remove it from Map, and insert it again with the new ID
138  T item = i->second;
139  myMap.erase(i);
140  myMap.insert(std::make_pair(newId, item));
141  return true;
142  }
143  }
144 
146  typename IDMap::const_iterator begin() const {
147  return myMap.begin();
148  }
149 
151  typename IDMap::const_iterator end() const {
152  return myMap.end();
153  }
154 
155 
156 private:
158  IDMap myMap;
159 };
160 
161 
162 #endif
163 
164 /****************************************************************************/
165 
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.