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