SUMO - Simulation of Urban MObility
NamedObjectCont.h
Go to the documentation of this file.
00001 /****************************************************************************/
00008 // A map of named object pointers
00009 /****************************************************************************/
00010 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00011 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00012 /****************************************************************************/
00013 //
00014 //   This file is part of SUMO.
00015 //   SUMO is free software: you can redistribute it and/or modify
00016 //   it under the terms of the GNU General Public License as published by
00017 //   the Free Software Foundation, either version 3 of the License, or
00018 //   (at your option) any later version.
00019 //
00020 /****************************************************************************/
00021 #ifndef NamedObjectCont_h
00022 #define NamedObjectCont_h
00023 
00024 
00025 // ===========================================================================
00026 // included modules
00027 // ===========================================================================
00028 #ifdef _MSC_VER
00029 #include <windows_config.h>
00030 #else
00031 #include <config.h>
00032 #endif
00033 
00034 #include <map>
00035 #include <string>
00036 #include <vector>
00037 #include <algorithm>
00038 
00039 
00040 // ===========================================================================
00041 // class definitions
00042 // ===========================================================================
00052 template<class T>
00053 class NamedObjectCont {
00054 public:
00056     typedef std::map< std::string, T > IDMap;
00057 
00059     NamedObjectCont() : myHaveChanged(false) { }
00060 
00061 
00063     virtual ~NamedObjectCont() {
00064         for (typename IDMap::iterator i = myMap.begin(); i != myMap.end(); i++) {
00065             delete(*i).second;
00066         }
00067     }
00068 
00069 
00079     virtual bool add(const std::string& id, T item) {
00080         if (myMap.find(id) != myMap.end()) {
00081             return false;
00082         }
00083         myMap.insert(std::make_pair(id, item));
00084         myHaveChanged = true;
00085         return true;
00086     }
00087 
00088 
00093     virtual bool remove(const std::string& id) {
00094         if (myMap.find(id) == myMap.end()) {
00095             return false;
00096         }
00097         typename std::map<std::string, T>::iterator i = myMap.find(id);
00098         delete i->second;
00099         myMap.erase(i);
00100         myHaveChanged = true;
00101         return true;
00102     }
00103 
00104 
00112     T get(const std::string& id) const {
00113         typename std::map<std::string, T>::const_iterator i = myMap.find(id);
00114         if (i == myMap.end()) {
00115             return 0;
00116         }
00117         return (*i).second;
00118     }
00119 
00120 
00122     void clear() {
00123         for (typename IDMap::iterator i = myMap.begin(); i != myMap.end(); i++) {
00124             delete(*i).second;
00125         }
00126         myMap.clear();
00127         myVector.clear();
00128         myHaveChanged = true;
00129     }
00130 
00131 
00136     unsigned int size() const {
00137         return (unsigned int) myMap.size();
00138     }
00139 
00140 
00150     bool erase(const std::string& id) {
00151         typename IDMap::iterator i = myMap.find(id);
00152         if (i == myMap.end()) {
00153             return false;
00154         }
00155         T o = (*i).second;
00156         myMap.erase(i);
00157         // and from the vector
00158         typename ObjectVector::iterator i2 =
00159             find(myVector.begin(), myVector.end(), o);
00160         myHaveChanged = true;
00161         if (i2 != myVector.end()) {
00162             myVector.erase(i2);
00163         }
00164         delete o;
00165         return true;
00166     }
00167 
00168 
00169     /* @brief Returns the reference to a vector that contains all objects.
00170      *
00171      * This method returns the reference to a vector which is stored within
00172      *  this class and contains all known objects stored within the map.
00173      * This vector is rebuild in prior if "myHaveChanged" indicates
00174      *  a change has taken place.
00175      *
00176      * @return Reference to a saved vector of objects within the map
00177      */
00178     const std::vector<T> &buildAndGetStaticVector() const {
00179         if (myHaveChanged) {
00180             myVector.clear();
00181             typename IDMap::const_iterator i;
00182             for (i = myMap.begin(); i != myMap.end(); ++i) {
00183                 myVector.push_back((*i).second);
00184             }
00185             myHaveChanged = false;
00186         }
00187         return myVector;
00188     }
00189 
00190 
00191     /* @brief Returns a vector that contains all objects.
00192      *
00193      * This method builds and returns a vector which contains all known
00194      *  objects stored within the map.
00195      *
00196      * @return A vector of objects within the map
00197      */
00198     std::vector<T> getTempVector() const {
00199         std::vector<T> ret;
00200         typename IDMap::const_iterator i;
00201         for (i = myMap.begin(); i != myMap.end(); ++i) {
00202             ret.push_back((*i).second);
00203         }
00204         return ret;
00205     }
00206 
00207 
00208     /* @brief Fills the given vector with the stored objects' ids
00209      * @param[in] into The container to fill
00210      */
00211     void insertIDs(std::vector<std::string> &into) const {
00212         typename IDMap::const_iterator i;
00213         for (i = myMap.begin(); i != myMap.end(); ++i) {
00214             into.push_back((*i).first);
00215         }
00216     }
00217 
00218 
00219     /* @brief Returns a reference to the internal map
00220      *
00221      * @return A reference to the internal map
00222      */
00223     const IDMap &getMyMap() const {
00224         return myMap;
00225     }
00226 
00227 
00228 private:
00230     typedef typename IDMap::iterator myContIt;
00231 
00233     IDMap myMap;
00234 
00236     typedef std::vector<T> ObjectVector;
00237 
00239     mutable ObjectVector myVector;
00240 
00242     mutable bool myHaveChanged;
00243 
00244 };
00245 
00246 
00247 #endif
00248 
00249 /****************************************************************************/
00250 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines