LCOV - code coverage report
Current view: top level - src/utils/common - NamedObjectCont.h (source / functions) Hit Total Coverage
Test: lcov.info Lines: 25 25 100.0 %
Date: 2024-05-02 15:31:40 Functions: 35 47 74.5 %

          Line data    Source code
       1             : /****************************************************************************/
       2             : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3             : // Copyright (C) 2002-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    NamedObjectCont.h
      15             : /// @author  Daniel Krajzewicz
      16             : /// @author  Michael Behrisch
      17             : /// @author  Jakob Erdmann
      18             : /// @date    Sept 2002
      19             : ///
      20             : // A map of named object pointers
      21             : /****************************************************************************/
      22             : #pragma once
      23             : #include <config.h>
      24             : #include <map>
      25             : #include <string>
      26             : #include <vector>
      27             : #include <algorithm>
      28             : 
      29             : 
      30             : // ===========================================================================
      31             : // class definitions
      32             : // ===========================================================================
      33             : /**
      34             :  * @class NamedObjectCont
      35             :  * @brief A map of named object pointers
      36             :  *
      37             :  * An associative storage (map) for objects (pointers to them to be exact),
      38             :  *  which do have a name.
      39             :  */
      40             : template<class T>
      41             : class NamedObjectCont {
      42             : public:
      43             :     /// @brief Definition of the key to pointer map type
      44             :     typedef std::map< std::string, T > IDMap;
      45             : 
      46             :     ///@brief Constructor
      47       94487 :     NamedObjectCont() {}
      48             : 
      49             :     ///@brief Destructor
      50      224917 :     virtual ~NamedObjectCont() {
      51             :         // iterate over all elements to delete it
      52     1145719 :         for (auto i : myMap) {
      53      920802 :             delete i.second;
      54             :         }
      55      224917 :     }
      56             : 
      57             :     /** @brief Adds an item
      58             :      *
      59             :      * If another item with the same name is already known, false is reported
      60             :      *  and the item is not added.
      61             :      *
      62             :      * @param[in] id The id of the item to add
      63             :      * @param[in] item The item to add
      64             :      * @return If the item could be added (no item with the same id was within the container before)
      65             :      */
      66     1201319 :     bool add(const std::string& id, T item) {
      67             :         const auto it = myMap.lower_bound(id);
      68     1201319 :         if (it == myMap.end() || it->first != id) {
      69     1201188 :             myMap.emplace_hint(it, id, item);
      70     1201188 :             return true;
      71             :         }
      72             :         return false;
      73             :     }
      74             : 
      75             :     /** @brief Removes an item
      76             :      * @param[in] id The id of the item to remove
      77             :      * @param[in] del delete item after removing of container
      78             :      * @return If the item could be removed (an item with the id was within the container before)
      79             :      */
      80      276627 :     bool remove(const std::string& id, const bool del = true) {
      81             :         auto it = myMap.find(id);
      82      276627 :         if (it == myMap.end()) {
      83             :             return false;
      84             :         } else {
      85      240494 :             if (del) {
      86      240494 :                 delete it->second;
      87             :             }
      88             :             myMap.erase(it);
      89      240494 :             return true;
      90             :         }
      91             :     }
      92             : 
      93             :     /** @brief Retrieves an item
      94             :      *
      95             :      * Returns 0 when no item with the given id is stored within the container
      96             :      *
      97             :      * @param[in] id The id of the item to retrieve
      98             :      * @return The item stored under the given id, or 0 if no such item exists
      99             :      */
     100             :     T get(const std::string& id) const {
     101             :         auto it = myMap.find(id);
     102     9159392 :         if (it == myMap.end()) {
     103             :             return 0;
     104             :         } else {
     105     8723610 :             return it->second;
     106             :         }
     107             :     }
     108             : 
     109             :     /// @brief Removes all items from the container (deletes them, too)
     110        4275 :     void clear() {
     111       34193 :         for (auto i : myMap) {
     112       29918 :             delete i.second;
     113             :         }
     114             :         myMap.clear();
     115        4275 :     }
     116             : 
     117             :     /// @brief Returns the number of stored items within the container
     118             :     int size() const {
     119       30887 :         return (int) myMap.size();
     120             :     }
     121             : 
     122             :     /* @brief Fills the given vector with the stored objects' ids
     123             :      * @param[in] into The container to fill
     124             :      */
     125        2757 :     void insertIDs(std::vector<std::string>& into) const {
     126        9400 :         for (auto i : myMap) {
     127        6643 :             into.push_back(i.first);
     128             :         }
     129        2757 :     }
     130             : 
     131             :     /// @brief change ID of a stored object
     132             :     bool changeID(const std::string& oldId, const std::string& newId) {
     133             :         auto i = myMap.find(oldId);
     134             :         if (i == myMap.end()) {
     135             :             return false;
     136             :         } else {
     137             :             // save Item, remove it from Map, and insert it again with the new ID
     138             :             T item = i->second;
     139             :             myMap.erase(i);
     140             :             myMap.insert(std::make_pair(newId, item));
     141             :             return true;
     142             :         }
     143             :     }
     144             : 
     145             :     /// @brief Returns a reference to the begin iterator for the internal map
     146             :     typename IDMap::const_iterator begin() const {
     147             :         return myMap.begin();
     148             :     }
     149             : 
     150             :     /// @brief Returns a reference to the end iterator for the internal map
     151             :     typename IDMap::const_iterator end() const {
     152             :         return myMap.end();
     153             :     }
     154             : 
     155             : 
     156             : private:
     157             :     /// @brief The map from key to object
     158             :     IDMap myMap;
     159             : };

Generated by: LCOV version 1.14