VTK: Common/Core/vtkVariant.h Source File

| VTK 9.3.1

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:
67 vtkVariant();
68
74 ~vtkVariant();
75
79 vtkVariant(const vtkVariant& other);
80
85 vtkVariant(bool value);
86
90 vtkVariant(char value);
91
96 vtkVariant(unsigned char value);
97
101 vtkVariant(signed char value);
102
106 vtkVariant(short value);
107
112 vtkVariant(unsigned short value);
113
117 vtkVariant(int value);
118
123 vtkVariant(unsigned int value);
124
128 vtkVariant(long value);
129
134 vtkVariant(unsigned long value);
135
139 vtkVariant(long long value);
140
145 vtkVariant(unsigned long long value);
146
150 vtkVariant(float value);
151
155 vtkVariant(double value);
156
162 vtkVariant(const char* value);
163
168 vtkVariant(vtkStdString value);
169
175 vtkVariant(vtkObjectBase* value);
176
183 vtkVariant(const vtkVariant& other, unsigned int type);
184
188 vtkVariant& operator=(const vtkVariant& other);
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
291 enum StringFormatting
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
353 vtkObjectBase* ToVTKObject() const;
354
358 vtkAbstractArray* ToArray() const;
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 {
418 vtkStdString* String;
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;
432 vtkObjectBase* VTKObject;
433 } Data;
434
435 bool Valid;
436 unsigned int Type;
437
438 friend struct vtkVariantLessThan;
439 friend struct vtkVariantEqual;
440 friend struct vtkVariantStrictWeakOrder;
441 friend struct vtkVariantStrictEquality;
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
vtkAbstractArray
Abstract superclass for all arrays.
Definition: vtkAbstractArray.h:76
vtkObjectBase
abstract base class for most VTK objects
Definition: vtkObjectBase.h:72
vtkStdString
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:38
vtkVariant
A type representing the union of many types.
Definition: vtkVariant.h:62
vtkVariant::ToTypeInt64
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::vtkVariant
vtkVariant(float value)
Create a float variant.
vtkVariant::IsArray
bool IsArray() const
Get whether the variant is a VTK array (i.e.
vtkVariant::ToLongLong
long long ToLongLong(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
vtkVariant::~vtkVariant
~vtkVariant()
Destruct the variant.
vtkVariant::ToChar
char ToChar() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition: vtkVariant.h:323
vtkVariant::UnsignedLongLong
unsigned long long UnsignedLongLong
Definition: vtkVariant.h:431
vtkVariant::ToDouble
double ToDouble(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
vtkVariant::ToLongLong
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
vtkVariant::ToTypeUInt64
vtkTypeUInt64 ToTypeUInt64(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
vtkVariant::ToUnsignedShort
unsigned short ToUnsignedShort(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
vtkVariant::vtkVariant
vtkVariant(unsigned long value)
Create an unsigned long variant.
vtkVariant::IsUnsignedChar
bool IsUnsignedChar() const
Get whether the variant is an unsigned char.
vtkVariant::GetType
unsigned int GetType() const
Get the type of the variant.
vtkVariant::ToUnsignedChar
unsigned char ToUnsignedChar(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
vtkVariant::ToUnsignedChar
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::vtkVariant
vtkVariant(unsigned int value)
Create an unsigned integer variant.
vtkVariant::vtkVariant
vtkVariant(char value)
Create a char variant.
vtkVariant::StringFormatting
StringFormatting
Definition: vtkVariant.h:292
vtkVariant::UnsignedShort
unsigned short UnsignedShort
Definition: vtkVariant.h:425
vtkVariant::operator<<
friend VTKCOMMONCORE_EXPORT ostream & operator<<(ostream &os, const vtkVariant &val)
vtkVariant::ToChar
char ToChar(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
vtkVariant::Int
int Int
Definition: vtkVariant.h:426
vtkVariant::IsUnsignedLong
bool IsUnsignedLong() const
Get whether the variant is an unsigned long.
vtkVariant::Short
short Short
Definition: vtkVariant.h:424
vtkVariant::vtkVariant
vtkVariant(int value)
Create an integer variant.
vtkVariant::IsDouble
bool IsDouble() const
Get whether the variant is a double.
vtkVariant::vtkVariant
vtkVariant(vtkStdString value)
Create a string variant from a std string.
vtkVariant::vtkVariant
vtkVariant(unsigned long long value)
Create an unsigned long long variant.
vtkVariant::ToLong
long ToLong() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition: vtkVariant.h:337
vtkVariant::ToSignedChar
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
vtkVariant::ToDouble
double ToDouble() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition: vtkVariant.h:321
vtkVariant::IsInt
bool IsInt() const
Get whether the variant is an int.
vtkVariant::Float
float Float
Definition: vtkVariant.h:419
vtkVariant::IsUnsignedInt
bool IsUnsignedInt() const
Get whether the variant is an unsigned int.
vtkVariant::ToUnsignedLong
unsigned long ToUnsignedLong(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
vtkVariant::IsFloat
bool IsFloat() const
Get whether the variant is a float.
vtkVariant::IsUnsignedShort
bool IsUnsignedShort() const
Get whether the variant is an unsigned short.
vtkVariant::vtkVariant
vtkVariant(long value)
Create an long variant.
vtkVariant::ToFloat
float ToFloat() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition: vtkVariant.h:319
vtkVariant::LongLong
long long LongLong
Definition: vtkVariant.h:430
vtkVariant::vtkVariant
vtkVariant(signed char value)
Create a signed char variant.
vtkVariant::vtkVariant
vtkVariant(vtkObjectBase *value)
Create a vtkObjectBase variant.
vtkVariant::ToString
vtkStdString ToString(int formatting=DEFAULT_FORMATTING, int precision=6) const
Convert the variant to a string.
vtkVariant::IsChar
bool IsChar() const
Get whether the variant is an char.
vtkVariant::IsVTKObject
bool IsVTKObject() const
Get whether the variant is a VTK object pointer (i.e.
vtkVariant::ToShort
short ToShort() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition: vtkVariant.h:329
vtkVariant::String
vtkStdString * String
Definition: vtkVariant.h:418
vtkVariant::vtkVariant
vtkVariant(long long value)
Create a long long variant.
vtkVariant::IsLongLong
bool IsLongLong() const
Get whether the variant is long long.
vtkVariant::ToVTKObject
vtkObjectBase * ToVTKObject() const
Return the VTK object, or nullptr if not of that type.
vtkVariant::ToSignedChar
signed char ToSignedChar(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
vtkVariant::ToUnsignedInt
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
vtkVariant::UnsignedInt
unsigned int UnsignedInt
Definition: vtkVariant.h:427
vtkVariant::ToFloat
float ToFloat(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
vtkVariant::IsValid
bool IsValid() const
Get whether the variant value is valid.
vtkVariant::ToInt
int ToInt(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
vtkVariant::vtkVariant
vtkVariant(bool value)
Create a bool variant.
vtkVariant::ToUnsignedLongLong
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
vtkVariant::ToLong
long ToLong(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
vtkVariant::IsSignedChar
bool IsSignedChar() const
Get whether the variant is an signed char.
vtkVariant::ToArray
vtkAbstractArray * ToArray() const
Return the array, or nullptr if not of that type.
vtkVariant::ToTypeInt64
vtkTypeInt64 ToTypeInt64(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
vtkVariant::GetTypeAsString
const char * GetTypeAsString() const
Get the type of the variant as a string.
vtkVariant::IsString
bool IsString() const
Get whether the variant is a string.
vtkVariant::ToUnsignedInt
unsigned int ToUnsignedInt(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
vtkVariant::vtkVariant
vtkVariant(const vtkVariant &other)
Copy constructor.
vtkVariant::Long
long Long
Definition: vtkVariant.h:428
vtkVariant::vtkVariant
vtkVariant(const vtkVariant &other, unsigned int type)
Create a new variant by copying the given variant but converting it to the given type.
vtkVariant::ToUnsignedLong
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::vtkVariant
vtkVariant(unsigned char value)
Create an unsigned char variant.
vtkVariant::UnsignedLong
unsigned long UnsignedLong
Definition: vtkVariant.h:429
vtkVariant::UnsignedChar
unsigned char UnsignedChar
Definition: vtkVariant.h:422
vtkVariant::vtkVariant
vtkVariant(short value)
Create a short variant.
vtkVariant::vtkVariant
vtkVariant(unsigned short value)
Create an unsigned short variant.
vtkVariant::vtkVariant
vtkVariant()
Create an invalid variant.
vtkVariant::IsLong
bool IsLong() const
Get whether the variant is an long.
vtkVariant::vtkVariant
vtkVariant(double value)
Create a double variant.
vtkVariant::ToUnsignedShort
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
vtkVariant::IsEqual
bool IsEqual(const vtkVariant &other) const
Determines whether two variants have the same value.
vtkVariant::Double
double Double
Definition: vtkVariant.h:420
vtkVariant::VTKObject
vtkObjectBase * VTKObject
Definition: vtkVariant.h:432
vtkVariant::SignedChar
signed char SignedChar
Definition: vtkVariant.h:423
vtkVariant::operator=
vtkVariant & operator=(const vtkVariant &other)
Copy the value of one variant into another.
vtkVariant::Char
char Char
Definition: vtkVariant.h:421
vtkVariant::IsNumeric
bool IsNumeric() const
Get whether the variant is any numeric type.
vtkVariant::ToInt
int ToInt() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition: vtkVariant.h:333
vtkVariant::IsShort
bool IsShort() const
Get whether the variant is an short.
vtkVariant::ToTypeUInt64
vtkTypeUInt64 ToTypeUInt64() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition: vtkVariant.h:347
vtkVariant::ToShort
short ToShort(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
vtkVariant::vtkVariant
vtkVariant(const char *value)
Create a string variant from a const char*.
vtkVariant::ToUnsignedLongLong
unsigned long long ToUnsignedLongLong(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
vtkVariant::IsUnsignedLongLong
bool IsUnsignedLongLong() const
Get whether the variant is unsigned long long.
detail::Type
Type
Definition: TestQtCommon.h:21
vtkX3D::value
@ value
Definition: vtkX3D.h:220
vtkX3D::type
@ type
Definition: vtkX3D.h:516
vtkVariantEqual
Definition: vtkVariant.h:461
vtkVariantEqual::operator()
bool operator()(const vtkVariant &s1, const vtkVariant &s2) const
vtkVariantLessThan
Definition: vtkVariant.h:455
vtkVariantLessThan::operator()
bool operator()(const vtkVariant &s1, const vtkVariant &s2) const
vtkVariantStrictEquality
Definition: vtkVariant.h:476
vtkVariantStrictEquality::operator()
bool operator()(const vtkVariant &s1, const vtkVariant &s2) const
vtkVariantStrictWeakOrder
Definition: vtkVariant.h:467
vtkVariantStrictWeakOrder::operator()
bool operator()(const vtkVariant &s1, const vtkVariant &s2) const
vtkObject.h
vtkStdString.h
operator>=
bool VTKCOMMONCORE_EXPORT operator>=(const std::string &a, const vtkStringToken &b)
operator==
bool VTKCOMMONCORE_EXPORT operator==(const std::string &a, const vtkStringToken &b)
operator>
bool VTKCOMMONCORE_EXPORT operator>(const std::string &a, const vtkStringToken &b)
operator<=
bool VTKCOMMONCORE_EXPORT operator<=(const std::string &a, const vtkStringToken &b)
operator<
bool VTKCOMMONCORE_EXPORT operator<(const std::string &a, const vtkStringToken &b)
operator!=
bool VTKCOMMONCORE_EXPORT operator!=(const std::string &a, const vtkStringToken &b)
vtkSystemIncludes.h
vtkType.h
vtkVariantInlineOperators.h
operator<<
VTKCOMMONCORE_EXPORT ostream & operator<<(ostream &os, const vtkVariant &val)