VTK-m  2.0
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/Types.h>
14 
15 namespace vtkm
16 {
17 
22 {
23 };
24 
29 {
30 };
31 
35 {
36 };
37 
42 {
43 };
44 
45 namespace internal
46 {
47 
48 template <vtkm::IdComponent numComponents>
49 struct VecTraitsMultipleComponentChooser
50 {
52 };
53 
54 template <>
55 struct VecTraitsMultipleComponentChooser<1>
56 {
58 };
59 
60 } // namespace internal
61 
65 template <class VecType>
67 {
68 #ifdef VTKM_DOXYGEN_ONLY
69  using ComponentType = typename VecType::ComponentType;
74 
81 
86  static constexpr vtkm::IdComponent NUM_COMPONENTS = VecType::NUM_COMPONENTS;
87 
90  static vtkm::IdComponent GetNumberOfComponents(const VecType& vec);
91 
97  using HasMultipleComponents =
99 
107 
110  VTKM_EXEC_CONT static const ComponentType& GetComponent(
111  const typename std::remove_const<VecType>::type& vector,
112  vtkm::IdComponent component);
113  VTKM_EXEC_CONT static ComponentType& GetComponent(
114  typename std::remove_const<VecType>::type& vector,
115  vtkm::IdComponent component);
116 
119  VTKM_EXEC_CONT static void SetComponent(VecType& vector,
120  vtkm::IdComponent component,
121  ComponentType value);
122 
130  template <typename NewComponentType>
131  using ReplaceComponentType = VecTemplate<NewComponentType, N>;
132 
139  template <typename NewComponentType>
140  using ReplaceBaseComponentType = VecTemplate<
142  N>;
143 
146  template <vktm::IdComponent destSize>
147  VTKM_EXEC_CONT static void CopyInto(const VecType& src, vtkm::Vec<ComponentType, destSize>& dest);
148 #endif // VTKM_DOXYGEN_ONLY
149 };
150 
151 namespace detail
152 {
153 
154 template <typename T>
155 struct HasVecTraitsImpl
156 {
157  template <typename A, typename S = typename vtkm::VecTraits<A>::ComponentType>
158  static std::true_type Test(A*);
159 
160  static std::false_type Test(...);
161 
162  using Type = decltype(Test(std::declval<T*>()));
163 };
164 
165 } // namespace detail
166 
175 template <typename T>
176 using HasVecTraits = typename detail::HasVecTraitsImpl<T>::Type;
177 
178 // This partial specialization allows you to define a non-const version of
179 // VecTraits and have it still work for const version.
180 //
181 template <typename T>
183 {
184 };
185 
186 #if defined(VTKM_GCC) && (__GNUC__ <= 5)
187 namespace detail
188 {
189 
190 template <typename NewT, vtkm::IdComponent Size>
191 struct VecReplaceComponentTypeGCC4or5
192 {
193  using type = vtkm::Vec<NewT, Size>;
194 };
195 
196 template <typename T, vtkm::IdComponent Size, typename NewT>
197 struct VecReplaceBaseComponentTypeGCC4or5
198 {
199  using type =
200  vtkm::Vec<typename vtkm::VecTraits<T>::template ReplaceBaseComponentType<NewT>, Size>;
201 };
202 
203 } // namespace detail
204 #endif // GCC Version 4.8
205 
206 template <typename T, vtkm::IdComponent Size>
208 {
210 
216 
223 
226  static constexpr vtkm::IdComponent NUM_COMPONENTS = VecType::NUM_COMPONENTS;
227 
231  static vtkm::IdComponent GetNumberOfComponents(const VecType&) { return NUM_COMPONENTS; }
232 
237  using HasMultipleComponents =
239 
247 
251  static const ComponentType& GetComponent(const VecType& vector, vtkm::IdComponent component)
252  {
253  return vector[component];
254  }
257  {
258  return vector[component];
259  }
260 
263  VTKM_EXEC_CONT static void SetComponent(VecType& vector,
264  vtkm::IdComponent component,
265  ComponentType value)
266  {
267  vector[component] = value;
268  }
269 
277 #if defined(VTKM_GCC) && (__GNUC__ <= 5)
278  // Silly workaround for bug in GCC <= 5
279  template <typename NewComponentType>
280  using ReplaceComponentType =
281  typename detail::VecReplaceComponentTypeGCC4or5<NewComponentType, Size>::type;
282 #else // !GCC <= 5
283  template <typename NewComponentType>
285 #endif
286 
294 #if defined(VTKM_GCC) && (__GNUC__ <= 5)
295  // Silly workaround for bug in GCC <= 5
296  template <typename NewComponentType>
298  typename detail::VecReplaceBaseComponentTypeGCC4or5<T, Size, NewComponentType>::type;
299 #else // !GCC <= 5
300  template <typename NewComponentType>
303  Size>;
304 #endif
305 
309  template <vtkm::IdComponent destSize>
311  {
312  src.CopyInto(dest);
313  }
314 };
315 
316 template <typename T>
318 {
320 
326 
333 
338  {
339  return vector.GetNumberOfComponents();
340  }
341 
351 
359 
363  static const ComponentType& GetComponent(const VecType& vector, vtkm::IdComponent component)
364  {
365  return vector[component];
366  }
369  {
370  return vector[component];
371  }
372 
376  static void SetComponent(VecType& vector, vtkm::IdComponent component, ComponentType value)
377  {
378  vector[component] = value;
379  }
380 
388  template <typename NewComponentType>
390 
397  template <typename NewComponentType>
400 
403  template <vtkm::IdComponent destSize>
405  {
406  src.CopyInto(dest);
407  }
408 };
409 
410 template <typename T>
412 {
414 
420 
427 
432  {
433  return vector.GetNumberOfComponents();
434  }
435 
445 
453 
457  static const ComponentType& GetComponent(const VecType& vector, vtkm::IdComponent component)
458  {
459  return vector[component];
460  }
461 
465  static void SetComponent(VecType& vector, vtkm::IdComponent component, ComponentType value)
466  {
467  vector[component] = value;
468  }
469 
477  template <typename NewComponentType>
479 
486  template <typename NewComponentType>
489 
492  template <vtkm::IdComponent destSize>
494  {
495  src.CopyInto(dest);
496  }
497 };
498 
499 namespace internal
500 {
501 
504 template <typename ScalarType>
505 struct VTKM_NEVER_EXPORT VecTraitsBasic
506 {
507  using ComponentType = ScalarType;
508  using BaseComponentType = ScalarType;
509  static constexpr vtkm::IdComponent NUM_COMPONENTS = 1;
510  using HasMultipleComponents = vtkm::VecTraitsTagSingleComponent;
511  using IsSizeStatic = vtkm::VecTraitsTagSizeStatic;
512 
514  static vtkm::IdComponent GetNumberOfComponents(const ScalarType&) { return 1; }
515 
517  static const ComponentType& GetComponent(const ScalarType& vector, vtkm::IdComponent)
518  {
519  return vector;
520  }
522  static ComponentType& GetComponent(ScalarType& vector, vtkm::IdComponent) { return vector; }
523 
524  VTKM_EXEC_CONT static void SetComponent(ScalarType& vector,
526  ComponentType value)
527  {
528  vector = value;
529  }
530 
531  template <typename NewComponentType>
532  using ReplaceComponentType = NewComponentType;
533 
534  template <typename NewComponentType>
535  using ReplaceBaseComponentType = NewComponentType;
536 
537  template <vtkm::IdComponent destSize>
538  VTKM_EXEC_CONT static void CopyInto(const ScalarType& src, vtkm::Vec<ScalarType, destSize>& dest)
539  {
540  dest[0] = src;
541  }
542 };
543 
544 namespace detail
545 {
546 
547 template <typename T, typename = vtkm::HasVecTraits<T>>
548 struct VTKM_NEVER_EXPORT SafeVecTraitsImpl;
549 
550 template <typename T>
551 struct VTKM_NEVER_EXPORT SafeVecTraitsImpl<T, std::true_type> : vtkm::VecTraits<T>
552 {
553 };
554 
555 template <typename T>
556 struct VTKM_NEVER_EXPORT SafeVecTraitsImpl<T, std::false_type> : vtkm::internal::VecTraitsBasic<T>
557 {
558 };
559 
560 } // namespace detail
561 
576 template <typename T>
577 struct VTKM_NEVER_EXPORT SafeVecTraits : detail::SafeVecTraitsImpl<T>
578 {
579 };
580 
581 } // namespace internal
582 
590 template <typename T, typename U>
592  : public vtkm::internal::VecTraitsBasic<vtkm::Pair<T, U>>
593 {
594 };
595 
596 } // namespace vtkm
597 
598 #define VTKM_BASIC_TYPE_VECTOR(type) \
599  namespace vtkm \
600  { \
601  template <> \
602  struct VTKM_NEVER_EXPORT VecTraits<type> : public vtkm::internal::VecTraitsBasic<type> \
603  { \
604  }; \
605  }
606 
608 
611 
614 VTKM_BASIC_TYPE_VECTOR(signed char)
615 VTKM_BASIC_TYPE_VECTOR(unsigned char)
617 VTKM_BASIC_TYPE_VECTOR(unsigned short)
619 VTKM_BASIC_TYPE_VECTOR(unsigned int)
621 VTKM_BASIC_TYPE_VECTOR(unsigned long)
622 VTKM_BASIC_TYPE_VECTOR(long long)
623 VTKM_BASIC_TYPE_VECTOR(unsigned long long)
624 
625 //#undef VTKM_BASIC_TYPE_VECTOR
626 
627 #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:426
vtkm::VecTraitsTagMultipleComponents
A tag for vectors that are "true" vectors (i.e.
Definition: VecTraits.h:21
vtkm::Vec::CopyInto
VTKM_EXEC_CONT void CopyInto(Vec< T, Size > &dest) const
Definition: Types.h:787
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::VecTraits< vtkm::VecC< T > >::CopyInto
static VTKM_EXEC_CONT void CopyInto(const VecType &src, vtkm::Vec< ComponentType, destSize > &dest)
Converts whatever type this vector is into the standard VTKm Tuple.
Definition: VecTraits.h:404
Types.h
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
vtkm::IdComponent
vtkm::Int32 IdComponent
Represents a component ID (index of component in a vector).
Definition: Types.h:168
vtkm::VecTraits< vtkm::VecC< T > >::SetComponent
static VTKM_EXEC_CONT void SetComponent(VecType &vector, vtkm::IdComponent component, ComponentType value)
Changes the value in a given component of the vector.
Definition: VecTraits.h:376
vtkm::VecTraits< vtkm::Vec< T, Size > >::CopyInto
static VTKM_EXEC_CONT void CopyInto(const VecType &src, vtkm::Vec< ComponentType, destSize > &dest)
Converts whatever type this vector is into the standard VTKm Tuple.
Definition: VecTraits.h:310
vtkm::VecTraits< vtkm::VecCConst< T > >::ComponentType
typename VecType::ComponentType ComponentType
Type of the components in the vector.
Definition: VecTraits.h:419
vtkm::VecTraits< vtkm::VecC< T > >::GetComponent
static const VTKM_EXEC_CONT ComponentType & GetComponent(const VecType &vector, vtkm::IdComponent component)
Returns the value in a given component of the vector.
Definition: VecTraits.h:363
vtkm::HasVecTraits
typename detail::HasVecTraitsImpl< T >::Type HasVecTraits
Determines whether the given type has VecTraits defined.
Definition: VecTraits.h:176
vtkm::Vec::ComponentType
T ComponentType
Definition: Types.h:773
vtkm::VecTraits< vtkm::Vec< T, Size > >::GetComponent
static VTKM_EXEC_CONT ComponentType & GetComponent(VecType &vector, vtkm::IdComponent component)
Definition: VecTraits.h:256
vtkm::VecC::GetNumberOfComponents
VTKM_EXEC_CONT vtkm::IdComponent GetNumberOfComponents() const
Definition: Types.h:1299
vtkm::VecTraits< T >::BaseComponentType
typename vtkm::VecTraits< ComponentType >::BaseComponentType BaseComponentType
Base component type in the vector.
Definition: VecTraits.h:80
vtkm::VecC::ComponentType
T ComponentType
Definition: Types.h:1247
vtkm::VecTraits< vtkm::VecCConst< T > >::GetComponent
static const VTKM_EXEC_CONT ComponentType & GetComponent(const VecType &vector, vtkm::IdComponent component)
Returns the value in a given component of the vector.
Definition: VecTraits.h:457
vtkm::VecTraits< vtkm::VecCConst< T > >::CopyInto
static VTKM_EXEC_CONT void CopyInto(const VecType &src, vtkm::Vec< ComponentType, destSize > &dest)
Converts whatever type this vector is into the standard VTKm Tuple.
Definition: VecTraits.h:493
vtkm::VecTraits< T >::ReplaceComponentType
VecTemplate< NewComponentType, N > ReplaceComponentType
Get a vector of the same type but with a different component.
Definition: VecTraits.h:131
vtkm::VecTraits< vtkm::VecC< T > >::GetNumberOfComponents
static VTKM_EXEC_CONT vtkm::IdComponent GetNumberOfComponents(const VecType &vector)
Number of components in the given vector.
Definition: VecTraits.h:337
vtkm::VecTraits< vtkm::Vec< T, Size > >::GetComponent
static const VTKM_EXEC_CONT ComponentType & GetComponent(const VecType &vector, vtkm::IdComponent component)
Returns the value in a given component of the vector.
Definition: VecTraits.h:251
vtkm::VecTraits< vtkm::VecC< T > >::GetComponent
static VTKM_EXEC_CONT ComponentType & GetComponent(VecType &vector, vtkm::IdComponent component)
Definition: VecTraits.h:368
vtkm::VecTraitsTagSizeVariable
A tag for vectors where the number of components are not determined until run time.
Definition: VecTraits.h:41
vtkm::VecTraits< T >::HasMultipleComponents
typename internal::VecTraitsMultipleComponentChooser< NUM_COMPONENTS >::Type HasMultipleComponents
A tag specifying whether this vector has multiple components (i.e.
Definition: VecTraits.h:98
vtkm::VecTraits< vtkm::Vec< T, Size > >::SetComponent
static VTKM_EXEC_CONT void SetComponent(VecType &vector, vtkm::IdComponent component, ComponentType value)
Changes the value in a given component of the vector.
Definition: VecTraits.h:263
vtkm::VecCConst
A const version of VecC.
Definition: Types.h:1331
vtkm::VecTraits< vtkm::VecCConst< T > >::SetComponent
static VTKM_EXEC_CONT void SetComponent(VecType &vector, vtkm::IdComponent component, ComponentType value)
Changes the value in a given component of the vector.
Definition: VecTraits.h:465
vtkm::VecCConst::GetNumberOfComponents
VTKM_EXEC_CONT vtkm::IdComponent GetNumberOfComponents() const
Definition: Types.h:1393
vtkm::VecTraits< T >::ReplaceBaseComponentType
VecTemplate< typename VecTraits< ComponentType >::template ReplaceBaseComponentType< NewComponentType >, N > ReplaceBaseComponentType
Get a vector of the same type but with a different base component.
Definition: VecTraits.h:142
vtkm::VecTraitsTagSingleComponent
A tag for vectors that are really just scalars (i.e.
Definition: VecTraits.h:28
vtkm::VecCConst::ComponentType
T ComponentType
Definition: Types.h:1341
vtkm::Vec
A short fixed-length array.
Definition: Types.h:767
vtkm::VecTraitsTagSizeStatic
A tag for vectors where the number of components are known at compile time.
Definition: VecTraits.h:34
vtkm::VecTraits< vtkm::VecC< T > >::BaseComponentType
typename vtkm::VecTraits< ComponentType >::BaseComponentType BaseComponentType
Base component type in the vector.
Definition: VecTraits.h:332
vtkm::VecTraits< vtkm::VecC< T > >::ComponentType
typename VecType::ComponentType ComponentType
Type of the components in the vector.
Definition: VecTraits.h:325
vtkm::VecTraits< vtkm::Vec< T, Size > >::GetNumberOfComponents
static VTKM_EXEC_CONT vtkm::IdComponent GetNumberOfComponents(const VecType &)
Number of components in the given vector.
Definition: VecTraits.h:231
vtkm::VecTraits::ComponentType
typename VecType::ComponentType ComponentType
Type of the components in the vector.
Definition: VecTraits.h:73
vtkm::VecTraits< vtkm::Vec< T, Size > >::BaseComponentType
typename vtkm::VecTraits< ComponentType >::BaseComponentType BaseComponentType
Base component type in the vector.
Definition: VecTraits.h:222
VTKM_NEVER_EXPORT
#define VTKM_NEVER_EXPORT
Definition: ExportMacros.h:93
vtkm::VecTraits
The VecTraits class gives several static members that define how to use a given type as a vector.
Definition: VecTraits.h:66
vtkm::VecC
A Vec-like representation for short arrays.
Definition: Types.h:1237
vtkm::VecTraits< vtkm::Vec< T, Size > >::ComponentType
typename VecType::ComponentType ComponentType
Type of the components in the vector.
Definition: VecTraits.h:215
vtkm::Pair
A vtkm::Pair is essentially the same as an STL pair object except that the methods (constructors and ...
Definition: Pair.h:29
vtkm::VecTraits< vtkm::VecCConst< T > >::GetNumberOfComponents
static VTKM_EXEC_CONT vtkm::IdComponent GetNumberOfComponents(const VecType &vector)
Number of components in the given vector.
Definition: VecTraits.h:431
VTKM_BASIC_TYPE_VECTOR
#define VTKM_BASIC_TYPE_VECTOR(type)
Definition: VecTraits.h:598