LCOV - code coverage report
Current view: top level - src/utils/common - Named.h (source / functions) Coverage Total Hit
Test: lcov.info Lines: 100.0 % 18 18
Test Date: 2026-06-15 15:46:12 Functions: 60.0 % 10 6

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

Generated by: LCOV version 2.0-1