From 77568789ea08e99225f7b404f33d622969704daa Mon Sep 17 00:00:00 2001 From: Kenneth Moreland Date: Sun, 20 Sep 2015 00:01:04 -0600 Subject: [PATCH] Make vtkm::Pair behave as a better core type These changes are basically to support some upcoming changes to ArrayHandleZip. The major additions are an implementation of VecTraits for Pair and an overloaded << operator to ostream for Pair. I also had to declare the operator<< for Pair to be in Types.h. Under some circumstances the operator has to either be declared before the template is declared or declared in the vtkm namespace. (The reasons are described at but I still don't understand.) I tried adding it to the vtkm namespace, but that caused several of the other operator<< to fail. Since there is no way to guarantee that Pair.h is declared before, say, ArrayHandle.h, I moved the implementation to Types.h. Since I was moving operator<< to Types.h, I went ahead and moved the TypeTraits and VecTraits to their respective headers. Since Pair is declared (but not implemented) in Types.h, these templated classes can be implemented without including Pair.h. --- vtkm/Pair.h | 24 ------------------------ vtkm/TypeTraits.h | 16 ++++++++++++++++ vtkm/Types.h | 14 ++++++++++++++ vtkm/VecTraits.h | 13 +++++++++++++ 4 files changed, 43 insertions(+), 24 deletions(-) diff --git a/vtkm/Pair.h b/vtkm/Pair.h index a55c93053..edf02de48 100644 --- a/vtkm/Pair.h +++ b/vtkm/Pair.h @@ -23,7 +23,6 @@ #include #include -#include #include #include @@ -150,30 +149,7 @@ vtkm::Pair make_Pair(const T1 &firstSrc, const T2 &secondSrc) return vtkm::Pair(firstSrc, secondSrc); } -/// Traits for Pair types. -/// -template -struct TypeTraits > -{ - typedef TypeTraitsUnknownTag NumericTag; - typedef TypeTraitsScalarTag DimensionalityTag; - - VTKM_EXEC_CONT_EXPORT - static vtkm::Pair ZeroInitialization() - { return vtkm::make_Pair(TypeTraits::ZeroInitialization(), - TypeTraits::ZeroInitialization()); } -}; - } // namespace vtkm -/// Helper function for printing out pairs during testing. -/// -template -VTKM_EXEC_CONT_EXPORT -std::ostream &operator<<(std::ostream &stream, const vtkm::Pair &vec) -{ - return stream << "[" << vec.first << "," << vec.second << "]"; -} - #endif //vtk_m_Pair_h diff --git a/vtkm/TypeTraits.h b/vtkm/TypeTraits.h index 8856fc632..dfecc0e0f 100644 --- a/vtkm/TypeTraits.h +++ b/vtkm/TypeTraits.h @@ -125,6 +125,22 @@ struct TypeTraits > { return vtkm::Vec( (T()) ); } }; +/// \brief Traits for Pair types. +/// +template +struct TypeTraits > +{ + typedef TypeTraitsUnknownTag NumericTag; + typedef TypeTraitsScalarTag DimensionalityTag; + + VTKM_EXEC_CONT_EXPORT + static vtkm::Pair ZeroInitialization() + { + return vtkm::Pair(TypeTraits::ZeroInitialization(), + TypeTraits::ZeroInitialization()); + } +}; + } // namespace vtkm #endif //vtk_m_TypeTraits_h diff --git a/vtkm/Types.h b/vtkm/Types.h index cefb5c3cf..8b96582af 100644 --- a/vtkm/Types.h +++ b/vtkm/Types.h @@ -1102,6 +1102,11 @@ vtkm::Vec make_Vec(const T &x, const T &y, const T &z, const T &w) return vtkm::Vec(x, y, z, w); } +// A pre-declaration of vtkm::Pair so that classes templated on them can refer +// to it. The actual implementation is in vtkm/Pair.h. +template +struct Pair; + template VTKM_EXEC_CONT_EXPORT T dot(const vtkm::Vec &a, const vtkm::Vec &b) @@ -1197,4 +1202,13 @@ std::ostream &operator<<(std::ostream &stream, const vtkm::Vec &vec) return stream << vec[Size-1] << "]"; } +/// Helper function for printing out pairs during testing. +/// +template +VTKM_EXEC_CONT_EXPORT +std::ostream &operator<<(std::ostream &stream, const vtkm::Pair &vec) +{ + return stream << "[" << vec.first << "," << vec.second << "]"; +} + #endif //vtk_m_Types_h diff --git a/vtkm/VecTraits.h b/vtkm/VecTraits.h index 31067fbd6..1e6346324 100644 --- a/vtkm/VecTraits.h +++ b/vtkm/VecTraits.h @@ -240,6 +240,19 @@ struct VecTraitsBasic { }; } // namespace internal +/// \brief VecTraits for Pair types +/// +/// Although a pair woudl seem better as a size-2 vector, we treat it as a +/// scalar. This is because a \c Vec is assumed to have the same type for +/// every component, and a pair in general has a different type for each +/// component. Thus we treat a pair as a "scalar" unit. +/// +template +struct VecTraits > + : public vtkm::internal::VecTraitsBasic > +{ +}; + } // anonymous namespace #define VTKM_BASIC_TYPE_VECTOR(type) \