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 <http://clang.llvm.org/compatibility.html#dep_lookup> 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.
This commit is contained in:
Kenneth Moreland 2015-09-20 00:01:04 -06:00
parent e761afc18e
commit 77568789ea
4 changed files with 43 additions and 24 deletions

@ -23,7 +23,6 @@
#include <vtkm/internal/Configure.h>
#include <vtkm/internal/ExportMacros.h>
#include <vtkm/TypeTraits.h>
#include <iostream>
#include <utility>
@ -150,30 +149,7 @@ vtkm::Pair<T1,T2> make_Pair(const T1 &firstSrc, const T2 &secondSrc)
return vtkm::Pair<T1,T2>(firstSrc, secondSrc);
}
/// Traits for Pair types.
///
template<typename T, typename U>
struct TypeTraits<vtkm::Pair<T,U> >
{
typedef TypeTraitsUnknownTag NumericTag;
typedef TypeTraitsScalarTag DimensionalityTag;
VTKM_EXEC_CONT_EXPORT
static vtkm::Pair<T,U> ZeroInitialization()
{ return vtkm::make_Pair(TypeTraits<T>::ZeroInitialization(),
TypeTraits<U>::ZeroInitialization()); }
};
} // namespace vtkm
/// Helper function for printing out pairs during testing.
///
template<typename T, typename U>
VTKM_EXEC_CONT_EXPORT
std::ostream &operator<<(std::ostream &stream, const vtkm::Pair<T,U> &vec)
{
return stream << "[" << vec.first << "," << vec.second << "]";
}
#endif //vtk_m_Pair_h

@ -125,6 +125,22 @@ struct TypeTraits<vtkm::Vec<T,Size> >
{ return vtkm::Vec<T,Size>( (T()) ); }
};
/// \brief Traits for Pair types.
///
template<typename T, typename U>
struct TypeTraits<vtkm::Pair<T,U> >
{
typedef TypeTraitsUnknownTag NumericTag;
typedef TypeTraitsScalarTag DimensionalityTag;
VTKM_EXEC_CONT_EXPORT
static vtkm::Pair<T,U> ZeroInitialization()
{
return vtkm::Pair<T,U>(TypeTraits<T>::ZeroInitialization(),
TypeTraits<U>::ZeroInitialization());
}
};
} // namespace vtkm
#endif //vtk_m_TypeTraits_h

@ -1102,6 +1102,11 @@ vtkm::Vec<T,4> make_Vec(const T &x, const T &y, const T &z, const T &w)
return vtkm::Vec<T,4>(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<typename U, typename V>
struct Pair;
template<typename T, vtkm::IdComponent Size>
VTKM_EXEC_CONT_EXPORT
T dot(const vtkm::Vec<T,Size> &a, const vtkm::Vec<T,Size> &b)
@ -1197,4 +1202,13 @@ std::ostream &operator<<(std::ostream &stream, const vtkm::Vec<T,Size> &vec)
return stream << vec[Size-1] << "]";
}
/// Helper function for printing out pairs during testing.
///
template<typename T, typename U>
VTKM_EXEC_CONT_EXPORT
std::ostream &operator<<(std::ostream &stream, const vtkm::Pair<T,U> &vec)
{
return stream << "[" << vec.first << "," << vec.second << "]";
}
#endif //vtk_m_Types_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<typename T, typename U>
struct VecTraits<vtkm::Pair<T,U> >
: public vtkm::internal::VecTraitsBasic<vtkm::Pair<T, U> >
{
};
} // anonymous namespace
#define VTKM_BASIC_TYPE_VECTOR(type) \