SUMO - Simulation of Urban MObility
GLObjectValuePassConnector.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
18 // Class passing values from a GUIGlObject to another object
19 /****************************************************************************/
20 #ifndef GLObjectValuePassConnector_h
21 #define GLObjectValuePassConnector_h
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #include <config.h>
28 
29 #include <algorithm>
30 #include <vector>
31 #include <map>
32 #include <functional>
37 
38 
39 // ===========================================================================
40 // class declarations
41 // ===========================================================================
42 class GUIGlObject;
43 
44 
45 // ===========================================================================
46 // class definitions
47 // ===========================================================================
59 template<typename T>
61 public:
68  : myObject(o), mySource(source), myRetriever(retriever) { /*, myIsInvalid(false) */
70  myContainer.push_back(this);
71  }
72 
73 
76  myLock.lock();
77  typename std::vector< GLObjectValuePassConnector<T>* >::iterator i = std::find(myContainer.begin(), myContainer.end(), this);
78  if (i != myContainer.end()) {
79  myContainer.erase(i);
80  }
81  myLock.unlock();
82  delete mySource;
83  }
84 
85 
88 
91  static void updateAll() {
93  std::for_each(myContainer.begin(), myContainer.end(), std::mem_fun(&GLObjectValuePassConnector<T>::passValue));
94  }
95 
96 
99  static void clear() {
101  while (!myContainer.empty()) {
102  delete(*myContainer.begin());
103  }
104  myContainer.clear();
105  }
106 
107 
113  static void removeObject(GUIGlObject& o) {
115  for (typename std::vector< GLObjectValuePassConnector<T>* >::iterator i = myContainer.begin(); i != myContainer.end();) {
116  if ((*i)->myObject.getGlID() == o.getGlID()) {
117  i = myContainer.erase(i);
118  } else {
119  ++i;
120  }
121  }
122  }
124 
125 
126 protected:
133  virtual bool passValue() {
134  myRetriever->addValue(mySource->getValue());
135  return true;
136  }
137 
138 
139 protected:
142 
145 
148 
150  static MFXMutex myLock;
151 
153  static std::vector< GLObjectValuePassConnector<T>* > myContainer;
154 
155 
156 private:
159 
162 
163 
164 };
165 
166 
167 template<typename T>
168 std::vector< GLObjectValuePassConnector<T>* > GLObjectValuePassConnector<T>::myContainer;
169 template<typename T>
171 
172 
173 #endif
174 
175 /****************************************************************************/
176 
virtual ~GLObjectValuePassConnector()
Destructor.
GUIGlObject & myObject
The object to get the values of (the object that must be active)
static void clear()
Deletes all instances.
GLObjectValuePassConnector(GUIGlObject &o, ValueSource< T > *source, ValueRetriever< T > *retriever)
Constructor.
GLObjectValuePassConnector< T > & operator=(const GLObjectValuePassConnector< T > &)
Invalidated assignment operator.
static std::vector< GLObjectValuePassConnector< T > *> myContainer
The container of items that shall be updated.
virtual bool passValue()
Passes the value to the retriever.
static MFXMutex myLock
The mutex used to avoid concurrent updates of the connectors container.
static void removeObject(GUIGlObject &o)
Removes all instances that pass values from the object with the given id.
void unlock()
release mutex lock
Definition: MFXMutex.cpp:87
ValueSource< T > * mySource
The source for values.
A mutex encapsulator which locks/unlocks the given mutex on construction/destruction, respectively.
Definition: AbstractMutex.h:59
ValueRetriever< T > * myRetriever
The destination for values.
void lock()
lock mutex
Definition: MFXMutex.cpp:77
GUIGlID getGlID() const
Returns the numerical id of the object.
static void updateAll()
Updates all instances (passes values)
Class passing values from a GUIGlObject to another object.