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 lengths 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 
44  template<class T>
45  bool operator()(const T* const a, const T* const b) const {
46  return a->getNumericalID() < b->getNumericalID();
47  }
48 };
49 
50 
51 // ===========================================================================
52 // class definitions
53 // ===========================================================================
58 class Named {
59 public:
63  Named(const std::string& id) : myID(id) { }
64 
65 
67  virtual ~Named() { }
68 
70  template<class T>
71  static std::string getIDSecure(const T* obj, const std::string& fallBack = "NULL") {
72  return obj == 0 ? fallBack : obj->getID();
73  }
74 
78  const std::string& getID() const {
79  return myID;
80  }
81 
82 
86  void setID(const std::string& newID) {
87  myID = newID;
88  }
89 
90 
95  public:
97  StoringVisitor(std::set<std::string>& ids) : myIDs(ids) {}
98 
101 
103  void add(const Named* const o) const {
104  myIDs.insert(o->getID());
105  }
106 
108  std::set<std::string>& myIDs;
109 
110  private:
112  StoringVisitor(const StoringVisitor& src);
113 
115  StoringVisitor& operator=(const StoringVisitor& src);
116  };
117 
118 
119 
123  void addTo(const StoringVisitor& cont) const {
124  cont.add(this);
125  }
126 
127 
128 protected:
130  std::string myID;
131 
132 };
133 
134 
135 #endif
136 
137 /****************************************************************************/
138 
virtual ~Named()
Destructor.
Definition: Named.h:67
void add(const Named *const o) const
Adds the given object to the container.
Definition: Named.h:103
void addTo(const StoringVisitor &cont) const
Adds this object to the given container.
Definition: Named.h:123
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:71
const std::string & getID() const
Returns the id.
Definition: Named.h:78
~StoringVisitor()
Destructor.
Definition: Named.h:100
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:97
Named(const std::string &id)
Constructor.
Definition: Named.h:63
Base class for objects which have an id.
Definition: Named.h:58
Function-object for stable sorting of objects with numerical ids.
Definition: Named.h:43
Allows to store the object; used as context while traveling the rtree in TraCI.
Definition: Named.h:94
std::string myID
The name of the object.
Definition: Named.h:130
void setID(const std::string &newID)
resets the id
Definition: Named.h:86
std::set< std::string > & myIDs
The container.
Definition: Named.h:108
Function-object for stable sorting of objects acting like Named without being derived (SUMOVehicle) ...
Definition: Named.h:34
bool operator()(const T *const a, const T *const b) const
Definition: Named.h:45