VTK  9.2.5
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 -------------------------------------------------------------------------*/
126 #ifndef vtkVariant_h
127 #define vtkVariant_h
128 
129 #include "vtkCommonCoreModule.h" // For export macro
130 #include "vtkObject.h" // For vtkObject's warning support
131 #include "vtkSetGet.h" // For vtkNotUsed macro
132 #include "vtkStdString.h"
133 #include "vtkSystemIncludes.h" // To define ostream
134 #include "vtkType.h" // To define type IDs and VTK_TYPE_USE_* flags
135 
136 //
137 // The following should be eventually placed in vtkSetGet.h
138 //
139 
140 // This is same as extended template macro with an additional case for VTK_VARIANT
141 #define vtkExtraExtendedTemplateMacro(call) \
142  vtkExtendedTemplateMacro(call); \
143  vtkTemplateMacroCase(VTK_VARIANT, vtkVariant, call)
144 
145 // This is same as Iterator Template macro with an additional case for VTK_VARIANT
146 #define vtkExtendedArrayIteratorTemplateMacro(call) \
147  vtkArrayIteratorTemplateMacro(call); \
148  vtkArrayIteratorTemplateMacroCase(VTK_VARIANT, vtkVariant, call)
149 
150 class vtkStdString;
151 class vtkObjectBase;
152 class vtkAbstractArray;
153 class vtkVariant;
154 struct vtkVariantLessThan;
155 
156 VTKCOMMONCORE_EXPORT ostream& operator<<(ostream& os, const vtkVariant& val);
157 
158 class VTKCOMMONCORE_EXPORT vtkVariant
159 {
160 public:
165 
170 
174  vtkVariant(const vtkVariant& other);
175 
180 
185 
189  vtkVariant(unsigned char value);
190 
194  vtkVariant(signed char value);
195 
200 
204  vtkVariant(unsigned short value);
205 
210 
214  vtkVariant(unsigned int value);
215 
220 
224  vtkVariant(unsigned long value);
225 
229  vtkVariant(long long value);
230 
234  vtkVariant(unsigned long long value);
235 
240 
244  vtkVariant(double value);
245 
249  vtkVariant(const char* value);
250 
255 
260 
264  vtkVariant(const vtkVariant& other, unsigned int type);
265 
270 
274  bool IsValid() const;
275 
279  bool IsString() const;
280 
284  bool IsNumeric() const;
285 
289  bool IsFloat() const;
290 
294  bool IsDouble() const;
295 
299  bool IsChar() const;
300 
304  bool IsUnsignedChar() const;
305 
309  bool IsSignedChar() const;
310 
314  bool IsShort() const;
315 
319  bool IsUnsignedShort() const;
320 
324  bool IsInt() const;
325 
329  bool IsUnsignedInt() const;
330 
334  bool IsLong() const;
335 
339  bool IsUnsignedLong() const;
340 
344  bool IsLongLong() const;
345 
349  bool IsUnsignedLongLong() const;
350 
354  bool IsVTKObject() const;
355 
359  bool IsArray() const;
360 
364  unsigned int GetType() const;
365 
369  const char* GetTypeAsString() const;
370 
372  {
373  DEFAULT_FORMATTING = 0,
374  FIXED_FORMATTING = 1,
375  SCIENTIFIC_FORMATTING = 2
376  };
377 
386  vtkStdString ToString(int formatting = DEFAULT_FORMATTING, int precision = 6) const;
387 
389 
398  float ToFloat(bool* valid) const;
399  float ToFloat() const { return this->ToFloat(nullptr); }
400  double ToDouble(bool* valid) const;
401  double ToDouble() const { return this->ToDouble(nullptr); }
402  char ToChar(bool* valid) const;
403  char ToChar() const { return this->ToChar(nullptr); }
404  unsigned char ToUnsignedChar(bool* valid) const;
405  unsigned char ToUnsignedChar() const { return this->ToUnsignedChar(nullptr); }
406  signed char ToSignedChar(bool* valid) const;
407  signed char ToSignedChar() const { return this->ToSignedChar(nullptr); }
408  short ToShort(bool* valid) const;
409  short ToShort() const { return this->ToShort(nullptr); }
410  unsigned short ToUnsignedShort(bool* valid) const;
411  unsigned short ToUnsignedShort() const { return this->ToUnsignedShort(nullptr); }
412  int ToInt(bool* valid) const;
413  int ToInt() const { return this->ToInt(nullptr); }
414  unsigned int ToUnsignedInt(bool* valid) const;
415  unsigned int ToUnsignedInt() const { return this->ToUnsignedInt(nullptr); }
416  long ToLong(bool* valid) const;
417  long ToLong() const { return this->ToLong(nullptr); }
418  unsigned long ToUnsignedLong(bool* valid) const;
419  unsigned long ToUnsignedLong() const { return this->ToUnsignedLong(nullptr); }
420  long long ToLongLong(bool* valid) const;
421  long long ToLongLong() const { return this->ToLongLong(nullptr); }
422  unsigned long long ToUnsignedLongLong(bool* valid) const;
423  unsigned long long ToUnsignedLongLong() const { return this->ToUnsignedLongLong(nullptr); }
424  vtkTypeInt64 ToTypeInt64(bool* valid) const;
425  vtkTypeInt64 ToTypeInt64() const { return this->ToTypeInt64(nullptr); }
426  vtkTypeUInt64 ToTypeUInt64(bool* valid) const;
427  vtkTypeUInt64 ToTypeUInt64() const { return this->ToTypeUInt64(nullptr); }
429 
434 
439 
450  bool IsEqual(const vtkVariant& other) const;
451 
453 
483  bool operator==(const vtkVariant& other) const;
484  bool operator!=(const vtkVariant& other) const;
485  bool operator<(const vtkVariant& other) const;
486  bool operator>(const vtkVariant& other) const;
487  bool operator<=(const vtkVariant& other) const;
488  bool operator>=(const vtkVariant& other) const;
490 
491  friend VTKCOMMONCORE_EXPORT ostream& operator<<(ostream& os, const vtkVariant& val);
492 
493 private:
494  template <typename T>
495  T ToNumeric(bool* valid, T* vtkNotUsed(ignored)) const;
496 
497  union {
499  float Float;
500  double Double;
501  char Char;
502  unsigned char UnsignedChar;
503  signed char SignedChar;
504  short Short;
505  unsigned short UnsignedShort;
506  int Int;
507  unsigned int UnsignedInt;
508  long Long;
509  unsigned long UnsignedLong;
510  long long LongLong;
511  unsigned long long UnsignedLongLong;
513  } Data;
514 
515  unsigned char Valid;
516  unsigned char Type;
517 
518  friend struct vtkVariantLessThan;
519  friend struct vtkVariantEqual;
522 };
523 
524 #include "vtkVariantInlineOperators.h" // needed for operator== and company
525 
526 // A STL-style function object so you can compare two variants using
527 // comp(s1,s2) where comp is an instance of vtkVariantStrictWeakOrder.
528 // This is a faster version of operator< that makes no attempt to
529 // compare values. It satisfies the STL requirement for a comparison
530 // function for ordered containers like map and set.
531 
532 struct VTKCOMMONCORE_EXPORT vtkVariantLessThan
533 {
534 public:
535  bool operator()(const vtkVariant& s1, const vtkVariant& s2) const;
536 };
537 
538 struct VTKCOMMONCORE_EXPORT vtkVariantEqual
539 {
540 public:
541  bool operator()(const vtkVariant& s1, const vtkVariant& s2) const;
542 };
543 
544 struct VTKCOMMONCORE_EXPORT vtkVariantStrictWeakOrder
545 {
546 public:
547  bool operator()(const vtkVariant& s1, const vtkVariant& s2) const;
548 };
549 
550 // Similarly, this is a fast version of operator== that requires that
551 // the types AND the values be equal in order to admit equality.
552 
553 struct VTKCOMMONCORE_EXPORT vtkVariantStrictEquality
554 {
555 public:
556  bool operator()(const vtkVariant& s1, const vtkVariant& s2) const;
557 };
558 
559 #endif
560 // VTK-HeaderTest-Exclude: vtkVariant.h
Abstract superclass for all arrays.
abstract base class for most VTK objects
Definition: vtkObjectBase.h:74
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:108
A atomic type representing the union of many types.
Definition: vtkVariant.h:159
vtkTypeInt64 ToTypeInt64() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition: vtkVariant.h:425
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:403
unsigned long long UnsignedLongLong
Definition: vtkVariant.h:511
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:421
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:405
vtkVariant(unsigned int value)
Create an unsigned integer variant.
vtkVariant(char value)
Create a char variant.
unsigned short UnsignedShort
Definition: vtkVariant.h:505
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:504
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:417
signed char ToSignedChar() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition: vtkVariant.h:407
double ToDouble() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition: vtkVariant.h:401
bool IsInt() const
Get whether the variant is an int.
float Float
Definition: vtkVariant.h:499
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:399
long long LongLong
Definition: vtkVariant.h:510
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:409
vtkStdString * String
Definition: vtkVariant.h:498
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:415
unsigned int UnsignedInt
Definition: vtkVariant.h:507
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:423
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:419
vtkVariant(unsigned char value)
Create an unsigned char variant.
unsigned long UnsignedLong
Definition: vtkVariant.h:509
unsigned char UnsignedChar
Definition: vtkVariant.h:502
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:411
bool IsEqual(const vtkVariant &other) const
Determines whether two variants have the same value.
double Double
Definition: vtkVariant.h:500
vtkObjectBase * VTKObject
Definition: vtkVariant.h:512
signed char SignedChar
Definition: vtkVariant.h:503
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:413
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:427
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)