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 
37 
38 // ===========================================================================
39 // class definitions
40 // ===========================================================================
45 class Named {
46 public:
50  Named(const std::string& id) : myID(id) { }
51 
52 
54  virtual ~Named() { }
55 
57  template<class T>
58  static std::string getIDSecure(const T* obj, const std::string& fallBack = "NULL") {
59  return obj == 0 ? fallBack : obj->getID();
60  }
61 
65  const std::string& getID() const {
66  return myID;
67  }
68 
69 
73  void setID(const std::string& newID) {
74  myID = newID;
75  }
76 
77 
79  // @note Numbers of different lengths will not be ordered by alphanumerical sorting
81  bool operator()(Named* const a, Named* const b) const {
82  return a->getID() < b->getID();
83  }
84  };
85 
87  // @note Numbers of different lenghts will not be ordered by alphanumerical sorting
88  template <class NamedLike>
90  bool operator()(const NamedLike* const a, const NamedLike* const b) const {
91  return a->getID() < b->getID();
92  }
93  };
94 
95 
100  public:
102  StoringVisitor(std::set<std::string>& ids) : myIDs(ids) {}
103 
106 
108  void add(const Named* const o) const {
109  myIDs.insert(o->getID());
110  }
111 
113  std::set<std::string>& myIDs;
114 
115  private:
117  StoringVisitor(const StoringVisitor& src);
118 
120  StoringVisitor& operator=(const StoringVisitor& src);
121  };
122 
123 
124 
128  void addTo(const StoringVisitor& cont) const {
129  cont.add(this);
130  }
131 
132 
133 protected:
135  std::string myID;
136 
137 };
138 
139 
140 #endif
141 
142 /****************************************************************************/
143 
virtual ~Named()
Destructor.
Definition: Named.h:54
void add(const Named *const o) const
Adds the given object to the container.
Definition: Named.h:108
Function-object for stable sorting of objects acting like Named without being derived (SUMOVehicle) ...
Definition: Named.h:89
void addTo(const StoringVisitor &cont) const
Adds this object to the given container.
Definition: Named.h:128
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:58
const std::string & getID() const
Returns the id.
Definition: Named.h:65
~StoringVisitor()
Destructor.
Definition: Named.h:105
bool operator()(const NamedLike *const a, const NamedLike *const b) const
Definition: Named.h:90
StoringVisitor(std::set< std::string > &ids)
Contructor.
Definition: Named.h:102
Named(const std::string &id)
Constructor.
Definition: Named.h:50
Base class for objects which have an id.
Definition: Named.h:45
Allows to store the object; used as context while traveling the rtree in TraCI.
Definition: Named.h:99
std::string myID
The name of the object.
Definition: Named.h:135
void setID(const std::string &newID)
resets the id
Definition: Named.h:73
bool operator()(Named *const a, Named *const b) const
Definition: Named.h:81
std::set< std::string > & myIDs
The container.
Definition: Named.h:113
Function-object for stable sorting in containers.
Definition: Named.h:80