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