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 #elsif defined(VTK_TYPE_USE_LONG_LONG)
17  return ((VariantType == VTK_LONG_LONG) ||
18  (VariantType == VTK_TYPE_INT64));
19 #elsif 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  if (IsFloatingPoint(this->Type) || IsFloatingPoint(other.Type))
151  {
152  return (this->ToDouble() == other.ToDouble());
153  }
154 
155  // Sixth: we must be comparing integers.
156 
157  // 6A: catch signed/unsigned comparison. If the signed object is
158  // less than zero then they cannot be equal.
159  bool thisSigned = IsSigned(this->Type);
160  bool otherSigned = IsSigned(other.Type);
161 
162  if (thisSigned ^ otherSigned)
163  {
164  if (thisSigned)
165  {
166  return CompareSignedUnsignedEqual(*this, other);
167  }
168  else
169  {
170  return CompareSignedUnsignedEqual(other, *this);
171  }
172  }
173  else // 6B: both are signed or both are unsigned. In either event
174  // all we have to do is check whether the bit patterns are
175  // equal.
176  {
177  return (this->ToTypeInt64() == other.ToTypeInt64());
178  }
179 }
180 
181 // ----------------------------------------------------------------------
182 
183 inline bool
185 {
186  // First test: a NULL value is less than anything except another
187  // NULL value. unequal to anything else.
188  if (! (this->Valid && other.Valid))
189  {
190  return ((!this->Valid) && (other.Valid));
191  }
192 
193  // Second test: VTK objects can only be compared with other VTK
194  // objects.
195  if ((this->Type == VTK_OBJECT) || (other.Type == VTK_OBJECT))
196  {
197  return ((this->Type == VTK_OBJECT) &&
198  (other.Type == VTK_OBJECT) &&
199  (this->Data.VTKObject < other.Data.VTKObject));
200  }
201 
202  // Third test: the STRING type dominates all else. If either item
203  // is a string then they must both be compared as strings.
204  if ((this->Type == VTK_STRING) ||
205  (other.Type == VTK_STRING))
206  {
207  return (this->ToString() < other.ToString());
208  }
209 
210  // Fourth test: the Unicode STRING type dominates all else. If either item
211  // is a unicode string then they must both be compared as strings.
212  if ((this->Type == VTK_UNICODE_STRING) ||
213  (other.Type == VTK_UNICODE_STRING))
214  {
215  return (this->ToUnicodeString() < other.ToUnicodeString());
216  }
217 
218  // Fourth: floating point dominates integer types.
219  if (IsFloatingPoint(this->Type) || IsFloatingPoint(other.Type))
220  {
221  return (this->ToDouble() < other.ToDouble());
222  }
223 
224  // Fifth: we must be comparing integers.
225 
226  // 5A: catch signed/unsigned comparison. If the signed object is
227  // less than zero then they cannot be equal.
228  bool thisSigned = IsSigned(this->Type);
229  bool otherSigned = IsSigned(other.Type);
230 
231  if (thisSigned ^ otherSigned)
232  {
233  if (thisSigned)
234  {
235  return CompareSignedUnsignedLessThan(*this, other);
236  }
237  else
238  {
239  return CompareUnsignedSignedLessThan(*this, other);
240  }
241  }
242  else if (thisSigned)
243  {
244  return CompareSignedLessThan(*this, other);
245  }
246  else
247  {
248  return CompareUnsignedLessThan(*this, other);
249  }
250 }
251 
252 // ----------------------------------------------------------------------
253 
254 // Below this point are operators defined in terms of other operators.
255 // Again, this may sacrifice some speed, but reduces the chance of
256 // inconsistent behavior.
257 
258 
259 // ----------------------------------------------------------------------
260 
261 inline bool
263 {
264  return ! (this->operator==(other));
265 }
266 
267 inline bool
269 {
270  return (!(this->operator==(other) ||
271  this->operator<(other)));
272 }
273 
274 inline bool
276 {
277  return (this->operator==(other) ||
278  this->operator<(other));
279 }
280 
281 inline bool
283 {
284  return (!this->operator<(other));
285 }
286 
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
#define VTK_UNICODE_STRING
Definition: vtkType.h:60
double ToDouble() const
Definition: vtkVariant.h:246
#define VTK_LONG_LONG
Definition: vtkType.h:45
A atomic type representing the union of many types.
Definition: vtkVariant.h:72
vtkObjectBase * VTKObject
Definition: vtkVariant.h:376
#define VTK_STRING
Definition: vtkType.h:41
#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:292
#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