1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
/*=========================================================================
Program: ParaView
Module: vtkSIProxyProperty.cxx
Copyright (c) Kitware, Inc.
All rights reserved.
See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkSIProxyProperty.h"
#include "vtkClientServerStream.h"
#include "vtkObjectFactory.h"
#include "vtkPVXMLElement.h"
#include "vtkSIObject.h"
#include "vtkSIProxy.h"
#include "vtkSmartPointer.h"
#include "vtkSMMessage.h"
#include <algorithm>
#include <assert.h>
#include <iterator>
#include <set>
//****************************************************************************/
// Internal Classes and typedefs
//****************************************************************************/
class vtkSIProxyProperty::InternalCache : public std::set<vtkTypeUInt32>
{
};
class vtkSIProxyProperty::vtkObjectCache :
public std::map<vtkTypeUInt32, vtkSmartPointer<vtkObjectBase> > {};
//****************************************************************************/
vtkStandardNewMacro(vtkSIProxyProperty);
//----------------------------------------------------------------------------
vtkSIProxyProperty::vtkSIProxyProperty()
{
this->Cache = new InternalCache();
this->ObjectCache = new vtkObjectCache();
this->CleanCommand = NULL;
this->RemoveCommand = NULL;
this->ArgumentType = vtkSIProxyProperty::VTK;
this->NullOnEmpty = false;
}
//----------------------------------------------------------------------------
vtkSIProxyProperty::~vtkSIProxyProperty()
{
this->SetCleanCommand(NULL);
this->SetRemoveCommand(NULL);
delete this->Cache;
delete this->ObjectCache;
}
//----------------------------------------------------------------------------
bool vtkSIProxyProperty::ReadXMLAttributes(
vtkSIProxy* proxyhelper, vtkPVXMLElement* element)
{
if (!this->Superclass::ReadXMLAttributes(proxyhelper, element))
{
return false;
}
this->SetCleanCommand(element->GetAttribute("clean_command"));
this->SetRemoveCommand(element->GetAttribute("remove_command"));
// Allow to choose the kind of object to pass as argument based on
// its global id.
const char* arg_type = element->GetAttribute("argument_type");
if(arg_type != NULL && arg_type[0] != 0)
{
if(strcmp(arg_type, "VTK") == 0)
{
this->ArgumentType = VTK;
}
else if(strcmp(arg_type, "SMProxy") == 0)
{
this->ArgumentType = SMProxy;
}
else if(strcmp(arg_type, "SIProxy") == 0)
{
this->ArgumentType = SIProxy;
}
}
else
{
// If not set, DEFAULT value
this->ArgumentType = VTK;
}
int null_on_empty;
if (element->GetScalarAttribute("null_on_empty", &null_on_empty))
{
this->SetNullOnEmpty(null_on_empty != 0);
}
if (this->InformationOnly)
{
vtkErrorMacro("InformationOnly proxy properties are not supported!");
return false;
}
return true;
}
//----------------------------------------------------------------------------
bool vtkSIProxyProperty::Push(vtkSMMessage* message, int offset)
{
assert(message->ExtensionSize(ProxyState::property) > offset);
const ProxyState_Property *prop = &message->GetExtension(ProxyState::property,
offset);
assert(strcmp(prop->name().c_str(), this->GetXMLName()) == 0);
std::set<vtkTypeUInt32> new_value;
for (int cc=0; cc < prop->value().proxy_global_id_size(); cc++)
{
new_value.insert( prop->value().proxy_global_id(cc) );
}
std::set<vtkTypeUInt32> to_add = new_value;
vtkClientServerStream stream;
vtkObjectBase* object = this->GetVTKObject();
// Deal with previous values to remove
if (this->CleanCommand)
{
stream << vtkClientServerStream::Invoke
<< object
<< this->CleanCommand
<< vtkClientServerStream::End;
this->ObjectCache->clear();
}
else if(this->RemoveCommand)
{
std::set<vtkTypeUInt32> to_remove;
std::set_difference(this->Cache->begin(), this->Cache->end(),
new_value.begin(), new_value.end(),
std::inserter(to_remove, to_remove.begin()));
for (std::set<vtkTypeUInt32>::iterator iter = to_remove.begin();
iter != to_remove.end(); ++iter)
{
vtkObjectBase* arg = this->GetObjectBase(*iter);
if (arg == NULL)
{
arg = (*this->ObjectCache)[*iter].GetPointer();
}
if(arg != NULL)
{
stream << vtkClientServerStream::Invoke
<< object
<< this->GetRemoveCommand()
<< arg
<< vtkClientServerStream::End;
this->ObjectCache->erase(*iter);
}
else
{
vtkWarningMacro("Failed to locate vtkObjectBase for id : " << *iter);
}
}
to_add.clear();
std::set_difference(new_value.begin(), new_value.end(),
this->Cache->begin(), this->Cache->end(),
std::inserter(to_add, to_add.begin()));
}
// Deal with proxy to add
for (std::set<vtkTypeUInt32>::iterator iter = to_add.begin();
iter != to_add.end(); ++iter)
{
vtkObjectBase* arg = this->GetObjectBase(*iter);
if(arg != NULL || this->IsValidNull(*iter))
{
stream << vtkClientServerStream::Invoke
<< object
<< this->GetCommand()
<< arg
<< vtkClientServerStream::End;
// we keep an object cache so that even if the object gets unregistered
// before the property is pushed, we have a reference to it.
(*this->ObjectCache)[*iter] = arg;
}
else
{
vtkWarningMacro("Try to ADD a Proxy to a ProxyProperty but the proxy was not found");
}
}
// Take care of the Empty case
if (this->NullOnEmpty && this->CleanCommand == NULL && new_value.size() == 0)
{
stream << vtkClientServerStream::Invoke
<< object
<< this->GetCommand()
<< vtkClientServerID(0)
<< vtkClientServerStream::End;
this->ObjectCache->clear();
}
this->Cache->clear();
std::copy(new_value.begin(), new_value.end(),
std::inserter(*this->Cache, this->Cache->begin()));
// Save to cache when pulled for collaboration
this->SaveValueToCache(message, offset);
return this->ProcessMessage(stream);
}
//----------------------------------------------------------------------------
void vtkSIProxyProperty::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
//----------------------------------------------------------------------------
vtkObjectBase* vtkSIProxyProperty::GetObjectBase(vtkTypeUInt32 globalId)
{
vtkSIProxy* siProxy = NULL;
switch(this->ArgumentType)
{
case VTK:
siProxy = vtkSIProxy::SafeDownCast(this->GetSIObject(globalId));
return (siProxy == NULL) ? NULL : siProxy->GetVTKObject();
case SMProxy:
return this->SIProxyObject->GetRemoteObject(globalId);
case SIProxy:
return this->SIProxyObject->GetSIObject(globalId);
}
return NULL;
}
//----------------------------------------------------------------------------
bool vtkSIProxyProperty::IsValidNull(vtkTypeUInt32 globalId)
{
if(globalId == 0)
{
return true;
}
vtkSIProxy* siProxy = vtkSIProxy::SafeDownCast(this->GetSIObject(globalId));
assert("SIProxy shouldn't be null otherwise it's a Proxy location issue in the XML" && siProxy != 0);
return siProxy->IsNullProxy();
}
|