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