VTK  9.0.1
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 -------------------------------------------------------------------------*/
31 #ifndef vtkVariant_h
32 #define vtkVariant_h
33 
34 #include "vtkCommonCoreModule.h" // For export macro
35 #include "vtkObject.h" // For vtkObject's warning support
36 #include "vtkSetGet.h" // For vtkNotUsed macro
37 #include "vtkStdString.h"
38 #include "vtkSystemIncludes.h" // To define ostream
39 #include "vtkType.h" // To define type IDs and VTK_TYPE_USE_* flags
40 #include "vtkUnicodeString.h"
41 
42 //
43 // The following should be eventually placed in vtkSetGet.h
44 //
45 
46 // This is same as extended template macro with an additional case for VTK_VARIANT
47 #define vtkExtraExtendedTemplateMacro(call) \
48  vtkExtendedTemplateMacro(call); \
49  vtkTemplateMacroCase(VTK_VARIANT, vtkVariant, call)
50 
51 // This is same as Iterator Template macro with an additional case for VTK_VARIANT
52 #define vtkExtendedArrayIteratorTemplateMacro(call) \
53  vtkArrayIteratorTemplateMacro(call); \
54  vtkArrayIteratorTemplateMacroCase(VTK_VARIANT, vtkVariant, call)
55 
56 class vtkStdString;
57 class vtkUnicodeString;
58 class vtkObjectBase;
59 class vtkAbstractArray;
60 class vtkVariant;
61 struct vtkVariantLessThan;
62 
63 VTKCOMMONCORE_EXPORT ostream& operator<<(ostream& os, const vtkVariant& val);
64 
65 class VTKCOMMONCORE_EXPORT vtkVariant
66 {
67 public:
72 
77 
81  vtkVariant(const vtkVariant& other);
82 
87 
92 
96  vtkVariant(unsigned char value);
97 
101  vtkVariant(signed char value);
102 
107 
111  vtkVariant(unsigned short value);
112 
117 
121  vtkVariant(unsigned int value);
122 
127 
131  vtkVariant(unsigned long value);
132 
136  vtkVariant(long long value);
137 
141  vtkVariant(unsigned long long value);
142 
147 
151  vtkVariant(double value);
152 
156  vtkVariant(const char* value);
157 
162 
167 
172 
176  vtkVariant(const vtkVariant& other, unsigned int type);
177 
182 
186  bool IsValid() const;
187 
191  bool IsString() const;
192 
196  bool IsUnicodeString() const;
197 
201  bool IsNumeric() const;
202 
206  bool IsFloat() const;
207 
211  bool IsDouble() const;
212 
216  bool IsChar() const;
217 
221  bool IsUnsignedChar() const;
222 
226  bool IsSignedChar() const;
227 
231  bool IsShort() const;
232 
236  bool IsUnsignedShort() const;
237 
241  bool IsInt() const;
242 
246  bool IsUnsignedInt() const;
247 
251  bool IsLong() const;
252 
256  bool IsUnsignedLong() const;
257 
261  bool Is__Int64() const;
262 
266  bool IsUnsigned__Int64() const;
267 
271  bool IsLongLong() const;
272 
276  bool IsUnsignedLongLong() const;
277 
281  bool IsVTKObject() const;
282 
286  bool IsArray() const;
287 
291  unsigned int GetType() const;
292 
296  const char* GetTypeAsString() const;
297 
302 
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 
354 
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 {
420  float Float;
421  double Double;
422  char Char;
423  unsigned char UnsignedChar;
424  signed char SignedChar;
425  short Short;
426  unsigned short UnsignedShort;
427  int Int;
428  unsigned int UnsignedInt;
429  long Long;
430  unsigned long UnsignedLong;
431  long long LongLong;
432  unsigned long long UnsignedLongLong;
434  } Data;
435 
436  unsigned char Valid;
437  unsigned char Type;
438 
439  friend struct vtkVariantLessThan;
440  friend struct vtkVariantEqual;
443 };
444 
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 struct VTKCOMMONCORE_EXPORT vtkVariantLessThan
454 {
455 public:
456  bool operator()(const vtkVariant& s1, const vtkVariant& s2) const;
457 };
458 
459 struct VTKCOMMONCORE_EXPORT vtkVariantEqual
460 {
461 public:
462  bool operator()(const vtkVariant& s1, const vtkVariant& s2) const;
463 };
464 
465 struct VTKCOMMONCORE_EXPORT vtkVariantStrictWeakOrder
466 {
467 public:
468  bool operator()(const vtkVariant& s1, const vtkVariant& s2) const;
469 };
470 
471 // Similarly, this is a fast version of operator== that requires that
472 // the types AND the values be equal in order to admit equality.
473 
474 struct VTKCOMMONCORE_EXPORT vtkVariantStrictEquality
475 {
476 public:
477  bool operator()(const vtkVariant& s1, const vtkVariant& s2) const;
478 };
479 
480 #endif
481 // VTK-HeaderTest-Exclude: vtkVariant.h
Abstract superclass for all arrays.
abstract base class for most VTK objects
Definition: vtkObjectBase.h:64
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:35
String class that stores Unicode text.
A atomic type representing the union of many types.
Definition: vtkVariant.h:66
bool Is__Int64() const
Legacy.
vtkTypeInt64 ToTypeInt64() const
Definition: vtkVariant.h:345
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
~vtkVariant()
Destruct the variant.
char ToChar() const
Definition: vtkVariant.h:323
unsigned long long UnsignedLongLong
Definition: vtkVariant.h:432
double ToDouble(bool *valid) const
long long ToLongLong() const
Definition: vtkVariant.h:341
vtkTypeUInt64 ToTypeUInt64(bool *valid) const
unsigned short ToUnsignedShort(bool *valid) const
vtkUnicodeString ToUnicodeString() const
convert the variant to a Unicode string.
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
unsigned char ToUnsignedChar() const
Definition: vtkVariant.h:325
vtkVariant(unsigned int value)
Create an unsigned integer variant.
vtkVariant(char value)
Create a char variant.
unsigned short UnsignedShort
Definition: vtkVariant.h:426
friend VTKCOMMONCORE_EXPORT ostream & operator<<(ostream &os, const vtkVariant &val)
char ToChar(bool *valid) const
vtkUnicodeString * UnicodeString
Definition: vtkVariant.h:419
bool IsUnsignedLong() const
Get whether the variant is an unsigned long.
short Short
Definition: vtkVariant.h:425
vtkVariant(int value)
Create an integer variant.
bool IsDouble() const
Get whether the variant is a double.
bool IsUnicodeString() const
Get whether the variant is a Unicode string.
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
Definition: vtkVariant.h:337
signed char ToSignedChar() const
Definition: vtkVariant.h:327
double ToDouble() const
Definition: vtkVariant.h:321
bool IsInt() const
Get whether the variant is an int.
float Float
Definition: vtkVariant.h:420
bool IsUnsignedInt() const
Get whether the variant is an unsigned int.
unsigned long ToUnsignedLong(bool *valid) const
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
Definition: vtkVariant.h:319
long long LongLong
Definition: vtkVariant.h:431
vtkVariant(signed char value)
Create a signed char variant.
vtkVariant(vtkObjectBase *value)
Create a vtkObjectBase variant.
vtkVariant(const vtkUnicodeString &value)
Create a Unicode string variant.
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
Definition: vtkVariant.h:329
vtkStdString * String
Definition: vtkVariant.h:418
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
unsigned int ToUnsignedInt() const
Definition: vtkVariant.h:335
unsigned int UnsignedInt
Definition: vtkVariant.h:428
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
vtkVariant(bool value)
Create a bool variant.
unsigned long long ToUnsignedLongLong() const
Definition: vtkVariant.h:343
long ToLong(bool *valid) const
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
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
vtkVariant(const vtkVariant &other)
Copy constructor.
vtkVariant(const vtkVariant &other, unsigned int type)
Create a variant of a specific type.
unsigned long ToUnsignedLong() const
Definition: vtkVariant.h:339
vtkVariant(unsigned char value)
Create an unsigned char variant.
vtkStdString ToString() const
Convert the variant to a string.
unsigned long UnsignedLong
Definition: vtkVariant.h:430
unsigned char UnsignedChar
Definition: vtkVariant.h:423
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
Definition: vtkVariant.h:331
bool IsEqual(const vtkVariant &other) const
Determines whether two variants have the same value.
double Double
Definition: vtkVariant.h:421
vtkObjectBase * VTKObject
Definition: vtkVariant.h:433
signed char SignedChar
Definition: vtkVariant.h:424
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
Definition: vtkVariant.h:333
bool IsShort() const
Get whether the variant is an short.
bool IsUnsigned__Int64() const
Legacy.
vtkTypeUInt64 ToTypeUInt64() const
Definition: vtkVariant.h:347
short ToShort(bool *valid) const
vtkVariant(const char *value)
Create a string variant from a const char*.
unsigned long long ToUnsignedLongLong(bool *valid) const
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
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)