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 /****************************************************************************/
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 
26 #include <iostream>
27 #include <string>
28 #include <set>
29 
30 
32 // @note Numbers of different lengths will not be ordered by alphanumerical sorting
34  template<class T>
35  bool operator()(const T* const a, const T* const b) const {
36  return a->getID() < b->getID();
37  }
38 };
39 
40 
43  template<class T>
44  bool operator()(const T* const a, const T* const b) const {
45  return a->getNumericalID() < b->getNumericalID();
46  }
47 };
48 
49 
50 // ===========================================================================
51 // class definitions
52 // ===========================================================================
57 class Named {
58 public:
62  Named(const std::string& id) : myID(id) { }
63 
64 
66  virtual ~Named() { }
67 
69  template<class T>
70  static std::string getIDSecure(const T* obj, const std::string& fallBack = "NULL") {
71  return obj == 0 ? fallBack : obj->getID();
72  }
73 
77  const std::string& getID() const {
78  return myID;
79  }
80 
81 
85  void setID(const std::string& newID) {
86  myID = newID;
87  }
88 
89 
94  public:
96  StoringVisitor(std::set<const Named*>& objects) : myIDs(nullptr), myObjects(&objects) {}
97  StoringVisitor(std::set<std::string>& objects) : myIDs(&objects), myObjects(nullptr) {}
98 
101 
103  void add(const Named* const o) const {
104  if (myObjects == nullptr) {
105  myIDs->insert(o->getID());
106  } else {
107  myObjects->insert(o);
108  }
109  }
110 
112  std::set<std::string>* myIDs;
113  std::set<const Named*>* myObjects;
114 
115  private:
117  StoringVisitor(const StoringVisitor& src);
118 
121  };
122 
123 
127  void addTo(const StoringVisitor& cont) const {
128  cont.add(this);
129  }
130 
131 
132 protected:
134  std::string myID;
135 
136 };
137 
138 
139 #endif
140 
141 /****************************************************************************/
142 
Named::StoringVisitor::myIDs
std::set< std::string > * myIDs
The container.
Definition: Named.h:112
Named
Base class for objects which have an id.
Definition: Named.h:57
ComparatorNumericalIdLess::operator()
bool operator()(const T *const a, const T *const b) const
Definition: Named.h:44
Named::~Named
virtual ~Named()
Destructor.
Definition: Named.h:66
Named::StoringVisitor
Allows to store the object; used as context while traveling the rtree in TraCI.
Definition: Named.h:93
Named::Named
Named(const std::string &id)
Constructor.
Definition: Named.h:62
ComparatorIdLess::operator()
bool operator()(const T *const a, const T *const b) const
Definition: Named.h:35
Named::StoringVisitor::~StoringVisitor
~StoringVisitor()
Destructor.
Definition: Named.h:100
ComparatorIdLess
Function-object for stable sorting of objects acting like Named without being derived (SUMOVehicle)
Definition: Named.h:33
Named::StoringVisitor::myObjects
std::set< const Named * > * myObjects
Definition: Named.h:113
Named::StoringVisitor::StoringVisitor
StoringVisitor(std::set< std::string > &objects)
Definition: Named.h:97
Named::addTo
void addTo(const StoringVisitor &cont) const
Adds this object to the given container.
Definition: Named.h:127
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:42
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:70
Named::StoringVisitor::add
void add(const Named *const o) const
Adds the given object to the container.
Definition: Named.h:103
Named::myID
std::string myID
The name of the object.
Definition: Named.h:134
Named::getID
const std::string & getID() const
Returns the id.
Definition: Named.h:77
Named::StoringVisitor::StoringVisitor
StoringVisitor(std::set< const Named * > &objects)
Contructor.
Definition: Named.h:96
Named::setID
void setID(const std::string &newID)
resets the id
Definition: Named.h:85