vtk-m/vtkm/cont/ArrayHandleCast.h

93 lines
3.0 KiB
C
Raw Normal View History

2015-09-01 21:42:45 +00:00
//=============================================================================
//
// 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 2015 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
2015-09-01 21:42:45 +00:00
// Copyright 2015 UT-Battelle, LLC.
// Copyright 2015 Los Alamos National Security.
//
// Under the terms of Contract DE-NA0003525 with NTESS,
2015-09-01 21:42:45 +00:00
// 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_ArrayHandleCast_h
#define vtk_m_cont_ArrayHandleCast_h
#include <vtkm/cont/ArrayHandleTransform.h>
2017-05-18 14:29:41 +00:00
namespace vtkm
{
namespace cont
{
2015-09-01 21:42:45 +00:00
2017-05-18 14:29:41 +00:00
namespace internal
{
2015-09-01 21:42:45 +00:00
2017-05-18 14:29:41 +00:00
template <typename FromType, typename ToType>
struct VTKM_ALWAYS_EXPORT Cast
2015-09-01 21:42:45 +00:00
{
VTKM_EXEC_CONT
2017-05-18 14:29:41 +00:00
ToType operator()(const FromType& val) const { return static_cast<ToType>(val); }
2015-09-01 21:42:45 +00:00
};
} // namespace internal
/// \brief Cast the values of an array to the specified type, on demand.
///
/// ArrayHandleCast is a specialization of ArrayHandleTransform. Given an ArrayHandle
/// and a type, it creates a new handle that returns the elements of the array cast
/// to the specified type.
///
template <typename T, typename ArrayHandleType>
2017-05-18 14:29:41 +00:00
class ArrayHandleCast
2017-05-30 14:00:01 +00:00
: public vtkm::cont::ArrayHandleTransform<ArrayHandleType,
2017-05-18 14:29:41 +00:00
internal::Cast<typename ArrayHandleType::ValueType, T>,
internal::Cast<T, typename ArrayHandleType::ValueType>>
{
public:
VTKM_ARRAY_HANDLE_SUBCLASS(
ArrayHandleCast,
(ArrayHandleCast<T, ArrayHandleType>),
2017-05-30 14:00:01 +00:00
(vtkm::cont::ArrayHandleTransform<ArrayHandleType,
2017-05-18 14:29:41 +00:00
internal::Cast<typename ArrayHandleType::ValueType, T>,
internal::Cast<T, typename ArrayHandleType::ValueType>>));
2017-05-18 14:29:41 +00:00
ArrayHandleCast(const ArrayHandleType& handle)
: Superclass(handle)
2017-05-18 14:29:41 +00:00
{
}
};
2015-09-01 21:42:45 +00:00
/// make_ArrayHandleCast is convenience function to generate an
/// ArrayHandleCast.
///
template <typename T, typename HandleType>
2017-05-18 14:29:41 +00:00
VTKM_CONT ArrayHandleCast<T, HandleType> make_ArrayHandleCast(const HandleType& handle,
const T& = T())
2015-09-01 21:42:45 +00:00
{
return ArrayHandleCast<T, HandleType>(handle);
}
/// Overriden version of \c make_ArrayHandleCast that returns the original object type.
///
template <typename T, typename Storage>
VTKM_CONT vtkm::cont::ArrayHandle<T, Storage> make_ArrayHandleCast(
const vtkm::cont::ArrayHandle<T, Storage>& handle,
const T& = T())
{
return handle;
}
2015-09-01 21:42:45 +00:00
}
} // namespace vtkm::cont
#endif // vtk_m_cont_ArrayHandleCast_h