VTK  9.4.20241226
vtkVariantInlineOperators.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-License-Identifier: BSD-3-Clause
3
4#ifndef vtkVariantInlineOperators_h
5#define vtkVariantInlineOperators_h
6
7#include "vtkABINamespace.h"
8
9#include <climits>
10
11// ----------------------------------------------------------------------
12
13// First we have several helper functions that will determine what
14// type we're actually dealing with. With any luck the compiler will
15// inline these so they have very little overhead.
16
17VTK_ABI_NAMESPACE_BEGIN
18inline bool IsSigned64Bit(int VariantType)
19{
20#if VTK_LONG_LONG == VTK_TYPE_INT64
21 return (VariantType == VTK_TYPE_INT64);
22#else
23 return ((VariantType == VTK_LONG_LONG) || (VariantType == VTK_TYPE_INT64));
24#endif
25}
26
27inline bool IsSigned(int VariantType)
28{
29#if (CHAR_MIN == SCHAR_MIN && CHAR_MAX == SCHAR_MAX)
30 // the char type is signed on this compiler
31 return ((VariantType == VTK_CHAR) || (VariantType == VTK_SIGNED_CHAR) ||
32 (VariantType == VTK_SHORT) || (VariantType == VTK_INT) || (VariantType == VTK_LONG) ||
33 (VariantType == VTK_ID_TYPE) || IsSigned64Bit(VariantType));
34#else
35 // char is unsigned
36 return ((VariantType == VTK_SIGNED_CHAR) || (VariantType == VTK_SHORT) ||
37 (VariantType == VTK_INT) || (VariantType == VTK_LONG) || (VariantType == VTK_ID_TYPE) ||
38 IsSigned64Bit(VariantType));
39#endif
40}
41
42// ----------------------------------------------------------------------
43
44inline bool IsFloatingPoint(int VariantType)
45{
46 return ((VariantType == VTK_FLOAT) || (VariantType == VTK_DOUBLE));
47}
48
49// ----------------------------------------------------------------------
50
52 const vtkVariant& SignedVariant, const vtkVariant& UnsignedVariant)
53{
54 // If the signed value is less than zero then they cannot possibly
55 // be equal.
56 vtkTypeInt64 A = SignedVariant.ToTypeInt64();
57 return (A >= 0) && (A == UnsignedVariant.ToTypeInt64());
58}
59
60// ----------------------------------------------------------------------
61
63 const vtkVariant& SignedVariant, const vtkVariant& UnsignedVariant)
64{
65 vtkTypeInt64 A = SignedVariant.ToTypeInt64();
66 return ((A < 0) || (static_cast<vtkTypeUInt64>(A) < UnsignedVariant.ToTypeUInt64()));
67}
68
69// ----------------------------------------------------------------------
70
72 const vtkVariant& UnsignedVariant, const vtkVariant& SignedVariant)
73{
74 vtkTypeInt64 B = SignedVariant.ToTypeInt64();
75 return ((B > 0) && (UnsignedVariant.ToTypeUInt64() < static_cast<vtkTypeUInt64>(B)));
76}
77
78// ----------------------------------------------------------------------
79
80inline bool CompareSignedLessThan(const vtkVariant& A, const vtkVariant& B)
81{
82 return (A.ToTypeInt64() < B.ToTypeInt64());
83}
84
85// ----------------------------------------------------------------------
86
87inline bool CompareUnsignedLessThan(const vtkVariant& A, const vtkVariant& B)
88{
89 return (A.ToTypeUInt64() < B.ToTypeUInt64());
90}
91
92// ----------------------------------------------------------------------
93
94inline bool vtkVariant::operator==(const vtkVariant& other) const
95{
96 // First test: nullptr values are always equal to one another and
97 // unequal to anything else.
98 if (!(this->Valid && other.Valid))
99 {
100 return (!(this->Valid || other.Valid));
101 }
102
103 // Second test: VTK objects can only be compared with other VTK
104 // objects.
105 if ((this->Type == VTK_OBJECT) || (other.Type == VTK_OBJECT))
106 {
107 return ((this->Type == VTK_OBJECT) && (other.Type == VTK_OBJECT) &&
108 (this->Data.VTKObject == other.Data.VTKObject));
109 }
110
111 // Third test: the STRING type dominates all else. If either item
112 // is a string then they must both be compared as strings.
113 if ((this->Type == VTK_STRING) || (other.Type == VTK_STRING))
114 {
115 return (this->ToString() == other.ToString());
116 }
117
118 // Fifth: floating point dominates integer types.
119 // Demote to the lowest-floating-point precision for the comparison.
120 // This effectively makes the lower-precision number an interval
121 // corresponding to the range of double values that get rounded to
122 // that float. Otherwise, comparisons of numbers that cannot fit in
123 // the smaller mantissa exactly will never be equal to their
124 // corresponding higher-precision representations.
125 if (this->Type == VTK_FLOAT || other.Type == VTK_FLOAT)
126 {
127 return this->ToFloat() == other.ToFloat();
128 }
129 else if (this->Type == VTK_DOUBLE || other.Type == VTK_DOUBLE)
130 {
131 return (this->ToDouble() == other.ToDouble());
132 }
133
134 // Sixth: we must be comparing integers.
135
136 // 6A: catch signed/unsigned comparison. If the signed object is
137 // less than zero then they cannot be equal.
138 bool thisSigned = IsSigned(this->Type);
139 bool otherSigned = IsSigned(other.Type);
140
141 if (thisSigned ^ otherSigned)
142 {
143 if (thisSigned)
144 {
145 return CompareSignedUnsignedEqual(*this, other);
146 }
147 else
148 {
149 return CompareSignedUnsignedEqual(other, *this);
150 }
151 }
152 else // 6B: both are signed or both are unsigned. In either event
153 // all we have to do is check whether the bit patterns are
154 // equal.
155 {
156 return (this->ToTypeInt64() == other.ToTypeInt64());
157 }
158}
159
160// ----------------------------------------------------------------------
161
162inline bool vtkVariant::operator<(const vtkVariant& other) const
163{
164 // First test: a nullptr value is less than anything except another
165 // nullptr value. unequal to anything else.
166 if (!(this->Valid && other.Valid))
167 {
168 return ((!this->Valid) && (other.Valid));
169 }
170
171 // Second test: VTK objects can only be compared with other VTK
172 // objects.
173 if ((this->Type == VTK_OBJECT) || (other.Type == VTK_OBJECT))
174 {
175 return ((this->Type == VTK_OBJECT) && (other.Type == VTK_OBJECT) &&
176 (this->Data.VTKObject < other.Data.VTKObject));
177 }
178
179 // Third test: the STRING type dominates all else. If either item
180 // is a string then they must both be compared as strings.
181 if ((this->Type == VTK_STRING) || (other.Type == VTK_STRING))
182 {
183 return (this->ToString() < other.ToString());
184 }
185
186 // Fourth: floating point dominates integer types.
187 // Demote to the lowest-floating-point precision for the comparison.
188 // This effectively makes the lower-precision number an interval
189 // corresponding to the range of double values that get rounded to
190 // that float. Otherwise, comparisons of numbers that cannot fit in
191 // the smaller mantissa exactly will never be equal to their
192 // corresponding higher-precision representations.
193 if (this->Type == VTK_FLOAT || other.Type == VTK_FLOAT)
194 {
195 return this->ToFloat() < other.ToFloat();
196 }
197 else if (this->Type == VTK_DOUBLE || other.Type == VTK_DOUBLE)
198 {
199 return (this->ToDouble() < other.ToDouble());
200 }
201
202 // Fifth: we must be comparing integers.
203
204 // 5A: catch signed/unsigned comparison. If the signed object is
205 // less than zero then they cannot be equal.
206 bool thisSigned = IsSigned(this->Type);
207 bool otherSigned = IsSigned(other.Type);
208
209 if (thisSigned ^ otherSigned)
210 {
211 if (thisSigned)
212 {
213 return CompareSignedUnsignedLessThan(*this, other);
214 }
215 else
216 {
217 return CompareUnsignedSignedLessThan(*this, other);
218 }
219 }
220 else if (thisSigned)
221 {
222 return CompareSignedLessThan(*this, other);
223 }
224 else
225 {
226 return CompareUnsignedLessThan(*this, other);
227 }
228}
229
230// ----------------------------------------------------------------------
231
232// Below this point are operators defined in terms of other operators.
233// Again, this may sacrifice some speed, but reduces the chance of
234// inconsistent behavior.
235
236// ----------------------------------------------------------------------
237
238inline bool vtkVariant::operator!=(const vtkVariant& other) const
239{
240 return !(this->operator==(other));
241}
242
243inline bool vtkVariant::operator>(const vtkVariant& other) const
244{
245 return (!(this->operator==(other) || this->operator<(other)));
246}
247
248inline bool vtkVariant::operator<=(const vtkVariant& other) const
249{
250 return (this->operator==(other) || this->operator<(other));
251}
252
253inline bool vtkVariant::operator>=(const vtkVariant& other) const
254{
255 return (!this->operator<(other));
256}
257
258VTK_ABI_NAMESPACE_END
259#endif
260// VTK-HeaderTest-Exclude: vtkVariantInlineOperators.h
A type representing the union of many types.
Definition vtkVariant.h:162
vtkTypeInt64 ToTypeInt64() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition vtkVariant.h:445
double ToDouble(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
vtkTypeUInt64 ToTypeUInt64(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
bool operator==(const vtkVariant &other) const
Compare two variants for equality, greater than, and less than.
bool operator>(const vtkVariant &other) const
Compare two variants for equality, greater than, and less than.
double ToDouble() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition vtkVariant.h:421
bool operator<=(const vtkVariant &other) const
Compare two variants for equality, greater than, and less than.
bool operator!=(const vtkVariant &other) const
Compare two variants for equality, greater than, and less than.
float ToFloat() const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
Definition vtkVariant.h:419
vtkStdString ToString(int formatting=DEFAULT_FORMATTING, int precision=6) const
Convert the variant to a string.
float ToFloat(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
bool operator>=(const vtkVariant &other) const
Compare two variants for equality, greater than, and less than.
bool operator<(const vtkVariant &other) const
Compare two variants for equality, greater than, and less than.
vtkTypeInt64 ToTypeInt64(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
vtkObjectBase * VTKObject
Definition vtkVariant.h:533
#define VTK_SHORT
Definition vtkType.h:36
#define VTK_OBJECT
Definition vtkType.h:56
#define VTK_LONG_LONG
Definition vtkType.h:51
#define VTK_DOUBLE
Definition vtkType.h:43
#define VTK_INT
Definition vtkType.h:38
#define VTK_SIGNED_CHAR
Definition vtkType.h:34
#define VTK_STRING
Definition vtkType.h:48
#define VTK_FLOAT
Definition vtkType.h:42
#define VTK_CHAR
Definition vtkType.h:33
#define VTK_LONG
Definition vtkType.h:40
#define VTK_ID_TYPE
Definition vtkType.h:44
bool CompareSignedUnsignedLessThan(const vtkVariant &SignedVariant, const vtkVariant &UnsignedVariant)
bool CompareSignedUnsignedEqual(const vtkVariant &SignedVariant, const vtkVariant &UnsignedVariant)
bool CompareUnsignedLessThan(const vtkVariant &A, const vtkVariant &B)
bool IsFloatingPoint(int VariantType)
bool CompareSignedLessThan(const vtkVariant &A, const vtkVariant &B)
bool IsSigned(int VariantType)
bool CompareUnsignedSignedLessThan(const vtkVariant &UnsignedVariant, const vtkVariant &SignedVariant)
bool IsSigned64Bit(int VariantType)