Path: blob/main/src/utils/gui/div/GLObjectValuePassConnector.h
169685 views
/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2001-2025 German Aerospace Center (DLR) and others.3// This program and the accompanying materials are made available under the4// terms of the Eclipse Public License 2.0 which is available at5// https://www.eclipse.org/legal/epl-2.0/6// This Source Code may also be made available under the following Secondary7// Licenses when the conditions for such availability set forth in the Eclipse8// Public License 2.0 are satisfied: GNU General Public License, version 29// or later which is available at10// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later12/****************************************************************************/13/// @file GLObjectValuePassConnector.h14/// @author Daniel Krajzewicz15/// @author Jakob Erdmann16/// @author Sascha Krieg17/// @author Michael Behrisch18/// @date Fri, 29.04.200519///20// Class passing values from a GUIGlObject to another object21/****************************************************************************/22#pragma once23#include <config.h>2425#include <algorithm>26#include <vector>27#include <map>28#include <functional>29#include <utils/foxtools/fxheader.h>30#include <utils/common/ValueSource.h>31#include <utils/common/ValueRetriever.h>32#include <utils/gui/globjects/GUIGlObject.h>333435// ===========================================================================36// class declarations37// ===========================================================================38class GUIGlObject;394041// ===========================================================================42// class definitions43// ===========================================================================44/**45* @class GLObjectValuePassConnector46* @brief Class passing values from a GUIGlObject to another object47*48* A templated instance has some static member variables. They have to be defined49* in a cpp file. They may be found in GUINet.cpp. Two instances are used:50* - one passing double-values51* - one passing time tls phase definitions52*53* @see GUIGlObject54*/55template<typename T>56class GLObjectValuePassConnector {57public:58/** @brief Constructor59* @param[in] o The object to get the value from60* @param[in] source The method for obtaining the value61* @param[in] retriever The object to pass the value to62*/63GLObjectValuePassConnector(GUIGlObject& o, ValueSource<T>* source, ValueRetriever<T>* retriever)64: myObject(o), mySource(source), myRetriever(retriever) { /*, myIsInvalid(false) */65FXMutexLock locker(myLock);66myContainer.push_back(this);67}686970/// @brief Destructor71virtual ~GLObjectValuePassConnector() {72myLock.lock();73typename std::vector< GLObjectValuePassConnector<T>* >::iterator i = std::find(myContainer.begin(), myContainer.end(), this);74if (i != myContainer.end()) {75myContainer.erase(i);76}77myLock.unlock();78delete mySource;79}808182/// @name static methods for interactions83/// @{8485/** @brief Updates all instances (passes values)86*/87static void updateAll() {88FXMutexLock locker(myLock);89for (GLObjectValuePassConnector<T>* const connector : myContainer) {90connector->passValue();91}92}939495/** @brief Deletes all instances96*/97static void clear() {98FXMutexLock locker(myLock);99while (!myContainer.empty()) {100delete (*myContainer.begin());101}102myContainer.clear();103}104105106/** @brief Removes all instances that pass values from the object with the given id107*108* Used if for example a vehicle leaves the network109* @param[in] o The object which shall no longer be asked for values110*/111static void removeObject(GUIGlObject& o) {112FXMutexLock locker(myLock);113for (typename std::vector< GLObjectValuePassConnector<T>* >::iterator i = myContainer.begin(); i != myContainer.end();) {114if ((*i)->myObject.getGlID() == o.getGlID()) {115i = myContainer.erase(i);116} else {117++i;118}119}120}121/// @}122123124protected:125/** @brief Passes the value to the retriever126*127* Retrieves the value from the object, in the case the object is active.128* Passes the value to the retriever.129* @see GUIGlObject::active130*/131virtual bool passValue() {132myRetriever->addValue(mySource->getValue());133return true;134}135136137protected:138/// @brief The object to get the values of (the object that must be active)139GUIGlObject& myObject;140141/// @brief The source for values142ValueSource<T>* mySource;143144/// @brief The destination for values145ValueRetriever<T>* myRetriever;146147/// @brief The mutex used to avoid concurrent updates of the connectors container148static FXMutex myLock;149150/// @brief The container of items that shall be updated151static std::vector< GLObjectValuePassConnector<T>* > myContainer;152153154private:155/// @brief Invalidated copy constructor.156GLObjectValuePassConnector(const GLObjectValuePassConnector<T>&);157158/// @brief Invalidated assignment operator.159GLObjectValuePassConnector<T>& operator=(const GLObjectValuePassConnector<T>&);160161162};163164165template<typename T>166std::vector< GLObjectValuePassConnector<T>* > GLObjectValuePassConnector<T>::myContainer;167template<typename T>168FXMutex GLObjectValuePassConnector<T>::myLock;169170171