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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
|
/*=========================================================================
Program: ParaView
Module: $RCSfile$
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 "vtkSIVectorPropertyTemplate.h"
#include "vtkClientServerStream.h"
#include "vtkObjectFactory.h"
#include "vtkPVXMLElement.h"
#include "vtkSIProxy.h"
#include "vtkSMMessage.h"
#include <vector>
#include <assert.h>
#include <sstream>
namespace
{
// ********* INT *************
vtkMaybeUnused("not used for non-int specializations")
std::vector<int>&
operator<< (std::vector<int>& values, const Variant& variant)
{
int num_elems = variant.integer_size();
values.resize(values.size() + num_elems);
for (int cc=0; cc < num_elems; cc++)
{
values[cc] = variant.integer(cc);
}
return values;
}
vtkMaybeUnused("not used for non-int specializations")
Variant& operator << (Variant& variant, const std::vector<int> &values)
{
variant.set_type(Variant::INT);
std::vector<int>::const_iterator iter;
for (iter = values.begin(); iter!= values.end(); ++iter)
{
variant.add_integer(*iter);
}
return variant;
}
vtkMaybeUnused("not used for non-int specializations")
bool operator << (std::vector<int>& values, const vtkClientServerStream& stream)
{
size_t cur_size = values.size();
int argType = stream.GetArgumentType(0, 0);
// If single value, all int types
if (
argType == vtkClientServerStream::int64_value ||
argType == vtkClientServerStream::uint64_value ||
argType == vtkClientServerStream::int32_value ||
argType == vtkClientServerStream::int16_value ||
argType == vtkClientServerStream::int8_value ||
argType == vtkClientServerStream::uint32_value ||
argType == vtkClientServerStream::uint16_value ||
argType == vtkClientServerStream::uint8_value ||
argType == vtkClientServerStream::bool_value)
{
int ires;
int retVal = stream.GetArgument(0, 0, &ires);
if (!retVal)
{
return false;
}
values.resize(cur_size + 1);
values[cur_size] = ires;
return true;
}
// if array, only 32 bit ints work
else if (argType == vtkClientServerStream::int32_array)
{
vtkTypeUInt32 length;
stream.GetArgumentLength(0, 0, &length);
values.resize(cur_size + length);
int retVal = stream.GetArgument(0, 0, &values[cur_size], length);
if (!retVal)
{
values.resize(cur_size);
return false;
}
return true;
}
#define copy_to_vector(type) \
do \
{ \
vtkTypeUInt32 length; \
stream.GetArgumentLength(0, 0, &length); \
std::vector<type> tmp_vec(length); \
int retVal = stream.GetArgument(0, 0, &tmp_vec[0], length); \
if (!retVal) \
{ \
return false; \
} \
values.resize(cur_size + length); \
std::copy(tmp_vec.begin(), tmp_vec.end(), &values[cur_size]); \
return true; \
} while (0)
// match the type for uint32
else if (argType == vtkClientServerStream::uint32_array)
{
copy_to_vector(vtkTypeUInt32);
}
// if 64 bit array, squash into 32 bit
else if (argType == vtkClientServerStream::int64_array)
{
copy_to_vector(vtkTypeInt64);
}
else if (argType == vtkClientServerStream::uint64_array)
{
copy_to_vector(vtkTypeUInt64);
}
#undef copy_to_vector
return false;
}
// ********* DOUBLE *************
vtkMaybeUnused("not used for non-double specializations")
std::vector<double>&
operator<< (std::vector<double>& values, const Variant& variant)
{
int num_elems = variant.float64_size();
values.resize(values.size() + num_elems);
for (int cc=0; cc < num_elems; cc++)
{
values[cc] = variant.float64(cc);
}
return values;
}
vtkMaybeUnused("not used for non-double specializations")
Variant& operator << (Variant& variant, const std::vector<double> &values)
{
variant.set_type(Variant::FLOAT64);
std::vector<double>::const_iterator iter;
for (iter = values.begin(); iter!= values.end(); ++iter)
{
variant.add_float64(*iter);
}
return variant;
}
vtkMaybeUnused("not used for non-double specializations")
bool operator << (std::vector<double>& values, const vtkClientServerStream& stream)
{
size_t cur_size = values.size();
int argType = stream.GetArgumentType(0, 0);
// If single value, both float and double works
if (argType == vtkClientServerStream::float64_value ||
argType == vtkClientServerStream::float32_value)
{
double ires;
int retVal = stream.GetArgument(0, 0, &ires);
if (!retVal)
{
return false;
}
values.resize(cur_size + 1);
values[cur_size] = ires;
}
// If array, handle 32 bit and 64 bit separately
else if (argType == vtkClientServerStream::float64_array)
{
vtkTypeUInt32 length;
stream.GetArgumentLength(0, 0, &length);
values.resize(cur_size + length);
int retVal = stream.GetArgument(0, 0, &values[cur_size], length);
if (!retVal)
{
return false;
}
}
else if (argType == vtkClientServerStream::float32_array)
{
vtkTypeUInt32 length;
stream.GetArgumentLength(0, 0, &length);
float *fvalues = new float[length+1];
int retVal = stream.GetArgument(0, 0, fvalues, length);
if (!retVal)
{
delete [] fvalues;
return false;
}
values.resize(cur_size + length);
delete [] fvalues;
std::copy(fvalues, fvalues + length, &values[cur_size]);
}
return false;
}
// ********* vtkIdType *************
vtkMaybeUnused("not used for non-vtkIdType specializations")
void OperatorIdType(std::vector<vtkIdType>& values, const Variant& variant)
{
int num_elems = variant.idtype_size();
values.resize(values.size() + num_elems);
for (int cc=0; cc < num_elems; cc++)
{
values[cc] = variant.idtype(cc);
}
}
vtkMaybeUnused("not used for non-vtkIdType specializations")
void OperatorIdType(Variant& variant, const std::vector<vtkIdType> &values)
{
variant.set_type(Variant::IDTYPE);
std::vector<vtkIdType>::const_iterator iter;
for (iter = values.begin(); iter!= values.end(); ++iter)
{
variant.add_idtype(*iter);
}
}
vtkMaybeUnused("not used for non-vtkIdType specializations")
bool OperatorIdType(std::vector<vtkIdType>& values, const vtkClientServerStream& stream)
{
size_t cur_size = values.size();
int argType = stream.GetArgumentType(0, 0);
// If single value, all int types
if (argType == vtkClientServerStream::int32_value ||
argType == vtkClientServerStream::int64_value)
{
vtkIdType ires;
int retVal = stream.GetArgument(0, 0, &ires);
if (!retVal)
{
return false;
}
values.resize(cur_size + 1);
values[cur_size] = ires;
return true;
}
// if array, only 32 or 64 bit ints work
else if (argType == vtkClientServerStream::int32_array ||
argType == vtkClientServerStream::int64_value)
{
vtkTypeUInt32 length;
stream.GetArgumentLength(0, 0, &length);
values.resize(cur_size + length);
int retVal = stream.GetArgument(0, 0, &values[cur_size], length);
if (!retVal)
{
values.resize(cur_size);
return false;
}
return true;
}
return false;
}
#if VTK_SIZEOF_ID_TYPE != VTK_SIZEOF_INT
vtkMaybeUnused("not used for non-vtkIdType specializations")
std::vector<vtkIdType>&
operator<< (std::vector<vtkIdType>& values, const Variant& variant)
{
OperatorIdType(values, variant);
return values;
}
vtkMaybeUnused("not used for non-vtkIdType specializations")
Variant& operator << (Variant& variant, const std::vector<vtkIdType> &values)
{
OperatorIdType(variant, values);
return variant;
}
vtkMaybeUnused("not used for non-vtkIdType specializations")
bool operator << (std::vector<vtkIdType>& values, const vtkClientServerStream& stream)
{
return OperatorIdType(values, stream);
}
#endif
// This absurdity is needed for cases where vtkIdType == int.
template <class TARG1, class TARG2, class force_idtype>
void AppendValues(TARG1& arg1, const TARG2& arg2, const force_idtype&)
{
arg1 << arg2;
}
template<class TARG1, class TARG2>
void AppendValues(TARG1& arg1, const TARG2& arg2, const bool&)
{
OperatorIdType(arg1, arg2);
}
}
//----------------------------------------------------------------------------
template <class T, class force_idtype>
vtkSIVectorPropertyTemplate<T, force_idtype>::vtkSIVectorPropertyTemplate()
{
this->ArgumentIsArray = false;
}
//----------------------------------------------------------------------------
template <class T, class force_idtype>
vtkSIVectorPropertyTemplate<T, force_idtype>::~vtkSIVectorPropertyTemplate()
{
}
//---------------------------------------------------------------------------
template <class T, class force_idtype>
bool vtkSIVectorPropertyTemplate<T, force_idtype>::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);
// Save to cache when pulled for collaboration
this->SaveValueToCache(message, offset);
const Variant *variant = &prop->value();
std::vector<T> values;
AppendValues(values, *variant, force_idtype());
return (values.size() > 0) ?
this->Push(&values[0], static_cast<int>(values.size())) :
this->Push(static_cast<T*>(NULL), static_cast<int>(values.size()));
}
//---------------------------------------------------------------------------
template <class T, class force_idtype>
bool vtkSIVectorPropertyTemplate<T, force_idtype>::Pull(vtkSMMessage* message)
{
if (!this->InformationOnly)
{
return this->Superclass::Pull(message);
}
if (!this->GetCommand())
{
// I would say that we should return false since an InformationOnly property
// as no meaning if no command is set, but for some legacy reason we just
// skip the processing if no command is provided.
return true;
}
// Invoke property's method on the root node of the server
vtkClientServerStream str;
str << vtkClientServerStream::Invoke
<< this->GetVTKObject() << this->GetCommand()
<< vtkClientServerStream::End;
this->ProcessMessage(str);
// Get the result
const vtkClientServerStream& res = this->GetLastResult();
int numMsgs = res.GetNumberOfMessages();
if (numMsgs < 1)
{
return true;
}
int numArgs = res.GetNumberOfArguments(0);
if (numArgs < 1)
{
return true;
}
std::vector<T> values;
AppendValues(values, res, force_idtype());
// now add 'values' to the message.
ProxyState_Property *prop = message->AddExtension(ProxyState::property);
prop->set_name(this->GetXMLName());
AppendValues(*prop->mutable_value(), values, force_idtype());
return true;
}
//---------------------------------------------------------------------------
template <class T, class force_idtype>
bool vtkSIVectorPropertyTemplate<T, force_idtype>::Push(T* values, int number_of_elements)
{
if (this->InformationOnly || !this->Command)
{
return true;
}
vtkClientServerStream stream;
vtkObjectBase* object = this->GetVTKObject();
if (this->CleanCommand)
{
stream << vtkClientServerStream::Invoke << object << this->CleanCommand;
if (this->InitialString)
{
stream << this->InitialString;
}
stream << vtkClientServerStream::End;
}
if (this->SetNumberCommand)
{
stream << vtkClientServerStream::Invoke
<< object
<< this->SetNumberCommand;
if (this->InitialString)
{
stream << this->InitialString;
}
stream << number_of_elements / this->NumberOfElementsPerCommand
<< vtkClientServerStream::End;
}
if (!this->Repeatable && number_of_elements > 0)
{
stream << vtkClientServerStream::Invoke << object << this->Command;
if (this->InitialString)
{
stream << this->InitialString;
}
if (this->ArgumentIsArray)
{
stream << vtkClientServerStream::InsertArray(values, number_of_elements);
}
else
{
for (int i=0; i<number_of_elements; i++)
{
stream << values[i];
}
}
stream << vtkClientServerStream::End;
}
else if (this->Repeatable)
{
int numCommands = number_of_elements / this->NumberOfElementsPerCommand;
for(int i=0; i<numCommands; i++)
{
stream << vtkClientServerStream::Invoke << object << this->Command;
if (this->InitialString)
{
stream << this->InitialString;
}
if (this->UseIndex)
{
stream << i;
}
if (this->ArgumentIsArray)
{
stream << vtkClientServerStream::InsertArray(
&(values[i*this->NumberOfElementsPerCommand]),
this->NumberOfElementsPerCommand);
}
else
{
for (int j=0; j<this->NumberOfElementsPerCommand; j++)
{
stream << values[i*this->NumberOfElementsPerCommand+j];
}
}
stream << vtkClientServerStream::End;
}
}
return this->ProcessMessage(stream);
}
//---------------------------------------------------------------------------
template <class T, class force_idtype>
bool vtkSIVectorPropertyTemplate<T, force_idtype>::ReadXMLAttributes(
vtkSIProxy* proxy, vtkPVXMLElement* element)
{
if (!this->Superclass::ReadXMLAttributes(proxy, element))
{
return false;
}
int number_of_elements = 0;
element->GetScalarAttribute("number_of_elements", &number_of_elements);
int arg_is_array;
if (element->GetScalarAttribute("argument_is_array", &arg_is_array))
{
this->ArgumentIsArray = (arg_is_array != 0);
}
if (number_of_elements > 0)
{
std::vector<T> values;
values.resize(number_of_elements);
if (element->GetAttribute("default_values") &&
strcmp("none", element->GetAttribute("default_values")) == 0 )
{
// initialized to nothing.
return true;
}
else
{
int numRead = element->GetVectorAttribute("default_values",
number_of_elements, &values[0]);
if (numRead > 0)
{
if (numRead != number_of_elements)
{
vtkErrorMacro("The number of default values does not match the "
"number of elements. Initialization failed.");
return false;
}
}
else
{
vtkErrorMacro("No default value is specified for property: "
<< this->GetXMLName()
<< ". This might lead to stability problems");
return false;
}
}
// don't push the values if the property in "internal".
return this->GetIsInternal() ? true :
this->Push(&values[0], number_of_elements);
}
return true;
}
//----------------------------------------------------------------------------
template <class T, class force_idtype>
void vtkSIVectorPropertyTemplate<T, force_idtype>::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
|