mirror of
https://gitlab.kitware.com/vtk/vtk-m
synced 2024-09-16 17:22:55 +00:00
Add a dynamic array handle.
The dynamic array handle holds a reference to an array handle of an unknown type. It contains the ability to try to cast it to an instance of array handle or to try lists of types and containers. There is currently an issue that is causing the test code not to compile. It is the case that some combinations of types and containers are not compatible. For example, an implict container is bound to a certain type, and the container is undefined if they do not agree. There needs to be a mechanism to detect these invalid combinations and skip over them in the MTP for each.
This commit is contained in:
parent
3d3965b87f
commit
d309fa7ae5
@ -21,7 +21,7 @@
|
|||||||
#define vtk_m_TypeListTag_h
|
#define vtk_m_TypeListTag_h
|
||||||
|
|
||||||
#ifndef VTKM_DEFAULT_TYPE_LIST_TAG
|
#ifndef VTKM_DEFAULT_TYPE_LIST_TAG
|
||||||
#define VTKM_DEFAULT_TYPE_LIST_TAG ::vtkm::TypeListTagCommon;
|
#define VTKM_DEFAULT_TYPE_LIST_TAG ::vtkm::TypeListTagCommon
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <vtkm/ListTag.h>
|
#include <vtkm/ListTag.h>
|
||||||
|
@ -105,6 +105,28 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//// If you are using this specalization of ArrayContainerControl, it means the
|
||||||
|
//// type of an ArrayHandle does not match the type of the array portal for the
|
||||||
|
//// array portal in an implicit array. Currently this use is invalid, but there
|
||||||
|
//// could be a case for implementing casting where appropriate.
|
||||||
|
//template<typename T, typename ArrayPortalType>
|
||||||
|
//class ArrayContainerControl<T, ArrayContainerControlTagImplicit<ArrayPortalType> >
|
||||||
|
//{
|
||||||
|
//public:
|
||||||
|
// // This is meant to be invalid because this class should not actually be used.
|
||||||
|
// struct PortalType
|
||||||
|
// {
|
||||||
|
// typedef void *ValueType;
|
||||||
|
// typedef void *IteratorType;
|
||||||
|
// };
|
||||||
|
// // This is meant to be invalid because this class should not actually be used.
|
||||||
|
// struct PortalConstType
|
||||||
|
// {
|
||||||
|
// typedef void *ValueType;
|
||||||
|
// typedef void *IteratorType;
|
||||||
|
// };
|
||||||
|
//};
|
||||||
|
|
||||||
template<typename T, class ArrayPortalType, class DeviceAdapterTag>
|
template<typename T, class ArrayPortalType, class DeviceAdapterTag>
|
||||||
class ArrayTransfer<
|
class ArrayTransfer<
|
||||||
T, ArrayContainerControlTagImplicit<ArrayPortalType>, DeviceAdapterTag>
|
T, ArrayContainerControlTagImplicit<ArrayPortalType>, DeviceAdapterTag>
|
||||||
|
@ -25,11 +25,13 @@ set(headers
|
|||||||
ArrayContainerControlBasic.h
|
ArrayContainerControlBasic.h
|
||||||
ArrayContainerControlImplicit.h
|
ArrayContainerControlImplicit.h
|
||||||
ArrayHandle.h
|
ArrayHandle.h
|
||||||
|
ArrayHandleCounting.h
|
||||||
ArrayPortal.h
|
ArrayPortal.h
|
||||||
Assert.h
|
Assert.h
|
||||||
ContainerListTag.h
|
ContainerListTag.h
|
||||||
DeviceAdapter.h
|
DeviceAdapter.h
|
||||||
DeviceAdapterSerial.h
|
DeviceAdapterSerial.h
|
||||||
|
DynamicArrayHandle.h
|
||||||
Error.h
|
Error.h
|
||||||
ErrorControl.h
|
ErrorControl.h
|
||||||
ErrorControlAssert.h
|
ErrorControlAssert.h
|
||||||
|
@ -21,7 +21,7 @@
|
|||||||
#define vtk_m_ContainerListTag_h
|
#define vtk_m_ContainerListTag_h
|
||||||
|
|
||||||
#ifndef VTKM_DEFAULT_CONTAINER_LIST_TAG
|
#ifndef VTKM_DEFAULT_CONTAINER_LIST_TAG
|
||||||
#define VTKM_DEFAULT_CONTAINER_LIST_TAG ::vtkm::cont::ContainerListTagBasic;
|
#define VTKM_DEFAULT_CONTAINER_LIST_TAG ::vtkm::cont::ContainerListTagBasic
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <vtkm/ListTag.h>
|
#include <vtkm/ListTag.h>
|
||||||
|
280
vtkm/cont/DynamicArrayHandle.h
Normal file
280
vtkm/cont/DynamicArrayHandle.h
Normal file
@ -0,0 +1,280 @@
|
|||||||
|
//============================================================================
|
||||||
|
// Copyright (c) Kitware, Inc.
|
||||||
|
// All rights reserved.
|
||||||
|
// See LICENSE.txt for details.
|
||||||
|
// This software is distributed WITHOUT ANY WARRANTY; without even
|
||||||
|
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
||||||
|
// PURPOSE. See the above copyright notice for more information.
|
||||||
|
//
|
||||||
|
// Copyright 2014 Sandia Corporation.
|
||||||
|
// Copyright 2014 UT-Battelle, LLC.
|
||||||
|
// Copyright 2014. Los Alamos National Security
|
||||||
|
//
|
||||||
|
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
|
||||||
|
// the U.S. Government retains certain rights in this software.
|
||||||
|
//
|
||||||
|
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
|
||||||
|
// Laboratory (LANL), the U.S. Government retains certain rights in
|
||||||
|
// this software.
|
||||||
|
//============================================================================
|
||||||
|
#ifndef vtk_m_cont_DynamicArrayHandle_h
|
||||||
|
#define vtk_m_cont_DynamicArrayHandle_h
|
||||||
|
|
||||||
|
#include <vtkm/TypeListTag.h>
|
||||||
|
|
||||||
|
#include <vtkm/cont/ArrayHandle.h>
|
||||||
|
#include <vtkm/cont/ContainerListTag.h>
|
||||||
|
#include <vtkm/cont/ErrorControlBadValue.h>
|
||||||
|
|
||||||
|
#include <vtkm/cont/internal/SimplePolymorphicContainer.h>
|
||||||
|
|
||||||
|
#include <boost/smart_ptr/shared_ptr.hpp>
|
||||||
|
|
||||||
|
namespace vtkm {
|
||||||
|
namespace cont {
|
||||||
|
|
||||||
|
namespace internal {
|
||||||
|
|
||||||
|
/// Behaves like (and is interchangable with) a DynamicArrayHandle. The
|
||||||
|
/// difference is that the list of types and list of containers to try when
|
||||||
|
/// calling CastAndCall is set to the class template arguments.
|
||||||
|
///
|
||||||
|
template<typename TypeList, typename ContainerList>
|
||||||
|
class DynamicArrayHandleCast;
|
||||||
|
|
||||||
|
} // namespace internal
|
||||||
|
|
||||||
|
class DynamicArrayHandle
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
VTKM_CONT_EXPORT
|
||||||
|
DynamicArrayHandle() { }
|
||||||
|
|
||||||
|
template<typename Type, typename Container>
|
||||||
|
VTKM_CONT_EXPORT
|
||||||
|
DynamicArrayHandle(const vtkm::cont::ArrayHandle<Type,Container> &array)
|
||||||
|
: ArrayContainer(new vtkm::cont::internal::SimplePolymorphicContainer<
|
||||||
|
vtkm::cont::ArrayHandle<Type,Container> >(array))
|
||||||
|
{ }
|
||||||
|
|
||||||
|
template<typename TypeList, typename ContainerList>
|
||||||
|
VTKM_CONT_EXPORT
|
||||||
|
DynamicArrayHandle(
|
||||||
|
const internal::DynamicArrayHandleCast<TypeList,ContainerList> &dynamicArray)
|
||||||
|
: ArrayContainer(dynamicArray.ArrayContainer) { }
|
||||||
|
|
||||||
|
/// Returns true if this array is of the provided type and uses the provided
|
||||||
|
/// container.
|
||||||
|
///
|
||||||
|
template<typename Type, typename Container>
|
||||||
|
VTKM_CONT_EXPORT
|
||||||
|
bool IsTypeAndContainer(Type = Type(), Container = Container()) const {
|
||||||
|
return (this->TryCastArrayContainer<Type,Container>() != NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns this array cast to an ArrayHandle object of the given type and
|
||||||
|
/// container. Throws ErrorControlBadValue if the cast does not work. Use
|
||||||
|
/// IsTypeAndContainer to check if the cast can happen.
|
||||||
|
///
|
||||||
|
template<typename Type, typename Container>
|
||||||
|
VTKM_CONT_EXPORT
|
||||||
|
vtkm::cont::ArrayHandle<Type, Container>
|
||||||
|
CastToArrayHandle(Type = Type(), Container = Container()) const {
|
||||||
|
vtkm::cont::internal::SimplePolymorphicContainer<
|
||||||
|
vtkm::cont::ArrayHandle<Type,Container> > *container =
|
||||||
|
this->TryCastArrayContainer<Type,Container>();
|
||||||
|
if (container == NULL)
|
||||||
|
{
|
||||||
|
throw vtkm::cont::ErrorControlBadValue("Bad cast of dynamic array.");
|
||||||
|
}
|
||||||
|
return container->Item;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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
|
||||||
|
/// returns a new dynamic array object. This method is particularly useful to
|
||||||
|
/// narrow down (or expand) the types when using an array of particular
|
||||||
|
/// constraints.
|
||||||
|
///
|
||||||
|
template<typename NewTypeList>
|
||||||
|
VTKM_CONT_EXPORT
|
||||||
|
internal::DynamicArrayHandleCast<NewTypeList,VTKM_DEFAULT_CONTAINER_LIST_TAG>
|
||||||
|
ResetTypeList(NewTypeList = NewTypeList()) const {
|
||||||
|
return internal::DynamicArrayHandleCast<
|
||||||
|
NewTypeList,VTKM_DEFAULT_CONTAINER_LIST_TAG>(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Changes the array containers to try casting to when resolving this
|
||||||
|
/// dynamic array, which is specified with a list tag like those in
|
||||||
|
/// ContainerListTag.h. 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 to narrow down (or expand) the types when
|
||||||
|
/// using an array of particular constraints.
|
||||||
|
///
|
||||||
|
template<typename NewContainerList>
|
||||||
|
VTKM_CONT_EXPORT
|
||||||
|
internal::DynamicArrayHandleCast<VTKM_DEFAULT_TYPE_LIST_TAG,NewContainerList>
|
||||||
|
ResetContainerList(NewContainerList = NewContainerList()) const {
|
||||||
|
return internal::DynamicArrayHandleCast<
|
||||||
|
VTKM_DEFAULT_TYPE_LIST_TAG,NewContainerList>(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Attempts to cast the held array to a specific value type and container,
|
||||||
|
/// then call the given functor with the cast array. The types and containers
|
||||||
|
/// tried in the cast are those in the lists defined by
|
||||||
|
/// VTKM_DEFAULT_TYPE_LIST_TAG and VTKM_DEFAULT_CONTAINER_LIST_TAG,
|
||||||
|
/// respectively, unless they have been changed with a previous call to
|
||||||
|
/// ResetTypeList or ResetContainerList.
|
||||||
|
///
|
||||||
|
template<typename Functor>
|
||||||
|
VTKM_CONT_EXPORT
|
||||||
|
void CastAndCall(const Functor &f) const
|
||||||
|
{
|
||||||
|
this->CastAndCall(
|
||||||
|
f, VTKM_DEFAULT_TYPE_LIST_TAG(), VTKM_DEFAULT_CONTAINER_LIST_TAG());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A version of CastAndCall that tries specified lists of types and
|
||||||
|
/// containers.
|
||||||
|
///
|
||||||
|
template<typename Functor, typename TypeList, typename ContainerList>
|
||||||
|
VTKM_CONT_EXPORT
|
||||||
|
void CastAndCall(const Functor &f, TypeList, ContainerList) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
boost::shared_ptr<vtkm::cont::internal::SimplePolymorphicContainerBase>
|
||||||
|
ArrayContainer;
|
||||||
|
|
||||||
|
template<typename Type, typename Container>
|
||||||
|
VTKM_CONT_EXPORT
|
||||||
|
vtkm::cont::internal::SimplePolymorphicContainer<
|
||||||
|
vtkm::cont::ArrayHandle<Type,Container> > *
|
||||||
|
TryCastArrayContainer() const {
|
||||||
|
return
|
||||||
|
dynamic_cast<
|
||||||
|
vtkm::cont::internal::SimplePolymorphicContainer<
|
||||||
|
vtkm::cont::ArrayHandle<Type,Container> > *>(
|
||||||
|
this->ArrayContainer.get());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
namespace detail {
|
||||||
|
|
||||||
|
template<typename Functor, typename Type>
|
||||||
|
struct DynamicArrayHandleTryContainer {
|
||||||
|
const DynamicArrayHandle Array;
|
||||||
|
const Functor &Function;
|
||||||
|
bool FoundCast;
|
||||||
|
|
||||||
|
VTKM_CONT_EXPORT
|
||||||
|
DynamicArrayHandleTryContainer(const DynamicArrayHandle &array,
|
||||||
|
const Functor &f)
|
||||||
|
: Array(array), Function(f), FoundCast(false) { }
|
||||||
|
|
||||||
|
template<typename Container>
|
||||||
|
VTKM_CONT_EXPORT
|
||||||
|
void operator()(Container) {
|
||||||
|
if (!this->FoundCast &&
|
||||||
|
this->Array.IsTypeAndContainer(Type(), Container()))
|
||||||
|
{
|
||||||
|
this->Function(this->Array.CastToArrayHandle(Type(), Container()));
|
||||||
|
this->FoundCast = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename Functor, typename ContainerList>
|
||||||
|
struct DynamicArrayHandleTryType {
|
||||||
|
const DynamicArrayHandle Array;
|
||||||
|
const Functor &Function;
|
||||||
|
bool FoundCast;
|
||||||
|
|
||||||
|
VTKM_CONT_EXPORT
|
||||||
|
DynamicArrayHandleTryType(const DynamicArrayHandle &array, const Functor &f)
|
||||||
|
: Array(array), Function(f), FoundCast(false) { }
|
||||||
|
|
||||||
|
template<typename Type>
|
||||||
|
VTKM_CONT_EXPORT
|
||||||
|
void operator()(Type) {
|
||||||
|
if (this->FoundCast) { return; }
|
||||||
|
typedef DynamicArrayHandleTryContainer<Functor, Type> TryContainerType;
|
||||||
|
TryContainerType tryContainer =
|
||||||
|
TryContainerType(this->Array, this->Function);
|
||||||
|
vtkm::ListForEach(tryContainer, ContainerList());
|
||||||
|
if (tryContainer.FoundCast)
|
||||||
|
{
|
||||||
|
this->FoundCast = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
|
template<typename Functor, typename TypeList, typename ContainerList>
|
||||||
|
VTKM_CONT_EXPORT
|
||||||
|
void DynamicArrayHandle::CastAndCall(const Functor &f,
|
||||||
|
TypeList,
|
||||||
|
ContainerList) const
|
||||||
|
{
|
||||||
|
typedef detail::DynamicArrayHandleTryType<Functor, ContainerList> TryTypeType;
|
||||||
|
TryTypeType tryType = TryTypeType(*this, f);
|
||||||
|
vtkm::ListForEach(tryType, TypeList());
|
||||||
|
if (!tryType.FoundCast)
|
||||||
|
{
|
||||||
|
throw vtkm::cont::ErrorControlBadValue(
|
||||||
|
"Could not find appropriate cast in CastAndCall.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace internal {
|
||||||
|
|
||||||
|
template<typename TypeList, typename ContainerList>
|
||||||
|
class DynamicArrayHandleCast : public vtkm::cont::DynamicArrayHandle
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
VTKM_CONT_EXPORT
|
||||||
|
DynamicArrayHandleCast(const vtkm::cont::DynamicArrayHandle &array)
|
||||||
|
: DynamicArrayHandle(array) { }
|
||||||
|
|
||||||
|
template<typename SrcTypeList, typename SrcContainerList>
|
||||||
|
VTKM_CONT_EXPORT
|
||||||
|
DynamicArrayHandleCast(
|
||||||
|
const DynamicArrayHandleCast<SrcTypeList,SrcContainerList> &array)
|
||||||
|
: DynamicArrayHandle(array) { }
|
||||||
|
|
||||||
|
template<typename NewTypeList>
|
||||||
|
VTKM_CONT_EXPORT
|
||||||
|
DynamicArrayHandleCast<NewTypeList,ContainerList>
|
||||||
|
ResetTypeList(NewTypeList = NewTypeList()) const {
|
||||||
|
return DynamicArrayHandleCast<NewTypeList,ContainerList>(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename NewContainerList>
|
||||||
|
VTKM_CONT_EXPORT
|
||||||
|
internal::DynamicArrayHandleCast<TypeList,NewContainerList>
|
||||||
|
ResetContainerList(NewContainerList = NewContainerList()) const {
|
||||||
|
return internal::DynamicArrayHandleCast<TypeList,NewContainerList>(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Functor>
|
||||||
|
VTKM_CONT_EXPORT
|
||||||
|
void CastAndCall(const Functor &f) const
|
||||||
|
{
|
||||||
|
this->CastAndCall(f, TypeList(), ContainerList());
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Functor, typename TL, typename CL>
|
||||||
|
VTKM_CONT_EXPORT
|
||||||
|
void CastAndCall(const Functor &f, TL, CL) const
|
||||||
|
{
|
||||||
|
this->DynamicArrayHandle::CastAndCall(f, TL(), CL());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace internal
|
||||||
|
|
||||||
|
}
|
||||||
|
} // namespace vtkm::cont
|
||||||
|
|
||||||
|
#endif //vtk_m_cont_DynamicArrayHandle_h
|
@ -33,6 +33,7 @@ set(headers
|
|||||||
DeviceAdapterTag.h
|
DeviceAdapterTag.h
|
||||||
DeviceAdapterTagSerial.h
|
DeviceAdapterTagSerial.h
|
||||||
IteratorFromArrayPortal.h
|
IteratorFromArrayPortal.h
|
||||||
|
SimplePolymorphicContainer.h
|
||||||
)
|
)
|
||||||
|
|
||||||
vtkm_declare_headers(${headers})
|
vtkm_declare_headers(${headers})
|
||||||
|
60
vtkm/cont/internal/SimplePolymorphicContainer.h
Normal file
60
vtkm/cont/internal/SimplePolymorphicContainer.h
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
//============================================================================
|
||||||
|
// Copyright (c) Kitware, Inc.
|
||||||
|
// All rights reserved.
|
||||||
|
// See LICENSE.txt for details.
|
||||||
|
// This software is distributed WITHOUT ANY WARRANTY; without even
|
||||||
|
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
||||||
|
// PURPOSE. See the above copyright notice for more information.
|
||||||
|
//
|
||||||
|
// Copyright 2014 Sandia Corporation.
|
||||||
|
// Copyright 2014 UT-Battelle, LLC.
|
||||||
|
// Copyright 2014. Los Alamos National Security
|
||||||
|
//
|
||||||
|
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
|
||||||
|
// the U.S. Government retains certain rights in this software.
|
||||||
|
//
|
||||||
|
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
|
||||||
|
// Laboratory (LANL), the U.S. Government retains certain rights in
|
||||||
|
// this software.
|
||||||
|
//============================================================================
|
||||||
|
#ifndef vtk_m_cont_internal_SimplePolymorphicContainer_h
|
||||||
|
#define vtk_m_cont_internal_SimplePolymorphicContainer_h
|
||||||
|
|
||||||
|
#include <vtkm/Types.h>
|
||||||
|
|
||||||
|
namespace vtkm {
|
||||||
|
namespace cont {
|
||||||
|
namespace internal {
|
||||||
|
|
||||||
|
/// \brief Base class for SimplePolymorphicContainer
|
||||||
|
///
|
||||||
|
struct SimplePolymorphicContainerBase {
|
||||||
|
virtual ~SimplePolymorphicContainerBase() { }
|
||||||
|
};
|
||||||
|
|
||||||
|
/// \brief Simple object container that can use C++ run-time type information.
|
||||||
|
///
|
||||||
|
/// The SimplePolymorphicContainer is a trivial structure that contains a
|
||||||
|
/// single object. The intention is to be able to pass around a pointer to the
|
||||||
|
/// superclass SimplePolymorphicContainerBase to methods that cannot know the
|
||||||
|
/// full type of the object at run-time. This is roughly equivalent to passing
|
||||||
|
/// around a void* except that C++ will capture run-time type information that
|
||||||
|
/// allows for safer dynamic downcasts.
|
||||||
|
///
|
||||||
|
template<typename T>
|
||||||
|
struct SimplePolymorphicContainer : public SimplePolymorphicContainerBase
|
||||||
|
{
|
||||||
|
T Item;
|
||||||
|
|
||||||
|
VTKM_CONT_EXPORT
|
||||||
|
SimplePolymorphicContainer() : Item() { }
|
||||||
|
|
||||||
|
VTKM_CONT_EXPORT
|
||||||
|
SimplePolymorphicContainer(const T &src) : Item(src) { }
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} // namespace vtkm::cont::internal
|
||||||
|
|
||||||
|
#endif //vtk_m_cont_internal_SimplePolymorphicContainer_h
|
@ -35,6 +35,7 @@ set(unit_tests
|
|||||||
UnitTestDeviceAdapterAlgorithmDependency.cxx
|
UnitTestDeviceAdapterAlgorithmDependency.cxx
|
||||||
UnitTestDeviceAdapterAlgorithmGeneral.cxx
|
UnitTestDeviceAdapterAlgorithmGeneral.cxx
|
||||||
UnitTestDeviceAdapterSerial.cxx
|
UnitTestDeviceAdapterSerial.cxx
|
||||||
|
UnitTestDynamicArrayHandle.cxx
|
||||||
)
|
)
|
||||||
|
|
||||||
vtkm_unit_tests(SOURCES ${unit_tests})
|
vtkm_unit_tests(SOURCES ${unit_tests})
|
||||||
|
294
vtkm/cont/testing/UnitTestDynamicArrayHandle.cxx
Normal file
294
vtkm/cont/testing/UnitTestDynamicArrayHandle.cxx
Normal file
@ -0,0 +1,294 @@
|
|||||||
|
//============================================================================
|
||||||
|
// Copyright (c) Kitware, Inc.
|
||||||
|
// All rights reserved.
|
||||||
|
// See LICENSE.txt for details.
|
||||||
|
// This software is distributed WITHOUT ANY WARRANTY; without even
|
||||||
|
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
||||||
|
// PURPOSE. See the above copyright notice for more information.
|
||||||
|
//
|
||||||
|
// Copyright 2014 Sandia Corporation.
|
||||||
|
// Copyright 2014 UT-Battelle, LLC.
|
||||||
|
// Copyright 2014. Los Alamos National Security
|
||||||
|
//
|
||||||
|
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
|
||||||
|
// the U.S. Government retains certain rights in this software.
|
||||||
|
//
|
||||||
|
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
|
||||||
|
// Laboratory (LANL), the U.S. Government retains certain rights in
|
||||||
|
// this software.
|
||||||
|
//============================================================================
|
||||||
|
|
||||||
|
|
||||||
|
#include <vtkm/cont/DynamicArrayHandle.h>
|
||||||
|
|
||||||
|
#include <vtkm/cont/ArrayContainerControlImplicit.h>
|
||||||
|
#include <vtkm/cont/internal/IteratorFromArrayPortal.h>
|
||||||
|
|
||||||
|
#include <vtkm/cont/testing/Testing.h>
|
||||||
|
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
const vtkm::Id ARRAY_SIZE = 10;
|
||||||
|
|
||||||
|
vtkm::Id TestValue(vtkm::Id index, vtkm::Id) {
|
||||||
|
return index*100;
|
||||||
|
}
|
||||||
|
|
||||||
|
vtkm::Scalar TestValue(vtkm::Id index, vtkm::Scalar) {
|
||||||
|
return static_cast<vtkm::Scalar>(index)/100;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, int N>
|
||||||
|
vtkm::Tuple<T,N> TestValue(vtkm::Id index, vtkm::Tuple<T,N>) {
|
||||||
|
vtkm::Tuple<T,N> value;
|
||||||
|
for (int i = 0; i < N; i++)
|
||||||
|
{
|
||||||
|
value[i] = TestValue(index, T()) + (i + 1);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string TestValue(vtkm::Id index, std::string) {
|
||||||
|
std::stringstream stream;
|
||||||
|
stream << index;
|
||||||
|
return stream.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
struct TypeListTagString : vtkm::ListTagBase<std::string> { };
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
struct UnusualPortal
|
||||||
|
{
|
||||||
|
typedef T ValueType;
|
||||||
|
|
||||||
|
VTKM_EXEC_CONT_EXPORT
|
||||||
|
vtkm::Id GetNumberOfValues() const { return ARRAY_SIZE; }
|
||||||
|
|
||||||
|
VTKM_EXEC_CONT_EXPORT
|
||||||
|
ValueType Get(vtkm::Id index) const { return TestValue(index, ValueType()); }
|
||||||
|
|
||||||
|
typedef vtkm::cont::internal::IteratorFromArrayPortal<UnusualPortal<T> >
|
||||||
|
IteratorType;
|
||||||
|
|
||||||
|
VTKM_CONT_EXPORT
|
||||||
|
IteratorType GetIteratorBegin() const {
|
||||||
|
return IteratorType(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
VTKM_CONT_EXPORT
|
||||||
|
IteratorType GetIteratorEnd() const {
|
||||||
|
return IteratorType(*this, this->GetNumberOfValues());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
class ArrayHandleWithUnusualContainer
|
||||||
|
: public vtkm::cont::ArrayHandle<T, vtkm::cont::ArrayContainerControlTagImplicit<UnusualPortal<T> > >
|
||||||
|
{
|
||||||
|
typedef vtkm::cont::ArrayHandle<T, vtkm::cont::ArrayContainerControlTagImplicit<UnusualPortal<T> > >
|
||||||
|
Superclass;
|
||||||
|
public:
|
||||||
|
VTKM_CONT_EXPORT
|
||||||
|
ArrayHandleWithUnusualContainer()
|
||||||
|
: Superclass(typename Superclass::PortalConstControl()) { }
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ContainerListTagUnusual :
|
||||||
|
vtkm::ListTagBase2<
|
||||||
|
ArrayHandleWithUnusualContainer<vtkm::Id>::ArrayContainerControlTag,
|
||||||
|
ArrayHandleWithUnusualContainer<std::string>::ArrayContainerControlTag>
|
||||||
|
{ };
|
||||||
|
|
||||||
|
bool CheckCalled;
|
||||||
|
|
||||||
|
struct CheckFunctor
|
||||||
|
{
|
||||||
|
template<typename T, typename Container>
|
||||||
|
void operator()(vtkm::cont::ArrayHandle<T, Container> array) const {
|
||||||
|
CheckCalled = true;
|
||||||
|
|
||||||
|
VTKM_TEST_ASSERT(array.GetNumberOfValues() == ARRAY_SIZE,
|
||||||
|
"Unexpected array size.");
|
||||||
|
|
||||||
|
typename vtkm::cont::ArrayHandle<T,Container>::PortalConstControl portal =
|
||||||
|
array.GetPortalConstControl();
|
||||||
|
for (vtkm::Id index = 0; index < ARRAY_SIZE; index++)
|
||||||
|
{
|
||||||
|
VTKM_TEST_ASSERT(portal.Get(index) == TestValue(index, T()),
|
||||||
|
"Got bad value in array. Perhaps a bad cast?");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
vtkm::cont::DynamicArrayHandle CreateDynamicArray(T)
|
||||||
|
{
|
||||||
|
// Declared static to prevent going out of scope.
|
||||||
|
static T buffer[ARRAY_SIZE];
|
||||||
|
for (vtkm::Id index = 0; index < ARRAY_SIZE; index++)
|
||||||
|
{
|
||||||
|
buffer[index] = TestValue(index, T());
|
||||||
|
}
|
||||||
|
|
||||||
|
return vtkm::cont::DynamicArrayHandle(
|
||||||
|
vtkm::cont::make_ArrayHandle(buffer, ARRAY_SIZE));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void TryDefaultType(T)
|
||||||
|
{
|
||||||
|
CheckCalled = false;
|
||||||
|
|
||||||
|
vtkm::cont::DynamicArrayHandle array = CreateDynamicArray(T());
|
||||||
|
|
||||||
|
array.CastAndCall(CheckFunctor());
|
||||||
|
|
||||||
|
VTKM_TEST_ASSERT(CheckCalled,
|
||||||
|
"The functor was never called (and apparently a bad value exception not thrown).");
|
||||||
|
}
|
||||||
|
|
||||||
|
struct TryBasicVTKmType
|
||||||
|
{
|
||||||
|
template<typename T>
|
||||||
|
void operator()(T) {
|
||||||
|
CheckCalled = false;
|
||||||
|
|
||||||
|
vtkm::cont::DynamicArrayHandle array = CreateDynamicArray(T());
|
||||||
|
|
||||||
|
array.ResetTypeList(vtkm::TypeListTagAll()).CastAndCall(CheckFunctor());
|
||||||
|
|
||||||
|
VTKM_TEST_ASSERT(CheckCalled,
|
||||||
|
"The functor was never called (and apparently a bad value exception not thrown).");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
void TryUnusualType()
|
||||||
|
{
|
||||||
|
// A string is an unlikely type to be declared elsewhere in VTK-m.
|
||||||
|
vtkm::cont::DynamicArrayHandle array = CreateDynamicArray(std::string());
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
array.CastAndCall(CheckFunctor());
|
||||||
|
VTKM_TEST_FAIL("CastAndCall failed to error for unrecognized type.");
|
||||||
|
}
|
||||||
|
catch (vtkm::cont::ErrorControlBadValue)
|
||||||
|
{
|
||||||
|
std::cout << " Caught exception for unrecognized type." << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
CheckCalled = false;
|
||||||
|
array.ResetTypeList(TypeListTagString()).CastAndCall(CheckFunctor());
|
||||||
|
VTKM_TEST_ASSERT(CheckCalled,
|
||||||
|
"The functor was never called (and apparently a bad value exception not thrown).");
|
||||||
|
std::cout << " Found type when type list was reset." << std:: endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TryUnusualContainer()
|
||||||
|
{
|
||||||
|
vtkm::cont::DynamicArrayHandle array =
|
||||||
|
ArrayHandleWithUnusualContainer<vtkm::Id>();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
array.CastAndCall(CheckFunctor());
|
||||||
|
VTKM_TEST_FAIL("CastAndCall failed to error for unrecognized container.");
|
||||||
|
}
|
||||||
|
catch (vtkm::cont::ErrorControlBadValue)
|
||||||
|
{
|
||||||
|
std::cout << " Caught exception for unrecognized container." << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
CheckCalled = false;
|
||||||
|
array.ResetContainerList(ContainerListTagUnusual()).CastAndCall(CheckFunctor());
|
||||||
|
VTKM_TEST_ASSERT(CheckCalled,
|
||||||
|
"The functor was never called (and apparently a bad value exception not thrown).");
|
||||||
|
std::cout << " Found instance when container list was reset." << std:: endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TryUnusualTypeAndContainer()
|
||||||
|
{
|
||||||
|
vtkm::cont::DynamicArrayHandle array =
|
||||||
|
ArrayHandleWithUnusualContainer<std::string>();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
array.CastAndCall(CheckFunctor());
|
||||||
|
VTKM_TEST_FAIL(
|
||||||
|
"CastAndCall failed to error for unrecognized type/container.");
|
||||||
|
}
|
||||||
|
catch (vtkm::cont::ErrorControlBadValue)
|
||||||
|
{
|
||||||
|
std::cout << " Caught exception for unrecognized type/container."
|
||||||
|
<< std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
array.ResetTypeList(TypeListTagString()).CastAndCall(CheckFunctor());
|
||||||
|
VTKM_TEST_FAIL("CastAndCall failed to error for unrecognized container.");
|
||||||
|
}
|
||||||
|
catch (vtkm::cont::ErrorControlBadValue)
|
||||||
|
{
|
||||||
|
std::cout << " Caught exception for unrecognized container." << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
array.ResetContainerList(ContainerListTagUnusual()).
|
||||||
|
CastAndCall(CheckFunctor());
|
||||||
|
VTKM_TEST_FAIL("CastAndCall failed to error for unrecognized type.");
|
||||||
|
}
|
||||||
|
catch (vtkm::cont::ErrorControlBadValue)
|
||||||
|
{
|
||||||
|
std::cout << " Caught exception for unrecognized type." << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
CheckCalled = false;
|
||||||
|
array
|
||||||
|
.ResetTypeList(TypeListTagString())
|
||||||
|
.ResetContainerList(ContainerListTagUnusual())
|
||||||
|
.CastAndCall(CheckFunctor());
|
||||||
|
VTKM_TEST_ASSERT(CheckCalled,
|
||||||
|
"The functor was never called (and apparently a bad value exception not thrown).");
|
||||||
|
std::cout << " Found instance when type and container lists were reset." << std:: endl;
|
||||||
|
|
||||||
|
CheckCalled = false;
|
||||||
|
array
|
||||||
|
.ResetContainerList(ContainerListTagUnusual())
|
||||||
|
.ResetTypeList(TypeListTagString())
|
||||||
|
.CastAndCall(CheckFunctor());
|
||||||
|
VTKM_TEST_ASSERT(CheckCalled,
|
||||||
|
"The functor was never called (and apparently a bad value exception not thrown).");
|
||||||
|
std::cout << " Found instance when container and type lists were reset." << std:: endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TestDynamicArrayHandle()
|
||||||
|
{
|
||||||
|
std::cout << "Try common types with default type lists." << std::endl;
|
||||||
|
TryDefaultType(vtkm::Id());
|
||||||
|
TryDefaultType(vtkm::Scalar());
|
||||||
|
TryDefaultType(vtkm::Vector3());
|
||||||
|
|
||||||
|
std::cout << "Try all VTK-m types." << std::endl;
|
||||||
|
vtkm::testing::Testing::TryAllTypes(TryBasicVTKmType());
|
||||||
|
|
||||||
|
std::cout << "Try unusual type." << std::endl;
|
||||||
|
TryUnusualType();
|
||||||
|
|
||||||
|
std::cout << "Try unusual container." << std::endl;
|
||||||
|
TryUnusualContainer();
|
||||||
|
|
||||||
|
std::cout << "Try unusual type in unusual container." << std::endl;
|
||||||
|
TryUnusualTypeAndContainer();
|
||||||
|
}
|
||||||
|
|
||||||
|
} // anonymous namespace
|
||||||
|
|
||||||
|
int UnitTestDynamicArrayHandle(int, char *[])
|
||||||
|
{
|
||||||
|
return vtkm::cont::testing::Testing::Run(TestDynamicArrayHandle);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user