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     NamedObjectCont() : myHaveChanged(false) { }
00057 
00058 
00060     virtual ~NamedObjectCont() {
00061         for (typename IDMap::iterator i = myMap.begin(); i != myMap.end(); i++) {
00062             delete(*i).second;
00063         }
00064     }
00065 
00066 
00076     virtual bool add(const std::string& id, T item) {
00077         if (myMap.find(id) != myMap.end()) {
00078             return false;
00079         }
00080         myMap.insert(std::make_pair(id, item));
00081         myHaveChanged = true;
00082         return true;
00083     }
00084 
00085 
00090     virtual bool remove(const std::string& id) {
00091         if (myMap.find(id) == myMap.end()) {
00092             return false;
00093         }
00094         typename std::map<std::string, T>::iterator i = myMap.find(id);
00095         delete i->second;
00096         myMap.erase(i);
00097         myHaveChanged = true;
00098         return true;
00099     }
00100 
00101 
00109     T get(const std::string& id) const {
00110         typename std::map<std::string, T>::const_iterator i = myMap.find(id);
00111         if (i == myMap.end()) {
00112             return 0;
00113         }
00114         return (*i).second;
00115     }
00116 
00117 
00119     void clear() {
00120         for (typename IDMap::iterator i = myMap.begin(); i != myMap.end(); i++) {
00121             delete(*i).second;
00122         }
00123         myMap.clear();
00124         myVector.clear();
00125         myHaveChanged = true;
00126     }
00127 
00128 
00133     unsigned int size() const {
00134         return (unsigned int) myMap.size();
00135     }
00136 
00137 
00147     bool erase(const std::string& id) {
00148         typename IDMap::iterator i = myMap.find(id);
00149         if (i == myMap.end()) {
00150             return false;
00151         }
00152         T o = (*i).second;
00153         myMap.erase(i);
00154         // and from the vector
00155         typename ObjectVector::iterator i2 =
00156             find(myVector.begin(), myVector.end(), o);
00157         myHaveChanged = true;
00158         if (i2 != myVector.end()) {
00159             myVector.erase(i2);
00160         }
00161         delete o;
00162         return true;
00163     }
00164 
00165 
00166     /* @brief Returns the reference to a vector that contains all objects.
00167      *
00168      * This method returns the reference to a vector which is stored within
00169      *  this class and contains all known objects stored within the map.
00170      * This vector is rebuild in prior if "myHaveChanged" indicates
00171      *  a change has taken place.
00172      *
00173      * @return Reference to a saved vector of objects within the map
00174      */
00175     const std::vector<T> &buildAndGetStaticVector() const {
00176         if (myHaveChanged) {
00177             myVector.clear();
00178             typename IDMap::const_iterator i;
00179             for (i = myMap.begin(); i != myMap.end(); ++i) {
00180                 myVector.push_back((*i).second);
00181             }
00182             myHaveChanged = false;
00183         }
00184         return myVector;
00185     }
00186 
00187 
00188     /* @brief Returns a vector that contains all objects.
00189      *
00190      * This method builds and returns a vector which contains all known
00191      *  objects stored within the map.
00192      *
00193      * @return A vector of objects within the map
00194      */
00195     std::vector<T> getTempVector() const {
00196         std::vector<T> ret;
00197         typename IDMap::const_iterator i;
00198         for (i = myMap.begin(); i != myMap.end(); ++i) {
00199             ret.push_back((*i).second);
00200         }
00201         return ret;
00202     }
00203 
00204 
00205     /* @brief Fills the given vector with the stored objects' ids
00206      * @param[in] into The container to fill
00207      */
00208     void insertIDs(std::vector<std::string> &into) const {
00209         typename IDMap::const_iterator i;
00210         for (i = myMap.begin(); i != myMap.end(); ++i) {
00211             into.push_back((*i).first);
00212         }
00213     }
00214 
00215 
00216     /* @brief Returns a reference to the internal map
00217      *
00218      * @return A reference to the internal map
00219      */
00220     const std::map<std::string, T> &getMyMap() const {
00221         return myMap;
00222     }
00223 
00224 
00225 private:
00227     typedef std::map< std::string, T > IDMap;
00228 
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