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-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
19 // Base class for objects which have an id.
20 /****************************************************************************/
21 #pragma once
22 #include <iostream>
23 #include <string>
24 #include <set>
25 
26 
28 // @note Numbers of different lengths will not be ordered by alphanumerical sorting
30  template<class T>
31  bool operator()(const T* const a, const T* const b) const {
32  return a->getID() < b->getID();
33  }
34 };
35 
36 
39  template<class T>
40  bool operator()(const T* const a, const T* const b) const {
41  return a->getNumericalID() < b->getNumericalID();
42  }
43 };
44 
45 
46 // ===========================================================================
47 // class definitions
48 // ===========================================================================
53 class Named {
54 public:
58  Named(const std::string& id) : myID(id) { }
59 
60 
62  virtual ~Named() { }
63 
65  template<class T>
66  static std::string getIDSecure(const T* obj, const std::string& fallBack = "NULL") {
67  return obj == 0 ? fallBack : obj->getID();
68  }
69 
73  const std::string& getID() const {
74  return myID;
75  }
76 
77 
81  virtual void setID(const std::string& newID) {
82  myID = newID;
83  }
84 
85 
90  public:
92  StoringVisitor(std::set<const Named*>& objects) : myObjects(objects) {}
93 
96 
98  void add(const Named* const o) const {
99  myObjects.insert(o);
100  }
101 
103  std::set<const Named*>& myObjects;
104 
105  private:
108 
111  };
112 
113 
117  void addTo(const StoringVisitor& cont) const {
118  cont.add(this);
119  }
120 
121 
122 protected:
124  std::string myID;
125 
126 };
Allows to store the object; used as context while traveling the rtree in TraCI.
Definition: Named.h:89
std::set< const Named * > & myObjects
The container.
Definition: Named.h:103
void add(const Named *const o) const
Adds the given object to the container.
Definition: Named.h:98
StoringVisitor & operator=(const StoringVisitor &src)
invalidated assignment operator
StoringVisitor(std::set< const Named * > &objects)
Contructor.
Definition: Named.h:92
StoringVisitor(const StoringVisitor &src)
invalidated copy constructor
~StoringVisitor()
Destructor.
Definition: Named.h:95
Base class for objects which have an id.
Definition: Named.h:53
Named(const std::string &id)
Constructor.
Definition: Named.h:58
std::string myID
The name of the object.
Definition: Named.h:124
void addTo(const StoringVisitor &cont) const
Adds this object to the given container.
Definition: Named.h:117
virtual void setID(const std::string &newID)
resets the id
Definition: Named.h:81
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:66
const std::string & getID() const
Returns the id.
Definition: Named.h:73
virtual ~Named()
Destructor.
Definition: Named.h:62
Function-object for stable sorting of objects acting like Named without being derived (SUMOVehicle)
Definition: Named.h:29
bool operator()(const T *const a, const T *const b) const
Definition: Named.h:31
Function-object for stable sorting of objects with numerical ids.
Definition: Named.h:38
bool operator()(const T *const a, const T *const b) const
Definition: Named.h:40