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
19 #ifndef vtkVariant_h
20 #define vtkVariant_h
21 
22 #include "vtkCommonCoreModule.h" // For export macro
23 #include "vtkObject.h" // For vtkObject's warning support
24 #include "vtkSetGet.h" // For vtkNotUsed macro
25 #include "vtkStdString.h"
26 #include "vtkSystemIncludes.h" // To define ostream
27 #include "vtkType.h" // To define type IDs and VTK_TYPE_USE_* flags
28 
29 //
30 // The following should be eventually placed in vtkSetGet.h
31 //
32 
33 // This is same as extended template macro with an additional case for VTK_VARIANT
34 #define vtkExtraExtendedTemplateMacro(call) \
35  vtkExtendedTemplateMacro(call); \
36  vtkTemplateMacroCase(VTK_VARIANT, vtkVariant, call)
37 
38 // This is same as Iterator Template macro with an additional case for VTK_VARIANT
39 #define vtkExtendedArrayIteratorTemplateMacro(call) \
40  vtkArrayIteratorTemplateMacro(call); \
41  vtkArrayIteratorTemplateMacroCase(VTK_VARIANT, vtkVariant, call)
42 
43 VTK_ABI_NAMESPACE_BEGIN
44 class vtkStdString;
45 class vtkObjectBase;
46 class vtkAbstractArray;
47 class vtkVariant;
48 struct vtkVariantLessThan;
49 
50 VTKCOMMONCORE_EXPORT ostream& operator<<(ostream& os, const vtkVariant& val);
51 
52 class VTKCOMMONCORE_EXPORT vtkVariant
53 {
54 public:
59 
66 
70  vtkVariant(const vtkVariant& other);
71 
77 
82 
87  vtkVariant(unsigned char value);
88 
92  vtkVariant(signed char value);
93 
97  vtkVariant(short value);
98 
103  vtkVariant(unsigned short value);
104 
109 
114  vtkVariant(unsigned int value);
115 
120 
125  vtkVariant(unsigned long value);
126 
130  vtkVariant(long long value);
131 
136  vtkVariant(unsigned long long value);
137 
142 
146  vtkVariant(double value);
147 
153  vtkVariant(const char* value);
154 
160 
167 
174  vtkVariant(const vtkVariant& other, unsigned int type);
175 
180 
185  bool IsValid() const;
186 
190  bool IsString() const;
191 
195  bool IsNumeric() const;
196 
200  bool IsFloat() const;
201 
205  bool IsDouble() const;
206 
210  bool IsChar() const;
211 
215  bool IsUnsignedChar() const;
216 
220  bool IsSignedChar() const;
221 
225  bool IsShort() const;
226 
230  bool IsUnsignedShort() const;
231 
235  bool IsInt() const;
236 
240  bool IsUnsignedInt() const;
241 
245  bool IsLong() const;
246 
250  bool IsUnsignedLong() const;
251 
255  bool IsLongLong() const;
256 
260  bool IsUnsignedLongLong() const;
261 
265  bool IsVTKObject() const;
266 
270  bool IsArray() const;
271 
275  unsigned int GetType() const;
276 
280  const char* GetTypeAsString() const;
281 
283  {
284  DEFAULT_FORMATTING = 0,
285  FIXED_FORMATTING = 1,
286  SCIENTIFIC_FORMATTING = 2
287  };
288 
297  vtkStdString ToString(int formatting = DEFAULT_FORMATTING, int precision = 6) const;
298 
300 
309  float ToFloat(bool* valid) const;
310  float ToFloat() const { return this->ToFloat(nullptr); }
311  double ToDouble(bool* valid) const;
312  double ToDouble() const { return this->ToDouble(nullptr); }
313  char ToChar(bool* valid) const;
314  char ToChar() const { return this->ToChar(nullptr); }
315  unsigned char ToUnsignedChar(bool* valid) const;
316  unsigned char ToUnsignedChar() const { return this->ToUnsignedChar(nullptr); }
317  signed char ToSignedChar(bool* valid) const;
318  signed char ToSignedChar() const { return this->ToSignedChar(nullptr); }
319  short ToShort(bool* valid) const;
320  short ToShort() const { return this->ToShort(nullptr); }
321  unsigned short ToUnsignedShort(bool* valid) const;
322  unsigned short ToUnsignedShort() const { return this->ToUnsignedShort(nullptr); }
323  int ToInt(bool* valid) const;
324  int ToInt() const { return this->ToInt(nullptr); }
325  unsigned int ToUnsignedInt(bool* valid) const;
326  unsigned int ToUnsignedInt() const { return this->ToUnsignedInt(nullptr); }
327  long ToLong(bool* valid) const;
328  long ToLong() const { return this->ToLong(nullptr); }
329  unsigned long ToUnsignedLong(bool* valid) const;
330  unsigned long ToUnsignedLong() const { return this->ToUnsignedLong(nullptr); }
331  long long ToLongLong(bool* valid) const;
332  long long ToLongLong() const { return this->ToLongLong(nullptr); }
333  unsigned long long ToUnsignedLongLong(bool* valid) const;
334  unsigned long long ToUnsignedLongLong() const { return this->ToUnsignedLongLong(nullptr); }
335  vtkTypeInt64 ToTypeInt64(bool* valid) const;
336  vtkTypeInt64 ToTypeInt64() const { return this->ToTypeInt64(nullptr); }
337  vtkTypeUInt64 ToTypeUInt64(bool* valid) const;
338  vtkTypeUInt64 ToTypeUInt64() const { return this->ToTypeUInt64(nullptr); }
340 
345 
350 
361  bool IsEqual(const vtkVariant& other) const;
362 
364 
394  bool operator==(const vtkVariant& other) const;
395  bool operator!=(const vtkVariant& other) const;
396  bool operator<(const vtkVariant& other) const;
397  bool operator>(const vtkVariant& other) const;
398  bool operator<=(const vtkVariant& other) const;
399  bool operator>=(const vtkVariant& other) const;
401 
402  friend VTKCOMMONCORE_EXPORT ostream& operator<<(ostream& os, const vtkVariant& val);
403 
404 private:
405  template <typename T>
406  T ToNumeric(bool* valid, T* vtkNotUsed(ignored)) const;
407 
408  union {
410  float Float;
411  double Double;
412  char Char;
413  unsigned char UnsignedChar;
414  signed char SignedChar;
415  short Short;
416  unsigned short UnsignedShort;
417  int Int;
418  unsigned int UnsignedInt;
419  long Long;
420  unsigned long UnsignedLong;
421  long long LongLong;
422  unsigned long long UnsignedLongLong;
424  } Data;
425 
426  bool Valid;
427  unsigned int Type;
428 
429  friend struct vtkVariantLessThan;
430  friend struct vtkVariantEqual;
433 };
434 
435 VTK_ABI_NAMESPACE_END
436 #include "vtkVariantInlineOperators.h" // needed for operator== and company
437 
438 // A STL-style function object so you can compare two variants using
439 // comp(s1,s2) where comp is an instance of vtkVariantStrictWeakOrder.
440 // This is a faster version of operator< that makes no attempt to
441 // compare values. It satisfies the STL requirement for a comparison
442 // function for ordered containers like map and set.
443 
444 VTK_ABI_NAMESPACE_BEGIN
445 struct VTKCOMMONCORE_EXPORT vtkVariantLessThan
446 {
447 public:
448  bool operator()(const vtkVariant& s1, const vtkVariant& s2) const;
449 };
450 
451 struct VTKCOMMONCORE_EXPORT vtkVariantEqual
452 {
453 public:
454  bool operator()(const vtkVariant& s1, const vtkVariant& s2) const;
455 };
456 
457 struct VTKCOMMONCORE_EXPORT vtkVariantStrictWeakOrder
458 {
459 public:
460  bool operator()(const vtkVariant& s1, const vtkVariant& s2) const;
461 };
462 
463 // Similarly, this is a fast version of operator== that requires that
464 // the types AND the values be equal in order to admit equality.
465 
466 struct VTKCOMMONCORE_EXPORT vtkVariantStrictEquality
467 {
468 public:
469  bool operator()(const vtkVariant& s1, const vtkVariant& s2) const;
470 };
471 
472 VTK_ABI_NAMESPACE_END
473 #endif
474 // VTK-HeaderTest-Exclude: vtkVariant.h
Abstract superclass for all arrays.
abstract base class for most VTK objects
Definition: vtkObjectBase.h:63
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:29
A type representing the union of many types.
Definition: vtkVariant.h:53
vtkTypeInt64 ToTypeInt64() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition: vtkVariant.h:336
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:314
unsigned long long UnsignedLongLong
Definition: vtkVariant.h:422
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:332
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:316
vtkVariant(unsigned int value)
Create an unsigned integer variant.
vtkVariant(char value)
Create a char variant.
unsigned short UnsignedShort
Definition: vtkVariant.h:416
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:415
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:328
signed char ToSignedChar() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition: vtkVariant.h:318
double ToDouble() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition: vtkVariant.h:312
bool IsInt() const
Get whether the variant is an int.
float Float
Definition: vtkVariant.h:410
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:310
long long LongLong
Definition: vtkVariant.h:421
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:320
vtkStdString * String
Definition: vtkVariant.h:409
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:326
unsigned int UnsignedInt
Definition: vtkVariant.h:418
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:334
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:330
vtkVariant(unsigned char value)
Create an unsigned char variant.
unsigned long UnsignedLong
Definition: vtkVariant.h:420
unsigned char UnsignedChar
Definition: vtkVariant.h:413
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:322
bool IsEqual(const vtkVariant &other) const
Determines whether two variants have the same value.
double Double
Definition: vtkVariant.h:411
vtkObjectBase * VTKObject
Definition: vtkVariant.h:423
signed char SignedChar
Definition: vtkVariant.h:414
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:324
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:338
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)