vtkClientServerStream.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ParaView
4  Module: vtkClientServerStream.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 =========================================================================*/
27 #ifndef vtkClientServerStream_h
28 #define vtkClientServerStream_h
29 
30 #include "vtkClientServerID.h" // for vtkClientServerID
31 #include "vtkVariant.h" // for vtkVariant
32 
33 class vtkClientServerStreamInternals;
34 
36 {
37 public:
39 
43  vtkClientServerStream(vtkObjectBase* owner = nullptr);
46 
48 
54 
60  enum Commands
61  {
62  New,
68  EndOfCommands
69  };
70 
76  enum Types
77  {
104  End
105  };
106 
111  void Reserve(size_t size);
112 
116  void Reset();
117 
121  void Copy(const vtkClientServerStream* source);
122 
123  //--------------------------------------------------------------------------
124  // Stream reading methods:
125 
130  int GetNumberOfMessages() const;
131 
136  vtkClientServerStream::Commands GetCommand(int message) const;
137 
142  int GetNumberOfArguments(int message) const;
143 
148  vtkClientServerStream::Types GetArgumentType(int message, int argument) const;
149 
151 
156  int GetArgument(int message, int argument, bool* value) const;
157  int GetArgument(int message, int argument, signed char* value) const;
158  int GetArgument(int message, int argument, char* value) const;
159  int GetArgument(int message, int argument, short* value) const;
160  int GetArgument(int message, int argument, int* value) const;
161  int GetArgument(int message, int argument, long* value) const;
162  int GetArgument(int message, int argument, unsigned char* value) const;
163  int GetArgument(int message, int argument, unsigned short* value) const;
164  int GetArgument(int message, int argument, unsigned int* value) const;
165  int GetArgument(int message, int argument, unsigned long* value) const;
166  int GetArgument(int message, int argument, float* value) const;
167  int GetArgument(int message, int argument, double* value) const;
168  int GetArgument(int message, int argument, long long* value) const;
169  int GetArgument(int message, int argument, unsigned long long* value) const;
170 #if defined(VTK_TYPE_USE___INT64)
171  int GetArgument(int message, int argument, __int64* value) const;
172  int GetArgument(int message, int argument, unsigned __int64* value) const;
173 #endif
174  int GetArgument(int message, int argument, signed char* value, vtkTypeUInt32 length) const;
175  int GetArgument(int message, int argument, char* value, vtkTypeUInt32 length) const;
176  int GetArgument(int message, int argument, short* value, vtkTypeUInt32 length) const;
177  int GetArgument(int message, int argument, int* value, vtkTypeUInt32 length) const;
178  int GetArgument(int message, int argument, long* value, vtkTypeUInt32 length) const;
179  int GetArgument(int message, int argument, unsigned char* value, vtkTypeUInt32 length) const;
180  int GetArgument(int message, int argument, unsigned short* value, vtkTypeUInt32 length) const;
181  int GetArgument(int message, int argument, unsigned int* value, vtkTypeUInt32 length) const;
182  int GetArgument(int message, int argument, unsigned long* value, vtkTypeUInt32 length) const;
183  int GetArgument(int message, int argument, float* value, vtkTypeUInt32 length) const;
184  int GetArgument(int message, int argument, double* value, vtkTypeUInt32 length) const;
185  int GetArgument(int message, int argument, long long* value, vtkTypeUInt32 length) const;
186  int GetArgument(int message, int argument, unsigned long long* value, vtkTypeUInt32 length) const;
187 #if defined(VTK_TYPE_USE___INT64)
188  int GetArgument(int message, int argument, __int64* value, vtkTypeUInt32 length) const;
189  int GetArgument(int message, int argument, unsigned __int64* value, vtkTypeUInt32 length) const;
190 #endif
191  int GetArgument(int message, int argument, const char** value) const;
192  int GetArgument(int message, int argument, char** value) const;
193  int GetArgument(int message, int argument, vtkStdString* value) const;
194  int GetArgument(int message, int argument, std::string* value) const;
195  int GetArgument(int message, int argument, vtkClientServerStream* value) const;
196  int GetArgument(int message, int argument, vtkClientServerID* value) const;
197  int GetArgument(int message, int argument, vtkObjectBase** value) const;
199 
209  int GetArgument(int message, int& argument, vtkVariant* value) const;
210 
215  int GetArgumentLength(int message, int argument, vtkTypeUInt32* length) const;
216 
222  int GetArgumentObject(int message, int argument, vtkObjectBase** value, const char* type) const;
223 
225 
229  struct Argument
230  {
231  const unsigned char* Data;
232  size_t Size;
233  };
235 
241  vtkClientServerStream::Argument GetArgument(int message, int argument) const;
242 
249  int GetData(const unsigned char** data, size_t* length) const;
250 
251  //--------------------------------------------------------------------------
252  // Stream writing methods:
253 
255 
259  struct Array
260  {
262  vtkTypeUInt32 Length;
263  vtkTypeUInt32 Size;
264  const void* Data;
265  };
267 
269 
282 
284 
287  vtkClientServerStream& operator<<(bool value);
288  vtkClientServerStream& operator<<(char value);
289  vtkClientServerStream& operator<<(short value);
290  vtkClientServerStream& operator<<(int value);
291  vtkClientServerStream& operator<<(long value);
292  vtkClientServerStream& operator<<(signed char value);
293  vtkClientServerStream& operator<<(unsigned char value);
294  vtkClientServerStream& operator<<(unsigned short value);
295  vtkClientServerStream& operator<<(unsigned int value);
296  vtkClientServerStream& operator<<(unsigned long value);
297  vtkClientServerStream& operator<<(long long value);
298  vtkClientServerStream& operator<<(unsigned long long value);
299 #if defined(VTK_TYPE_USE___INT64)
300  vtkClientServerStream& operator<<(__int64 value);
301  vtkClientServerStream& operator<<(unsigned __int64 value);
302 #endif
303  vtkClientServerStream& operator<<(float value);
304  vtkClientServerStream& operator<<(double value);
305  vtkClientServerStream& operator<<(const char* value);
307 
309 
312  static vtkClientServerStream::Array InsertArray(const char*, int);
313  static vtkClientServerStream::Array InsertArray(const short*, int);
314  static vtkClientServerStream::Array InsertArray(const int*, int);
315  static vtkClientServerStream::Array InsertArray(const long*, int);
316  static vtkClientServerStream::Array InsertArray(const signed char*, int);
317  static vtkClientServerStream::Array InsertArray(const unsigned char*, int);
318  static vtkClientServerStream::Array InsertArray(const unsigned short*, int);
319  static vtkClientServerStream::Array InsertArray(const unsigned int*, int);
320  static vtkClientServerStream::Array InsertArray(const unsigned long*, int);
321  static vtkClientServerStream::Array InsertArray(const long long*, int);
322  static vtkClientServerStream::Array InsertArray(const unsigned long long*, int);
323 #if defined(VTK_TYPE_USE___INT64)
324  static vtkClientServerStream::Array InsertArray(const __int64*, int);
325  static vtkClientServerStream::Array InsertArray(const unsigned __int64*, int);
326 #endif
327  static vtkClientServerStream::Array InsertArray(const float*, int);
328  static vtkClientServerStream::Array InsertArray(const double*, int);
330 
336  int SetData(const unsigned char* data, size_t length);
337 
338  //--------------------------------------------------------------------------
339  // Utility methods:
340 
342 
349  static const char* GetStringFromType(vtkClientServerStream::Types type);
350  static const char* GetStringFromType(vtkClientServerStream::Types type, int index);
352 
357  static vtkClientServerStream::Types GetTypeFromString(const char* name);
358 
363  static const char* GetStringFromCommand(vtkClientServerStream::Commands cmd);
364 
370  static vtkClientServerStream::Commands GetCommandFromString(const char* name);
371 
373 
376  void Print(ostream&) const;
377  void Print(ostream&, vtkIndent) const;
378  void PrintMessage(ostream&, int message) const;
379  void PrintMessage(ostream&, int message, vtkIndent) const;
380  void PrintArgument(ostream&, int message, int argument) const;
381  void PrintArgument(ostream&, int message, int argument, vtkIndent) const;
382  void PrintArgumentValue(ostream&, int message, int argument) const;
384 
386 
389  const char* StreamToString() const;
390  void StreamToString(ostream& os) const;
392 
398  int StreamFromString(const char* str);
399 
400 protected:
401  // Write arbitrary data to the stream. Used internally.
402  vtkClientServerStream& Write(const void* data, size_t length);
403 
404  // Data parsing utilities for SetData.
405  int ParseData();
406  unsigned char* ParseCommand(
407  int order, unsigned char* data, unsigned char* begin, unsigned char* end);
408  void ParseEnd();
409  unsigned char* ParseType(int order, unsigned char* data, unsigned char* begin, unsigned char* end,
411  unsigned char* ParseValue(
412  int order, unsigned char* data, unsigned char* end, unsigned int wordSize);
413  unsigned char* ParseArray(
414  int order, unsigned char* data, unsigned char* end, unsigned int wordSize);
415  unsigned char* ParseString(int order, unsigned char* data, unsigned char* end);
416  unsigned char* ParseStream(int order, unsigned char* data, unsigned char* end);
417 
418  // Enumeration of possible byte orderings of data in the stream.
419  enum
420  {
422  LittleEndian
423  };
424 
425  // Byte swap data in the given byte order to match the current
426  // machine's byte order.
427  void PerformByteSwap(
428  int dataByteOrder, unsigned char* data, unsigned int numWords, unsigned int wordSize);
429 
430  // Get a pointer to the given value within the given message.
431  // Returns 0 if either index is out of range.
432  const unsigned char* GetValue(int message, int value) const;
433 
434  // Get the number of values in the given message. The count
435  // includes the Command and End portions of the message. Returns 0
436  // if the given index is out of range.
437  int GetNumberOfValues(int message) const;
438 
439  // Internal implementation shared between PrintArgument and
440  // PrintArgumentValue.
441  void PrintArgumentInternal(ostream&, int message, int argument, int annotate, vtkIndent) const;
442 
443  // String writing routines.
444  void StreamToString(ostream& os, vtkIndent indent) const;
445  void MessageToString(ostream& os, int m) const;
446  void MessageToString(ostream& os, int m, vtkIndent indent) const;
447  void ArgumentToString(ostream& os, int m, int a) const;
448  void ArgumentToString(ostream& os, int m, int a, vtkIndent indent) const;
449  void ArgumentValueToString(ostream& os, int m, int a, vtkIndent indent) const;
450 
451  // Allow strings without null terminators to be passed into the stream.
452  static vtkClientServerStream::Array InsertString(const char* begin, const char* end);
453 
454  // String reading routines.
455  static vtkClientServerStream::Types GetTypeFromString(const char* begin, const char* end);
456  static vtkClientServerStream::Commands GetCommandFromString(const char* begin, const char* end);
457 
458  int StreamFromStringInternal(const char* begin, const char* end);
459  int AddMessageFromString(const char* begin, const char* end, const char** next);
460  int AddArgumentFromString(const char* begin, const char* end, const char** next);
461 
462 private:
463  vtkClientServerStreamInternals* Internal;
464  friend class vtkClientServerStreamInternals;
465 };
466 
468 
473 template <class T>
475  const vtkClientServerStream& msg, int message, int argument, T** result, const char* type)
476 {
477  vtkObjectBase* obj;
478  if (msg.GetArgumentObject(message, argument, &obj, type))
479  {
480  *result = reinterpret_cast<T*>(obj);
481  return 1;
482  }
483  return 0;
484 }
486 
487 #if defined(VTK_WRAPPING_CXX)
488 // Extract the given argument of the given message as a data array.
489 // This is for use only in generated wrappers.
490 template <class T>
491 class vtkClientServerStreamDataArg
492 {
493 public:
494  // Constructor checks the argument type and length, allocates
495  // memory, and extracts the data from the message.
496  vtkClientServerStreamDataArg(const vtkClientServerStream& msg, int message, int argument)
497  : Data(0)
498  {
499  // Check the argument length.
500  vtkTypeUInt32 length = 0;
501  if (msg.GetArgumentLength(message, argument, &length) && length > 0)
502  {
503  // Allocate memory without throwing.
504  try
505  {
506  this->Data = new T[length];
507  }
508  catch (...)
509  {
510  }
511  }
512 
513  // Extract the data into the allocated memory.
514  if (this->Data && !msg.GetArgument(message, argument, this->Data, length))
515  {
516  delete[] this->Data;
517  this->Data = 0;
518  }
519  }
520 
521  // Destructor frees data memory.
522  ~vtkClientServerStreamDataArg()
523  {
524  if (this->Data)
525  {
526  delete[] this->Data;
527  }
528  }
529 
530  // Allow this object to be passed as if it were a pointer.
531  operator T*() { return this->Data; }
532 
533 private:
534  T* Data;
535 };
536 #endif
537 
538 #endif
539 
540 // VTK-HeaderTest-Exclude: vtkClientServerStream.h
Proxy-object returned by InsertArray and used to insert array data into the stream.
int GetArgument(int message, int argument, bool *value) const
Get the value of the given argument in the given message.
Types
Enumeration of data types that may be stored in a stream.
length
Proxy-object returned by the two-argument form of GetArgument.
int GetArgumentObject(int message, int argument, vtkObjectBase **value, const char *type) const
Get the given argument in the given message as an object of a particular vtkObjectBase type...
T * operator<<(T *LHS, const pqConnect &RHS)
Makes a Qt connection.
Definition: pqConnect.h:64
Store messages for the interpreter.
ValueType GetValue(vtkIdType valueIdx) const
int GetArgumentLength(int message, int argument, vtkTypeUInt32 *length) const
Get the length of an argument of an array type.
Commands
Enumeration of message types that may be stored in a stream.
#define VTKREMOTINGCLIENTSERVERSTREAM_EXPORT
Identifier for a ClientServer object.
int vtkClientServerStreamGetArgumentObject(const vtkClientServerStream &msg, int message, int argument, T **result, const char *type)
Get the given argument of the given message as a pointer to a vtkObjectBase instance of a specific ty...