vtkSMSessionProxyManagerInternals.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ParaView
4  Module: vtkSMProxyManagerInternals.h
5 
6  Copyright (c) Kitware, Inc.
7  All rights reserved.
8  See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
15 
16 #ifndef vtkSMProxyManagerInternals_h
17 #define vtkSMProxyManagerInternals_h
18 
19 #include "vtkCollection.h"
20 #include "vtkCommand.h"
21 #include "vtkDebugLeaks.h"
22 #include "vtkNew.h"
24 #include "vtkSMLink.h"
25 #include "vtkSMMessage.h"
26 #include "vtkSMOutputPort.h"
27 #include "vtkSMProxyLocator.h"
28 #include "vtkSMProxyManager.h"
30 #include "vtkSMSession.h"
31 #include "vtkSMSessionClient.h"
33 #include "vtkSMSourceProxy.h"
34 #include "vtkSmartPointer.h"
35 #include "vtkStdString.h"
36 
37 #include <map>
38 #include <set>
39 #include <sstream>
40 #include <vector>
41 #include <vtksys/RegularExpression.hxx>
42 
43 // Sub-classed to avoid symbol length explosion.
45 {
46 public:
48 
50  unsigned long ModifiedObserverTag;
51  unsigned long StateChangedObserverTag;
52  unsigned long UpdateObserverTag;
54 
56  {
57  // This is required everytime we're implementing our own New() to avoid
58  // "Deleting unknown object" warning from vtkDebugLeaks.
60  ret->InitializeObjectBase();
61  return ret;
62  }
63 
64 private:
66  {
67  this->ModifiedObserverTag = 0;
68  this->StateChangedObserverTag = 0;
69  this->UpdateObserverTag = 0;
70  this->UpdateInformationObserverTag = 0;
71  }
72  ~vtkSMProxyManagerProxyInfo() override
73  {
74  // Remove observers.
75  if (this->ModifiedObserverTag && this->Proxy.GetPointer())
76  {
77  this->Proxy.GetPointer()->RemoveObserver(this->ModifiedObserverTag);
78  this->ModifiedObserverTag = 0;
79  }
80  if (this->StateChangedObserverTag && this->Proxy.GetPointer())
81  {
82  this->Proxy.GetPointer()->RemoveObserver(this->StateChangedObserverTag);
83  this->StateChangedObserverTag = 0;
84  }
85  if (this->UpdateObserverTag && this->Proxy.GetPointer())
86  {
87  this->Proxy.GetPointer()->RemoveObserver(this->UpdateObserverTag);
88  this->UpdateObserverTag = 0;
89  }
90  if (this->UpdateInformationObserverTag && this->Proxy.GetPointer())
91  {
92  this->Proxy.GetPointer()->RemoveObserver(this->UpdateInformationObserverTag);
93  this->UpdateInformationObserverTag = 0;
94  }
95  }
96 };
97 
98 //-----------------------------------------------------------------------------
100  : public std::vector<vtkSmartPointer<vtkSMProxyManagerProxyInfo> >
101 {
102 public:
103  // Returns if the proxy exists in this vector.
104  bool Contains(vtkSMProxy* proxy)
105  {
106  vtkSMProxyManagerProxyListType::iterator iter = this->begin();
107  for (; iter != this->end(); ++iter)
108  {
109  if (iter->GetPointer()->Proxy == proxy)
110  {
111  return true;
112  }
113  }
114  return false;
115  }
116  vtkSMProxyManagerProxyListType::iterator Find(vtkSMProxy* proxy)
117  {
118  vtkSMProxyManagerProxyListType::iterator iter = this->begin();
119  for (; iter != this->end(); ++iter)
120  {
121  if (iter->GetPointer()->Proxy.GetPointer() == proxy)
122  {
123  return iter;
124  }
125  }
126  return this->end();
127  }
128 };
129 
130 //-----------------------------------------------------------------------------
131 class vtkSMProxyManagerProxyMapType : public std::map<vtkStdString, vtkSMProxyManagerProxyListType>
132 {
133 };
134 //-----------------------------------------------------------------------------
136 {
137  std::string Group;
138  std::string Name;
140 
141  vtkSMProxyManagerEntry(const char* group, const char* name, vtkSMProxy* proxy)
142  {
143  this->Group = group;
144  this->Name = name;
145  this->Proxy = proxy;
146  }
147 
148  bool operator==(const vtkSMProxyManagerEntry& other) const
149  {
150  return this->Group == other.Group && this->Name == other.Name &&
151  this->Proxy->GetGlobalID() == other.Proxy->GetGlobalID();
152  }
153 
154  bool operator<(const vtkSMProxyManagerEntry& other) const
155  {
156  if (this->Proxy->GetGlobalID() < other.Proxy->GetGlobalID())
157  {
158  return true;
159  }
160  else if (this->Proxy->GetGlobalID() == other.Proxy->GetGlobalID() && this->Group == other.Group)
161  {
162  return this->Name < other.Name;
163  }
164  else if (this->Proxy->GetGlobalID() == other.Proxy->GetGlobalID())
165  {
166  return this->Group < other.Group;
167  }
168  return false;
169  }
170 
171  bool operator>(const vtkSMProxyManagerEntry& other) const
172  {
173  if (this->Proxy->GetGlobalID() > other.Proxy->GetGlobalID())
174  {
175  return true;
176  }
177  else if (this->Proxy->GetGlobalID() == other.Proxy->GetGlobalID() && this->Group == other.Group)
178  {
179  return this->Name > other.Name;
180  }
181  else if (this->Proxy->GetGlobalID() == other.Proxy->GetGlobalID())
182  {
183  return this->Group > other.Group;
184  }
185  return false;
186  }
187 };
188 //-----------------------------------------------------------------------------
190 {
191  // This data structure stores actual proxy instances grouped in
192  // collections.
193  typedef std::map<vtkStdString, vtkSMProxyManagerProxyMapType> ProxyGroupType;
195 
196  // This data structure stores the tuples(group, name, proxy) to compute
197  // diff when a state is loaded.
198  typedef std::set<vtkSMProxyManagerEntry> GroupNameProxySet;
200 
201  // This data structure stores a set of proxies that have been modified.
202  typedef std::set<vtkSMProxy*> SetOfProxies;
204 
205  // Data structure to save registered links.
206  typedef std::map<vtkStdString, vtkSmartPointer<vtkSMLink> > LinkType;
208 
209  // Data structure for selection models.
210  typedef std::map<std::string, vtkSmartPointer<vtkSMProxySelectionModel> > SelectionModelsType;
212 
213  // Data structure for storing the fullState
215 
216  // Keep ref to the proxyManager to access the session
218 
219  // Helper methods -----------------------------------------------------------
220  void FindProxyTuples(vtkSMProxy* proxy, std::set<vtkSMProxyManagerEntry>& tuplesFounds)
221  {
222  std::set<vtkSMProxyManagerEntry>::iterator iter;
223  iter = this->RegisteredProxyTuple.begin();
224  while (iter != this->RegisteredProxyTuple.end())
225  {
226  if (iter->Proxy == proxy)
227  {
228  tuplesFounds.insert(*iter);
229  }
230  iter++;
231  }
232  }
233 
234  // --------------------------------------------------------------------------
235  void ComputeDelta(const vtkSMMessage* newState, vtkSMProxyLocator* locator,
236  std::set<vtkSMProxyManagerEntry>& toRegister, std::set<vtkSMProxyManagerEntry>& toUnregister)
237  {
238  // Fill equivalent temporary data structure
239  std::set<vtkSMProxyManagerEntry> newStateContent;
240  int max = newState->ExtensionSize(PXMRegistrationState::registered_proxy);
241  for (int cc = 0; cc < max; cc++)
242  {
243  PXMRegistrationState_Entry reg =
244  newState->GetExtension(PXMRegistrationState::registered_proxy, cc);
245  vtkSMProxy* proxy = locator->LocateProxy(reg.global_id());
246  if (proxy)
247  {
248  newStateContent.insert(
249  vtkSMProxyManagerEntry(reg.group().c_str(), reg.name().c_str(), proxy));
250  }
251  }
252 
253  // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
254  // FIXME there might be a better way to do that
255  // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
256 
257  // Look for proxy to Register
258  std::set<vtkSMProxyManagerEntry>::iterator iter;
259  iter = newStateContent.begin();
260  while (iter != newStateContent.end())
261  {
262  if (this->RegisteredProxyTuple.find(*iter) == this->RegisteredProxyTuple.end())
263  {
264  toRegister.insert(*iter);
265  }
266  iter++;
267  }
268 
269  // Look for proxy to Unregister
270  iter = this->RegisteredProxyTuple.begin();
271  vtksys::RegularExpression prototypesRe("_prototypes$");
272  while (iter != this->RegisteredProxyTuple.end())
273  {
274  if (newStateContent.find(*iter) == newStateContent.end() &&
275  !prototypesRe.find(iter->Group.c_str()))
276  {
277  toUnregister.insert(*iter);
278  }
279  iter++;
280  }
281  }
282 
283  // --------------------------------------------------------------------------
284  void RemoveTuples(const char* name, std::set<vtkSMProxyManagerEntry>& removedEntries)
285  {
286  // Convert param
287  std::string nameString = name;
288 
289  // Deal with set
290  GroupNameProxySet resultSet;
291  std::set<vtkSMProxyManagerEntry>::iterator iter;
292  iter = this->RegisteredProxyTuple.begin();
293  while (iter != this->RegisteredProxyTuple.end())
294  {
295  if (iter->Name != nameString)
296  {
297  resultSet.insert(*iter);
298  }
299  iter++;
300  }
301  this->RegisteredProxyTuple = resultSet;
302 
303  // Deal with map only (true)
304  vtkSMSessionProxyManagerInternals::ProxyGroupType::iterator it =
305  this->RegisteredProxyMap.begin();
306  for (; it != this->RegisteredProxyMap.end(); it++)
307  {
308  vtkSMProxyManagerProxyMapType::iterator it2 = it->second.find(name);
309  if (it2 != it->second.end())
310  {
311  this->RemoveTuples(it->first.c_str(), name, removedEntries, true);
312  }
313  }
314 
315  // Deal with State
316  vtkSMMessage backup;
317  backup.CopyFrom(this->State);
318  int nbRegisteredProxy = this->State.ExtensionSize(PXMRegistrationState::registered_proxy);
319  this->State.ClearExtension(PXMRegistrationState::registered_proxy);
320  for (int cc = 0; cc < nbRegisteredProxy; ++cc)
321  {
322  const PXMRegistrationState_Entry* reg =
323  &backup.GetExtension(PXMRegistrationState::registered_proxy, cc);
324 
325  if (reg->name() != nameString) // Keep it
326  {
327  this->State.AddExtension(PXMRegistrationState::registered_proxy)->CopyFrom(*reg);
328  }
329  }
330  }
331 
332  // --------------------------------------------------------------------------
333  void RemoveTuples(const char* group, const char* name,
334  std::set<vtkSMProxyManagerEntry>& removedEntries, bool doMapOnly)
335  {
336  // Convert parameters
337  std::string groupString = group;
338  std::string nameString = name;
339 
340  // Deal with set
341  if (!doMapOnly)
342  {
343  GroupNameProxySet resultSet;
344  std::set<vtkSMProxyManagerEntry>::iterator iter;
345  iter = this->RegisteredProxyTuple.begin();
346  while (iter != this->RegisteredProxyTuple.end())
347  {
348  if (iter->Group != groupString || iter->Name != nameString)
349  {
350  resultSet.insert(*iter);
351  }
352  iter++;
353  }
354  this->RegisteredProxyTuple = resultSet;
355  }
356 
357  // Deal with map
358  vtkSMSessionProxyManagerInternals::ProxyGroupType::iterator it =
359  this->RegisteredProxyMap.find(group);
360  if (it != this->RegisteredProxyMap.end())
361  {
362  vtkSMProxyManagerProxyMapType::iterator it2 = it->second.find(name);
363  if (it2 != it->second.end())
364  {
365  vtkSMProxyManagerProxyListType::iterator it3 = it2->second.begin();
366  while (it3 != it2->second.end())
367  {
368  removedEntries.insert(vtkSMProxyManagerEntry(group, name, (*it3)->Proxy));
369  it3++;
370  }
371  it->second.erase(it2);
372  }
373  }
374 
375  // Deal with state
376  vtksys::RegularExpression prototypesRe("_prototypes$");
377  if (!doMapOnly && !prototypesRe.find(group))
378  {
379  vtkSMMessage backup;
380  backup.CopyFrom(this->State);
381  int nbRegisteredProxy = this->State.ExtensionSize(PXMRegistrationState::registered_proxy);
382  this->State.ClearExtension(PXMRegistrationState::registered_proxy);
383  for (int cc = 0; cc < nbRegisteredProxy; ++cc)
384  {
385  const PXMRegistrationState_Entry* reg =
386  &backup.GetExtension(PXMRegistrationState::registered_proxy, cc);
387 
388  if (reg->group() != groupString || reg->name() != nameString) // Keep it
389  {
390  this->State.AddExtension(PXMRegistrationState::registered_proxy)->CopyFrom(*reg);
391  }
392  }
393  }
394  }
395 
396  // --------------------------------------------------------------------------
397  // Return true if the given tuple has been found
398  bool RemoveTuples(const char* group, const char* name, vtkSMProxy* proxy)
399  {
400  // Convert parameters
401  std::string groupString = group;
402  std::string nameString = name;
403 
404  // Will be overridden if the proxy is found
405  bool found = false;
406 
407  // Deal with set
408  this->RegisteredProxyTuple.erase(vtkSMProxyManagerEntry(group, name, proxy));
409 
410  // Deal with map
411  vtkSMSessionProxyManagerInternals::ProxyGroupType::iterator it =
412  this->RegisteredProxyMap.find(group);
413  if (it != this->RegisteredProxyMap.end())
414  {
415  vtkSMProxyManagerProxyMapType::iterator it2 = it->second.find(name);
416  if (it2 != it->second.end())
417  {
418  vtkSMProxyManagerProxyListType::iterator it3 = it2->second.Find(proxy);
419  if (it3 != it2->second.end())
420  {
421  found = true;
422  it2->second.erase(it3);
423  }
424  if (it2->second.size() == 0)
425  {
426  it->second.erase(it2);
427  }
428  }
429  }
430 
431  // Deal with state
432  vtksys::RegularExpression prototypesRe("_prototypes$");
433  if (!prototypesRe.find(group))
434  {
435  vtkSMMessage backup;
436  backup.CopyFrom(this->State);
437 
438  int nbRegisteredProxy = this->State.ExtensionSize(PXMRegistrationState::registered_proxy);
439  this->State.ClearExtension(PXMRegistrationState::registered_proxy);
440 
441  for (int cc = 0; cc < nbRegisteredProxy; ++cc)
442  {
443  const PXMRegistrationState_Entry* reg =
444  &backup.GetExtension(PXMRegistrationState::registered_proxy, cc);
445 
446  if (reg->group() == groupString && reg->name() == nameString &&
447  reg->global_id() == proxy->GetGlobalID())
448  {
449  // Do not keep it
450  }
451  else
452  {
453  // Keep it
454  this->State.AddExtension(PXMRegistrationState::registered_proxy)->CopyFrom(*reg);
455  }
456  }
457  }
458 
459  return found;
460  }
461 
462  // --------------------------------------------------------------------------
464  {
465  this->State.ClearExtension(PXMRegistrationState::registered_selection_model);
466  std::map<std::string, vtkSmartPointer<vtkSMProxySelectionModel> >::iterator iter;
467  for (iter = this->SelectionModels.begin(); iter != this->SelectionModels.end(); iter++)
468  {
469  PXMRegistrationState_Entry* selModel =
470  this->State.AddExtension(PXMRegistrationState::registered_selection_model);
471  selModel->set_name(iter->first);
472  selModel->set_global_id(iter->second->GetGlobalID());
473  }
474  }
475  // --------------------------------------------------------------------------
477  {
478  this->State.ClearExtension(PXMRegistrationState::registered_link);
479 
480  LinkType::iterator iter;
481  for (iter = this->RegisteredLinkMap.begin(); iter != this->RegisteredLinkMap.end(); iter++)
482  {
483  PXMRegistrationState_Entry* linkEntry =
484  this->State.AddExtension(PXMRegistrationState::registered_link);
485  linkEntry->set_name(iter->first);
486  linkEntry->set_global_id(iter->second->GetGlobalID());
487  }
488  }
489  // --------------------------------------------------------------------------
490 };
491 
492 #endif
493 
494 // VTK-HeaderTest-Exclude: vtkSMSessionProxyManagerInternals.h
bool operator>(const vtkSMProxyManagerEntry &other) const
std::map< vtkStdString, vtkSMProxyManagerProxyMapType > ProxyGroupType
void ComputeDelta(const vtkSMMessage *newState, vtkSMProxyLocator *locator, std::set< vtkSMProxyManagerEntry > &toRegister, std::set< vtkSMProxyManagerEntry > &toUnregister)
static vtkSMProxyManagerProxyInfo * New()
vtkSMProxyManagerEntry(const char *group, const char *name, vtkSMProxy *proxy)
void RemoveTuples(const char *name, std::set< vtkSMProxyManagerEntry > &removedEntries)
void RemoveObserver(unsigned long tag)
virtual vtkSMProxy * LocateProxy(vtkTypeUInt32 globalID)
Locate a proxy with the given "name".
name
Header class that setup every thing in order to use Protobuf messages in a transparent manner.
void FindProxyTuples(vtkSMProxy *proxy, std::set< vtkSMProxyManagerEntry > &tuplesFounds)
std::map< std::string, vtkSmartPointer< vtkSMProxySelectionModel > > SelectionModelsType
bool operator<(const vtkSMProxyManagerEntry &other) const
std::set< vtkSMProxyManagerEntry > GroupNameProxySet
bool RemoveTuples(const char *group, const char *name, vtkSMProxy *proxy)
The vtkSMSessionProxyManager is esponsible for creating and managing proxies for a given session.
void RemoveTuples(const char *group, const char *name, std::set< vtkSMProxyManagerEntry > &removedEntries, bool doMapOnly)
proxy for a VTK object(s) on a server
Definition: vtkSMProxy.h:152
is used to locate proxies referred to in state xmls while loading state files.
vtkSMProxyManagerProxyListType::iterator Find(vtkSMProxy *proxy)
T * GetPointer() const
bool operator==(const vtkSMProxyManagerEntry &other) const
vtkBaseTypeMacro(vtkSMProxyManagerProxyInfo, vtkObjectBase)
virtual vtkTypeUInt32 GetGlobalID()
Get the global unique id for this object.
vtkSmartPointer< vtkSMProxy > Proxy
#define max(a, b)
std::map< vtkStdString, vtkSmartPointer< vtkSMLink > > LinkType