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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
18 // Base class for objects which have an id.
19 /****************************************************************************/
20 #ifndef Named_h
21 #define Named_h
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <iostream>
34 #include <string>
35 #include <set>
36 
38 // @note Numbers of different lenghts will not be ordered by alphanumerical sorting
40  template<class T>
41  bool operator()(const T* const a, const T* const b) const {
42  return a->getID() < b->getID();
43  }
44 };
45 
46 
47 // ===========================================================================
48 // class definitions
49 // ===========================================================================
54 class Named {
55 public:
59  Named(const std::string& id) : myID(id) { }
60 
61 
63  virtual ~Named() { }
64 
66  template<class T>
67  static std::string getIDSecure(const T* obj, const std::string& fallBack = "NULL") {
68  return obj == 0 ? fallBack : obj->getID();
69  }
70 
74  const std::string& getID() const {
75  return myID;
76  }
77 
78 
82  void setID(const std::string& newID) {
83  myID = newID;
84  }
85 
86 
91  public:
93  StoringVisitor(std::set<std::string>& ids) : myIDs(ids) {}
94 
97 
99  void add(const Named* const o) const {
100  myIDs.insert(o->getID());
101  }
102 
104  std::set<std::string>& myIDs;
105 
106  private:
108  StoringVisitor(const StoringVisitor& src);
109 
111  StoringVisitor& operator=(const StoringVisitor& src);
112  };
113 
114 
115 
119  void addTo(const StoringVisitor& cont) const {
120  cont.add(this);
121  }
122 
123 
124 protected:
126  std::string myID;
127 
128 };
129 
130 
131 #endif
132 
133 /****************************************************************************/
134 
virtual ~Named()
Destructor.
Definition: Named.h:63
void add(const Named *const o) const
Adds the given object to the container.
Definition: Named.h:99
void addTo(const StoringVisitor &cont) const
Adds this object to the given container.
Definition: Named.h:119
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:67
const std::string & getID() const
Returns the id.
Definition: Named.h:74
~StoringVisitor()
Destructor.
Definition: Named.h:96
bool operator()(const T *const a, const T *const b) const
Definition: Named.h:41
StoringVisitor(std::set< std::string > &ids)
Contructor.
Definition: Named.h:93
Named(const std::string &id)
Constructor.
Definition: Named.h:59
Base class for objects which have an id.
Definition: Named.h:54
Allows to store the object; used as context while traveling the rtree in TraCI.
Definition: Named.h:90
std::string myID
The name of the object.
Definition: Named.h:126
void setID(const std::string &newID)
resets the id
Definition: Named.h:82
std::set< std::string > & myIDs
The container.
Definition: Named.h:104
Function-object for stable sorting of objects acting like Named without being derived (SUMOVehicle) ...
Definition: Named.h:39