Eclipse 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-2019 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 
27 #include <map>
28 #include <string>
29 #include <vector>
30 #include <algorithm>
31 
32 
33 // ===========================================================================
34 // class definitions
35 // ===========================================================================
43 template<class T>
45 public:
47  typedef std::map< std::string, T > IDMap;
48 
50  virtual ~NamedObjectCont() {
51  // iterate over all elements to delete it
52  for (auto i : myMap) {
53  delete i.second;
54  }
55  }
56 
66  bool add(const std::string& id, T item) {
67  if (myMap.find(id) != myMap.end()) {
68  return false;
69  }
70  myMap.insert(std::make_pair(id, item));
71  return true;
72  }
73 
79  bool remove(const std::string& id, const bool del = true) {
80  auto it = myMap.find(id);
81  if (it == myMap.end()) {
82  return false;
83  } else {
84  if (del) {
85  delete it->second;
86  }
87  myMap.erase(it);
88  return true;
89  }
90  }
91 
99  T get(const std::string& id) const {
100  auto it = myMap.find(id);
101  if (it == myMap.end()) {
102  return 0;
103  } else {
104  return it->second;
105  }
106  }
107 
109  void clear() {
110  for (auto i : myMap) {
111  delete i.second;
112  }
113  myMap.clear();
114  }
115 
117  int size() const {
118  return (int) myMap.size();
119  }
120 
121  /* @brief Fills the given vector with the stored objects' ids
122  * @param[in] into The container to fill
123  */
124  void insertIDs(std::vector<std::string>& into) const {
125  for (auto i : myMap) {
126  into.push_back(i.first);
127  }
128  }
129 
131  bool changeID(const std::string& oldId, const std::string& newId) {
132  auto i = myMap.find(oldId);
133  if (i == myMap.end()) {
134  return false;
135  } else {
136  // save Item, remove it from Map, and insert it again with the new ID
137  T item = i->second;
138  myMap.erase(i);
139  myMap.insert(std::make_pair(newId, item));
140  return true;
141  }
142  }
143 
145  typename IDMap::const_iterator begin() const {
146  return myMap.begin();
147  }
148 
150  typename IDMap::const_iterator end() const {
151  return myMap.end();
152  }
153 
154 
155 private:
158 };
159 
160 
161 #endif
162 
163 /****************************************************************************/
164 
NamedObjectCont::insertIDs
void insertIDs(std::vector< std::string > &into) const
Definition: NamedObjectCont.h:124
NamedObjectCont::~NamedObjectCont
virtual ~NamedObjectCont()
Destructor.
Definition: NamedObjectCont.h:50
NamedObjectCont::size
int size() const
Returns the number of stored items within the container.
Definition: NamedObjectCont.h:117
NamedObjectCont
A map of named object pointers.
Definition: NamedObjectCont.h:44
NamedObjectCont::remove
bool remove(const std::string &id, const bool del=true)
Removes an item.
Definition: NamedObjectCont.h:79
NamedObjectCont::begin
IDMap::const_iterator begin() const
Returns a reference to the begin iterator for the internal map.
Definition: NamedObjectCont.h:145
NamedObjectCont::myMap
IDMap myMap
The map from key to object.
Definition: NamedObjectCont.h:157
NamedObjectCont::end
IDMap::const_iterator end() const
Returns a reference to the end iterator for the internal map.
Definition: NamedObjectCont.h:150
NamedObjectCont::changeID
bool changeID(const std::string &oldId, const std::string &newId)
change ID of a stored object
Definition: NamedObjectCont.h:131
NamedObjectCont::get
T get(const std::string &id) const
Retrieves an item.
Definition: NamedObjectCont.h:99
NamedObjectCont::clear
void clear()
Removes all items from the container (deletes them, too)
Definition: NamedObjectCont.h:109
NamedObjectCont::IDMap
std::map< std::string, T > IDMap
Definition of the key to pointer map type.
Definition: NamedObjectCont.h:47
NamedObjectCont::add
bool add(const std::string &id, T item)
Adds an item.
Definition: NamedObjectCont.h:66