VTK  9.1.0
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 -------------------------------------------------------------------------*/
120 #ifndef vtkVariant_h
121 #define vtkVariant_h
122 
123 #include "vtkCommonCoreModule.h" // For export macro
124 #include "vtkDeprecation.h" // For VTK_DEPRECATED_IN_9_1_0
125 #include "vtkObject.h" // For vtkObject's warning support
126 #include "vtkSetGet.h" // For vtkNotUsed macro
127 #include "vtkStdString.h"
128 #include "vtkSystemIncludes.h" // To define ostream
129 #include "vtkType.h" // To define type IDs and VTK_TYPE_USE_* flags
130 
131 //
132 // The following should be eventually placed in vtkSetGet.h
133 //
134 
135 // This is same as extended template macro with an additional case for VTK_VARIANT
136 #define vtkExtraExtendedTemplateMacro(call) \
137  vtkExtendedTemplateMacro(call); \
138  vtkTemplateMacroCase(VTK_VARIANT, vtkVariant, call)
139 
140 // This is same as Iterator Template macro with an additional case for VTK_VARIANT
141 #define vtkExtendedArrayIteratorTemplateMacro(call) \
142  vtkArrayIteratorTemplateMacro(call); \
143  vtkArrayIteratorTemplateMacroCase(VTK_VARIANT, vtkVariant, call)
144 
145 class vtkStdString;
146 class vtkUnicodeString;
147 class vtkObjectBase;
148 class vtkAbstractArray;
149 class vtkVariant;
150 struct vtkVariantLessThan;
151 
152 VTKCOMMONCORE_EXPORT ostream& operator<<(ostream& os, const vtkVariant& val);
153 
154 class VTKCOMMONCORE_EXPORT vtkVariant
155 {
156 public:
161 
166 
170  vtkVariant(const vtkVariant& other);
171 
176 
181 
185  vtkVariant(unsigned char value);
186 
190  vtkVariant(signed char value);
191 
196 
200  vtkVariant(unsigned short value);
201 
206 
210  vtkVariant(unsigned int value);
211 
216 
220  vtkVariant(unsigned long value);
221 
225  vtkVariant(long long value);
226 
230  vtkVariant(unsigned long long value);
231 
236 
240  vtkVariant(double value);
241 
245  vtkVariant(const char* value);
246 
251 
255  VTK_DEPRECATED_IN_9_1_0("Use vtkVariant(vtkStdString value)")
257 
262 
266  vtkVariant(const vtkVariant& other, unsigned int type);
267 
271  vtkVariant& operator=(const vtkVariant& other);
272 
276  bool IsValid() const;
277 
281  bool IsString() const;
282 
286  VTK_DEPRECATED_IN_9_1_0("Use bool IsString() const")
287  bool IsUnicodeString() const;
288 
292  bool IsNumeric() const;
293 
297  bool IsFloat() const;
298 
302  bool IsDouble() const;
303 
307  bool IsChar() const;
308 
312  bool IsUnsignedChar() const;
313 
317  bool IsSignedChar() const;
318 
322  bool IsShort() const;
323 
327  bool IsUnsignedShort() const;
328 
332  bool IsInt() const;
333 
337  bool IsUnsignedInt() const;
338 
342  bool IsLong() const;
343 
347  bool IsUnsignedLong() const;
348 
352  VTK_DEPRECATED_IN_9_1_0("Legacy. Returns false. The variant is never an __int64")
353  bool Is__Int64() const;
354 
358  VTK_DEPRECATED_IN_9_1_0("Legacy. Returns false. The variant is never an unsigned __int64")
359  bool IsUnsigned__Int64() 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 
417  "Use vtkStdString ToString(int formatting = DEFAULT_FORMATTING, int precision = 6)")
418  vtkUnicodeString ToUnicodeString(int formatting = DEFAULT_FORMATTING, int precision = 6) const;
419 
421 
430  float ToFloat(bool* valid) const;
431  float ToFloat() const { return this->ToFloat(nullptr); }
432  double ToDouble(bool* valid) const;
433  double ToDouble() const { return this->ToDouble(nullptr); }
434  char ToChar(bool* valid) const;
435  char ToChar() const { return this->ToChar(nullptr); }
436  unsigned char ToUnsignedChar(bool* valid) const;
437  unsigned char ToUnsignedChar() const { return this->ToUnsignedChar(nullptr); }
438  signed char ToSignedChar(bool* valid) const;
439  signed char ToSignedChar() const { return this->ToSignedChar(nullptr); }
440  short ToShort(bool* valid) const;
441  short ToShort() const { return this->ToShort(nullptr); }
442  unsigned short ToUnsignedShort(bool* valid) const;
443  unsigned short ToUnsignedShort() const { return this->ToUnsignedShort(nullptr); }
444  int ToInt(bool* valid) const;
445  int ToInt() const { return this->ToInt(nullptr); }
446  unsigned int ToUnsignedInt(bool* valid) const;
447  unsigned int ToUnsignedInt() const { return this->ToUnsignedInt(nullptr); }
448  long ToLong(bool* valid) const;
449  long ToLong() const { return this->ToLong(nullptr); }
450  unsigned long ToUnsignedLong(bool* valid) const;
451  unsigned long ToUnsignedLong() const { return this->ToUnsignedLong(nullptr); }
452  long long ToLongLong(bool* valid) const;
453  long long ToLongLong() const { return this->ToLongLong(nullptr); }
454  unsigned long long ToUnsignedLongLong(bool* valid) const;
455  unsigned long long ToUnsignedLongLong() const { return this->ToUnsignedLongLong(nullptr); }
456  vtkTypeInt64 ToTypeInt64(bool* valid) const;
457  vtkTypeInt64 ToTypeInt64() const { return this->ToTypeInt64(nullptr); }
458  vtkTypeUInt64 ToTypeUInt64(bool* valid) const;
459  vtkTypeUInt64 ToTypeUInt64() const { return this->ToTypeUInt64(nullptr); }
461 
466 
471 
482  bool IsEqual(const vtkVariant& other) const;
483 
485 
515  bool operator==(const vtkVariant& other) const;
516  bool operator!=(const vtkVariant& other) const;
517  bool operator<(const vtkVariant& other) const;
518  bool operator>(const vtkVariant& other) const;
519  bool operator<=(const vtkVariant& other) const;
520  bool operator>=(const vtkVariant& other) const;
522 
523  friend VTKCOMMONCORE_EXPORT ostream& operator<<(ostream& os, const vtkVariant& val);
524 
525 private:
526  template <typename T>
527  T ToNumeric(bool* valid, T* vtkNotUsed(ignored)) const;
528 
529  union {
532  float Float;
533  double Double;
534  char Char;
535  unsigned char UnsignedChar;
536  signed char SignedChar;
537  short Short;
538  unsigned short UnsignedShort;
539  int Int;
540  unsigned int UnsignedInt;
541  long Long;
542  unsigned long UnsignedLong;
543  long long LongLong;
544  unsigned long long UnsignedLongLong;
546  } Data;
547 
548  // XXX(9.1): Remove with VTK_DEPRECATED_IN_9_1_0().
549  bool CheckUnicodeStringEqual(const vtkVariant& other) const;
550  bool CheckUnicodeStringLessThan(const vtkVariant& other) const;
551 
552  unsigned char Valid;
553  unsigned char Type;
554 
555  friend struct vtkVariantLessThan;
556  friend struct vtkVariantEqual;
559 };
560 
561 #include "vtkVariantInlineOperators.h" // needed for operator== and company
562 
563 // A STL-style function object so you can compare two variants using
564 // comp(s1,s2) where comp is an instance of vtkVariantStrictWeakOrder.
565 // This is a faster version of operator< that makes no attempt to
566 // compare values. It satisfies the STL requirement for a comparison
567 // function for ordered containers like map and set.
568 
569 struct VTKCOMMONCORE_EXPORT vtkVariantLessThan
570 {
571 public:
572  bool operator()(const vtkVariant& s1, const vtkVariant& s2) const;
573 };
574 
575 struct VTKCOMMONCORE_EXPORT vtkVariantEqual
576 {
577 public:
578  bool operator()(const vtkVariant& s1, const vtkVariant& s2) const;
579 };
580 
581 struct VTKCOMMONCORE_EXPORT vtkVariantStrictWeakOrder
582 {
583 public:
584  bool operator()(const vtkVariant& s1, const vtkVariant& s2) const;
585 };
586 
587 // Similarly, this is a fast version of operator== that requires that
588 // the types AND the values be equal in order to admit equality.
589 
590 struct VTKCOMMONCORE_EXPORT vtkVariantStrictEquality
591 {
592 public:
593  bool operator()(const vtkVariant& s1, const vtkVariant& s2) const;
594 };
595 
596 #endif
597 // VTK-HeaderTest-Exclude: vtkVariant.h
Abstract superclass for all arrays.
abstract base class for most VTK objects
Definition: vtkObjectBase.h:70
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:105
String class that stores Unicode text.
A atomic type representing the union of many types.
Definition: vtkVariant.h:155
vtkTypeInt64 ToTypeInt64() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition: vtkVariant.h:457
vtkVariant(float value)
Create a float variant.
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:435
unsigned long long UnsignedLongLong
Definition: vtkVariant.h:544
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:453
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.
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:437
vtkVariant(unsigned int value)
Create an unsigned integer variant.
vtkVariant(char value)
Create a char variant.
unsigned short UnsignedShort
Definition: vtkVariant.h:538
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.
vtkUnicodeString * UnicodeString
Definition: vtkVariant.h:531
short Short
Definition: vtkVariant.h:537
vtkVariant(int value)
Create an integer variant.
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:449
signed char ToSignedChar() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition: vtkVariant.h:439
double ToDouble() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition: vtkVariant.h:433
float Float
Definition: vtkVariant.h:532
unsigned long ToUnsignedLong(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
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:431
long long LongLong
Definition: vtkVariant.h:543
vtkVariant(signed char value)
Create a signed char variant.
vtkStdString ToString(int formatting=DEFAULT_FORMATTING, int precision=6) const
Convert the variant to a string.
short ToShort() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition: vtkVariant.h:441
vtkStdString * String
Definition: vtkVariant.h:530
vtkVariant(long long value)
Create a long long variant.
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:447
unsigned int UnsignedInt
Definition: vtkVariant.h:540
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:455
long ToLong(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
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.
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.
unsigned long ToUnsignedLong() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition: vtkVariant.h:451
vtkVariant(unsigned char value)
Create an unsigned char variant.
unsigned long UnsignedLong
Definition: vtkVariant.h:542
unsigned char UnsignedChar
Definition: vtkVariant.h:535
vtkVariant(short value)
Create a short variant.
vtkVariant(unsigned short value)
Create an unsigned short variant.
vtkVariant()
Create an invalid variant.
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:443
bool IsEqual(const vtkVariant &other) const
Determines whether two variants have the same value.
double Double
Definition: vtkVariant.h:533
vtkObjectBase * VTKObject
Definition: vtkVariant.h:545
signed char SignedChar
Definition: vtkVariant.h:536
int ToInt() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition: vtkVariant.h:445
vtkTypeUInt64 ToTypeUInt64() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition: vtkVariant.h:459
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.
@ 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
#define VTK_DEPRECATED_IN_9_1_0(reason)
VTKCOMMONCORE_EXPORT bool operator!=(const vtkUnicodeString &lhs, const vtkUnicodeString &rhs)
VTKCOMMONCORE_EXPORT bool operator==(const vtkUnicodeString &lhs, const vtkUnicodeString &rhs)
VTKCOMMONCORE_EXPORT bool operator>(const vtkUnicodeString &lhs, const vtkUnicodeString &rhs)
VTKCOMMONCORE_EXPORT bool operator<(const vtkUnicodeString &lhs, const vtkUnicodeString &rhs)
VTKCOMMONCORE_EXPORT bool operator>=(const vtkUnicodeString &lhs, const vtkUnicodeString &rhs)
VTKCOMMONCORE_EXPORT bool operator<=(const vtkUnicodeString &lhs, const vtkUnicodeString &rhs)
VTKCOMMONCORE_EXPORT ostream & operator<<(ostream &os, const vtkVariant &val)