VTK-m  2.2
VecTraits.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 #ifndef vtk_m_VecTraits_h
11 #define vtk_m_VecTraits_h
12 
13 #include <vtkm/Deprecated.h>
14 #include <vtkm/StaticAssert.h>
15 #include <vtkm/Types.h>
16 
17 namespace vtkm
18 {
19 
24 {
25 };
26 
31 {
32 };
33 
37 {
38 };
39 
44 {
45 };
46 
60 template <class T>
62 {
63  // The base VecTraits should not be used with qualifiers.
64  VTKM_STATIC_ASSERT_MSG((std::is_same<std::remove_pointer_t<std::decay_t<T>>, T>::value),
65  "The base VecTraits should not be used with qualifiers.");
66 
71  using ComponentType = T;
72 
78  using BaseComponentType = T;
79 
85  static constexpr vtkm::IdComponent NUM_COMPONENTS = 1;
86 
94  static constexpr vtkm::IdComponent GetNumberOfComponents(const T&) { return NUM_COMPONENTS; }
95 
106 
114 
117  VTKM_EXEC_CONT static const ComponentType& GetComponent(const T& vector,
118  vtkm::IdComponent vtkmNotUsed(component))
119  {
120  return vector;
121  }
124  vtkm::IdComponent vtkmNotUsed(component))
125  {
126  return vector;
127  }
128 
131  VTKM_EXEC_CONT static void SetComponent(T& vector,
132  vtkm::IdComponent vtkmNotUsed(component),
133  ComponentType value)
134  {
135  vector = value;
136  }
137 
145  // Note: the `%` in the code samples above is a hint to doxygen to avoid attempting
146  // to link to the object (i.e. `ReplaceBaseComponentType`), which results in a warning.
147  // The `%` is removed from the doxygen text.
148  template <typename NewComponentType>
149  using ReplaceComponentType = NewComponentType;
150 
157  // Note: the `%` in the code samples above is a hint to doxygen to avoid attempting
158  // to link to the object (i.e. `ReplaceBaseComponentType`), which results in a warning.
159  // The `%` is removed from the doxygen text.
160  template <typename NewComponentType>
161  using ReplaceBaseComponentType = NewComponentType;
162 
165  template <vtkm::IdComponent destSize>
167  {
168  dest[0] = src;
169  }
170 };
171 
172 template <typename T>
173 using HasVecTraits VTKM_DEPRECATED(2.1, "All types now have VecTraits defined.") = std::true_type;
174 
175 // These partial specializations allow VecTraits to work with const and reference qualifiers.
176 template <typename T>
178 {
179 };
180 template <typename T>
182 {
183 };
184 template <typename T>
186 {
187 };
188 
189 // This partial specialization allows VecTraits to work with pointers.
190 template <typename T>
192 {
194  {
195  return VecTraits<T>::GetNumberOfComponents(*vector);
196  }
197  VTKM_EXEC_CONT static auto GetComponent(const T* vector, vtkm::IdComponent component)
198  -> decltype(VecTraits<T>::GetComponent(*vector, component))
199  {
200  return VecTraits<T>::GetComponent(*vector, component);
201  }
202  VTKM_EXEC_CONT static auto GetComponent(T* vector, vtkm::IdComponent component)
203  -> decltype(VecTraits<T>::GetComponent(*vector, component))
204  {
205  return VecTraits<T>::GetComponent(*vector, component);
206  }
207  VTKM_EXEC_CONT static void SetComponent(T* vector,
208  vtkm::IdComponent component,
209  typename VecTraits<T>::ComponentType value)
210  {
211  VecTraits<T>::SetComponent(*vector, component, value);
212  }
213  template <typename NewComponentType>
214  using ReplaceComponentType =
216  template <typename NewComponentType>
219  template <vtkm::IdComponent destSize>
221  const T* src,
222  vtkm::Vec<typename VecTraits<T>::ComponentType, destSize>& dest)
223  {
224  VecTraits<T>::CopyInto(*src, dest);
225  }
226 };
227 template <typename T>
229 {
230 };
231 
232 #if defined(VTKM_GCC) && (__GNUC__ <= 5)
233 namespace detail
234 {
235 
236 template <typename NewT, vtkm::IdComponent Size>
237 struct VecReplaceComponentTypeGCC4or5
238 {
239  using type = vtkm::Vec<NewT, Size>;
240 };
241 
242 template <typename T, vtkm::IdComponent Size, typename NewT>
243 struct VecReplaceBaseComponentTypeGCC4or5
244 {
245  using type =
246  vtkm::Vec<typename vtkm::VecTraits<T>::template ReplaceBaseComponentType<NewT>, Size>;
247 };
248 
249 } // namespace detail
250 #endif // GCC Version 4.8
251 
252 namespace internal
253 {
254 
255 template <vtkm::IdComponent numComponents, typename ComponentType>
256 struct VecTraitsMultipleComponentChooser
257 {
259 };
260 
261 template <typename ComponentType>
262 struct VecTraitsMultipleComponentChooser<1, ComponentType>
263 {
265 };
266 
267 } // namespace internal
268 
269 template <typename T, vtkm::IdComponent Size>
271 {
273 
279 
286 
289  static constexpr vtkm::IdComponent NUM_COMPONENTS = VecType::NUM_COMPONENTS;
290 
294  static vtkm::IdComponent GetNumberOfComponents(const VecType&) { return NUM_COMPONENTS; }
295 
300  using HasMultipleComponents =
302 
310 
314  static const ComponentType& GetComponent(const VecType& vector, vtkm::IdComponent component)
315  {
316  return vector[component];
317  }
320  {
321  return vector[component];
322  }
323 
326  VTKM_EXEC_CONT static void SetComponent(VecType& vector,
327  vtkm::IdComponent component,
328  ComponentType value)
329  {
330  vector[component] = value;
331  }
332 
340 #if defined(VTKM_GCC) && (__GNUC__ <= 5)
341  // Silly workaround for bug in GCC <= 5
342  template <typename NewComponentType>
343  using ReplaceComponentType =
344  typename detail::VecReplaceComponentTypeGCC4or5<NewComponentType, Size>::type;
345 #else // !GCC <= 5
346  template <typename NewComponentType>
348 #endif
349 
357 #if defined(VTKM_GCC) && (__GNUC__ <= 5)
358  // Silly workaround for bug in GCC <= 5
359  template <typename NewComponentType>
361  typename detail::VecReplaceBaseComponentTypeGCC4or5<T, Size, NewComponentType>::type;
362 #else // !GCC <= 5
363  template <typename NewComponentType>
366  Size>;
367 #endif
368 
372  template <vtkm::IdComponent destSize>
374  {
375  src.CopyInto(dest);
376  }
377 };
378 
379 template <typename T>
381 {
383 
389 
396 
401  {
402  return vector.GetNumberOfComponents();
403  }
404 
414 
422 
426  static const ComponentType& GetComponent(const VecType& vector, vtkm::IdComponent component)
427  {
428  return vector[component];
429  }
432  {
433  return vector[component];
434  }
435 
439  static void SetComponent(VecType& vector, vtkm::IdComponent component, ComponentType value)
440  {
441  vector[component] = value;
442  }
443 
451  template <typename NewComponentType>
453 
460  template <typename NewComponentType>
463 
466  template <vtkm::IdComponent destSize>
468  {
469  src.CopyInto(dest);
470  }
471 };
472 
473 template <typename T>
475 {
477 
483 
490 
495  {
496  return vector.GetNumberOfComponents();
497  }
498 
508 
516 
520  static const ComponentType& GetComponent(const VecType& vector, vtkm::IdComponent component)
521  {
522  return vector[component];
523  }
524 
528  static void SetComponent(VecType& vector, vtkm::IdComponent component, ComponentType value)
529  {
530  vector[component] = value;
531  }
532 
540  template <typename NewComponentType>
542 
549  template <typename NewComponentType>
552 
555  template <vtkm::IdComponent destSize>
557  {
558  src.CopyInto(dest);
559  }
560 };
561 
562 namespace internal
563 {
564 
567 template <typename ScalarType>
568 struct VTKM_DEPRECATED(2.1, "VecTraitsBasic is now the default implementation for VecTraits.")
569  VTKM_NEVER_EXPORT VecTraitsBasic
570 {
571  using ComponentType = ScalarType;
572  using BaseComponentType = ScalarType;
573  static constexpr vtkm::IdComponent NUM_COMPONENTS = 1;
574  using HasMultipleComponents = vtkm::VecTraitsTagSingleComponent;
575  using IsSizeStatic = vtkm::VecTraitsTagSizeStatic;
576 
578  static vtkm::IdComponent GetNumberOfComponents(const ScalarType&) { return 1; }
579 
581  static const ComponentType& GetComponent(const ScalarType& vector, vtkm::IdComponent)
582  {
583  return vector;
584  }
586  static ComponentType& GetComponent(ScalarType& vector, vtkm::IdComponent) { return vector; }
587 
588  VTKM_EXEC_CONT static void SetComponent(ScalarType& vector,
590  ComponentType value)
591  {
592  vector = value;
593  }
594 
595  template <typename NewComponentType>
596  using ReplaceComponentType = NewComponentType;
597 
598  template <typename NewComponentType>
599  using ReplaceBaseComponentType = NewComponentType;
600 
601  template <vtkm::IdComponent destSize>
602  VTKM_EXEC_CONT static void CopyInto(const ScalarType& src, vtkm::Vec<ScalarType, destSize>& dest)
603  {
604  dest[0] = src;
605  }
606 };
607 
608 template <typename T>
609 struct VTKM_DEPRECATED(2.1 "VecTraits now safe to use on any type.") VTKM_NEVER_EXPORT SafeVecTraits
610  : vtkm::VecTraits<T>
611 {
612 };
613 
614 } // namespace internal
615 
616 namespace detail
617 {
618 
619 struct VTKM_DEPRECATED(2.1,
620  "VTKM_BASIC_TYPE_VECTOR is no longer necessary because VecTraits implements "
621  "basic type by default.") VTKM_BASIC_TYPE_VECTOR_is_deprecated
622 {
623 };
624 
625 template <typename T>
626 struct issue_VTKM_BASIC_TYPE_VECTOR_deprecation_warning;
627 
628 }
629 
630 } // namespace vtkm
631 
632 #define VTKM_BASIC_TYPE_VECTOR(type) \
633  namespace vtkm \
634  { \
635  namespace detail \
636  { \
637  template <> \
638  struct issue_VTKM_BASIC_TYPE_VECTOR_deprecation_warning<type> \
639  : public vtkm::detail::VTKM_BASIC_TYPE_VECTOR_is_deprecated \
640  { \
641  }; \
642  } \
643  }
644 
645 #endif //vtk_m_VecTraits_h
vtkm::VecTraits< vtkm::VecCConst< T > >::BaseComponentType
typename vtkm::VecTraits< ComponentType >::BaseComponentType BaseComponentType
Base component type in the vector.
Definition: VecTraits.h:489
vtkm::VecTraits< vtkm::VecCConst< T > >::GetNumberOfComponents
static vtkm::IdComponent GetNumberOfComponents(const VecType &vector)
Number of components in the given vector.
Definition: VecTraits.h:494
vtkm::VecTraits< vtkm::Vec< T, Size > >::SetComponent
static void SetComponent(VecType &vector, vtkm::IdComponent component, ComponentType value)
Changes the value in a given component of the vector.
Definition: VecTraits.h:326
vtkm::VecTraits< T * >::SetComponent
static void SetComponent(T *vector, vtkm::IdComponent component, typename VecTraits< T >::ComponentType value)
Definition: VecTraits.h:207
vtkm::VecTraits< vtkm::Vec< T, Size > >::GetNumberOfComponents
static vtkm::IdComponent GetNumberOfComponents(const VecType &)
Number of components in the given vector.
Definition: VecTraits.h:294
vtkm::VecTraitsTagMultipleComponents
A tag for vectors that are "true" vectors (i.e.
Definition: VecTraits.h:23
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
Types.h
vtkm::VecTraits< vtkm::Vec< T, Size > >::CopyInto
static void CopyInto(const VecType &src, vtkm::Vec< ComponentType, destSize > &dest)
Converts whatever type this vector is into the standard VTKm Tuple.
Definition: VecTraits.h:373
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
vtkm::IdComponent
vtkm::Int32 IdComponent
Base type to use to index small lists.
Definition: Types.h:194
vtkm::VecTraits::CopyInto
static void CopyInto(const T &src, vtkm::Vec< ComponentType, destSize > &dest)
Copies the components in the given vector into a given Vec object.
Definition: VecTraits.h:166
vtkm::VecTraits< vtkm::VecCConst< T > >::ComponentType
typename VecType::ComponentType ComponentType
Type of the components in the vector.
Definition: VecTraits.h:482
vtkm::VecTraits::ComponentType
T ComponentType
Type of the components in the vector.
Definition: VecTraits.h:71
vtkm::Vec::ComponentType
T ComponentType
Definition: Types.h:814
vtkm::VecC::ComponentType
T ComponentType
Definition: Types.h:1288
vtkm::VecTraits< vtkm::VecC< T > >::GetComponent
static ComponentType & GetComponent(VecType &vector, vtkm::IdComponent component)
Definition: VecTraits.h:431
vtkm::VecTraits::BaseComponentType
T BaseComponentType
Base component type in the vector.
Definition: VecTraits.h:78
vtkm::VecTraits::ReplaceComponentType
NewComponentType ReplaceComponentType
Get a vector of the same type but with a different component.
Definition: VecTraits.h:149
vtkm::VecTraitsTagSizeVariable
A tag for vectors where the number of components are not determined until run time.
Definition: VecTraits.h:43
vtkm::VecTraits::GetNumberOfComponents
static constexpr vtkm::IdComponent GetNumberOfComponents(const T &)
Returns the number of components in the given vector.
Definition: VecTraits.h:94
vtkm::VecTraits< vtkm::VecCConst< T > >::SetComponent
static void SetComponent(VecType &vector, vtkm::IdComponent component, ComponentType value)
Changes the value in a given component of the vector.
Definition: VecTraits.h:528
vtkm::VecTraits< vtkm::VecC< T > >::GetNumberOfComponents
static vtkm::IdComponent GetNumberOfComponents(const VecType &vector)
Number of components in the given vector.
Definition: VecTraits.h:400
vtkm::VecCConst
A const version of VecC.
Definition: Types.h:363
VTKM_STATIC_ASSERT_MSG
#define VTKM_STATIC_ASSERT_MSG(condition, message)
Definition: StaticAssert.h:18
vtkm::VecTraits< vtkm::VecC< T > >::CopyInto
static void CopyInto(const VecType &src, vtkm::Vec< ComponentType, destSize > &dest)
Converts whatever type this vector is into the standard VTKm Tuple.
Definition: VecTraits.h:467
vtkm::VecTraits::GetComponent
static ComponentType & GetComponent(T &vector, vtkm::IdComponent)
Returns the value in a given component of the vector.
Definition: VecTraits.h:123
vtkm::VecTraits::SetComponent
static void SetComponent(T &vector, vtkm::IdComponent, ComponentType value)
Changes the value in a given component of the vector.
Definition: VecTraits.h:131
vtkm::VecTraits< vtkm::VecC< T > >::GetComponent
static const ComponentType & GetComponent(const VecType &vector, vtkm::IdComponent component)
Returns the value in a given component of the vector.
Definition: VecTraits.h:426
vtkm::VecTraits< vtkm::VecCConst< T > >::CopyInto
static void CopyInto(const VecType &src, vtkm::Vec< ComponentType, destSize > &dest)
Converts whatever type this vector is into the standard VTKm Tuple.
Definition: VecTraits.h:556
vtkm::VecCConst::GetNumberOfComponents
constexpr vtkm::IdComponent GetNumberOfComponents() const
Definition: Types.h:1434
vtkm::VecTraits< vtkm::Vec< T, Size > >::GetComponent
static ComponentType & GetComponent(VecType &vector, vtkm::IdComponent component)
Definition: VecTraits.h:319
vtkmNotUsed
#define vtkmNotUsed(parameter_name)
Simple macro to identify a parameter as unused.
Definition: ExportMacros.h:128
vtkm::VecTraits< vtkm::VecCConst< T > >::GetComponent
static const ComponentType & GetComponent(const VecType &vector, vtkm::IdComponent component)
Returns the value in a given component of the vector.
Definition: VecTraits.h:520
vtkm::VecTraitsTagSingleComponent
A tag for vectors that are really just scalars (i.e.
Definition: VecTraits.h:30
vtkm::VecCConst::ComponentType
T ComponentType
Definition: Types.h:1382
vtkm::VecTraits< T * >::GetComponent
static auto GetComponent(const T *vector, vtkm::IdComponent component) -> decltype(VecTraits< T >::GetComponent(*vector, component))
Definition: VecTraits.h:197
vtkm::VecTraits< T * >::GetComponent
static auto GetComponent(T *vector, vtkm::IdComponent component) -> decltype(VecTraits< T >::GetComponent(*vector, component))
Definition: VecTraits.h:202
vtkm::Vec
A short fixed-length array.
Definition: Types.h:357
vtkm::VecTraits< vtkm::VecC< T > >::SetComponent
static void SetComponent(VecType &vector, vtkm::IdComponent component, ComponentType value)
Changes the value in a given component of the vector.
Definition: VecTraits.h:439
vtkm::VecTraits::GetComponent
static const ComponentType & GetComponent(const T &vector, vtkm::IdComponent)
Returns the value in a given component of the vector.
Definition: VecTraits.h:117
vtkm::VecTraitsTagSizeStatic
A tag for vectors where the number of components are known at compile time.
Definition: VecTraits.h:36
vtkm::VecTraits< vtkm::VecC< T > >::BaseComponentType
typename vtkm::VecTraits< ComponentType >::BaseComponentType BaseComponentType
Base component type in the vector.
Definition: VecTraits.h:395
vtkm::VecTraits< vtkm::Vec< T, Size > >::GetComponent
static const ComponentType & GetComponent(const VecType &vector, vtkm::IdComponent component)
Returns the value in a given component of the vector.
Definition: VecTraits.h:314
vtkm::VecTraits< T * >::GetNumberOfComponents
static vtkm::IdComponent GetNumberOfComponents(const T *vector)
Definition: VecTraits.h:193
StaticAssert.h
vtkm::VecTraits< vtkm::VecC< T > >::ComponentType
typename VecType::ComponentType ComponentType
Type of the components in the vector.
Definition: VecTraits.h:388
Deprecated.h
vtkm::Vec::CopyInto
void CopyInto(Vec< T, Size > &dest) const
Definition: Types.h:828
vtkm::VecTraits< vtkm::Vec< T, Size > >::BaseComponentType
typename vtkm::VecTraits< ComponentType >::BaseComponentType BaseComponentType
Base component type in the vector.
Definition: VecTraits.h:285
vtkm::VecTraits< T * >::CopyInto
static void CopyInto(const T *src, vtkm::Vec< typename VecTraits< T >::ComponentType, destSize > &dest)
Definition: VecTraits.h:220
VTKM_NEVER_EXPORT
#define VTKM_NEVER_EXPORT
Definition: ExportMacros.h:90
vtkm::VecTraits
Traits that can be queried to treat any type as a Vec.
Definition: VecTraits.h:61
vtkm::VecC
A Vec-like representation for short arrays.
Definition: Types.h:360
vtkm::VecTraits< vtkm::Vec< T, Size > >::ComponentType
typename VecType::ComponentType ComponentType
Type of the components in the vector.
Definition: VecTraits.h:278
vtkm::VecC::GetNumberOfComponents
constexpr vtkm::IdComponent GetNumberOfComponents() const
Definition: Types.h:1340
vtkm::VecTraits::ReplaceBaseComponentType
NewComponentType ReplaceBaseComponentType
Get a vector of the same type but with a different base component.
Definition: VecTraits.h:161
VTKM_DEPRECATED
#define VTKM_DEPRECATED(...)
Definition: Deprecated.h:145
vtkm::HasVecTraits
std::true_type HasVecTraits
Definition: VecTraits.h:173
vtkm::VecTraits< T * >
Definition: VecTraits.h:191
VTKM_BASIC_TYPE_VECTOR
#define VTKM_BASIC_TYPE_VECTOR(type)
Definition: VecTraits.h:632