Viskores  1.0
VecTraits.h
Go to the documentation of this file.
1 //============================================================================
2 // The contents of this file are covered by the Viskores license. See
3 // LICENSE.txt for details.
4 //
5 // By contributing to this file, all contributors agree to the Developer
6 // Certificate of Origin Version 1.1 (DCO 1.1) as stated in DCO.txt.
7 //============================================================================
8 
9 //============================================================================
10 // Copyright (c) Kitware, Inc.
11 // All rights reserved.
12 // See LICENSE.txt for details.
13 //
14 // This software is distributed WITHOUT ANY WARRANTY; without even
15 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 // PURPOSE. See the above copyright notice for more information.
17 //============================================================================
18 #ifndef viskores_VecTraits_h
19 #define viskores_VecTraits_h
20 
21 #include <viskores/Deprecated.h>
22 #include <viskores/StaticAssert.h>
23 #include <viskores/Types.h>
24 
25 namespace viskores
26 {
27 
32 {
33 };
34 
39 {
40 };
41 
45 {
46 };
47 
52 {
53 };
54 
68 template <class T>
69 struct VISKORES_NEVER_EXPORT VecTraits
70 {
71  // The base VecTraits should not be used with qualifiers.
72  VISKORES_STATIC_ASSERT_MSG((std::is_same<std::remove_pointer_t<std::decay_t<T>>, T>::value),
73  "The base VecTraits should not be used with qualifiers.");
74 
79  using ComponentType = T;
80 
86  using BaseComponentType = T;
87 
93  static constexpr viskores::IdComponent NUM_COMPONENTS = 1;
94 
102  static constexpr viskores::IdComponent GetNumberOfComponents(const T&) { return NUM_COMPONENTS; }
103 
114 
122 
126  const T& vector,
128  {
129  return vector;
130  }
133  T& vector,
135  {
136  return vector;
137  }
138 
141  VISKORES_EXEC_CONT static void SetComponent(T& vector,
143  ComponentType value)
144  {
145  vector = value;
146  }
147 
155  // Note: the `%` in the code samples above is a hint to doxygen to avoid attempting
156  // to link to the object (i.e. `ReplaceBaseComponentType`), which results in a warning.
157  // The `%` is removed from the doxygen text.
158  template <typename NewComponentType>
159  using ReplaceComponentType = NewComponentType;
160 
167  // Note: the `%` in the code samples above is a hint to doxygen to avoid attempting
168  // to link to the object (i.e. `ReplaceBaseComponentType`), which results in a warning.
169  // The `%` is removed from the doxygen text.
170  template <typename NewComponentType>
171  using ReplaceBaseComponentType = NewComponentType;
172 
175  template <viskores::IdComponent destSize>
176  VISKORES_EXEC_CONT static void CopyInto(const T& src,
178  {
179  dest[0] = src;
180  }
181 };
182 
183 template <typename T>
185  "All types now have VecTraits defined.") = std::true_type;
186 
187 // These partial specializations allow VecTraits to work with const and reference qualifiers.
188 template <typename T>
189 struct VISKORES_NEVER_EXPORT VecTraits<const T> : VecTraits<T>
190 {
191 };
192 template <typename T>
193 struct VISKORES_NEVER_EXPORT VecTraits<T&> : VecTraits<T>
194 {
195 };
196 template <typename T>
197 struct VISKORES_NEVER_EXPORT VecTraits<const T&> : VecTraits<T>
198 {
199 };
200 
201 // This partial specialization allows VecTraits to work with pointers.
202 template <typename T>
203 struct VISKORES_NEVER_EXPORT VecTraits<T*> : VecTraits<T>
204 {
206  {
207  return VecTraits<T>::GetNumberOfComponents(*vector);
208  }
209  VISKORES_EXEC_CONT static auto GetComponent(const T* vector, viskores::IdComponent component)
210  -> decltype(VecTraits<T>::GetComponent(*vector, component))
211  {
212  return VecTraits<T>::GetComponent(*vector, component);
213  }
214  VISKORES_EXEC_CONT static auto GetComponent(T* vector, viskores::IdComponent component)
215  -> decltype(VecTraits<T>::GetComponent(*vector, component))
216  {
217  return VecTraits<T>::GetComponent(*vector, component);
218  }
219  VISKORES_EXEC_CONT static void SetComponent(T* vector,
220  viskores::IdComponent component,
221  typename VecTraits<T>::ComponentType value)
222  {
223  VecTraits<T>::SetComponent(*vector, component, value);
224  }
225  template <typename NewComponentType>
226  using ReplaceComponentType =
228  template <typename NewComponentType>
231  template <viskores::IdComponent destSize>
233  const T* src,
234  viskores::Vec<typename VecTraits<T>::ComponentType, destSize>& dest)
235  {
236  VecTraits<T>::CopyInto(*src, dest);
237  }
238 };
239 template <typename T>
240 struct VISKORES_NEVER_EXPORT VecTraits<const T*> : VecTraits<T*>
241 {
242 };
243 
244 #if defined(VISKORES_GCC) && (__GNUC__ <= 5)
245 namespace detail
246 {
247 
248 template <typename NewT, viskores::IdComponent Size>
249 struct VecReplaceComponentTypeGCC4or5
250 {
251  using type = viskores::Vec<NewT, Size>;
252 };
253 
254 template <typename T, viskores::IdComponent Size, typename NewT>
255 struct VecReplaceBaseComponentTypeGCC4or5
256 {
257  using type =
258  viskores::Vec<typename viskores::VecTraits<T>::template ReplaceBaseComponentType<NewT>, Size>;
259 };
260 
261 } // namespace detail
262 #endif // GCC Version 4.8
263 
264 namespace internal
265 {
266 
267 template <viskores::IdComponent numComponents, typename ComponentType>
268 struct VecTraitsMultipleComponentChooser
269 {
271 };
272 
273 template <typename ComponentType>
274 struct VecTraitsMultipleComponentChooser<1, ComponentType>
275 {
277 };
278 
279 } // namespace internal
280 
281 template <typename T, viskores::IdComponent Size>
282 struct VISKORES_NEVER_EXPORT VecTraits<viskores::Vec<T, Size>>
283 {
285 
291 
298 
301  static constexpr viskores::IdComponent NUM_COMPONENTS = VecType::NUM_COMPONENTS;
302 
306  static viskores::IdComponent GetNumberOfComponents(const VecType&) { return NUM_COMPONENTS; }
307 
312  using HasMultipleComponents =
314 
322 
326  static const ComponentType& GetComponent(const VecType& vector, viskores::IdComponent component)
327  {
328  return vector[component];
329  }
332  {
333  return vector[component];
334  }
335 
339  viskores::IdComponent component,
340  ComponentType value)
341  {
342  vector[component] = value;
343  }
344 
352 #if defined(VISKORES_GCC) && (__GNUC__ <= 5)
353  // Silly workaround for bug in GCC <= 5
354  template <typename NewComponentType>
355  using ReplaceComponentType =
356  typename detail::VecReplaceComponentTypeGCC4or5<NewComponentType, Size>::type;
357 #else // !GCC <= 5
358  template <typename NewComponentType>
360 #endif
361 
369 #if defined(VISKORES_GCC) && (__GNUC__ <= 5)
370  // Silly workaround for bug in GCC <= 5
371  template <typename NewComponentType>
373  typename detail::VecReplaceBaseComponentTypeGCC4or5<T, Size, NewComponentType>::type;
374 #else // !GCC <= 5
375  template <typename NewComponentType>
378  NewComponentType>,
379  Size>;
380 #endif
381 
385  template <viskores::IdComponent destSize>
386  VISKORES_EXEC_CONT static void CopyInto(const VecType& src,
388  {
389  src.CopyInto(dest);
390  }
391 };
392 
393 template <typename T>
394 struct VISKORES_NEVER_EXPORT VecTraits<viskores::VecC<T>>
395 {
397 
403 
410 
415  {
416  return vector.GetNumberOfComponents();
417  }
418 
428 
436 
440  static const ComponentType& GetComponent(const VecType& vector, viskores::IdComponent component)
441  {
442  return vector[component];
443  }
446  {
447  return vector[component];
448  }
449 
453  static void SetComponent(VecType& vector, viskores::IdComponent component, ComponentType value)
454  {
455  vector[component] = value;
456  }
457 
465  template <typename NewComponentType>
467 
474  template <typename NewComponentType>
477 
480  template <viskores::IdComponent destSize>
481  VISKORES_EXEC_CONT static void CopyInto(const VecType& src,
483  {
484  src.CopyInto(dest);
485  }
486 };
487 
488 template <typename T>
489 struct VISKORES_NEVER_EXPORT VecTraits<viskores::VecCConst<T>>
490 {
492 
498 
505 
510  {
511  return vector.GetNumberOfComponents();
512  }
513 
523 
531 
535  static const ComponentType& GetComponent(const VecType& vector, viskores::IdComponent component)
536  {
537  return vector[component];
538  }
539 
543  static void SetComponent(VecType& vector, viskores::IdComponent component, ComponentType value)
544  {
545  vector[component] = value;
546  }
547 
555  template <typename NewComponentType>
557 
564  template <typename NewComponentType>
567 
570  template <viskores::IdComponent destSize>
571  VISKORES_EXEC_CONT static void CopyInto(const VecType& src,
573  {
574  src.CopyInto(dest);
575  }
576 };
577 
578 namespace internal
579 {
580 
583 template <typename ScalarType>
584 struct VISKORES_DEPRECATED(2.1, "VecTraitsBasic is now the default implementation for VecTraits.")
585  VISKORES_NEVER_EXPORT VecTraitsBasic
586 {
587  using ComponentType = ScalarType;
588  using BaseComponentType = ScalarType;
589  static constexpr viskores::IdComponent NUM_COMPONENTS = 1;
590  using HasMultipleComponents = viskores::VecTraitsTagSingleComponent;
591  using IsSizeStatic = viskores::VecTraitsTagSizeStatic;
592 
594  static viskores::IdComponent GetNumberOfComponents(const ScalarType&) { return 1; }
595 
597  static const ComponentType& GetComponent(const ScalarType& vector, viskores::IdComponent)
598  {
599  return vector;
600  }
602  static ComponentType& GetComponent(ScalarType& vector, viskores::IdComponent) { return vector; }
603 
604  VISKORES_EXEC_CONT static void SetComponent(ScalarType& vector,
606  ComponentType value)
607  {
608  vector = value;
609  }
610 
611  template <typename NewComponentType>
612  using ReplaceComponentType = NewComponentType;
613 
614  template <typename NewComponentType>
615  using ReplaceBaseComponentType = NewComponentType;
616 
617  template <viskores::IdComponent destSize>
618  VISKORES_EXEC_CONT static void CopyInto(const ScalarType& src,
620  {
621  dest[0] = src;
622  }
623 };
624 
625 template <typename T>
626 struct VISKORES_DEPRECATED(2.1 "VecTraits now safe to use on any type.")
627  VISKORES_NEVER_EXPORT SafeVecTraits : viskores::VecTraits<T>
628 {
629 };
630 
631 } // namespace internal
632 
633 namespace detail
634 {
635 
636 struct VISKORES_DEPRECATED(
637  2.1,
638  "VISKORES_BASIC_TYPE_VECTOR is no longer necessary because VecTraits implements "
639  "basic type by default.") VISKORES_BASIC_TYPE_VECTOR_is_deprecated
640 {
641 };
642 
643 template <typename T>
644 struct issue_VISKORES_BASIC_TYPE_VECTOR_deprecation_warning;
645 
646 }
647 
648 } // namespace viskores
649 
650 #define VISKORES_BASIC_TYPE_VECTOR(type) \
651  namespace viskores \
652  { \
653  namespace detail \
654  { \
655  template <> \
656  struct issue_VISKORES_BASIC_TYPE_VECTOR_deprecation_warning<type> \
657  : public viskores::detail::VISKORES_BASIC_TYPE_VECTOR_is_deprecated \
658  { \
659  }; \
660  } \
661  }
662 
663 #endif //viskores_VecTraits_h
viskores::VecTraits::SetComponent
static void SetComponent(T &vector, viskores::IdComponent, ComponentType value)
Changes the value in a given component of the vector.
Definition: VecTraits.h:141
viskores::VecTraits< T * >::CopyInto
static void CopyInto(const T *src, viskores::Vec< typename VecTraits< T >::ComponentType, destSize > &dest)
Definition: VecTraits.h:232
viskores::VecTraits< viskores::VecC< T > >::CopyInto
static void CopyInto(const VecType &src, viskores::Vec< ComponentType, destSize > &dest)
Converts whatever type this vector is into the standard Viskores Tuple.
Definition: VecTraits.h:481
viskores::VecTraits< viskores::VecC< T > >::BaseComponentType
typename viskores::VecTraits< ComponentType >::BaseComponentType BaseComponentType
Base component type in the vector.
Definition: VecTraits.h:409
viskores::VecTraits< viskores::VecCConst< T > >::BaseComponentType
typename viskores::VecTraits< ComponentType >::BaseComponentType BaseComponentType
Base component type in the vector.
Definition: VecTraits.h:504
viskores::VecTraits< viskores::VecCConst< T > >::SetComponent
static void SetComponent(VecType &vector, viskores::IdComponent component, ComponentType value)
Changes the value in a given component of the vector.
Definition: VecTraits.h:543
viskores::VecTraits< viskores::Vec< T, Size > >::CopyInto
static void CopyInto(const VecType &src, viskores::Vec< ComponentType, destSize > &dest)
Converts whatever type this vector is into the standard Viskores Tuple.
Definition: VecTraits.h:386
viskores::VecCConst
A const version of VecC.
Definition: Types.h:371
viskores::VecCConst::GetNumberOfComponents
constexpr viskores::IdComponent GetNumberOfComponents() const
Definition: Types.h:1444
Types.h
viskores::VecTraits< viskores::Vec< T, Size > >::GetNumberOfComponents
static viskores::IdComponent GetNumberOfComponents(const VecType &)
Number of components in the given vector.
Definition: VecTraits.h:306
viskores::VecTraitsTagSizeStatic
A tag for vectors where the number of components are known at compile time.
Definition: VecTraits.h:44
viskores::VecTraitsTagSizeVariable
A tag for vectors where the number of components are not determined until run time.
Definition: VecTraits.h:51
viskores::VecC::GetNumberOfComponents
constexpr viskores::IdComponent GetNumberOfComponents() const
Definition: Types.h:1350
viskores::VecC
A Vec-like representation for short arrays.
Definition: Types.h:368
viskores::HasVecTraits
std::true_type HasVecTraits
Definition: VecTraits.h:185
viskoresNotUsed
#define viskoresNotUsed(parameter_name)
Simple macro to identify a parameter as unused.
Definition: ExportMacros.h:136
viskores::Vec::CopyInto
void CopyInto(Vec< T, Size > &dest) const
Definition: Types.h:837
viskores::VecTraitsTagMultipleComponents
A tag for vectors that are "true" vectors (i.e.
Definition: VecTraits.h:31
viskores::VecTraitsTagSingleComponent
A tag for vectors that are really just scalars (i.e.
Definition: VecTraits.h:38
viskores::IdComponent
viskores::Int32 IdComponent
Base type to use to index small lists.
Definition: Types.h:202
viskores::VecTraits< viskores::VecC< T > >::SetComponent
static void SetComponent(VecType &vector, viskores::IdComponent component, ComponentType value)
Changes the value in a given component of the vector.
Definition: VecTraits.h:453
VISKORES_EXEC_CONT
#define VISKORES_EXEC_CONT
Definition: ExportMacros.h:60
viskores::VecTraits< T * >::GetNumberOfComponents
static viskores::IdComponent GetNumberOfComponents(const T *vector)
Definition: VecTraits.h:205
viskores::VecTraits< viskores::VecCConst< T > >::GetNumberOfComponents
static viskores::IdComponent GetNumberOfComponents(const VecType &vector)
Number of components in the given vector.
Definition: VecTraits.h:509
viskores::VecTraits< T * >::GetComponent
static auto GetComponent(const T *vector, viskores::IdComponent component) -> decltype(VecTraits< T >::GetComponent(*vector, component))
Definition: VecTraits.h:209
viskores::VecTraits::GetComponent
static const ComponentType & GetComponent(const T &vector, viskores::IdComponent)
Returns the value in a given component of the vector.
Definition: VecTraits.h:125
viskores::VecC::ComponentType
T ComponentType
Definition: Types.h:1298
viskores::VecTraits::GetNumberOfComponents
static constexpr viskores::IdComponent GetNumberOfComponents(const T &)
Returns the number of components in the given vector.
Definition: VecTraits.h:102
viskores
Groups connected points that have the same field value.
Definition: Atomic.h:27
viskores::VecTraits::ReplaceComponentType
NewComponentType ReplaceComponentType
Get a vector of the same type but with a different component.
Definition: VecTraits.h:159
viskores::VecTraits< viskores::VecC< T > >::GetComponent
static ComponentType & GetComponent(VecType &vector, viskores::IdComponent component)
Definition: VecTraits.h:445
viskores::VecTraits::GetComponent
static ComponentType & GetComponent(T &vector, viskores::IdComponent)
Returns the value in a given component of the vector.
Definition: VecTraits.h:132
viskores::VecTraits
Traits that can be queried to treat any type as a Vec.
Definition: VecTraits.h:69
viskores::VecTraits< viskores::Vec< T, Size > >::ComponentType
typename VecType::ComponentType ComponentType
Type of the components in the vector.
Definition: VecTraits.h:290
VISKORES_DEPRECATED
#define VISKORES_DEPRECATED(...)
Definition: Deprecated.h:156
viskores::VecTraits::CopyInto
static void CopyInto(const T &src, viskores::Vec< ComponentType, destSize > &dest)
Copies the components in the given vector into a given Vec object.
Definition: VecTraits.h:176
viskores::VecTraits< T * >::GetComponent
static auto GetComponent(T *vector, viskores::IdComponent component) -> decltype(VecTraits< T >::GetComponent(*vector, component))
Definition: VecTraits.h:214
VISKORES_STATIC_ASSERT_MSG
#define VISKORES_STATIC_ASSERT_MSG(condition, message)
Definition: StaticAssert.h:26
viskores::VecTraits< viskores::Vec< T, Size > >::BaseComponentType
typename viskores::VecTraits< ComponentType >::BaseComponentType BaseComponentType
Base component type in the vector.
Definition: VecTraits.h:297
viskores::VecTraits< viskores::VecCConst< T > >::GetComponent
static const ComponentType & GetComponent(const VecType &vector, viskores::IdComponent component)
Returns the value in a given component of the vector.
Definition: VecTraits.h:535
viskores::VecTraits< viskores::VecC< T > >::ComponentType
typename VecType::ComponentType ComponentType
Type of the components in the vector.
Definition: VecTraits.h:402
viskores::VecTraits< viskores::VecCConst< T > >::CopyInto
static void CopyInto(const VecType &src, viskores::Vec< ComponentType, destSize > &dest)
Converts whatever type this vector is into the standard Viskores Tuple.
Definition: VecTraits.h:571
viskores::VecTraits< viskores::Vec< T, Size > >::GetComponent
static ComponentType & GetComponent(VecType &vector, viskores::IdComponent component)
Definition: VecTraits.h:331
StaticAssert.h
viskores::VecCConst::ComponentType
T ComponentType
Definition: Types.h:1392
viskores::VecTraits::ReplaceBaseComponentType
NewComponentType ReplaceBaseComponentType
Get a vector of the same type but with a different base component.
Definition: VecTraits.h:171
Deprecated.h
viskores::VecTraits::ComponentType
T ComponentType
Type of the components in the vector.
Definition: VecTraits.h:79
viskores::VecTraits< T * >
Definition: VecTraits.h:203
viskores::VecTraits< viskores::Vec< T, Size > >::GetComponent
static const ComponentType & GetComponent(const VecType &vector, viskores::IdComponent component)
Returns the value in a given component of the vector.
Definition: VecTraits.h:326
viskores::Vec::ComponentType
T ComponentType
Definition: Types.h:823
viskores::VecTraits::BaseComponentType
T BaseComponentType
Base component type in the vector.
Definition: VecTraits.h:86
viskores::VecTraits< viskores::VecC< T > >::GetComponent
static const ComponentType & GetComponent(const VecType &vector, viskores::IdComponent component)
Returns the value in a given component of the vector.
Definition: VecTraits.h:440
viskores::VecTraits< viskores::Vec< T, Size > >::SetComponent
static void SetComponent(VecType &vector, viskores::IdComponent component, ComponentType value)
Changes the value in a given component of the vector.
Definition: VecTraits.h:338
viskores::VecTraits< viskores::VecCConst< T > >::ComponentType
typename VecType::ComponentType ComponentType
Type of the components in the vector.
Definition: VecTraits.h:497
viskores::VecTraits< viskores::VecC< T > >::GetNumberOfComponents
static viskores::IdComponent GetNumberOfComponents(const VecType &vector)
Number of components in the given vector.
Definition: VecTraits.h:414
viskores::Vec
A short fixed-length array.
Definition: Types.h:365
viskores::VecTraits< T * >::SetComponent
static void SetComponent(T *vector, viskores::IdComponent component, typename VecTraits< T >::ComponentType value)
Definition: VecTraits.h:219
VISKORES_BASIC_TYPE_VECTOR
#define VISKORES_BASIC_TYPE_VECTOR(type)
Definition: VecTraits.h:650