VTK  9.3.20240423
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
128#ifndef vtkVariant_h
129#define vtkVariant_h
130
131#include "vtkCommonCoreModule.h" // For export macro
132#include "vtkObject.h" // For vtkObject's warning support
133#include "vtkSetGet.h" // For vtkNotUsed macro
134#include "vtkStdString.h"
135#include "vtkSystemIncludes.h" // To define ostream
136#include "vtkType.h" // To define type IDs and VTK_TYPE_USE_* flags
137
138//
139// The following should be eventually placed in vtkSetGet.h
140//
141
142// This is same as extended template macro with an additional case for VTK_VARIANT
143#define vtkExtraExtendedTemplateMacro(call) \
144 vtkExtendedTemplateMacro(call); \
145 vtkTemplateMacroCase(VTK_VARIANT, vtkVariant, call)
146
147// This is same as Iterator Template macro with an additional case for VTK_VARIANT
148#define vtkExtendedArrayIteratorTemplateMacro(call) \
149 vtkArrayIteratorTemplateMacro(call); \
150 vtkArrayIteratorTemplateMacroCase(VTK_VARIANT, vtkVariant, call)
151
152VTK_ABI_NAMESPACE_BEGIN
153class vtkStdString;
154class vtkObjectBase;
155class vtkAbstractArray;
156class vtkVariant;
157struct vtkVariantLessThan;
158
159VTKCOMMONCORE_EXPORT ostream& operator<<(ostream& os, const vtkVariant& val);
160
161class VTKCOMMONCORE_EXPORT vtkVariant
162{
163public:
168
175
179 vtkVariant(const vtkVariant& other);
180
185 vtkVariant(bool value);
186
190 vtkVariant(char value);
191
196 vtkVariant(unsigned char value);
197
201 vtkVariant(signed char value);
202
206 vtkVariant(short value);
207
212 vtkVariant(unsigned short value);
213
217 vtkVariant(int value);
218
223 vtkVariant(unsigned int value);
224
228 vtkVariant(long value);
229
234 vtkVariant(unsigned long value);
235
239 vtkVariant(long long value);
240
245 vtkVariant(unsigned long long value);
246
250 vtkVariant(float value);
251
255 vtkVariant(double value);
256
262 vtkVariant(const char* value);
263
269
276
283 vtkVariant(const vtkVariant& other, unsigned int type);
284
289
294 bool IsValid() const;
295
299 bool IsString() const;
300
304 bool IsNumeric() const;
305
309 bool IsFloat() const;
310
314 bool IsDouble() const;
315
319 bool IsChar() const;
320
324 bool IsUnsignedChar() const;
325
329 bool IsSignedChar() const;
330
334 bool IsShort() const;
335
339 bool IsUnsignedShort() const;
340
344 bool IsInt() const;
345
349 bool IsUnsignedInt() const;
350
354 bool IsLong() const;
355
359 bool IsUnsignedLong() const;
360
364 bool IsLongLong() const;
365
369 bool IsUnsignedLongLong() const;
370
374 bool IsVTKObject() const;
375
379 bool IsArray() const;
380
384 unsigned int GetType() const;
385
389 const char* GetTypeAsString() const;
390
392 {
393 DEFAULT_FORMATTING = 0,
394 FIXED_FORMATTING = 1,
395 SCIENTIFIC_FORMATTING = 2
396 };
397
406 vtkStdString ToString(int formatting = DEFAULT_FORMATTING, int precision = 6) const;
407
409
418 float ToFloat(bool* valid) const;
419 float ToFloat() const { return this->ToFloat(nullptr); }
420 double ToDouble(bool* valid) const;
421 double ToDouble() const { return this->ToDouble(nullptr); }
422 char ToChar(bool* valid) const;
423 char ToChar() const { return this->ToChar(nullptr); }
424 unsigned char ToUnsignedChar(bool* valid) const;
425 unsigned char ToUnsignedChar() const { return this->ToUnsignedChar(nullptr); }
426 signed char ToSignedChar(bool* valid) const;
427 signed char ToSignedChar() const { return this->ToSignedChar(nullptr); }
428 short ToShort(bool* valid) const;
429 short ToShort() const { return this->ToShort(nullptr); }
430 unsigned short ToUnsignedShort(bool* valid) const;
431 unsigned short ToUnsignedShort() const { return this->ToUnsignedShort(nullptr); }
432 int ToInt(bool* valid) const;
433 int ToInt() const { return this->ToInt(nullptr); }
434 unsigned int ToUnsignedInt(bool* valid) const;
435 unsigned int ToUnsignedInt() const { return this->ToUnsignedInt(nullptr); }
436 long ToLong(bool* valid) const;
437 long ToLong() const { return this->ToLong(nullptr); }
438 unsigned long ToUnsignedLong(bool* valid) const;
439 unsigned long ToUnsignedLong() const { return this->ToUnsignedLong(nullptr); }
440 long long ToLongLong(bool* valid) const;
441 long long ToLongLong() const { return this->ToLongLong(nullptr); }
442 unsigned long long ToUnsignedLongLong(bool* valid) const;
443 unsigned long long ToUnsignedLongLong() const { return this->ToUnsignedLongLong(nullptr); }
444 vtkTypeInt64 ToTypeInt64(bool* valid) const;
445 vtkTypeInt64 ToTypeInt64() const { return this->ToTypeInt64(nullptr); }
446 vtkTypeUInt64 ToTypeUInt64(bool* valid) const;
447 vtkTypeUInt64 ToTypeUInt64() const { return this->ToTypeUInt64(nullptr); }
449
454
459
470 bool IsEqual(const vtkVariant& other) const;
471
473
503 bool operator==(const vtkVariant& other) const;
504 bool operator!=(const vtkVariant& other) const;
505 bool operator<(const vtkVariant& other) const;
506 bool operator>(const vtkVariant& other) const;
507 bool operator<=(const vtkVariant& other) const;
508 bool operator>=(const vtkVariant& other) const;
510
511 friend VTKCOMMONCORE_EXPORT ostream& operator<<(ostream& os, const vtkVariant& val);
512
513private:
514 template <typename T>
515 T ToNumeric(bool* valid, T* vtkNotUsed(ignored)) const;
516
517 union {
519 float Float;
520 double Double;
521 char Char;
522 unsigned char UnsignedChar;
523 signed char SignedChar;
524 short Short;
525 unsigned short UnsignedShort;
526 int Int;
527 unsigned int UnsignedInt;
528 long Long;
529 unsigned long UnsignedLong;
530 long long LongLong;
531 unsigned long long UnsignedLongLong;
533 } Data;
534
535 bool Valid;
536 unsigned int Type;
537
538 friend struct vtkVariantLessThan;
539 friend struct vtkVariantEqual;
542};
543
544VTK_ABI_NAMESPACE_END
545#include "vtkVariantInlineOperators.h" // needed for operator== and company
546
547// A STL-style function object so you can compare two variants using
548// comp(s1,s2) where comp is an instance of vtkVariantStrictWeakOrder.
549// This is a faster version of operator< that makes no attempt to
550// compare values. It satisfies the STL requirement for a comparison
551// function for ordered containers like map and set.
552
553VTK_ABI_NAMESPACE_BEGIN
554struct VTKCOMMONCORE_EXPORT vtkVariantLessThan
555{
556public:
557 bool operator()(const vtkVariant& s1, const vtkVariant& s2) const;
558};
559
560struct VTKCOMMONCORE_EXPORT vtkVariantEqual
561{
562public:
563 bool operator()(const vtkVariant& s1, const vtkVariant& s2) const;
564};
565
566struct VTKCOMMONCORE_EXPORT vtkVariantStrictWeakOrder
567{
568public:
569 bool operator()(const vtkVariant& s1, const vtkVariant& s2) const;
570};
571
572// Similarly, this is a fast version of operator== that requires that
573// the types AND the values be equal in order to admit equality.
574
575struct VTKCOMMONCORE_EXPORT vtkVariantStrictEquality
576{
577public:
578 bool operator()(const vtkVariant& s1, const vtkVariant& s2) const;
579};
580
581VTK_ABI_NAMESPACE_END
582#endif
583// VTK-HeaderTest-Exclude: vtkVariant.h
Abstract superclass for all arrays.
abstract base class for most VTK objects
Wrapper around std::string to keep symbols short.
A type representing the union of many types.
Definition vtkVariant.h:162
vtkTypeInt64 ToTypeInt64() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition vtkVariant.h:445
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 & operator=(const vtkVariant &other)
Copy the value of one variant into another.
~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:423
unsigned long long UnsignedLongLong
Definition vtkVariant.h:531
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:441
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:425
vtkVariant(unsigned int value)
Create an unsigned integer variant.
vtkVariant(char value)
Create a char variant.
unsigned short UnsignedShort
Definition vtkVariant.h:525
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:524
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:437
const char * GetTypeAsString() const
Get the type of the variant as a string.
signed char ToSignedChar() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition vtkVariant.h:427
double ToDouble() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition vtkVariant.h:421
bool IsInt() const
Get whether the variant is an int.
float Float
Definition vtkVariant.h:519
vtkObjectBase * ToVTKObject() const
Return the VTK object, or nullptr if not of that type.
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.
friend VTKCOMMONCORE_EXPORT ostream & operator<<(ostream &os, const vtkVariant &val)
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:419
long long LongLong
Definition vtkVariant.h:530
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:429
vtkStdString * String
Definition vtkVariant.h:518
vtkVariant(long long value)
Create a long long variant.
bool IsLongLong() const
Get whether the variant is long long.
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:435
unsigned int UnsignedInt
Definition vtkVariant.h:527
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.
vtkAbstractArray * ToArray() const
Return the array, or nullptr if not of that type.
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:443
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.
vtkTypeInt64 ToTypeInt64(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
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:439
vtkVariant(unsigned char value)
Create an unsigned char variant.
unsigned long UnsignedLong
Definition vtkVariant.h:529
unsigned char UnsignedChar
Definition vtkVariant.h:522
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:431
bool IsEqual(const vtkVariant &other) const
Determines whether two variants have the same value.
double Double
Definition vtkVariant.h:520
vtkObjectBase * VTKObject
Definition vtkVariant.h:532
signed char SignedChar
Definition vtkVariant.h:523
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:433
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:447
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.
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)