SUMO - Simulation of Urban MObility

GUIGlObjectStorage.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // A storage for displayed objects via their numerical id
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright (C) 2001-2011 DLR (http://www.dlr.de/) and contributors
00011 /****************************************************************************/
00012 //
00013 //   This program is free software; you can redistribute it and/or modify
00014 //   it under the terms of the GNU General Public License as published by
00015 //   the Free Software Foundation; either version 2 of the License, or
00016 //   (at your option) any later version.
00017 //
00018 /****************************************************************************/
00019 
00020 
00021 // ===========================================================================
00022 // included modules
00023 // ===========================================================================
00024 #ifdef _MSC_VER
00025 #include <windows_config.h>
00026 #else
00027 #include <config.h>
00028 #endif
00029 
00030 #include <map>
00031 #include <iostream>
00032 #include <cassert>
00033 #include <utils/foxtools/MFXMutex.h>
00034 #include "GUIGlObject.h"
00035 #include "GUIGlObjectStorage.h"
00036 
00037 #ifdef CHECK_MEMORY_LEAKS
00038 #include <foreign/nvwa/debug_new.h>
00039 #endif // CHECK_MEMORY_LEAKS
00040 
00041 
00042 // ===========================================================================
00043 // static variables (instances in this case)
00044 // ===========================================================================
00045 GUIGlObjectStorage GUIGlObjectStorage::gIDStorage;
00046 
00047 
00048 // ===========================================================================
00049 // method definitions
00050 // ===========================================================================
00051 GUIGlObjectStorage::GUIGlObjectStorage() throw()
00052     : myAktID(0) {}
00053 
00054 
00055 GUIGlObjectStorage::~GUIGlObjectStorage() throw() {}
00056 
00057 
00058 GUIGlID
00059 GUIGlObjectStorage::registerObject(GUIGlObject* object, const std::string& fullName) throw() {
00060     myLock.lock();
00061     GUIGlID id = myAktID++;
00062     myMap[id] = object;
00063     myFullNameMap[fullName] = object;
00064     myLock.unlock();
00065     return id;
00066 }
00067 
00068 
00069 GUIGlObject*
00070 GUIGlObjectStorage::getObjectBlocking(GUIGlID id) throw() {
00071     myLock.lock();
00072     ObjectMap::iterator i=myMap.find(id);
00073     if (i==myMap.end()) {
00074         i = myBlocked.find(id);
00075         if (i!=myBlocked.end()) {
00076             GUIGlObject* o = (*i).second;
00077             myLock.unlock();
00078             return o;
00079         }
00080         myLock.unlock();
00081         return 0;
00082     }
00083     GUIGlObject* o = (*i).second;
00084     myMap.erase(id);
00085     myBlocked[id] = o;
00086     myLock.unlock();
00087     return o;
00088 }
00089 
00090 
00091 GUIGlObject*
00092 GUIGlObjectStorage::getObjectBlocking(const std::string& fullName) throw() {
00093     myLock.lock();
00094     if (myFullNameMap.count(fullName)) {
00095         GUIGlID id = myFullNameMap[fullName]->getGlID();
00096         myLock.unlock();
00097         return getObjectBlocking(id);
00098     } else {
00099         myLock.unlock();
00100         return 0;
00101     }
00102 }
00103 
00104 
00105 bool
00106 GUIGlObjectStorage::remove(GUIGlID id) throw() {
00107     myLock.lock();
00108     ObjectMap::iterator i=myMap.find(id);
00109     if (i==myMap.end()) {
00110         i = myBlocked.find(id);
00111         assert(i!=myBlocked.end());
00112         GUIGlObject* o = (*i).second;
00113         myFullNameMap.erase(o->getFullName());
00114         myBlocked.erase(id);
00115         my2Delete[id] = o;
00116         myLock.unlock();
00117         return false;
00118     } else {
00119         myFullNameMap.erase(i->second->getFullName());
00120         myMap.erase(id);
00121         myLock.unlock();
00122         return true;
00123     }
00124 }
00125 
00126 
00127 void
00128 GUIGlObjectStorage::clear() throw() {
00129     myLock.lock();
00130     myMap.clear();
00131     myAktID = 0;
00132     myLock.unlock();
00133 }
00134 
00135 
00136 void
00137 GUIGlObjectStorage::unblockObject(GUIGlID id) throw() {
00138     myLock.lock();
00139     ObjectMap::iterator i=myBlocked.find(id);
00140     if (i==myBlocked.end()) {
00141         myLock.unlock();
00142         return;
00143     }
00144     GUIGlObject* o = (*i).second;
00145     myBlocked.erase(id);
00146     myMap[id] = o;
00147     myLock.unlock();
00148 }
00149 
00150 
00151 std::set<GUIGlID>
00152 GUIGlObjectStorage::getAllIDs() const {
00153     std::set<GUIGlID> result;
00154     for (ObjectMap::const_iterator it=myMap.begin(); it!=myMap.end(); it++) {
00155         result.insert(it->first);
00156     }
00157     return result;
00158 }
00159 /****************************************************************************/
00160 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines