VTK  9.2.6
vtkVariant.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkVariant.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm 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  Copyright 2008 Sandia Corporation.
17  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
18  the U.S. Government retains certain rights in this software.
19 -------------------------------------------------------------------------*/
46 #ifndef vtkVariant_h
47 #define vtkVariant_h
48 
49 #include "vtkCommonCoreModule.h" // For export macro
50 #include "vtkObject.h" // For vtkObject's warning support
51 #include "vtkSetGet.h" // For vtkNotUsed macro
52 #include "vtkStdString.h"
53 #include "vtkSystemIncludes.h" // To define ostream
54 #include "vtkType.h" // To define type IDs and VTK_TYPE_USE_* flags
55 
56 //
57 // The following should be eventually placed in vtkSetGet.h
58 //
59 
60 // This is same as extended template macro with an additional case for VTK_VARIANT
61 #define vtkExtraExtendedTemplateMacro(call) \
62  vtkExtendedTemplateMacro(call); \
63  vtkTemplateMacroCase(VTK_VARIANT, vtkVariant, call)
64 
65 // This is same as Iterator Template macro with an additional case for VTK_VARIANT
66 #define vtkExtendedArrayIteratorTemplateMacro(call) \
67  vtkArrayIteratorTemplateMacro(call); \
68  vtkArrayIteratorTemplateMacroCase(VTK_VARIANT, vtkVariant, call)
69 
70 class vtkStdString;
71 class vtkObjectBase;
72 class vtkAbstractArray;
73 class vtkVariant;
74 struct vtkVariantLessThan;
75 
76 VTKCOMMONCORE_EXPORT ostream& operator<<(ostream& os, const vtkVariant& val);
77 
78 class VTKCOMMONCORE_EXPORT vtkVariant
79 {
80 public:
85 
90 
94  vtkVariant(const vtkVariant& other);
95 
100 
105 
109  vtkVariant(unsigned char value);
110 
114  vtkVariant(signed char value);
115 
120 
124  vtkVariant(unsigned short value);
125 
130 
134  vtkVariant(unsigned int value);
135 
140 
144  vtkVariant(unsigned long value);
145 
149  vtkVariant(long long value);
150 
154  vtkVariant(unsigned long long value);
155 
160 
164  vtkVariant(double value);
165 
169  vtkVariant(const char* value);
170 
175 
180 
184  vtkVariant(const vtkVariant& other, unsigned int type);
185 
190 
194  bool IsValid() const;
195 
199  bool IsString() const;
200 
204  bool IsNumeric() const;
205 
209  bool IsFloat() const;
210 
214  bool IsDouble() const;
215 
219  bool IsChar() const;
220 
224  bool IsUnsignedChar() const;
225 
229  bool IsSignedChar() const;
230 
234  bool IsShort() const;
235 
239  bool IsUnsignedShort() const;
240 
244  bool IsInt() const;
245 
249  bool IsUnsignedInt() const;
250 
254  bool IsLong() const;
255 
259  bool IsUnsignedLong() const;
260 
264  bool IsLongLong() const;
265 
269  bool IsUnsignedLongLong() const;
270 
274  bool IsVTKObject() const;
275 
279  bool IsArray() const;
280 
284  unsigned int GetType() const;
285 
289  const char* GetTypeAsString() const;
290 
292  {
293  DEFAULT_FORMATTING = 0,
294  FIXED_FORMATTING = 1,
295  SCIENTIFIC_FORMATTING = 2
296  };
297 
306  vtkStdString ToString(int formatting = DEFAULT_FORMATTING, int precision = 6) const;
307 
309 
318  float ToFloat(bool* valid) const;
319  float ToFloat() const { return this->ToFloat(nullptr); }
320  double ToDouble(bool* valid) const;
321  double ToDouble() const { return this->ToDouble(nullptr); }
322  char ToChar(bool* valid) const;
323  char ToChar() const { return this->ToChar(nullptr); }
324  unsigned char ToUnsignedChar(bool* valid) const;
325  unsigned char ToUnsignedChar() const { return this->ToUnsignedChar(nullptr); }
326  signed char ToSignedChar(bool* valid) const;
327  signed char ToSignedChar() const { return this->ToSignedChar(nullptr); }
328  short ToShort(bool* valid) const;
329  short ToShort() const { return this->ToShort(nullptr); }
330  unsigned short ToUnsignedShort(bool* valid) const;
331  unsigned short ToUnsignedShort() const { return this->ToUnsignedShort(nullptr); }
332  int ToInt(bool* valid) const;
333  int ToInt() const { return this->ToInt(nullptr); }
334  unsigned int ToUnsignedInt(bool* valid) const;
335  unsigned int ToUnsignedInt() const { return this->ToUnsignedInt(nullptr); }
336  long ToLong(bool* valid) const;
337  long ToLong() const { return this->ToLong(nullptr); }
338  unsigned long ToUnsignedLong(bool* valid) const;
339  unsigned long ToUnsignedLong() const { return this->ToUnsignedLong(nullptr); }
340  long long ToLongLong(bool* valid) const;
341  long long ToLongLong() const { return this->ToLongLong(nullptr); }
342  unsigned long long ToUnsignedLongLong(bool* valid) const;
343  unsigned long long ToUnsignedLongLong() const { return this->ToUnsignedLongLong(nullptr); }
344  vtkTypeInt64 ToTypeInt64(bool* valid) const;
345  vtkTypeInt64 ToTypeInt64() const { return this->ToTypeInt64(nullptr); }
346  vtkTypeUInt64 ToTypeUInt64(bool* valid) const;
347  vtkTypeUInt64 ToTypeUInt64() const { return this->ToTypeUInt64(nullptr); }
349 
354 
359 
370  bool IsEqual(const vtkVariant& other) const;
371 
373 
403  bool operator==(const vtkVariant& other) const;
404  bool operator!=(const vtkVariant& other) const;
405  bool operator<(const vtkVariant& other) const;
406  bool operator>(const vtkVariant& other) const;
407  bool operator<=(const vtkVariant& other) const;
408  bool operator>=(const vtkVariant& other) const;
410 
411  friend VTKCOMMONCORE_EXPORT ostream& operator<<(ostream& os, const vtkVariant& val);
412 
413 private:
414  template <typename T>
415  T ToNumeric(bool* valid, T* vtkNotUsed(ignored)) const;
416 
417  union {
419  float Float;
420  double Double;
421  char Char;
422  unsigned char UnsignedChar;
423  signed char SignedChar;
424  short Short;
425  unsigned short UnsignedShort;
426  int Int;
427  unsigned int UnsignedInt;
428  long Long;
429  unsigned long UnsignedLong;
430  long long LongLong;
431  unsigned long long UnsignedLongLong;
433  } Data;
434 
435  unsigned char Valid;
436  unsigned char Type;
437 
438  friend struct vtkVariantLessThan;
439  friend struct vtkVariantEqual;
442 };
443 
444 #include "vtkVariantInlineOperators.h" // needed for operator== and company
445 
446 // A STL-style function object so you can compare two variants using
447 // comp(s1,s2) where comp is an instance of vtkVariantStrictWeakOrder.
448 // This is a faster version of operator< that makes no attempt to
449 // compare values. It satisfies the STL requirement for a comparison
450 // function for ordered containers like map and set.
451 
452 struct VTKCOMMONCORE_EXPORT vtkVariantLessThan
453 {
454 public:
455  bool operator()(const vtkVariant& s1, const vtkVariant& s2) const;
456 };
457 
458 struct VTKCOMMONCORE_EXPORT vtkVariantEqual
459 {
460 public:
461  bool operator()(const vtkVariant& s1, const vtkVariant& s2) const;
462 };
463 
464 struct VTKCOMMONCORE_EXPORT vtkVariantStrictWeakOrder
465 {
466 public:
467  bool operator()(const vtkVariant& s1, const vtkVariant& s2) const;
468 };
469 
470 // Similarly, this is a fast version of operator== that requires that
471 // the types AND the values be equal in order to admit equality.
472 
473 struct VTKCOMMONCORE_EXPORT vtkVariantStrictEquality
474 {
475 public:
476  bool operator()(const vtkVariant& s1, const vtkVariant& s2) const;
477 };
478 
479 #endif
480 // VTK-HeaderTest-Exclude: vtkVariant.h
Abstract superclass for all arrays.
abstract base class for most VTK objects
Definition: vtkObjectBase.h:83
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:48
A atomic type representing the union of many types.
Definition: vtkVariant.h:79
vtkTypeInt64 ToTypeInt64() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition: vtkVariant.h:345
vtkVariant(float value)
Create a float variant.
bool IsArray() const
Get whether the variant is a VTK array (i.e.
long long ToLongLong(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
~vtkVariant()
Destruct the variant.
char ToChar() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition: vtkVariant.h:323
unsigned long long UnsignedLongLong
Definition: vtkVariant.h:431
double ToDouble(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
long long ToLongLong() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition: vtkVariant.h:341
vtkTypeUInt64 ToTypeUInt64(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
unsigned short ToUnsignedShort(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
vtkVariant(unsigned long value)
Create an unsigned long variant.
bool IsUnsignedChar() const
Get whether the variant is an unsigned char.
unsigned int GetType() const
Get the type of the variant.
unsigned char ToUnsignedChar(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
unsigned char ToUnsignedChar() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition: vtkVariant.h:325
vtkVariant(unsigned int value)
Create an unsigned integer variant.
vtkVariant(char value)
Create a char variant.
unsigned short UnsignedShort
Definition: vtkVariant.h:425
friend VTKCOMMONCORE_EXPORT ostream & operator<<(ostream &os, const vtkVariant &val)
char ToChar(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
bool IsUnsignedLong() const
Get whether the variant is an unsigned long.
short Short
Definition: vtkVariant.h:424
vtkVariant(int value)
Create an integer variant.
bool IsDouble() const
Get whether the variant is a double.
vtkVariant(vtkStdString value)
Create a string variant from a std string.
vtkVariant(unsigned long long value)
Create an unsigned long long variant.
long ToLong() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition: vtkVariant.h:337
signed char ToSignedChar() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition: vtkVariant.h:327
double ToDouble() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition: vtkVariant.h:321
bool IsInt() const
Get whether the variant is an int.
float Float
Definition: vtkVariant.h:419
bool IsUnsignedInt() const
Get whether the variant is an unsigned int.
unsigned long ToUnsignedLong(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
bool IsFloat() const
Get whether the variant is a float.
bool IsUnsignedShort() const
Get whether the variant is an unsigned short.
vtkVariant(long value)
Create an long variant.
float ToFloat() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition: vtkVariant.h:319
long long LongLong
Definition: vtkVariant.h:430
vtkVariant(signed char value)
Create a signed char variant.
vtkVariant(vtkObjectBase *value)
Create a vtkObjectBase variant.
vtkStdString ToString(int formatting=DEFAULT_FORMATTING, int precision=6) const
Convert the variant to a string.
bool IsChar() const
Get whether the variant is an char.
bool IsVTKObject() const
Get whether the variant is a VTK object pointer.
short ToShort() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition: vtkVariant.h:329
vtkStdString * String
Definition: vtkVariant.h:418
vtkVariant(long long value)
Create a long long variant.
bool IsLongLong() const
Get whether the variant is long long.
vtkObjectBase * ToVTKObject() const
Return the VTK object, or nullptr if not of that type.
signed char ToSignedChar(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
unsigned int ToUnsignedInt() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition: vtkVariant.h:335
unsigned int UnsignedInt
Definition: vtkVariant.h:427
float ToFloat(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
bool IsValid() const
Get whether the variant value is valid.
int ToInt(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
vtkVariant(bool value)
Create a bool variant.
unsigned long long ToUnsignedLongLong() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition: vtkVariant.h:343
long ToLong(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
bool IsSignedChar() const
Get whether the variant is an signed char.
vtkAbstractArray * ToArray() const
Return the array, or nullptr if not of that type.
vtkTypeInt64 ToTypeInt64(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
const char * GetTypeAsString() const
Get the type of the variant as a string.
bool IsString() const
Get whether the variant is a string.
unsigned int ToUnsignedInt(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
vtkVariant(const vtkVariant &other)
Copy constructor.
vtkVariant(const vtkVariant &other, unsigned int type)
Create a variant of a specific type.
unsigned long ToUnsignedLong() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition: vtkVariant.h:339
vtkVariant(unsigned char value)
Create an unsigned char variant.
unsigned long UnsignedLong
Definition: vtkVariant.h:429
unsigned char UnsignedChar
Definition: vtkVariant.h:422
vtkVariant(short value)
Create a short variant.
vtkVariant(unsigned short value)
Create an unsigned short variant.
vtkVariant()
Create an invalid variant.
bool IsLong() const
Get whether the variant is an long.
vtkVariant(double value)
Create a double variant.
unsigned short ToUnsignedShort() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition: vtkVariant.h:331
bool IsEqual(const vtkVariant &other) const
Determines whether two variants have the same value.
double Double
Definition: vtkVariant.h:420
vtkObjectBase * VTKObject
Definition: vtkVariant.h:432
signed char SignedChar
Definition: vtkVariant.h:423
vtkVariant & operator=(const vtkVariant &other)
Copy the value of one variant into another.
bool IsNumeric() const
Get whether the variant is any numeric type.
int ToInt() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition: vtkVariant.h:333
bool IsShort() const
Get whether the variant is an short.
vtkTypeUInt64 ToTypeUInt64() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition: vtkVariant.h:347
short ToShort(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
vtkVariant(const char *value)
Create a string variant from a const char*.
unsigned long long ToUnsignedLongLong(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
bool IsUnsignedLongLong() const
Get whether the variant is unsigned long long.
@ value
Definition: vtkX3D.h:226
@ type
Definition: vtkX3D.h:522
bool operator()(const vtkVariant &s1, const vtkVariant &s2) const
bool operator()(const vtkVariant &s1, const vtkVariant &s2) const
bool operator()(const vtkVariant &s1, const vtkVariant &s2) const
bool operator()(const vtkVariant &s1, const vtkVariant &s2) const
bool VTKCOMMONDATAMODEL_EXPORT operator==(vtkEdgeBase e1, vtkEdgeBase e2)
bool VTKCOMMONDATAMODEL_EXPORT operator!=(vtkEdgeBase e1, vtkEdgeBase e2)
bool operator<(const vtkPixelExtent &l, const vtkPixelExtent &r)
VTKCOMMONCORE_EXPORT ostream & operator<<(ostream &os, const vtkVariant &val)