Add new version of DynamicArrayHandle::CastToArrayHandle

This takes a reference to an array handle and fills it. This removes a
lot of the pain of determining template arguments.
This commit is contained in:
Kenneth Moreland 2015-09-17 22:31:58 -06:00
parent fd21a12f4a
commit e761afc18e
2 changed files with 57 additions and 2 deletions

@ -193,8 +193,8 @@ public:
}
/// Returns this array cast to an ArrayHandle object of the given type and
/// storage. Throws ErrorControlBadValue if the cast does not work. Use
/// IsTypeAndStorage to check if the cast can happen.
/// storage. Throws \c ErrorControlBadValue if the cast does not work. Use
/// \c IsTypeAndStorage to check if the cast can happen.
///
template<typename Type, typename Storage>
VTKM_CONT_EXPORT
@ -209,6 +209,20 @@ public:
return container->Array;
}
/// Given a refernce to an ArrayHandle object, casts this array to the
/// ArrayHandle's type and sets the given ArrayHandle to this array. Throws
/// \c ErrorControlBadValue if the cast does not work. Use \c
/// IsTypeAndStorage to check if the cast can happen.
///
template<typename ArrayHandleType>
VTKM_CONT_EXPORT
void CastToArrayHandle(ArrayHandleType &array) const {
VTKM_IS_ARRAY_HANDLE(ArrayHandleType);
typedef typename ArrayHandleType::ValueType ValueType;
typedef typename ArrayHandleType::StorageTag StorageTag;
array = this->CastToArrayHandle(ValueType(), StorageTag());
}
/// Changes the types to try casting to when resolving this dynamic array,
/// which is specified with a list tag like those in TypeListTag.h. Since C++
/// does not allow you to actually change the template arguments, this method

@ -153,6 +153,33 @@ vtkm::cont::DynamicArrayHandle CreateDynamicArray(T)
vtkm::cont::make_ArrayHandle(buffer, ARRAY_SIZE));
}
template<typename ArrayHandleType>
void CheckCastToArrayHandle(const ArrayHandleType &array)
{
VTKM_IS_ARRAY_HANDLE(ArrayHandleType);
vtkm::cont::DynamicArrayHandle dynamicArray = array;
ArrayHandleType castArray1;
dynamicArray.CastToArrayHandle(castArray1);
for (vtkm::Id index = 0; index < array.GetNumberOfValues(); index++)
{
VTKM_TEST_ASSERT(test_equal(array.GetPortalConstControl().Get(index),
castArray1.GetPortalConstControl().Get(index)),
"Cast array has bad value.");
}
ArrayHandleType castArray2 =
dynamicArray.CastToArrayHandle(typename ArrayHandleType::ValueType(),
typename ArrayHandleType::StorageTag());
for (vtkm::Id index = 0; index < array.GetNumberOfValues(); index++)
{
VTKM_TEST_ASSERT(test_equal(array.GetPortalConstControl().Get(index),
castArray2.GetPortalConstControl().Get(index)),
"Cast array has bad value.");
}
}
template<typename T, typename DynamicArrayType>
void TryNewInstance(T, DynamicArrayType originalArray)
{
@ -306,6 +333,17 @@ void TryUnusualTypeAndStorage()
std::cout << " Found instance when storage and type lists were reset." << std:: endl;
}
void TryCastToArrayHandle()
{
std::cout << " Normal array handle." << std::endl;
vtkm::FloatDefault buffer[ARRAY_SIZE];
for (vtkm::Id index = 0; index < ARRAY_SIZE; index++)
{
buffer[index] = TestValue(index, vtkm::FloatDefault());
}
CheckCastToArrayHandle(vtkm::cont::make_ArrayHandle(buffer, ARRAY_SIZE));
}
void TestDynamicArrayHandle()
{
std::cout << "Try common types with default type lists." << std::endl;
@ -333,6 +371,9 @@ void TestDynamicArrayHandle()
std::cout << "Try unusual type in unusual storage." << std::endl;
TryUnusualTypeAndStorage();
std::cout << "Try CastToArrayHandle" << std::endl;
TryCastToArrayHandle();
}
} // anonymous namespace