Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2002-2024 German Aerospace Center (DLR) and others.
4 : // This program and the accompanying materials are made available under the
5 : // terms of the Eclipse Public License 2.0 which is available at
6 : // https://www.eclipse.org/legal/epl-2.0/
7 : // This Source Code may also be made available under the following Secondary
8 : // Licenses when the conditions for such availability set forth in the Eclipse
9 : // Public License 2.0 are satisfied: GNU General Public License, version 2
10 : // or later which is available at
11 : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 : /****************************************************************************/
14 : /// @file NamedObjectCont.h
15 : /// @author Daniel Krajzewicz
16 : /// @author Michael Behrisch
17 : /// @author Jakob Erdmann
18 : /// @date Sept 2002
19 : ///
20 : // A map of named object pointers
21 : /****************************************************************************/
22 : #pragma once
23 : #include <config.h>
24 : #include <map>
25 : #include <string>
26 : #include <vector>
27 : #include <algorithm>
28 :
29 :
30 : // ===========================================================================
31 : // class definitions
32 : // ===========================================================================
33 : /**
34 : * @class NamedObjectCont
35 : * @brief A map of named object pointers
36 : *
37 : * An associative storage (map) for objects (pointers to them to be exact),
38 : * which do have a name.
39 : */
40 : template<class T>
41 : class NamedObjectCont {
42 : public:
43 : /// @brief Definition of the key to pointer map type
44 : typedef std::map< std::string, T > IDMap;
45 :
46 : ///@brief Constructor
47 130477 : NamedObjectCont() {}
48 :
49 : ///@brief Destructor
50 292449 : virtual ~NamedObjectCont() {
51 : // iterate over all elements to delete it
52 1515246 : for (auto i : myMap) {
53 1222797 : delete i.second;
54 : }
55 292449 : }
56 :
57 : /** @brief Adds an item
58 : *
59 : * If another item with the same name is already known, false is reported
60 : * and the item is not added.
61 : *
62 : * @param[in] id The id of the item to add
63 : * @param[in] item The item to add
64 : * @return If the item could be added (no item with the same id was within the container before)
65 : */
66 1430036 : bool add(const std::string& id, T item) {
67 : const auto it = myMap.lower_bound(id);
68 1430036 : if (it == myMap.end() || it->first != id) {
69 1429997 : myMap.emplace_hint(it, id, item);
70 1429997 : return true;
71 : }
72 : return false;
73 : }
74 :
75 : /** @brief Removes an item
76 : * @param[in] id The id of the item to remove
77 : * @param[in] del delete item after removing of container
78 : * @return If the item could be removed (an item with the id was within the container before)
79 : */
80 152040 : bool remove(const std::string& id, const bool del = true) {
81 : auto it = myMap.find(id);
82 152040 : if (it == myMap.end()) {
83 : return false;
84 : } else {
85 127682 : if (del) {
86 127682 : delete it->second;
87 : }
88 : myMap.erase(it);
89 127682 : return true;
90 : }
91 : }
92 :
93 : /** @brief Retrieves an item
94 : *
95 : * Returns 0 when no item with the given id is stored within the container
96 : *
97 : * @param[in] id The id of the item to retrieve
98 : * @return The item stored under the given id, or 0 if no such item exists
99 : */
100 : T get(const std::string& id) const {
101 : auto it = myMap.find(id);
102 9170450 : if (it == myMap.end()) {
103 : return 0;
104 : } else {
105 8793004 : return it->second;
106 : }
107 : }
108 :
109 : /// @brief Removes all items from the container (deletes them, too)
110 10032 : void clear() {
111 48151 : for (auto i : myMap) {
112 38119 : delete i.second;
113 : }
114 : myMap.clear();
115 10032 : }
116 :
117 : /// @brief Returns the number of stored items within the container
118 : int size() const {
119 39175 : 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 2939 : void insertIDs(std::vector<std::string>& into) const {
126 11063 : for (auto i : myMap) {
127 8124 : into.push_back(i.first);
128 : }
129 2939 : }
130 :
131 : /// @brief change ID of a stored object
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 :
145 : /// @brief Returns a reference to the begin iterator for the internal map
146 : typename IDMap::const_iterator begin() const {
147 : return myMap.begin();
148 : }
149 :
150 : /// @brief Returns a reference to the end iterator for the internal map
151 : typename IDMap::const_iterator end() const {
152 : return myMap.end();
153 : }
154 :
155 :
156 : private:
157 : /// @brief The map from key to object
158 : IDMap myMap;
159 : };
|