VTK  9.3.0
vtkVariant.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-FileCopyrightText: Copyright 2008 Sandia Corporation
3 // SPDX-License-Identifier: LicenseRef-BSD-3-Clause-Sandia-USGov
28 #ifndef vtkVariant_h
29 #define vtkVariant_h
30 
31 #include "vtkCommonCoreModule.h" // For export macro
32 #include "vtkObject.h" // For vtkObject's warning support
33 #include "vtkSetGet.h" // For vtkNotUsed macro
34 #include "vtkStdString.h"
35 #include "vtkSystemIncludes.h" // To define ostream
36 #include "vtkType.h" // To define type IDs and VTK_TYPE_USE_* flags
37 
38 //
39 // The following should be eventually placed in vtkSetGet.h
40 //
41 
42 // This is same as extended template macro with an additional case for VTK_VARIANT
43 #define vtkExtraExtendedTemplateMacro(call) \
44  vtkExtendedTemplateMacro(call); \
45  vtkTemplateMacroCase(VTK_VARIANT, vtkVariant, call)
46 
47 // This is same as Iterator Template macro with an additional case for VTK_VARIANT
48 #define vtkExtendedArrayIteratorTemplateMacro(call) \
49  vtkArrayIteratorTemplateMacro(call); \
50  vtkArrayIteratorTemplateMacroCase(VTK_VARIANT, vtkVariant, call)
51 
52 VTK_ABI_NAMESPACE_BEGIN
53 class vtkStdString;
54 class vtkObjectBase;
55 class vtkAbstractArray;
56 class vtkVariant;
57 struct vtkVariantLessThan;
58 
59 VTKCOMMONCORE_EXPORT ostream& operator<<(ostream& os, const vtkVariant& val);
60 
61 class VTKCOMMONCORE_EXPORT vtkVariant
62 {
63 public:
68 
75 
79  vtkVariant(const vtkVariant& other);
80 
86 
91 
96  vtkVariant(unsigned char value);
97 
101  vtkVariant(signed char value);
102 
107 
112  vtkVariant(unsigned short value);
113 
118 
123  vtkVariant(unsigned int value);
124 
129 
134  vtkVariant(unsigned long value);
135 
139  vtkVariant(long long value);
140 
145  vtkVariant(unsigned long long value);
146 
151 
155  vtkVariant(double value);
156 
162  vtkVariant(const char* value);
163 
169 
176 
183  vtkVariant(const vtkVariant& other, unsigned int type);
184 
189 
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  bool Valid;
436  unsigned int Type;
437 
438  friend struct vtkVariantLessThan;
439  friend struct vtkVariantEqual;
442 };
443 
444 VTK_ABI_NAMESPACE_END
445 #include "vtkVariantInlineOperators.h" // needed for operator== and company
446 
447 // A STL-style function object so you can compare two variants using
448 // comp(s1,s2) where comp is an instance of vtkVariantStrictWeakOrder.
449 // This is a faster version of operator< that makes no attempt to
450 // compare values. It satisfies the STL requirement for a comparison
451 // function for ordered containers like map and set.
452 
453 VTK_ABI_NAMESPACE_BEGIN
454 struct VTKCOMMONCORE_EXPORT vtkVariantLessThan
455 {
456 public:
457  bool operator()(const vtkVariant& s1, const vtkVariant& s2) const;
458 };
459 
460 struct VTKCOMMONCORE_EXPORT vtkVariantEqual
461 {
462 public:
463  bool operator()(const vtkVariant& s1, const vtkVariant& s2) const;
464 };
465 
466 struct VTKCOMMONCORE_EXPORT vtkVariantStrictWeakOrder
467 {
468 public:
469  bool operator()(const vtkVariant& s1, const vtkVariant& s2) const;
470 };
471 
472 // Similarly, this is a fast version of operator== that requires that
473 // the types AND the values be equal in order to admit equality.
474 
475 struct VTKCOMMONCORE_EXPORT vtkVariantStrictEquality
476 {
477 public:
478  bool operator()(const vtkVariant& s1, const vtkVariant& s2) const;
479 };
480 
481 VTK_ABI_NAMESPACE_END
482 #endif
483 // VTK-HeaderTest-Exclude: vtkVariant.h
Abstract superclass for all arrays.
abstract base class for most VTK objects
Definition: vtkObjectBase.h:72
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:38
A type representing the union of many types.
Definition: vtkVariant.h:62
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 (i.e.
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 new variant by copying the given variant but converting it to the given 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:220
@ type
Definition: vtkX3D.h:516
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 VTKCOMMONCORE_EXPORT operator>=(const std::string &a, const vtkStringToken &b)
bool VTKCOMMONCORE_EXPORT operator==(const std::string &a, const vtkStringToken &b)
bool VTKCOMMONCORE_EXPORT operator>(const std::string &a, const vtkStringToken &b)
bool VTKCOMMONCORE_EXPORT operator<=(const std::string &a, const vtkStringToken &b)
bool VTKCOMMONCORE_EXPORT operator<(const std::string &a, const vtkStringToken &b)
bool VTKCOMMONCORE_EXPORT operator!=(const std::string &a, const vtkStringToken &b)
VTKCOMMONCORE_EXPORT ostream & operator<<(ostream &os, const vtkVariant &val)