Eclipse 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-2019 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 /****************************************************************************/
15 // Base class for objects which have an id.
16 /****************************************************************************/
17 #ifndef Named_h
18 #define Named_h
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 
25 #include <iostream>
26 #include <string>
27 #include <set>
28 
29 
31 // @note Numbers of different lengths will not be ordered by alphanumerical sorting
33  template<class T>
34  bool operator()(const T* const a, const T* const b) const {
35  return a->getID() < b->getID();
36  }
37 };
38 
39 
42  template<class T>
43  bool operator()(const T* const a, const T* const b) const {
44  return a->getNumericalID() < b->getNumericalID();
45  }
46 };
47 
48 
49 // ===========================================================================
50 // class definitions
51 // ===========================================================================
56 class Named {
57 public:
61  Named(const std::string& id) : myID(id) { }
62 
63 
65  virtual ~Named() { }
66 
68  template<class T>
69  static std::string getIDSecure(const T* obj, const std::string& fallBack = "NULL") {
70  return obj == 0 ? fallBack : obj->getID();
71  }
72 
76  const std::string& getID() const {
77  return myID;
78  }
79 
80 
84  void setID(const std::string& newID) {
85  myID = newID;
86  }
87 
88 
93  public:
95  StoringVisitor(std::set<const Named*>& objects) : myIDs(nullptr), myObjects(&objects) {}
96  StoringVisitor(std::set<std::string>& objects) : myIDs(&objects), myObjects(nullptr) {}
97 
100 
102  void add(const Named* const o) const {
103  if (myObjects == nullptr) {
104  myIDs->insert(o->getID());
105  } else {
106  myObjects->insert(o);
107  }
108  }
109 
111  std::set<std::string>* myIDs;
112  std::set<const Named*>* myObjects;
113 
114  private:
116  StoringVisitor(const StoringVisitor& src);
117 
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 
Named::StoringVisitor::myIDs
std::set< std::string > * myIDs
The container.
Definition: Named.h:111
Named
Base class for objects which have an id.
Definition: Named.h:56
ComparatorNumericalIdLess::operator()
bool operator()(const T *const a, const T *const b) const
Definition: Named.h:43
Named::~Named
virtual ~Named()
Destructor.
Definition: Named.h:65
Named::StoringVisitor
Allows to store the object; used as context while traveling the rtree in TraCI.
Definition: Named.h:92
Named::Named
Named(const std::string &id)
Constructor.
Definition: Named.h:61
ComparatorIdLess::operator()
bool operator()(const T *const a, const T *const b) const
Definition: Named.h:34
Named::StoringVisitor::~StoringVisitor
~StoringVisitor()
Destructor.
Definition: Named.h:99
ComparatorIdLess
Function-object for stable sorting of objects acting like Named without being derived (SUMOVehicle)
Definition: Named.h:32
Named::StoringVisitor::myObjects
std::set< const Named * > * myObjects
Definition: Named.h:112
Named::StoringVisitor::StoringVisitor
StoringVisitor(std::set< std::string > &objects)
Definition: Named.h:96
Named::addTo
void addTo(const StoringVisitor &cont) const
Adds this object to the given container.
Definition: Named.h:126
Named::StoringVisitor::operator=
StoringVisitor & operator=(const StoringVisitor &src)
invalidated assignment operator
ComparatorNumericalIdLess
Function-object for stable sorting of objects with numerical ids.
Definition: Named.h:41
Named::getIDSecure
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:69
Named::StoringVisitor::add
void add(const Named *const o) const
Adds the given object to the container.
Definition: Named.h:102
Named::myID
std::string myID
The name of the object.
Definition: Named.h:133
Named::getID
const std::string & getID() const
Returns the id.
Definition: Named.h:76
Named::StoringVisitor::StoringVisitor
StoringVisitor(std::set< const Named * > &objects)
Contructor.
Definition: Named.h:95
Named::setID
void setID(const std::string &newID)
resets the id
Definition: Named.h:84