10 #ifndef vtk_m_cont_ArrayHandleCast_h
11 #define vtk_m_cont_ArrayHandleCast_h
27 template <
typename SourceT,
typename SourceStorage>
35 template <
typename FromType,
typename ToType>
47 #if (!(defined(VTKM_CUDA) && (__CUDACC_VER_MAJOR__ < 8)))
48 #if (defined(VTKM_GCC) || defined(VTKM_CLANG))
49 #pragma GCC diagnostic push
50 #pragma GCC diagnostic ignored "-Wunknown-pragmas"
51 #pragma GCC diagnostic ignored "-Wpragmas"
52 #pragma GCC diagnostic ignored "-Wconversion"
53 #pragma GCC diagnostic ignored "-Wfloat-conversion"
54 #endif // gcc || clang
55 #endif //not using cuda < 8
56 #if defined(VTKM_MSVC)
58 #pragma warning(disable : 4244)
62 ToType operator()(
const FromType& val)
const {
return static_cast<ToType
>(val); }
64 #if (!(defined(VTKM_CUDA) && (__CUDACC_VER_MAJOR__ < 8)))
65 #if (defined(VTKM_GCC) || defined(VTKM_CLANG))
66 #pragma GCC diagnostic pop
67 #endif // gcc || clang
68 #endif // not using cuda < 8
69 #if defined(VTKM_MSVC)
77 template <
typename TargetT,
typename SourceT,
typename SourceStorage,
bool... CastFlags>
78 struct ArrayHandleCastTraits;
80 template <
typename TargetT,
typename SourceT,
typename SourceStorage>
81 struct ArrayHandleCastTraits<TargetT, SourceT, SourceStorage>
82 : ArrayHandleCastTraits<TargetT,
85 std::is_convertible<SourceT, TargetT>::value,
86 std::is_convertible<TargetT, SourceT>::value>
91 template <
typename TargetT,
typename SourceT,
typename SourceStorage,
bool CanCastBackward>
92 struct ArrayHandleCastTraits<TargetT, SourceT, SourceStorage, false, CanCastBackward>
94 struct StorageSuperclass : vtkm::cont::internal::UndefinedStorage
96 using PortalType = vtkm::cont::internal::detail::UndefinedArrayPortal<TargetT>;
97 using PortalConstType = vtkm::cont::internal::detail::UndefinedArrayPortal<TargetT>;
102 template <
typename TargetT,
typename SourceT,
typename SourceStorage>
103 struct ArrayHandleCastTraits<TargetT, SourceT, SourceStorage, true, false>
105 using StorageTagSuperclass = StorageTagTransform<vtkm::cont::ArrayHandle<SourceT, SourceStorage>,
106 vtkm::cont::internal::Cast<SourceT, TargetT>>;
107 using StorageSuperclass = vtkm::cont::internal::Storage<TargetT, StorageTagSuperclass>;
111 template <
typename TargetT,
typename SourceT,
typename SourceStorage>
112 struct ArrayHandleCastTraits<TargetT, SourceT, SourceStorage, true, true>
114 using StorageTagSuperclass = StorageTagTransform<vtkm::cont::ArrayHandle<SourceT, SourceStorage>,
115 vtkm::cont::internal::Cast<SourceT, TargetT>,
116 vtkm::cont::internal::Cast<TargetT, SourceT>>;
117 using StorageSuperclass = vtkm::cont::internal::Storage<TargetT, StorageTagSuperclass>;
122 template <
typename TargetT,
typename SourceT,
typename SourceStorage_>
123 struct Storage<TargetT,
vtkm::cont::StorageTagCast<SourceT, SourceStorage_>>
124 : detail::ArrayHandleCastTraits<TargetT, SourceT, SourceStorage_>::StorageSuperclass
127 typename detail::ArrayHandleCastTraits<TargetT, SourceT, SourceStorage_>::StorageSuperclass;
129 using Superclass::Superclass;
140 template <
typename T,
typename ArrayHandleType>
144 StorageTagCast<typename ArrayHandleType::ValueType, typename ArrayHandleType::StorageTag>>
156 typename ArrayHandleType::StorageTag>& handle)
159 this->ValidateTypeCast<typename ArrayHandleType::ValueType>();
171 return Superclass::StorageType::GetArray(this->
GetBuffers());
176 template <
typename SrcValueType>
177 VTKM_CONT static typename std::enable_if<!std::is_same<T, SrcValueType>::value>::type
180 #ifdef VTKM_ENABLE_LOGGING
181 using DstValueType = T;
184 using SrcLimits = std::numeric_limits<SrcComp>;
185 using DstLimits = std::numeric_limits<DstComp>;
187 const vtkm::Range SrcRange{ SrcLimits::lowest(), SrcLimits::max() };
188 const vtkm::Range DstRange{ DstLimits::lowest(), DstLimits::max() };
190 const bool RangeLoss = (SrcRange.Max > DstRange.Max || SrcRange.Min < DstRange.Min);
191 const bool PrecLoss = SrcLimits::digits > DstLimits::digits;
193 if (RangeLoss && PrecLoss)
196 "ArrayHandleCast: Casting ComponentType of "
197 "%s to %s reduces range and precision.",
198 vtkm::cont::TypeToString<SrcValueType>().c_str(),
199 vtkm::cont::TypeToString<DstValueType>().c_str());
204 "ArrayHandleCast: Casting ComponentType of "
205 "%s to %s reduces range.",
206 vtkm::cont::TypeToString<SrcValueType>().c_str(),
207 vtkm::cont::TypeToString<DstValueType>().c_str());
212 "ArrayHandleCast: Casting ComponentType of "
213 "%s to %s reduces precision.",
214 vtkm::cont::TypeToString<SrcValueType>().c_str(),
215 vtkm::cont::TypeToString<DstValueType>().c_str());
220 template <
typename SrcValueType>
221 VTKM_CONT static typename std::enable_if<std::is_same<T, SrcValueType>::value>::type
231 template <
typename CastType,
typename OriginalType,
typename ArrayType>
232 struct MakeArrayHandleCastImpl
236 VTKM_CONT static ReturnType DoMake(
const ArrayType& array) {
return ReturnType(array); }
239 template <
typename T,
typename ArrayType>
240 struct MakeArrayHandleCastImpl<T, T, ArrayType>
242 using ReturnType = ArrayType;
244 VTKM_CONT static ReturnType DoMake(
const ArrayType& array) {
return array; }
252 template <
typename T,
typename ArrayType>
254 typename detail::MakeArrayHandleCastImpl<T, typename ArrayType::ValueType, ArrayType>::ReturnType
258 using MakeImpl = detail::MakeArrayHandleCastImpl<T, typename ArrayType::ValueType, ArrayType>;
259 return MakeImpl::DoMake(array);
273 template <
typename T,
typename AH>
274 struct SerializableTypeString<
vtkm::cont::ArrayHandleCast<T, AH>>
278 static std::string name =
284 template <
typename T1,
typename T2,
typename S>
285 struct SerializableTypeString<
vtkm::cont::ArrayHandle<T1, vtkm::cont::StorageTagCast<T2, S>>>
286 : SerializableTypeString<vtkm::cont::ArrayHandleCast<T1, vtkm::cont::ArrayHandle<T2, S>>>
295 template <
typename TargetT,
typename SourceT,
typename SourceStorage>
296 struct Serialization<
297 vtkm::cont::ArrayHandle<TargetT, vtkm::cont::StorageTagCast<SourceT, SourceStorage>>>
304 static VTKM_CONT void save(BinaryBuffer& bb,
const BaseType& obj)
315 obj = vtkm::cont::make_ArrayHandleCast<TargetT>(array);
319 template <
typename TargetT,
typename AH>
320 struct Serialization<
vtkm::cont::ArrayHandleCast<TargetT, AH>>
321 : Serialization<vtkm::cont::ArrayHandle<
323 vtkm::cont::StorageTagCast<typename AH::ValueType, typename AH::StorageTag>>>
330 #endif // vtk_m_cont_ArrayHandleCast_h