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-------------------------------------------------------------------------*/
116#ifndef vtkVariant_h
117#define vtkVariant_h
118
119#include "vtkCommonCoreModule.h" // For export macro
120#include "vtkObject.h" // For vtkObject's warning support
121#include "vtkSetGet.h" // For vtkNotUsed macro
122#include "vtkStdString.h"
123#include "vtkSystemIncludes.h" // To define ostream
124#include "vtkType.h" // To define type IDs and VTK_TYPE_USE_* flags
125
126//
127// The following should be eventually placed in vtkSetGet.h
128//
129
130// This is same as extended template macro with an additional case for VTK_VARIANT
131#define vtkExtraExtendedTemplateMacro(call) \
132 vtkExtendedTemplateMacro(call); \
133 vtkTemplateMacroCase(VTK_VARIANT, vtkVariant, call)
134
135// This is same as Iterator Template macro with an additional case for VTK_VARIANT
136#define vtkExtendedArrayIteratorTemplateMacro(call) \
137 vtkArrayIteratorTemplateMacro(call); \
138 vtkArrayIteratorTemplateMacroCase(VTK_VARIANT, vtkVariant, call)
139
140class vtkStdString;
141class vtkObjectBase;
142class vtkAbstractArray;
143class vtkVariant;
144struct vtkVariantLessThan;
145
146VTKCOMMONCORE_EXPORT ostream& operator<<(ostream& os, const vtkVariant& val);
147
148class VTKCOMMONCORE_EXPORT vtkVariant
149{
150public:
155
160
164 vtkVariant(const vtkVariant& other);
165
170
175
179 vtkVariant(unsigned char value);
180
184 vtkVariant(signed char value);
185
190
194 vtkVariant(unsigned short value);
195
200
204 vtkVariant(unsigned int value);
205
210
214 vtkVariant(unsigned long value);
215
219 vtkVariant(long long value);
220
224 vtkVariant(unsigned long long value);
225
230
235
239 vtkVariant(const char* value);
240
245
250
254 vtkVariant(const vtkVariant& other, unsigned int type);
255
260
264 bool IsValid() const;
265
269 bool IsString() const;
270
274 bool IsNumeric() const;
275
279 bool IsFloat() const;
280
284 bool IsDouble() const;
285
289 bool IsChar() const;
290
294 bool IsUnsignedChar() const;
295
299 bool IsSignedChar() const;
300
304 bool IsShort() const;
305
309 bool IsUnsignedShort() const;
310
314 bool IsInt() const;
315
319 bool IsUnsignedInt() const;
320
324 bool IsLong() const;
325
329 bool IsUnsignedLong() const;
330
334 bool IsLongLong() const;
335
339 bool IsUnsignedLongLong() const;
340
344 bool IsVTKObject() const;
345
349 bool IsArray() const;
350
354 unsigned int GetType() const;
355
359 const char* GetTypeAsString() const;
360
362 {
363 DEFAULT_FORMATTING = 0,
364 FIXED_FORMATTING = 1,
365 SCIENTIFIC_FORMATTING = 2
366 };
367
376 vtkStdString ToString(int formatting = DEFAULT_FORMATTING, int precision = 6) const;
377
379
388 float ToFloat(bool* valid) const;
389 float ToFloat() const { return this->ToFloat(nullptr); }
390 double ToDouble(bool* valid) const;
391 double ToDouble() const { return this->ToDouble(nullptr); }
392 char ToChar(bool* valid) const;
393 char ToChar() const { return this->ToChar(nullptr); }
394 unsigned char ToUnsignedChar(bool* valid) const;
395 unsigned char ToUnsignedChar() const { return this->ToUnsignedChar(nullptr); }
396 signed char ToSignedChar(bool* valid) const;
397 signed char ToSignedChar() const { return this->ToSignedChar(nullptr); }
398 short ToShort(bool* valid) const;
399 short ToShort() const { return this->ToShort(nullptr); }
400 unsigned short ToUnsignedShort(bool* valid) const;
401 unsigned short ToUnsignedShort() const { return this->ToUnsignedShort(nullptr); }
402 int ToInt(bool* valid) const;
403 int ToInt() const { return this->ToInt(nullptr); }
404 unsigned int ToUnsignedInt(bool* valid) const;
405 unsigned int ToUnsignedInt() const { return this->ToUnsignedInt(nullptr); }
406 long ToLong(bool* valid) const;
407 long ToLong() const { return this->ToLong(nullptr); }
408 unsigned long ToUnsignedLong(bool* valid) const;
409 unsigned long ToUnsignedLong() const { return this->ToUnsignedLong(nullptr); }
410 long long ToLongLong(bool* valid) const;
411 long long ToLongLong() const { return this->ToLongLong(nullptr); }
412 unsigned long long ToUnsignedLongLong(bool* valid) const;
413 unsigned long long ToUnsignedLongLong() const { return this->ToUnsignedLongLong(nullptr); }
414 vtkTypeInt64 ToTypeInt64(bool* valid) const;
415 vtkTypeInt64 ToTypeInt64() const { return this->ToTypeInt64(nullptr); }
416 vtkTypeUInt64 ToTypeUInt64(bool* valid) const;
417 vtkTypeUInt64 ToTypeUInt64() const { return this->ToTypeUInt64(nullptr); }
419
424
429
440 bool IsEqual(const vtkVariant& other) const;
441
443
473 bool operator==(const vtkVariant& other) const;
474 bool operator!=(const vtkVariant& other) const;
475 bool operator<(const vtkVariant& other) const;
476 bool operator>(const vtkVariant& other) const;
477 bool operator<=(const vtkVariant& other) const;
478 bool operator>=(const vtkVariant& other) const;
480
481 friend VTKCOMMONCORE_EXPORT ostream& operator<<(ostream& os, const vtkVariant& val);
482
483private:
484 template <typename T>
485 T ToNumeric(bool* valid, T* vtkNotUsed(ignored)) const;
486
487 union {
489 float Float;
490 double Double;
491 char Char;
492 unsigned char UnsignedChar;
493 signed char SignedChar;
494 short Short;
495 unsigned short UnsignedShort;
496 int Int;
497 unsigned int UnsignedInt;
498 long Long;
499 unsigned long UnsignedLong;
500 long long LongLong;
501 unsigned long long UnsignedLongLong;
503 } Data;
504
505 unsigned char Valid;
506 unsigned char Type;
507
508 friend struct vtkVariantLessThan;
509 friend struct vtkVariantEqual;
512};
513
514#include "vtkVariantInlineOperators.h" // needed for operator== and company
515
516// A STL-style function object so you can compare two variants using
517// comp(s1,s2) where comp is an instance of vtkVariantStrictWeakOrder.
518// This is a faster version of operator< that makes no attempt to
519// compare values. It satisfies the STL requirement for a comparison
520// function for ordered containers like map and set.
521
522struct VTKCOMMONCORE_EXPORT vtkVariantLessThan
523{
524public:
525 bool operator()(const vtkVariant& s1, const vtkVariant& s2) const;
526};
527
528struct VTKCOMMONCORE_EXPORT vtkVariantEqual
529{
530public:
531 bool operator()(const vtkVariant& s1, const vtkVariant& s2) const;
532};
533
534struct VTKCOMMONCORE_EXPORT vtkVariantStrictWeakOrder
535{
536public:
537 bool operator()(const vtkVariant& s1, const vtkVariant& s2) const;
538};
539
540// Similarly, this is a fast version of operator== that requires that
541// the types AND the values be equal in order to admit equality.
542
543struct VTKCOMMONCORE_EXPORT vtkVariantStrictEquality
544{
545public:
546 bool operator()(const vtkVariant& s1, const vtkVariant& s2) const;
547};
548
549#endif
550// 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:149
vtkTypeInt64 ToTypeInt64() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition: vtkVariant.h:415
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:393
unsigned long long UnsignedLongLong
Definition: vtkVariant.h:501
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:411
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:395
vtkVariant(unsigned int value)
Create an unsigned integer variant.
vtkVariant(char value)
Create a char variant.
unsigned short UnsignedShort
Definition: vtkVariant.h:495
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:494
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:407
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:397
double ToDouble() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition: vtkVariant.h:391
bool IsInt() const
Get whether the variant is an int.
float Float
Definition: vtkVariant.h:489
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:389
long long LongLong
Definition: vtkVariant.h:500
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:399
vtkStdString * String
Definition: vtkVariant.h:488
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:405
unsigned int UnsignedInt
Definition: vtkVariant.h:497
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:413
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 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:409
vtkVariant(unsigned char value)
Create an unsigned char variant.
unsigned long UnsignedLong
Definition: vtkVariant.h:499
unsigned char UnsignedChar
Definition: vtkVariant.h:492
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:401
bool IsEqual(const vtkVariant &other) const
Determines whether two variants have the same value.
double Double
Definition: vtkVariant.h:490
vtkObjectBase * VTKObject
Definition: vtkVariant.h:502
signed char SignedChar
Definition: vtkVariant.h:493
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:403
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:417
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)