Line data Source code
1 : /****************************************************************************/ 2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo 3 : // Copyright (C) 2001-2024 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 : /****************************************************************************/ 14 : /// @file Named.h 15 : /// @author Daniel Krajzewicz 16 : /// @author Jakob Erdmann 17 : /// @date Sept 2002 18 : /// 19 : // Base class for objects which have an id. 20 : /****************************************************************************/ 21 : #pragma once 22 : #include <config.h> 23 : #include <iostream> 24 : #include <string> 25 : #include <set> 26 : 27 : 28 : /// @brief Function-object for stable sorting of objects acting like Named without being derived (SUMOVehicle) 29 : // @note Numbers of different lengths will not be ordered by alphanumerical sorting 30 : struct ComparatorIdLess { 31 : template<class T> 32 903503 : bool operator()(const T* const a, const T* const b) const { 33 903503 : return a->getID() < b->getID(); 34 : } 35 : }; 36 : 37 : 38 : /// @brief Function-object for stable sorting of objects with numerical ids 39 : struct ComparatorNumericalIdLess { 40 : template<class T> 41 : bool operator()(const T* const a, const T* const b) const { 42 13686759785 : return a->getNumericalID() < b->getNumericalID(); 43 : } 44 : }; 45 : 46 : 47 : // =========================================================================== 48 : // class definitions 49 : // =========================================================================== 50 : /** 51 : * @class Named 52 : * @brief Base class for objects which have an id. 53 : */ 54 303667 : class Named { 55 : public: 56 : /** @brief Constructor 57 : * @param[in] id The id of the object 58 : */ 59 21114817 : Named(const std::string& id) : myID(id) { } 60 : 61 : 62 : /// @brief Destructor 63 20683416 : virtual ~Named() { } 64 : 65 : /// @brief get an identifier for Named-like object which may be Null 66 : template<class T> 67 16712 : static std::string getIDSecure(const T* obj, const std::string& fallBack = "NULL") { 68 5351728 : return obj == 0 ? fallBack : obj->getID(); 69 : } 70 : 71 : /** @brief Returns the id 72 : * @return The stored id 73 : */ 74 : const std::string& getID() const { 75 623002305 : return myID; 76 : } 77 : 78 : 79 : /** @brief resets the id 80 : * @param[in] newID The new id of this object 81 : */ 82 25524 : virtual void setID(const std::string& newID) { 83 25612 : myID = newID; 84 25524 : } 85 : 86 : 87 : /** @class StoringVisitor 88 : * @brief Allows to store the object; used as context while traveling the rtree in TraCI 89 : */ 90 : class StoringVisitor { 91 : public: 92 : /// @brief Constructor 93 226562 : StoringVisitor(std::set<const Named*>& objects) : myObjects(objects) {} 94 : 95 : /// @brief Destructor 96 227728 : ~StoringVisitor() {} 97 : 98 : /// @brief Adds the given object to the container 99 : void add(const Named* const o) const { 100 339352 : myObjects.insert(o); 101 : } 102 : 103 : /// @brief The container 104 : std::set<const Named*>& myObjects; 105 : 106 : private: 107 : /// @brief invalidated copy constructor 108 : StoringVisitor(const StoringVisitor& src); 109 : 110 : /// @brief invalidated assignment operator 111 : StoringVisitor& operator=(const StoringVisitor& src); 112 : }; 113 : 114 : 115 : /** @brief Adds this object to the given container 116 : * @param[in, filled] cont The container to add this item to 117 : */ 118 339352 : void addTo(const StoringVisitor& cont) const { 119 339352 : cont.add(this); 120 339352 : } 121 : 122 : 123 : protected: 124 : /// @brief The name of the object 125 : std::string myID; 126 : 127 : };