Line data Source code
1 : /****************************************************************************/ 2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo 3 : // Copyright (C) 2001-2024 German Aerospace Center (DLR) and others. 4 : // This program and the accompanying materials are made available under the 5 : // terms of the Eclipse Public License 2.0 which is available at 6 : // https://www.eclipse.org/legal/epl-2.0/ 7 : // This Source Code may also be made available under the following Secondary 8 : // Licenses when the conditions for such availability set forth in the Eclipse 9 : // Public License 2.0 are satisfied: GNU General Public License, version 2 10 : // or later which is available at 11 : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html 12 : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later 13 : /****************************************************************************/ 14 : /// @file GLObjectValuePassConnector.h 15 : /// @author Daniel Krajzewicz 16 : /// @author Jakob Erdmann 17 : /// @author Sascha Krieg 18 : /// @author Michael Behrisch 19 : /// @date Fri, 29.04.2005 20 : /// 21 : // Class passing values from a GUIGlObject to another object 22 : /****************************************************************************/ 23 : #pragma once 24 : #include <config.h> 25 : 26 : #include <algorithm> 27 : #include <vector> 28 : #include <map> 29 : #include <functional> 30 : #include <utils/foxtools/fxheader.h> 31 : #include <utils/common/ValueSource.h> 32 : #include <utils/common/ValueRetriever.h> 33 : #include <utils/gui/globjects/GUIGlObject.h> 34 : 35 : 36 : // =========================================================================== 37 : // class declarations 38 : // =========================================================================== 39 : class GUIGlObject; 40 : 41 : 42 : // =========================================================================== 43 : // class definitions 44 : // =========================================================================== 45 : /** 46 : * @class GLObjectValuePassConnector 47 : * @brief Class passing values from a GUIGlObject to another object 48 : * 49 : * A templated instance has some static member variables. They have to be defined 50 : * in a cpp file. They may be found in GUINet.cpp. Two instances are used: 51 : * - one passing double-values 52 : * - one passing time tls phase definitions 53 : * 54 : * @see GUIGlObject 55 : */ 56 : template<typename T> 57 : class GLObjectValuePassConnector { 58 : public: 59 : /** @brief Constructor 60 : * @param[in] o The object to get the value from 61 : * @param[in] source The method for obtaining the value 62 : * @param[in] retriever The object to pass the value to 63 : */ 64 0 : GLObjectValuePassConnector(GUIGlObject& o, ValueSource<T>* source, ValueRetriever<T>* retriever) 65 0 : : myObject(o), mySource(source), myRetriever(retriever) { /*, myIsInvalid(false) */ 66 : FXMutexLock locker(myLock); 67 0 : myContainer.push_back(this); 68 0 : } 69 : 70 : 71 : /// @brief Destructor 72 0 : virtual ~GLObjectValuePassConnector() { 73 0 : myLock.lock(); 74 0 : typename std::vector< GLObjectValuePassConnector<T>* >::iterator i = std::find(myContainer.begin(), myContainer.end(), this); 75 0 : if (i != myContainer.end()) { 76 : myContainer.erase(i); 77 : } 78 0 : myLock.unlock(); 79 0 : delete mySource; 80 0 : } 81 : 82 : 83 : /// @name static methods for interactions 84 : /// @{ 85 : 86 : /** @brief Updates all instances (passes values) 87 : */ 88 10664874 : static void updateAll() { 89 : FXMutexLock locker(myLock); 90 10664874 : for (GLObjectValuePassConnector<T>* const connector : myContainer) { 91 0 : connector->passValue(); 92 : } 93 10664874 : } 94 : 95 : 96 : /** @brief Deletes all instances 97 : */ 98 : static void clear() { 99 : FXMutexLock locker(myLock); 100 : while (!myContainer.empty()) { 101 : delete (*myContainer.begin()); 102 : } 103 : myContainer.clear(); 104 : } 105 : 106 : 107 : /** @brief Removes all instances that pass values from the object with the given id 108 : * 109 : * Used if for example a vehicle leaves the network 110 : * @param[in] o The object which shall no longer be asked for values 111 : */ 112 1446023 : static void removeObject(GUIGlObject& o) { 113 : FXMutexLock locker(myLock); 114 1446023 : for (typename std::vector< GLObjectValuePassConnector<T>* >::iterator i = myContainer.begin(); i != myContainer.end();) { 115 0 : if ((*i)->myObject.getGlID() == o.getGlID()) { 116 : i = myContainer.erase(i); 117 : } else { 118 : ++i; 119 : } 120 : } 121 1446023 : } 122 : /// @} 123 : 124 : 125 : protected: 126 : /** @brief Passes the value to the retriever 127 : * 128 : * Retrieves the value from the object, in the case the object is active. 129 : * Passes the value to the retriever. 130 : * @see GUIGlObject::active 131 : */ 132 0 : virtual bool passValue() { 133 0 : myRetriever->addValue(mySource->getValue()); 134 0 : return true; 135 : } 136 : 137 : 138 : protected: 139 : /// @brief The object to get the values of (the object that must be active) 140 : GUIGlObject& myObject; 141 : 142 : /// @brief The source for values 143 : ValueSource<T>* mySource; 144 : 145 : /// @brief The destination for values 146 : ValueRetriever<T>* myRetriever; 147 : 148 : /// @brief The mutex used to avoid concurrent updates of the connectors container 149 : static FXMutex myLock; 150 : 151 : /// @brief The container of items that shall be updated 152 : static std::vector< GLObjectValuePassConnector<T>* > myContainer; 153 : 154 : 155 : private: 156 : /// @brief Invalidated copy constructor. 157 : GLObjectValuePassConnector<T>(const GLObjectValuePassConnector<T>&); 158 : 159 : /// @brief Invalidated assignment operator. 160 : GLObjectValuePassConnector<T>& operator=(const GLObjectValuePassConnector<T>&); 161 : 162 : 163 : }; 164 : 165 : 166 : template<typename T> 167 : std::vector< GLObjectValuePassConnector<T>* > GLObjectValuePassConnector<T>::myContainer; 168 : template<typename T> 169 : FXMutex GLObjectValuePassConnector<T>::myLock;