SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00010 // Class passing values from a GUIGlObject to another object 00011 /****************************************************************************/ 00012 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ 00013 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors 00014 /****************************************************************************/ 00015 // 00016 // This file is part of SUMO. 00017 // SUMO is free software: you can redistribute it and/or modify 00018 // it under the terms of the GNU General Public License as published by 00019 // the Free Software Foundation, either version 3 of the License, or 00020 // (at your option) any later version. 00021 // 00022 /****************************************************************************/ 00023 #ifndef GLObjectValuePassConnector_h 00024 #define GLObjectValuePassConnector_h 00025 00026 00027 // =========================================================================== 00028 // included modules 00029 // =========================================================================== 00030 #ifdef _MSC_VER 00031 #include <windows_config.h> 00032 #else 00033 #include <config.h> 00034 #endif 00035 00036 #include <algorithm> 00037 #include <vector> 00038 #include <map> 00039 #include <utils/common/ValueSource.h> 00040 #include <utils/common/ValueRetriever.h> 00041 #include <utils/gui/globjects/GUIGlObject.h> 00042 #include <utils/foxtools/MFXMutex.h> 00043 00044 00045 // =========================================================================== 00046 // class declarations 00047 // =========================================================================== 00048 class GUIGlObject; 00049 00050 00051 // =========================================================================== 00052 // class definitions 00053 // =========================================================================== 00065 template<typename T> 00066 class GLObjectValuePassConnector { 00067 public: 00073 GLObjectValuePassConnector(GUIGlObject& o, ValueSource<T> *source, ValueRetriever<T> *retriever) 00074 : myObject(o), mySource(source), myRetriever(retriever) { /*, myIsInvalid(false) */ 00075 myLock.lock(); 00076 myContainer.push_back(this); 00077 myLock.unlock(); 00078 } 00079 00080 00082 virtual ~GLObjectValuePassConnector() { 00083 myLock.lock(); 00084 typename std::vector< GLObjectValuePassConnector<T>* >::iterator i = std::find(myContainer.begin(), myContainer.end(), this); 00085 if (i != myContainer.end()) { 00086 myContainer.erase(i); 00087 } 00088 myLock.unlock(); 00089 delete mySource; 00090 } 00091 00092 00095 00098 static void updateAll() { 00099 myLock.lock(); 00100 std::for_each(myContainer.begin(), myContainer.end(), std::mem_fun(&GLObjectValuePassConnector<T>::passValue)); 00101 myLock.unlock(); 00102 } 00103 00104 00107 static void clear() { 00108 myLock.lock(); 00109 while (!myContainer.empty()) { 00110 delete(*myContainer.begin()); 00111 } 00112 myContainer.clear(); 00113 myLock.unlock(); 00114 } 00115 00116 00122 static void removeObject(GUIGlObject& o) { 00123 myLock.lock(); 00124 for (typename std::vector< GLObjectValuePassConnector<T>* >::iterator i = myContainer.begin(); i != myContainer.end();) { 00125 if ((*i)->myObject.getGlID() == o.getGlID()) { 00126 i = myContainer.erase(i); 00127 } else { 00128 ++i; 00129 } 00130 } 00131 myLock.unlock(); 00132 } 00134 00135 00136 protected: 00143 virtual bool passValue() { 00144 myRetriever->addValue(mySource->getValue()); 00145 return true; 00146 } 00147 00148 00149 protected: 00151 GUIGlObject& myObject; 00152 00154 ValueSource<T> *mySource; 00155 00157 ValueRetriever<T> *myRetriever; 00158 00160 static MFXMutex myLock; 00161 00163 static std::vector< GLObjectValuePassConnector<T>* > myContainer; 00164 00165 00166 }; 00167 00168 00169 template<typename T> 00170 std::vector< GLObjectValuePassConnector<T>* > GLObjectValuePassConnector<T>::myContainer; 00171 template<typename T> 00172 MFXMutex GLObjectValuePassConnector<T>::myLock; 00173 00174 00175 #endif 00176 00177 /****************************************************************************/ 00178