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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
20 // Class passing values from a GUIGlObject to another object
21 /****************************************************************************/
22 #ifndef GLObjectValuePassConnector_h
23 #define GLObjectValuePassConnector_h
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <algorithm>
36 #include <vector>
37 #include <map>
38 #include <functional>
43 
44 
45 // ===========================================================================
46 // class declarations
47 // ===========================================================================
48 class GUIGlObject;
49 
50 
51 // ===========================================================================
52 // class definitions
53 // ===========================================================================
65 template<typename T>
67 public:
74  : myObject(o), mySource(source), myRetriever(retriever) { /*, myIsInvalid(false) */
76  myContainer.push_back(this);
77  }
78 
79 
82  myLock.lock();
83  typename std::vector< GLObjectValuePassConnector<T>* >::iterator i = std::find(myContainer.begin(), myContainer.end(), this);
84  if (i != myContainer.end()) {
85  myContainer.erase(i);
86  }
87  myLock.unlock();
88  delete mySource;
89  }
90 
91 
94 
97  static void updateAll() {
99  std::for_each(myContainer.begin(), myContainer.end(), std::mem_fun(&GLObjectValuePassConnector<T>::passValue));
100  }
101 
102 
105  static void clear() {
107  while (!myContainer.empty()) {
108  delete(*myContainer.begin());
109  }
110  myContainer.clear();
111  }
112 
113 
119  static void removeObject(GUIGlObject& o) {
121  for (typename std::vector< GLObjectValuePassConnector<T>* >::iterator i = myContainer.begin(); i != myContainer.end();) {
122  if ((*i)->myObject.getGlID() == o.getGlID()) {
123  i = myContainer.erase(i);
124  } else {
125  ++i;
126  }
127  }
128  }
130 
131 
132 protected:
139  virtual bool passValue() {
140  myRetriever->addValue(mySource->getValue());
141  return true;
142  }
143 
144 
145 protected:
148 
151 
154 
156  static MFXMutex myLock;
157 
159  static std::vector< GLObjectValuePassConnector<T>* > myContainer;
160 
161 
162 private:
165 
168 
169 
170 };
171 
172 
173 template<typename T>
174 std::vector< GLObjectValuePassConnector<T>* > GLObjectValuePassConnector<T>::myContainer;
175 template<typename T>
177 
178 
179 #endif
180 
181 /****************************************************************************/
182 
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:93
ValueSource< T > * mySource
The source for values.
A mutex encapsulator which locks/unlocks the given mutex on construction/destruction, respectively.
Definition: AbstractMutex.h:70
ValueRetriever< T > * myRetriever
The destination for values.
void lock()
lock mutex
Definition: MFXMutex.cpp:83
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.