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