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 /****************************************************************************/ 10 /// @file Named.h 11 /// @author Daniel Krajzewicz 12 /// @author Jakob Erdmann 13 /// @date Sept 2002 14 /// @version $Id$ 15 /// 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 31 /// @brief Function-object for stable sorting of objects acting like Named without being derived (SUMOVehicle) 32 // @note Numbers of different lengths will not be ordered by alphanumerical sorting 33 struct ComparatorIdLess { 34 template<class T> operatorComparatorIdLess35 bool operator()(const T* const a, const T* const b) const { 36 return a->getID() < b->getID(); 37 } 38 }; 39 40 41 /// @brief Function-object for stable sorting of objects with numerical ids 42 struct ComparatorNumericalIdLess { 43 template<class T> operatorComparatorNumericalIdLess44 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 // =========================================================================== 53 /** 54 * @class Named 55 * @brief Base class for objects which have an id. 56 */ 57 class Named { 58 public: 59 /** @brief Constructor 60 * @param[in] id The id of the object 61 */ Named(const std::string & id)62 Named(const std::string& id) : myID(id) { } 63 64 65 /// @brief Destructor ~Named()66 virtual ~Named() { } 67 68 /// @brief get an identifier for Named-like object which may be Null 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 74 /** @brief Returns the id 75 * @return The stored id 76 */ getID()77 const std::string& getID() const { 78 return myID; 79 } 80 81 82 /** @brief resets the id 83 * @param[in] newID The new id of this object 84 */ setID(const std::string & newID)85 void setID(const std::string& newID) { 86 myID = newID; 87 } 88 89 90 /** @class StoringVisitor 91 * @brief Allows to store the object; used as context while traveling the rtree in TraCI 92 */ 93 class StoringVisitor { 94 public: 95 /// @brief Contructor StoringVisitor(std::set<const Named * > & objects)96 StoringVisitor(std::set<const Named*>& objects) : myIDs(nullptr), myObjects(&objects) {} StoringVisitor(std::set<std::string> & objects)97 StoringVisitor(std::set<std::string>& objects) : myIDs(&objects), myObjects(nullptr) {} 98 99 /// @brief Destructor ~StoringVisitor()100 ~StoringVisitor() {} 101 102 /// @brief Adds the given object to the container add(const Named * const o)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 111 /// @brief The container 112 std::set<std::string>* myIDs; 113 std::set<const Named*>* myObjects; 114 115 private: 116 /// @brief invalidated copy constructor 117 StoringVisitor(const StoringVisitor& src); 118 119 /// @brief invalidated assignment operator 120 StoringVisitor& operator=(const StoringVisitor& src); 121 }; 122 123 124 /** @brief Adds this object to the given container 125 * @param[in, filled] cont The container to add this item to 126 */ addTo(const StoringVisitor & cont)127 void addTo(const StoringVisitor& cont) const { 128 cont.add(this); 129 } 130 131 132 protected: 133 /// @brief The name of the object 134 std::string myID; 135 136 }; 137 138 139 #endif 140 141 /****************************************************************************/ 142 143