SUMO - Simulation of Urban MObility

GLObjectValuePassConnector.h

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // Class passing values from a GUIGlObject to another object
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 #ifndef GLObjectValuePassConnector_h
00020 #define GLObjectValuePassConnector_h
00021 
00022 
00023 // ===========================================================================
00024 // included modules
00025 // ===========================================================================
00026 #ifdef _MSC_VER
00027 #include <windows_config.h>
00028 #else
00029 #include <config.h>
00030 #endif
00031 
00032 #include <algorithm>
00033 #include <vector>
00034 #include <map>
00035 #include <utils/common/ValueSource.h>
00036 #include <utils/common/ValueRetriever.h>
00037 #include <utils/gui/globjects/GUIGlObject.h>
00038 #include <utils/foxtools/MFXMutex.h>
00039 
00040 
00041 // ===========================================================================
00042 // class declarations
00043 // ===========================================================================
00044 class GUIGlObject;
00045 
00046 
00047 // ===========================================================================
00048 // class definitions
00049 // ===========================================================================
00061 template<typename T>
00062 class GLObjectValuePassConnector {
00063 public:
00069     GLObjectValuePassConnector(GUIGlObject& o, ValueSource<T> *source, ValueRetriever<T> *retriever) throw()
00070         : myObject(o), mySource(source), myRetriever(retriever) { /*, myIsInvalid(false) */
00071         myLock.lock();
00072         myContainer.push_back(this);
00073         myLock.unlock();
00074     }
00075 
00076 
00078     virtual ~GLObjectValuePassConnector() throw() {
00079         myLock.lock();
00080         typename std::vector< GLObjectValuePassConnector<T>* >::iterator i = std::find(myContainer.begin(), myContainer.end(), this);
00081         if (i!=myContainer.end()) {
00082             myContainer.erase(i);
00083         }
00084         myLock.unlock();
00085         delete mySource;
00086     }
00087 
00088 
00091 
00094     static void updateAll() throw() {
00095         myLock.lock();
00096         std::for_each(myContainer.begin(), myContainer.end(), std::mem_fun(&GLObjectValuePassConnector<T>::passValue));
00097         myLock.unlock();
00098     }
00099 
00100 
00103     static void clear() throw() {
00104         myLock.lock();
00105         while (!myContainer.empty()) {
00106             delete(*myContainer.begin());
00107         }
00108         myContainer.clear();
00109         myLock.unlock();
00110     }
00111 
00112 
00118     static void removeObject(GUIGlObject& o) throw() {
00119         myLock.lock();
00120         for (typename std::vector< GLObjectValuePassConnector<T>* >::iterator i=myContainer.begin(); i!=myContainer.end();) {
00121             if ((*i)->myObject.getGlID()==o.getGlID()) {
00122                 i = myContainer.erase(i);
00123             } else {
00124                 ++i;
00125             }
00126         }
00127         myLock.unlock();
00128     }
00130 
00131 
00132 protected:
00139     virtual bool passValue() throw() {
00140         myRetriever->addValue(mySource->getValue());
00141         return true;
00142     }
00143 
00144 
00145 protected:
00147     GUIGlObject& myObject;
00148 
00150     ValueSource<T> *mySource;
00151 
00153     ValueRetriever<T> *myRetriever;
00154 
00156     static MFXMutex myLock;
00157 
00159     static std::vector< GLObjectValuePassConnector<T>* > myContainer;
00160 
00161 
00162 };
00163 
00164 
00165 template<typename T>
00166 std::vector< GLObjectValuePassConnector<T>* > GLObjectValuePassConnector<T>::myContainer;
00167 template<typename T>
00168 MFXMutex GLObjectValuePassConnector<T>::myLock;
00169 
00170 
00171 #endif
00172 
00173 /****************************************************************************/
00174 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines