VTK  9.2.6
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 -------------------------------------------------------------------------*/
37 #ifndef vtkVariant_h
38 #define vtkVariant_h
39 
40 #include "vtkCommonCoreModule.h" // For export macro
41 #include "vtkObject.h" // For vtkObject's warning support
42 #include "vtkSetGet.h" // For vtkNotUsed macro
43 #include "vtkStdString.h"
44 #include "vtkSystemIncludes.h" // To define ostream
45 #include "vtkType.h" // To define type IDs and VTK_TYPE_USE_* flags
46 
47 //
48 // The following should be eventually placed in vtkSetGet.h
49 //
50 
51 // This is same as extended template macro with an additional case for VTK_VARIANT
52 #define vtkExtraExtendedTemplateMacro(call) \
53  vtkExtendedTemplateMacro(call); \
54  vtkTemplateMacroCase(VTK_VARIANT, vtkVariant, call)
55 
56 // This is same as Iterator Template macro with an additional case for VTK_VARIANT
57 #define vtkExtendedArrayIteratorTemplateMacro(call) \
58  vtkArrayIteratorTemplateMacro(call); \
59  vtkArrayIteratorTemplateMacroCase(VTK_VARIANT, vtkVariant, call)
60 
61 class vtkStdString;
62 class vtkObjectBase;
63 class vtkAbstractArray;
64 class vtkVariant;
65 struct vtkVariantLessThan;
66 
67 VTKCOMMONCORE_EXPORT ostream& operator<<(ostream& os, const vtkVariant& val);
68 
69 class VTKCOMMONCORE_EXPORT vtkVariant
70 {
71 public:
75  vtkVariant();
76 
80  ~vtkVariant();
81 
85  vtkVariant(const vtkVariant& other);
86 
90  vtkVariant(bool value);
91 
95  vtkVariant(char value);
96 
100  vtkVariant(unsigned char value);
101 
105  vtkVariant(signed char value);
106 
110  vtkVariant(short value);
111 
115  vtkVariant(unsigned short value);
116 
120  vtkVariant(int value);
121 
125  vtkVariant(unsigned int value);
126 
130  vtkVariant(long value);
131 
135  vtkVariant(unsigned long value);
136 
140  vtkVariant(long long value);
141 
145  vtkVariant(unsigned long long value);
146 
150  vtkVariant(float value);
151 
155  vtkVariant(double value);
156 
160  vtkVariant(const char* value);
161 
165  vtkVariant(vtkStdString value);
166 
170  vtkVariant(vtkObjectBase* value);
171 
175  vtkVariant(const vtkVariant& other, unsigned int type);
176 
180  vtkVariant& operator=(const vtkVariant& other);
181 
185  bool IsValid() const;
186 
190  bool IsString() const;
191 
195  bool IsNumeric() const;
196 
200  bool IsFloat() const;
201 
205  bool IsDouble() const;
206 
210  bool IsChar() const;
211 
215  bool IsUnsignedChar() const;
216 
220  bool IsSignedChar() const;
221 
225  bool IsShort() const;
226 
230  bool IsUnsignedShort() const;
231 
235  bool IsInt() const;
236 
240  bool IsUnsignedInt() const;
241 
245  bool IsLong() const;
246 
250  bool IsUnsignedLong() const;
251 
255  bool IsLongLong() const;
256 
260  bool IsUnsignedLongLong() const;
261 
265  bool IsVTKObject() const;
266 
270  bool IsArray() const;
271 
275  unsigned int GetType() const;
276 
280  const char* GetTypeAsString() const;
281 
283  {
284  DEFAULT_FORMATTING = 0,
285  FIXED_FORMATTING = 1,
286  SCIENTIFIC_FORMATTING = 2
287  };
288 
297  vtkStdString ToString(int formatting = DEFAULT_FORMATTING, int precision = 6) const;
298 
300 
309  float ToFloat(bool* valid) const;
310  float ToFloat() const { return this->ToFloat(nullptr); }
311  double ToDouble(bool* valid) const;
312  double ToDouble() const { return this->ToDouble(nullptr); }
313  char ToChar(bool* valid) const;
314  char ToChar() const { return this->ToChar(nullptr); }
315  unsigned char ToUnsignedChar(bool* valid) const;
316  unsigned char ToUnsignedChar() const { return this->ToUnsignedChar(nullptr); }
317  signed char ToSignedChar(bool* valid) const;
318  signed char ToSignedChar() const { return this->ToSignedChar(nullptr); }
319  short ToShort(bool* valid) const;
320  short ToShort() const { return this->ToShort(nullptr); }
321  unsigned short ToUnsignedShort(bool* valid) const;
322  unsigned short ToUnsignedShort() const { return this->ToUnsignedShort(nullptr); }
323  int ToInt(bool* valid) const;
324  int ToInt() const { return this->ToInt(nullptr); }
325  unsigned int ToUnsignedInt(bool* valid) const;
326  unsigned int ToUnsignedInt() const { return this->ToUnsignedInt(nullptr); }
327  long ToLong(bool* valid) const;
328  long ToLong() const { return this->ToLong(nullptr); }
329  unsigned long ToUnsignedLong(bool* valid) const;
330  unsigned long ToUnsignedLong() const { return this->ToUnsignedLong(nullptr); }
331  long long ToLongLong(bool* valid) const;
332  long long ToLongLong() const { return this->ToLongLong(nullptr); }
333  unsigned long long ToUnsignedLongLong(bool* valid) const;
334  unsigned long long ToUnsignedLongLong() const { return this->ToUnsignedLongLong(nullptr); }
335  vtkTypeInt64 ToTypeInt64(bool* valid) const;
336  vtkTypeInt64 ToTypeInt64() const { return this->ToTypeInt64(nullptr); }
337  vtkTypeUInt64 ToTypeUInt64(bool* valid) const;
338  vtkTypeUInt64 ToTypeUInt64() const { return this->ToTypeUInt64(nullptr); }
340 
344  vtkObjectBase* ToVTKObject() const;
345 
349  vtkAbstractArray* ToArray() const;
350 
361  bool IsEqual(const vtkVariant& other) const;
362 
364 
394  bool operator==(const vtkVariant& other) const;
395  bool operator!=(const vtkVariant& other) const;
396  bool operator<(const vtkVariant& other) const;
397  bool operator>(const vtkVariant& other) const;
398  bool operator<=(const vtkVariant& other) const;
399  bool operator>=(const vtkVariant& other) const;
401 
402  friend VTKCOMMONCORE_EXPORT ostream& operator<<(ostream& os, const vtkVariant& val);
403 
404 private:
405  template <typename T>
406  T ToNumeric(bool* valid, T* vtkNotUsed(ignored)) const;
407 
408  union {
410  float Float;
411  double Double;
412  char Char;
413  unsigned char UnsignedChar;
414  signed char SignedChar;
415  short Short;
416  unsigned short UnsignedShort;
417  int Int;
418  unsigned int UnsignedInt;
419  long Long;
420  unsigned long UnsignedLong;
421  long long LongLong;
422  unsigned long long UnsignedLongLong;
424  } Data;
425 
426  unsigned char Valid;
427  unsigned char Type;
428 
429  friend struct vtkVariantLessThan;
430  friend struct vtkVariantEqual;
433 };
434 
435 #include "vtkVariantInlineOperators.h" // needed for operator== and company
436 
437 // A STL-style function object so you can compare two variants using
438 // comp(s1,s2) where comp is an instance of vtkVariantStrictWeakOrder.
439 // This is a faster version of operator< that makes no attempt to
440 // compare values. It satisfies the STL requirement for a comparison
441 // function for ordered containers like map and set.
442 
443 struct VTKCOMMONCORE_EXPORT vtkVariantLessThan
444 {
445 public:
446  bool operator()(const vtkVariant& s1, const vtkVariant& s2) const;
447 };
448 
449 struct VTKCOMMONCORE_EXPORT vtkVariantEqual
450 {
451 public:
452  bool operator()(const vtkVariant& s1, const vtkVariant& s2) const;
453 };
454 
455 struct VTKCOMMONCORE_EXPORT vtkVariantStrictWeakOrder
456 {
457 public:
458  bool operator()(const vtkVariant& s1, const vtkVariant& s2) const;
459 };
460 
461 // Similarly, this is a fast version of operator== that requires that
462 // the types AND the values be equal in order to admit equality.
463 
464 struct VTKCOMMONCORE_EXPORT vtkVariantStrictEquality
465 {
466 public:
467  bool operator()(const vtkVariant& s1, const vtkVariant& s2) const;
468 };
469 
470 #endif
471 // VTK-HeaderTest-Exclude: vtkVariant.h
short Short
Definition: vtkVariant.h:415
unsigned int ToUnsignedInt() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type...
Definition: vtkVariant.h:326
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:38
Abstract superclass for all arrays.
bool VTKCOMMONDATAMODEL_EXPORT operator!=(vtkEdgeBase e1, vtkEdgeBase e2)
signed char SignedChar
Definition: vtkVariant.h:414
unsigned int UnsignedInt
Definition: vtkVariant.h:418
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:334
float ToFloat() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type...
Definition: vtkVariant.h:310
char ToChar() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type...
Definition: vtkVariant.h:314
double ToDouble() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type...
Definition: vtkVariant.h:312
VTKCOMMONCORE_EXPORT ostream & operator<<(ostream &os, const vtkVariant &val)
bool operator<(const vtkPixelExtent &l, const vtkPixelExtent &r)
unsigned char UnsignedChar
Definition: vtkVariant.h:413
A atomic type representing the union of many types.
Definition: vtkVariant.h:69
unsigned char ToUnsignedChar() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type...
Definition: vtkVariant.h:316
vtkObjectBase * VTKObject
Definition: vtkVariant.h:423
long long LongLong
Definition: vtkVariant.h:421
unsigned short UnsignedShort
Definition: vtkVariant.h:416
short ToShort() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type...
Definition: vtkVariant.h:320
bool VTKCOMMONDATAMODEL_EXPORT operator==(vtkEdgeBase e1, vtkEdgeBase e2)
vtkTypeInt64 ToTypeInt64() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type...
Definition: vtkVariant.h:336
unsigned long UnsignedLong
Definition: vtkVariant.h:420
abstract base class for most VTK objects
Definition: vtkObjectBase.h:73
double Double
Definition: vtkVariant.h:411
float Float
Definition: vtkVariant.h:410
vtkTypeUInt64 ToTypeUInt64() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type...
Definition: vtkVariant.h:338
vtkStdString * String
Definition: vtkVariant.h:409
int ToInt() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type...
Definition: vtkVariant.h:324
unsigned long long UnsignedLongLong
Definition: vtkVariant.h:422
unsigned long ToUnsignedLong() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type...
Definition: vtkVariant.h:330
unsigned short ToUnsignedShort() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type...
Definition: vtkVariant.h:322
signed char ToSignedChar() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type...
Definition: vtkVariant.h:318
long long ToLongLong() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type...
Definition: vtkVariant.h:332
long ToLong() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type...
Definition: vtkVariant.h:328