10 #ifndef vtk_m_cont_UnknownArrayHandle_h
11 #define vtk_m_cont_UnknownArrayHandle_h
13 #include <vtkm/cont/vtkm_cont_export.h>
37 template <
typename T,
typename S>
38 void UnknownAHDelete(
void* mem)
41 AH* arrayHandle =
reinterpret_cast<AH*
>(mem);
45 template <
typename T,
typename S>
46 void* UnknownAHNewInstance()
51 template <
typename T,
typename S>
52 vtkm::Id UnknownAHNumberOfValues(
void* mem)
55 AH* arrayHandle =
reinterpret_cast<AH*
>(mem);
59 template <typename T, typename StaticSize = typename vtkm::internal::SafeVecTraits<T>::IsSizeStatic>
60 struct UnknownAHNumberOfComponentsImpl;
62 struct UnknownAHNumberOfComponentsImpl<T,
vtkm::VecTraitsTagSizeStatic>
64 static constexpr
vtkm::IdComponent Value = vtkm::internal::SafeVecTraits<T>::NUM_COMPONENTS;
67 struct UnknownAHNumberOfComponentsImpl<T,
vtkm::VecTraitsTagSizeVariable>
75 return UnknownAHNumberOfComponentsImpl<T>::Value;
79 typename =
typename vtkm::internal::SafeVecTraits<T>::IsSizeStatic,
81 struct UnknownAHNumberOfComponentsFlatImpl;
83 struct UnknownAHNumberOfComponentsFlatImpl<T,
vtkm::VecTraitsTagSizeStatic, std::true_type>
88 struct UnknownAHNumberOfComponentsFlatImpl<T,
vtkm::VecTraitsTagSizeVariable, std::true_type>
93 struct UnknownAHNumberOfComponentsFlatImpl<T,
vtkm::VecTraitsTagSizeStatic, std::false_type>
101 return UnknownAHNumberOfComponentsFlatImpl<T>::Value;
104 template <
typename T,
typename S>
105 void UnknownAHAllocate(
void* mem,
111 AH* arrayHandle =
reinterpret_cast<AH*
>(mem);
112 arrayHandle->
Allocate(numValues, preserve, token);
115 template <
typename T,
typename S>
116 void UnknownAHShallowCopy(
const void* sourceMem,
void* destinationMem)
119 const AH* source =
reinterpret_cast<const AH*
>(sourceMem);
120 AH* destination =
reinterpret_cast<AH*
>(destinationMem);
121 *destination = *source;
124 template <
typename T,
typename S>
125 void UnknownAHDeepCopy(
const void* sourceMem,
void* destinationMem)
128 const AH* source =
reinterpret_cast<const AH*
>(sourceMem);
129 AH* destination =
reinterpret_cast<AH*
>(destinationMem);
133 template <
typename T,
typename S>
134 std::vector<vtkm::cont::internal::Buffer>
138 AH* arrayHandle =
reinterpret_cast<AH*
>(mem);
140 return componentArray.GetBuffers();
143 template <
typename T,
typename S>
144 void UnknownAHReleaseResources(
void* mem)
147 AH* arrayHandle =
reinterpret_cast<AH*
>(mem);
151 template <
typename T,
typename S>
152 void UnknownAHReleaseResourcesExecution(
void* mem)
155 AH* arrayHandle =
reinterpret_cast<AH*
>(mem);
159 template <
typename T,
typename S>
160 void UnknownAHPrintSummary(
void* mem, std::ostream& out,
bool full)
163 AH* arrayHandle =
reinterpret_cast<AH*
>(mem);
167 struct VTKM_CONT_EXPORT UnknownAHContainer;
169 struct MakeUnknownAHContainerFunctor
171 template <
typename T,
typename S>
175 struct VTKM_CONT_EXPORT UnknownAHComponentInfo
177 std::type_index Type;
183 UnknownAHComponentInfo() =
delete;
185 bool operator==(
const UnknownAHComponentInfo& rhs);
187 template <
typename T>
188 static UnknownAHComponentInfo Make()
190 return UnknownAHComponentInfo{
typeid(T),
191 std::is_integral<T>::value,
192 std::is_floating_point<T>::value,
193 std::is_signed<T>::value,
198 UnknownAHComponentInfo(std::type_index&& type,
203 : Type(std::move(type))
204 , IsIntegral(isIntegral)
212 struct VTKM_CONT_EXPORT UnknownAHContainer
214 void* ArrayHandlePointer;
216 std::type_index ValueType;
217 std::type_index StorageType;
218 UnknownAHComponentInfo BaseComponentType;
220 using DeleteType = void(
void*);
221 DeleteType* DeleteFunction;
223 using NewInstanceType =
void*();
224 NewInstanceType* NewInstance;
226 using NewInstanceBasicType = std::shared_ptr<UnknownAHContainer>();
227 NewInstanceBasicType* NewInstanceBasic;
228 NewInstanceBasicType* NewInstanceFloatBasic;
230 using NumberOfValuesType =
vtkm::Id(
void*);
231 NumberOfValuesType* NumberOfValues;
234 NumberOfComponentsType* NumberOfComponents;
235 NumberOfComponentsType* NumberOfComponentsFlat;
238 AllocateType* Allocate;
240 using ShallowCopyType = void(
const void*,
void*);
241 ShallowCopyType* ShallowCopy;
243 using DeepCopyType = void(
const void*,
void*);
244 DeepCopyType* DeepCopy;
246 using ExtractComponentType = std::vector<vtkm::cont::internal::Buffer>(
void*,
249 ExtractComponentType* ExtractComponent;
251 using ReleaseResourcesType = void(
void*);
252 ReleaseResourcesType* ReleaseResources;
253 ReleaseResourcesType* ReleaseResourcesExecution;
255 using PrintSummaryType = void(
void*, std::ostream&,
bool);
256 PrintSummaryType* PrintSummary;
258 void operator=(
const UnknownAHContainer&) =
delete;
260 ~UnknownAHContainer() { this->DeleteFunction(this->ArrayHandlePointer); }
262 std::shared_ptr<UnknownAHContainer> MakeNewInstance()
const;
264 template <
typename T,
typename S>
267 return std::shared_ptr<UnknownAHContainer>(
new UnknownAHContainer(array));
270 template <
typename TargetT,
typename SourceT,
typename SourceS>
271 static std::shared_ptr<UnknownAHContainer> Make(
279 template <
typename T,
typename... Ss>
280 static std::shared_ptr<UnknownAHContainer> Make(
284 .GetArrayHandleVariant();
285 if (variant.IsValid())
287 return variant.CastAndCall(MakeUnknownAHContainerFunctor{});
291 return std::shared_ptr<UnknownAHContainer>{};
296 UnknownAHContainer(
const UnknownAHContainer&) =
default;
298 template <
typename T,
typename S>
302 template <
typename T>
307 template <
typename T>
311 vtkm::cont::TypeToString<T>());
313 template <
typename T>
314 std::shared_ptr<UnknownAHContainer> UnknownAHNewInstanceBasic()
316 return UnknownAHNewInstanceBasic<T>(
typename vtkm::internal::SafeVecTraits<T>::IsSizeStatic{});
319 template <
typename T>
322 using FloatT =
typename vtkm::internal::SafeVecTraits<T>::template ReplaceBaseComponentType<
326 template <
typename T>
330 vtkm::cont::TypeToString<T>());
332 template <
typename T>
333 std::shared_ptr<UnknownAHContainer> UnknownAHNewInstanceFloatBasic()
335 return UnknownAHNewInstanceFloatBasic<T>(
336 typename vtkm::internal::SafeVecTraits<T>::IsSizeStatic{});
339 template <
typename T,
typename S>
341 : ArrayHandlePointer(new
vtkm::cont::ArrayHandle<T, S>(array))
342 , ValueType(typeid(T))
343 , StorageType(typeid(S))
345 UnknownAHComponentInfo::Make<typename
vtkm::internal::SafeVecTraits<T>::BaseComponentType>())
346 , DeleteFunction(detail::UnknownAHDelete<T, S>)
347 , NewInstance(detail::UnknownAHNewInstance<T, S>)
348 , NewInstanceBasic(detail::UnknownAHNewInstanceBasic<T>)
349 , NewInstanceFloatBasic(detail::UnknownAHNewInstanceFloatBasic<T>)
350 , NumberOfValues(detail::UnknownAHNumberOfValues<T, S>)
351 , NumberOfComponents(detail::UnknownAHNumberOfComponents<T>)
352 , NumberOfComponentsFlat(detail::UnknownAHNumberOfComponentsFlat<T>)
353 , Allocate(detail::UnknownAHAllocate<T, S>)
354 , ShallowCopy(detail::UnknownAHShallowCopy<T, S>)
355 , DeepCopy(detail::UnknownAHDeepCopy<T, S>)
356 , ExtractComponent(detail::UnknownAHExtractComponent<T, S>)
357 , ReleaseResources(detail::UnknownAHReleaseResources<T, S>)
358 , ReleaseResourcesExecution(detail::UnknownAHReleaseResourcesExecution<T, S>)
359 , PrintSummary(detail::UnknownAHPrintSummary<T, S>)
363 template <
typename T,
typename S>
364 inline std::shared_ptr<UnknownAHContainer> MakeUnknownAHContainerFunctor::operator()(
367 return UnknownAHContainer::Make(array);
373 template <
typename ValueTypeList,
typename StorageTypeList>
374 class UncertainArrayHandle;
410 VTKM_CONT bool IsValueTypeImpl(std::type_index type)
const;
411 VTKM_CONT bool IsStorageTypeImpl(std::type_index type)
const;
412 VTKM_CONT bool IsBaseComponentTypeImpl(
const detail::UnknownAHComponentInfo& type)
const;
417 template <
typename T,
typename S>
419 : Container(detail::UnknownAHContainer::Make(array))
469 VTKM_CONT std::string GetValueTypeName()
const;
474 VTKM_CONT std::string GetBaseComponentTypeName()
const;
479 VTKM_CONT std::string GetStorageTypeName()
const;
486 VTKM_CONT std::string GetArrayTypeName()
const;
490 template <
typename ValueType>
493 return this->IsValueTypeImpl(
typeid(ValueType));
498 template <
typename StorageType>
501 return this->IsStorageTypeImpl(
typeid(StorageType));
515 template <
typename BaseComponentType>
518 return this->IsBaseComponentTypeImpl(detail::UnknownAHComponentInfo::Make<BaseComponentType>());
533 template <
typename ArrayHandleType>
537 return (this->IsValueType<typename ArrayHandleType::ValueType>() &&
538 this->IsStorageType<typename ArrayHandleType::StorageTag>());
549 template <
typename NewValueTypeList,
typename NewStorageTypeList>
551 NewValueTypeList = NewValueTypeList{},
552 NewStorageTypeList = NewStorageTypeList{})
const;
599 template <
typename ArrayHandleType>
610 template <
typename T,
typename S>
619 if (!this->IsType<ArrayType>())
625 array = *
reinterpret_cast<ArrayType*
>(this->Container->ArrayHandlePointer);
628 template <
typename T,
typename... Ss>
632 template <
typename TargetT,
typename SourceT,
typename SourceS>
638 this->AsArrayHandle<ContainedArrayType>());
641 template <
typename ArrayType>
646 this->AsArrayHandle(array);
729 template <
typename BaseComponentType>
735 if (!this->IsBaseComponentType<BaseComponentType>())
739 "component array of " + vtkm::cont::TypeToString<BaseComponentType>());
742 auto buffers = this->Container->ExtractComponent(
743 this->Container->ArrayHandlePointer, componentIndex, allowCopy);
744 return ComponentArrayType(buffers);
788 template <
typename BaseComponentType>
810 template <
typename TypeList,
typename StorageList,
typename Functor,
typename... Args>
811 VTKM_CONT void CastAndCallForTypes(Functor&& functor, Args&&... args)
const;
826 template <
typename TypeList,
typename StorageList,
typename Functor,
typename... Args>
827 VTKM_CONT void CastAndCallForTypesWithFloatFallback(Functor&& functor, Args&&... args)
const;
850 template <
typename Functor,
typename... Args>
851 VTKM_CONT void CastAndCallWithExtractedArray(Functor&& functor, Args&&... args)
const;
856 VTKM_CONT void ReleaseResourcesExecution()
const;
862 VTKM_CONT void PrintSummary(std::ostream& out,
bool full =
false)
const;
871 template <
typename T,
typename S>
872 struct UnknownArrayHandleCanConvert
880 template <
typename TargetT,
typename SourceT,
typename SourceS>
881 struct UnknownArrayHandleCanConvert<TargetT,
vtkm::cont::StorageTagCast<SourceT, SourceS>>
885 return UnknownArrayHandleCanConvert<SourceT, SourceS>{}(array);
889 template <
typename T>
890 struct UnknownArrayHandleCanConvertTry
892 template <
typename S>
895 canConvert |= UnknownArrayHandleCanConvert<T, S>{}(array);
899 template <
typename T,
typename... Ss>
900 struct UnknownArrayHandleCanConvert<T,
vtkm::cont::StorageTagMultiplexer<Ss...>>
904 bool canConvert =
false;
912 template <
typename ArrayHandleType>
917 return detail::UnknownArrayHandleCanConvert<
typename ArrayHandleType::ValueType,
918 typename ArrayHandleType::StorageTag>{}(*this);
924 struct UnknownArrayHandleMultiplexerCastTry
926 template <
typename T,
typename S,
typename... Ss>
931 bool& converted)
const
936 if (converted && !unknownArray.
IsType<ArrayType>())
954 template <
typename T,
typename... Ss>
955 void UnknownArrayHandle::AsArrayHandle(
958 bool converted =
false;
960 detail::UnknownArrayHandleMultiplexerCastTry{},
vtkm::List<Ss...>{}, *
this, array, converted);
972 struct UnknownArrayHandleTry
974 template <
typename T,
typename S,
typename Functor,
typename... Args>
979 Args&&... args)
const
982 if (!called && unknownArray.
CanConvert<DerivedArrayType>())
985 DerivedArrayType derivedArray;
997 f(derivedArray, std::forward<Args>(args)...);
1010 template <
typename T>
1011 struct IsUndefinedArrayType
1014 template <
typename T,
typename S>
1015 struct IsUndefinedArrayType<
vtkm::List<T, S>> : vtkm::cont::internal::IsInvalidArrayHandle<T, S>
1021 template <
typename ValueTypeList,
typename StorageTypeList>
1022 using ListAllArrayTypes =
1026 const std::type_info&);
1030 template <
typename TypeList,
typename StorageTagList,
typename Functor,
typename... Args>
1031 inline void UnknownArrayHandle::CastAndCallForTypes(Functor&& f, Args&&... args)
const
1033 using crossProduct = internal::ListAllArrayTypes<TypeList, StorageTagList>;
1035 bool called =
false;
1038 std::forward<Functor>(f),
1041 std::forward<Args>(args)...);
1046 internal::ThrowCastAndCallException(*
this,
typeid(TypeList));
1050 template <
typename TypeList,
typename StorageTagList,
typename Functor,
typename... Args>
1051 VTKM_CONT void UnknownArrayHandle::CastAndCallForTypesWithFloatFallback(Functor&& functor,
1052 Args&&... args)
const
1054 using crossProduct = internal::ListAllArrayTypes<TypeList, StorageTagList>;
1056 bool called =
false;
1059 std::forward<Functor>(functor),
1062 std::forward<Args>(args)...);
1070 std::forward<Functor>(functor),
1073 std::forward<Args>(args)...);
1079 internal::ThrowCastAndCallException(*
this,
typeid(TypeList));
1088 template <
typename ArrayHandleType>
1091 return array.template IsType<ArrayHandleType>();
1098 template <
typename ArrayHandleType>
1101 return array.template AsArrayHandle<ArrayHandleType>();
1107 struct UnknownArrayHandleTryExtract
1109 template <
typename T,
typename Functor,
typename... Args>
1114 Args&&... args)
const
1129 f(extractedArray, std::forward<Args>(args)...);
1136 template <
typename Functor,
typename... Args>
1137 inline void UnknownArrayHandle::CastAndCallWithExtractedArray(Functor&& functor,
1138 Args&&... args)
const
1140 bool called =
false;
1143 std::forward<Functor>(functor),
1146 std::forward<Args>(args)...);
1170 struct VTKM_CONT_EXPORT SerializableTypeString<
vtkm::cont::UnknownArrayHandle>
1181 struct VTKM_CONT_EXPORT Serialization<
vtkm::cont::UnknownArrayHandle>
1192 #endif //vtk_m_cont_UnknownArrayHandle_h