SUMO - Simulation of Urban MObility
Named.h
Go to the documentation of this file.
1 /****************************************************************************/
8 // Base class for objects which have an id.
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
11 // Copyright (C) 2001-2015 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 /****************************************************************************/
21 #ifndef Named_h
22 #define Named_h
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
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 
80  bool operator()(Named* const a, Named* const b) const {
81  return a->getID() < b->getID();
82  }
83  };
84 
86  template <class NamedLike>
88  bool operator()(const NamedLike* const a, const NamedLike* const b) const {
89  return a->getID() < b->getID();
90  }
91  };
92 
93 
98  public:
100  StoringVisitor(std::set<std::string>& ids) : myIDs(ids) {}
101 
104 
106  void add(const Named* const o) const {
107  myIDs.insert(o->getID());
108  }
109 
111  std::set<std::string>& myIDs;
112 
113  private:
115  StoringVisitor(const StoringVisitor& src);
116 
118  StoringVisitor& operator=(const StoringVisitor& src);
119  };
120 
121 
122 
126  void addTo(const StoringVisitor& cont) const {
127  cont.add(this);
128  }
129 
130 
131 protected:
133  std::string myID;
134 
135 };
136 
137 
138 #endif
139 
140 /****************************************************************************/
141 
virtual ~Named()
Destructor.
Definition: Named.h:54
Function-object for stable sorting of objects acting like Named without being derived (SUMOVehicle) ...
Definition: Named.h:87
bool operator()(const NamedLike *const a, const NamedLike *const b) const
Definition: Named.h:88
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
~StoringVisitor()
Destructor.
Definition: Named.h:103
const std::string & getID() const
Returns the id.
Definition: Named.h:65
StoringVisitor(std::set< std::string > &ids)
Contructor.
Definition: Named.h:100
void addTo(const StoringVisitor &cont) const
Adds this object to the given container.
Definition: Named.h:126
Named(const std::string &id)
Constructor.
Definition: Named.h:50
void add(const Named *const o) const
Adds the given object to the container.
Definition: Named.h:106
Base class for objects which have an id.
Definition: Named.h:45
bool operator()(Named *const a, Named *const b) const
Definition: Named.h:80
Allows to store the object; used as context while traveling the rtree in TraCI.
Definition: Named.h:97
std::string myID
The name of the object.
Definition: Named.h:133
void setID(const std::string &newID)
resets the id
Definition: Named.h:73
std::set< std::string > & myIDs
The container.
Definition: Named.h:111
Function-object for stable sorting in containers.
Definition: Named.h:79