Allow resetting the Type and Storage of a DynamicArrayHandle in a single call.

vtkm::filter has the use case where we need to reset both the type and
storage of an array handle, by doing both at the same time we can reduce
the number of temporary objects, and invalid conversions of arrays.
This commit is contained in:
Robert Maynard 2016-01-04 17:00:08 -05:00
parent b93658b403
commit b70a000ac0
2 changed files with 36 additions and 0 deletions

@ -276,6 +276,22 @@ public:
return DynamicArrayHandleBase<TypeList,NewStorageList>(*this); return DynamicArrayHandleBase<TypeList,NewStorageList>(*this);
} }
/// Changes the value, and array storage types to try casting to when
/// resolving this dynamic array. Since C++ does not allow you to actually
/// change the template arguments, this method returns a new dynamic array
/// object. This method is particularly useful when you have both custom
/// types and traits.
///
template<typename NewTypeList, typename NewStorageList>
VTKM_CONT_EXPORT
DynamicArrayHandleBase<NewTypeList,NewStorageList>
ResetTypeAndStorageLists(NewTypeList = NewTypeList(),
NewStorageList = NewStorageList()) const {
VTKM_IS_LIST_TAG(NewTypeList);
VTKM_IS_LIST_TAG(NewStorageList);
return DynamicArrayHandleBase<NewTypeList,NewStorageList>(*this);
}
/// Attempts to cast the held array to a specific value type and storage, /// Attempts to cast the held array to a specific value type and storage,
/// then call the given functor with the cast array. The types and storage /// then call the given functor with the cast array. The types and storage
/// tried in the cast are those in the lists defined by the TypeList and /// tried in the cast are those in the lists defined by the TypeList and

@ -332,6 +332,19 @@ void TryUnusualTypeAndStorage()
std::cout << " Caught exception for unrecognized type." << std::endl; std::cout << " Caught exception for unrecognized type." << std::endl;
} }
try
{
//resetting the string and tag should result in a valid array handle
CheckDynamicArray(array.ResetTypeAndStorageLists(TypeListTagString(),
StorageListTagUnusual()),
1);
}
catch (vtkm::cont::ErrorControlBadValue)
{
VTKM_TEST_FAIL("ResetTypeAndStorageLists should have handled the custom type/storage.");
}
CheckCalled = false; CheckCalled = false;
CheckDynamicArray(array CheckDynamicArray(array
.ResetTypeList(TypeListTagString()) .ResetTypeList(TypeListTagString())
@ -345,6 +358,13 @@ void TryUnusualTypeAndStorage()
.ResetTypeList(TypeListTagString()), .ResetTypeList(TypeListTagString()),
1); 1);
std::cout << " Found instance when storage and type lists were reset." << std:: endl; std::cout << " Found instance when storage and type lists were reset." << std:: endl;
CheckCalled = false;
CheckDynamicArray(array
.ResetTypeAndStorageLists(TypeListTagString(),
StorageListTagUnusual()),
1);
std::cout << " Found instance when storage and type lists were reset." << std:: endl;
} }
void TryCastToArrayHandle() void TryCastToArrayHandle()