SUMO - Simulation of Urban MObility
GLObjectValuePassConnector.h
Go to the documentation of this file.
1 /****************************************************************************/
10 // Class passing values from a GUIGlObject to another object
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2001-2016 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 #ifndef GLObjectValuePassConnector_h
24 #define GLObjectValuePassConnector_h
25 
26 
27 // ===========================================================================
28 // included modules
29 // ===========================================================================
30 #ifdef _MSC_VER
31 #include <windows_config.h>
32 #else
33 #include <config.h>
34 #endif
35 
36 #include <algorithm>
37 #include <vector>
38 #include <map>
39 #include <functional>
44 
45 
46 // ===========================================================================
47 // class declarations
48 // ===========================================================================
49 class GUIGlObject;
50 
51 
52 // ===========================================================================
53 // class definitions
54 // ===========================================================================
66 template<typename T>
68 public:
75  : myObject(o), mySource(source), myRetriever(retriever) { /*, myIsInvalid(false) */
77  myContainer.push_back(this);
78  }
79 
80 
83  myLock.lock();
84  typename std::vector< GLObjectValuePassConnector<T>* >::iterator i = std::find(myContainer.begin(), myContainer.end(), this);
85  if (i != myContainer.end()) {
86  myContainer.erase(i);
87  }
88  myLock.unlock();
89  delete mySource;
90  }
91 
92 
95 
98  static void updateAll() {
100  std::for_each(myContainer.begin(), myContainer.end(), std::mem_fun(&GLObjectValuePassConnector<T>::passValue));
101  }
102 
103 
106  static void clear() {
108  while (!myContainer.empty()) {
109  delete(*myContainer.begin());
110  }
111  myContainer.clear();
112  }
113 
114 
120  static void removeObject(GUIGlObject& o) {
122  for (typename std::vector< GLObjectValuePassConnector<T>* >::iterator i = myContainer.begin(); i != myContainer.end();) {
123  if ((*i)->myObject.getGlID() == o.getGlID()) {
124  i = myContainer.erase(i);
125  } else {
126  ++i;
127  }
128  }
129  }
131 
132 
133 protected:
140  virtual bool passValue() {
141  myRetriever->addValue(mySource->getValue());
142  return true;
143  }
144 
145 
146 protected:
149 
152 
155 
157  static MFXMutex myLock;
158 
160  static std::vector< GLObjectValuePassConnector<T>* > myContainer;
161 
162 
163 private:
166 
169 
170 
171 };
172 
173 
174 template<typename T>
175 std::vector< GLObjectValuePassConnector<T>* > GLObjectValuePassConnector<T>::myContainer;
176 template<typename T>
178 
179 
180 #endif
181 
182 /****************************************************************************/
183 
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.
GUIGlID getGlID() const
Returns the numerical id of the object.
Definition: GUIGlObject.h:123
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:96
ValueSource< T > * mySource
The source for values.
A mutex encapsulator which locks/unlocks the given mutex on construction/destruction, respectively.
Definition: AbstractMutex.h:71
ValueRetriever< T > * myRetriever
The destination for values.
void lock()
lock mutex
Definition: MFXMutex.cpp:86
static void updateAll()
Updates all instances (passes values)
static std::vector< GLObjectValuePassConnector< T > * > myContainer
The container of items that shall be updated.
Class passing values from a GUIGlObject to another object.