SUMO - Simulation of Urban MObility
Named.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-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 /****************************************************************************/
16 // Base class for objects which have an id.
17 /****************************************************************************/
18 #ifndef Named_h
19 #define Named_h
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <iostream>
28 #include <string>
29 #include <set>
30 
31 
33 // @note Numbers of different lenghts will not be ordered by alphanumerical sorting
35  template<class T>
36  bool operator()(const T* const a, const T* const b) const {
37  return a->getID() < b->getID();
38  }
39 };
40 
41 
42 // ===========================================================================
43 // class definitions
44 // ===========================================================================
49 class Named {
50 public:
54  Named(const std::string& id) : myID(id) { }
55 
56 
58  virtual ~Named() { }
59 
61  template<class T>
62  static std::string getIDSecure(const T* obj, const std::string& fallBack = "NULL") {
63  return obj == 0 ? fallBack : obj->getID();
64  }
65 
69  const std::string& getID() const {
70  return myID;
71  }
72 
73 
77  void setID(const std::string& newID) {
78  myID = newID;
79  }
80 
81 
86  public:
88  StoringVisitor(std::set<std::string>& ids) : myIDs(ids) {}
89 
92 
94  void add(const Named* const o) const {
95  myIDs.insert(o->getID());
96  }
97 
99  std::set<std::string>& myIDs;
100 
101  private:
103  StoringVisitor(const StoringVisitor& src);
104 
106  StoringVisitor& operator=(const StoringVisitor& src);
107  };
108 
109 
110 
114  void addTo(const StoringVisitor& cont) const {
115  cont.add(this);
116  }
117 
118 
119 protected:
121  std::string myID;
122 
123 };
124 
125 
126 #endif
127 
128 /****************************************************************************/
129 
virtual ~Named()
Destructor.
Definition: Named.h:58
void add(const Named *const o) const
Adds the given object to the container.
Definition: Named.h:94
void addTo(const StoringVisitor &cont) const
Adds this object to the given container.
Definition: Named.h:114
static std::string getIDSecure(const T *obj, const std::string &fallBack="NULL")
get an identifier for Named-like object which may be Null
Definition: Named.h:62
const std::string & getID() const
Returns the id.
Definition: Named.h:69
~StoringVisitor()
Destructor.
Definition: Named.h:91
bool operator()(const T *const a, const T *const b) const
Definition: Named.h:36
StoringVisitor(std::set< std::string > &ids)
Contructor.
Definition: Named.h:88
Named(const std::string &id)
Constructor.
Definition: Named.h:54
Base class for objects which have an id.
Definition: Named.h:49
Allows to store the object; used as context while traveling the rtree in TraCI.
Definition: Named.h:85
std::string myID
The name of the object.
Definition: Named.h:121
void setID(const std::string &newID)
resets the id
Definition: Named.h:77
std::set< std::string > & myIDs
The container.
Definition: Named.h:99
Function-object for stable sorting of objects acting like Named without being derived (SUMOVehicle) ...
Definition: Named.h:34