10 #ifndef vtk_m_cont_UnknownArrayHandle_h
11 #define vtk_m_cont_UnknownArrayHandle_h
39 template <
typename T,
typename S>
40 void UnknownAHDelete(
void* mem)
43 AH* arrayHandle =
reinterpret_cast<AH*
>(mem);
47 template <
typename T,
typename S>
48 const std::vector<vtkm::cont::internal::Buffer>& UnknownAHBuffers(
void* mem)
51 AH* arrayHandle =
reinterpret_cast<AH*
>(mem);
55 template <
typename T,
typename S>
56 void* UnknownAHNewInstance()
61 template <
typename T,
typename S>
62 vtkm::Id UnknownAHNumberOfValues(
void* mem)
65 AH* arrayHandle =
reinterpret_cast<AH*
>(mem);
69 template <
typename T,
typename S>
73 AH* arrayHandle =
reinterpret_cast<AH*
>(mem);
78 template <
typename T,
typename S>
79 inline auto UnknownAHNumberOfComponentsImpl(
void* mem)
80 -> decltype(vtkm::cont::internal::Storage<T, S>::GetNumberOfComponents(
81 std::vector<vtkm::cont::internal::Buffer>()))
84 AH* arrayHandle =
reinterpret_cast<AH*
>(mem);
85 return vtkm::cont::internal::Storage<T, S>::GetNumberOfComponents(arrayHandle->GetBuffers());
89 template <
typename T,
typename S>
97 template <
typename T,
typename S>
100 return UnknownAHNumberOfComponentsFlat<T, S>(mem);
103 template <
typename T,
typename S>
109 template <
typename T,
typename S>
110 void UnknownAHAllocate(
void* mem,
116 AH* arrayHandle =
reinterpret_cast<AH*
>(mem);
117 arrayHandle->
Allocate(numValues, preserve, token);
120 template <
typename T,
typename S>
121 void UnknownAHShallowCopy(
const void* sourceMem,
void* destinationMem)
124 const AH* source =
reinterpret_cast<const AH*
>(sourceMem);
125 AH* destination =
reinterpret_cast<AH*
>(destinationMem);
126 *destination = *source;
129 template <
typename T,
typename S>
130 void UnknownAHDeepCopy(
const void* sourceMem,
void* destinationMem)
133 const AH* source =
reinterpret_cast<const AH*
>(sourceMem);
134 AH* destination =
reinterpret_cast<AH*
>(destinationMem);
138 template <
typename T,
typename S>
139 std::vector<vtkm::cont::internal::Buffer>
143 AH* arrayHandle =
reinterpret_cast<AH*
>(mem);
145 return componentArray.GetBuffers();
148 template <
typename T,
typename S>
149 void UnknownAHReleaseResources(
void* mem)
152 AH* arrayHandle =
reinterpret_cast<AH*
>(mem);
156 template <
typename T,
typename S>
157 void UnknownAHReleaseResourcesExecution(
void* mem)
160 AH* arrayHandle =
reinterpret_cast<AH*
>(mem);
164 template <
typename T,
typename S>
165 void UnknownAHPrintSummary(
void* mem, std::ostream& out,
bool full)
168 AH* arrayHandle =
reinterpret_cast<AH*
>(mem);
174 struct MakeUnknownAHContainerFunctor
176 template <
typename T,
typename S>
182 std::type_index Type;
188 UnknownAHComponentInfo() =
delete;
190 bool operator==(
const UnknownAHComponentInfo& rhs);
192 template <
typename T>
193 static UnknownAHComponentInfo Make()
195 return UnknownAHComponentInfo{
typeid(T),
196 std::is_integral<T>::value,
197 std::is_floating_point<T>::value,
198 std::is_signed<T>::value,
203 UnknownAHComponentInfo(std::type_index&& type,
208 : Type(std::move(type))
209 , IsIntegral(isIntegral)
219 void* ArrayHandlePointer;
221 std::type_index ValueType;
222 std::type_index StorageType;
223 UnknownAHComponentInfo BaseComponentType;
225 using DeleteType = void(
void*);
226 DeleteType* DeleteFunction;
228 using BuffersType =
const std::vector<vtkm::cont::internal::Buffer>&(
void*);
229 BuffersType* Buffers;
231 using NewInstanceType =
void*();
232 NewInstanceType* NewInstance;
234 using NewInstanceBasicType = std::shared_ptr<UnknownAHContainer>(
void*);
235 NewInstanceBasicType* NewInstanceBasic;
236 NewInstanceBasicType* NewInstanceFloatBasic;
238 using NumberOfValuesType =
vtkm::Id(
void*);
239 NumberOfValuesType* NumberOfValues;
242 NumberOfComponentsType* NumberOfComponents;
243 NumberOfComponentsType* NumberOfComponentsFlat;
246 AllocateType* Allocate;
248 using ShallowCopyType = void(
const void*,
void*);
249 ShallowCopyType* ShallowCopy;
251 using DeepCopyType = void(
const void*,
void*);
252 DeepCopyType* DeepCopy;
254 using ExtractComponentType = std::vector<vtkm::cont::internal::Buffer>(
void*,
257 ExtractComponentType* ExtractComponent;
259 using ReleaseResourcesType = void(
void*);
260 ReleaseResourcesType* ReleaseResources;
261 ReleaseResourcesType* ReleaseResourcesExecution;
263 using PrintSummaryType = void(
void*, std::ostream&,
bool);
264 PrintSummaryType* PrintSummary;
266 void operator=(
const UnknownAHContainer&) =
delete;
268 ~UnknownAHContainer() { this->DeleteFunction(this->ArrayHandlePointer); }
270 std::shared_ptr<UnknownAHContainer> MakeNewInstance()
const;
272 template <
typename T,
typename S>
275 return std::shared_ptr<UnknownAHContainer>(
new UnknownAHContainer(array));
278 template <
typename TargetT,
typename SourceT,
typename SourceS>
279 static std::shared_ptr<UnknownAHContainer> Make(
287 template <
typename T,
typename... Ss>
288 static std::shared_ptr<UnknownAHContainer> Make(
292 .GetArrayHandleVariant();
293 if (variant.IsValid())
295 return variant.CastAndCall(MakeUnknownAHContainerFunctor{});
299 return std::shared_ptr<UnknownAHContainer>{};
304 UnknownAHContainer(
const UnknownAHContainer&) =
default;
306 template <
typename T,
typename S>
310 template <
typename T,
typename S>
315 template <
typename T,
typename S>
316 std::shared_ptr<UnknownAHContainer> UnknownAHNewInstanceBasic(
void* mem,
320 if (numComponents < 1)
324 vtkm::cont::TypeToString<T>());
329 template <
typename T,
typename S>
330 std::shared_ptr<UnknownAHContainer> UnknownAHNewInstanceBasic(
void* mem)
335 template <
typename T,
typename S>
336 std::shared_ptr<UnknownAHContainer> UnknownAHNewInstanceFloatBasic(
void*,
342 template <
typename T,
typename S>
343 std::shared_ptr<UnknownAHContainer> UnknownAHNewInstanceFloatBasic(
void* mem,
347 if (numComponents < 1)
351 vtkm::cont::TypeToString<T>());
353 return UnknownAHContainer::Make(
356 template <
typename T,
typename S>
357 std::shared_ptr<UnknownAHContainer> UnknownAHNewInstanceFloatBasic(
void* mem)
362 template <
typename T,
typename S>
364 : ArrayHandlePointer(new
vtkm::cont::ArrayHandle<T, S>(array))
365 , ValueType(typeid(T))
366 , StorageType(typeid(S))
368 UnknownAHComponentInfo::Make<typename
vtkm::VecTraits<T>::BaseComponentType>())
369 , DeleteFunction(detail::UnknownAHDelete<T, S>)
370 , Buffers(detail::UnknownAHBuffers<T, S>)
371 , NewInstance(detail::UnknownAHNewInstance<T, S>)
372 , NewInstanceBasic(detail::UnknownAHNewInstanceBasic<T, S>)
373 , NewInstanceFloatBasic(detail::UnknownAHNewInstanceFloatBasic<T, S>)
374 , NumberOfValues(detail::UnknownAHNumberOfValues<T, S>)
375 , NumberOfComponents(detail::UnknownAHNumberOfComponents<T, S>)
376 , NumberOfComponentsFlat(detail::UnknownAHNumberOfComponentsFlat<T, S>)
377 , Allocate(detail::UnknownAHAllocate<T, S>)
378 , ShallowCopy(detail::UnknownAHShallowCopy<T, S>)
379 , DeepCopy(detail::UnknownAHDeepCopy<T, S>)
380 , ExtractComponent(detail::UnknownAHExtractComponent<T, S>)
381 , ReleaseResources(detail::UnknownAHReleaseResources<T, S>)
382 , ReleaseResourcesExecution(detail::UnknownAHReleaseResourcesExecution<T, S>)
383 , PrintSummary(detail::UnknownAHPrintSummary<T, S>)
387 template <
typename T,
typename S>
388 inline std::shared_ptr<UnknownAHContainer> MakeUnknownAHContainerFunctor::operator()(
391 return UnknownAHContainer::Make(array);
397 template <
typename ValueTypeList,
typename StorageTypeList>
398 class UncertainArrayHandle;
434 VTKM_CONT bool IsValueTypeImpl(std::type_index type)
const;
435 VTKM_CONT bool IsStorageTypeImpl(std::type_index type)
const;
436 VTKM_CONT bool IsBaseComponentTypeImpl(
const detail::UnknownAHComponentInfo& type)
const;
441 template <
typename T,
typename S>
443 : Container(detail::UnknownAHContainer::Make(array))
493 VTKM_CONT std::string GetValueTypeName()
const;
498 VTKM_CONT std::string GetBaseComponentTypeName()
const;
503 VTKM_CONT std::string GetStorageTypeName()
const;
510 VTKM_CONT std::string GetArrayTypeName()
const;
514 template <
typename ValueType>
517 return this->IsValueTypeImpl(
typeid(ValueType));
522 template <
typename StorageType>
525 return this->IsStorageTypeImpl(
typeid(StorageType));
539 template <
typename BaseComponentType>
542 return this->IsBaseComponentTypeImpl(detail::UnknownAHComponentInfo::Make<BaseComponentType>());
557 template <
typename ArrayHandleType>
561 return (this->IsValueType<typename ArrayHandleType::ValueType>() &&
562 this->IsStorageType<typename ArrayHandleType::StorageTag>());
573 template <
typename NewValueTypeList,
typename NewStorageTypeList>
575 NewValueTypeList = NewValueTypeList{},
576 NewStorageTypeList = NewStorageTypeList{})
const;
625 template <
typename ArrayHandleType>
638 template <
typename T,
typename S>
642 if (!this->IsType<ArrayType>())
648 array = *
reinterpret_cast<ArrayType*
>(this->Container->ArrayHandlePointer);
656 template <
typename T,
typename S>
659 this->BaseAsArrayHandle(array);
662 template <
typename T>
665 template <
typename T,
typename... Ss>
669 template <
typename TargetT,
typename SourceT,
typename SourceS>
675 this->AsArrayHandle<ContainedArrayType>());
678 template <
typename T>
682 using BaseT =
typename T::ComponentType;
683 if (this->IsStorageType<vtkm::cont::StorageTagBasic>() && this->IsBaseComponentType<BaseT>())
688 this->Container->Buffers(this->Container->ArrayHandlePointer));
694 this->BaseAsArrayHandle(array);
698 template <
typename ArrayType>
703 this->AsArrayHandle(array);
786 template <
typename BaseComponentType>
792 if (!this->IsBaseComponentType<BaseComponentType>())
796 "component array of " + vtkm::cont::TypeToString<BaseComponentType>());
799 auto buffers = this->Container->ExtractComponent(
800 this->Container->ArrayHandlePointer, componentIndex, allowCopy);
801 return ComponentArrayType(buffers);
846 template <
typename BaseComponentType>
868 template <
typename TypeList,
typename StorageList,
typename Functor,
typename... Args>
869 VTKM_CONT void CastAndCallForTypes(Functor&& functor, Args&&... args)
const;
884 template <
typename TypeList,
typename StorageList,
typename Functor,
typename... Args>
885 VTKM_CONT void CastAndCallForTypesWithFloatFallback(Functor&& functor, Args&&... args)
const;
908 template <
typename Functor,
typename... Args>
909 VTKM_CONT void CastAndCallWithExtractedArray(Functor&& functor, Args&&... args)
const;
914 VTKM_CONT void ReleaseResourcesExecution()
const;
921 VTKM_CONT void PrintSummary(std::ostream& out,
bool full =
false)
const;
930 template <
typename T,
typename S>
931 struct UnknownArrayHandleCanConvert
939 template <
typename T>
940 struct UnknownArrayHandleCanConvert<T,
vtkm::cont::StorageTagBasic>
944 using UnrolledVec = vtkm::internal::UnrollVec<T>;
952 template <
typename TargetT,
typename SourceT,
typename SourceS>
953 struct UnknownArrayHandleCanConvert<TargetT,
vtkm::cont::StorageTagCast<SourceT, SourceS>>
957 return UnknownArrayHandleCanConvert<SourceT, SourceS>{}(array);
961 template <
typename T>
962 struct UnknownArrayHandleCanConvertTry
964 template <
typename S>
967 canConvert |= UnknownArrayHandleCanConvert<T, S>{}(array);
971 template <
typename T,
typename... Ss>
972 struct UnknownArrayHandleCanConvert<T,
vtkm::cont::StorageTagMultiplexer<Ss...>>
976 bool canConvert =
false;
982 template <
typename T>
983 struct UnknownArrayHandleCanConvert<T,
vtkm::cont::StorageTagRuntimeVec>
987 using BaseComponentType =
typename T::ComponentType;
996 template <
typename ArrayHandleType>
997 VTKM_CONT inline bool UnknownArrayHandle::CanConvert()
const
1001 return detail::UnknownArrayHandleCanConvert<
typename ArrayHandleType::ValueType,
1002 typename ArrayHandleType::StorageTag>{}(*this);
1008 template <
typename T,
1011 struct UnknownArrayHandleRuntimeVecAsBasic
1014 const detail::UnknownAHContainer*,
1024 template <
typename T>
1025 struct UnknownArrayHandleRuntimeVecAsBasic<T, 1>
1028 const detail::UnknownAHContainer* container,
1031 using UnrolledVec = vtkm::internal::UnrollVec<T>;
1032 using ComponentType =
typename UnrolledVec::ComponentType;
1034 self->IsBaseComponentType<ComponentType>() &&
1035 UnrolledVec::NUM_COMPONENTS == self->GetNumberOfComponentsFlat())
1041 using StorageRuntimeVec =
1042 vtkm::cont::internal::Storage<RuntimeVecType, vtkm::cont::StorageTagRuntimeVec>;
1043 StorageRuntimeVec::AsArrayHandleBasic(container->Buffers(container->ArrayHandlePointer),
1056 template <
typename T>
1059 if (!detail::UnknownArrayHandleRuntimeVecAsBasic<T>{}(
this, this->Container.get(), array))
1061 this->BaseAsArrayHandle(array);
1068 struct UnknownArrayHandleMultiplexerCastTry
1070 template <
typename T,
typename S,
typename... Ss>
1075 bool& converted)
const
1080 if (converted && !unknownArray.
IsType<ArrayType>())
1098 template <
typename T,
typename... Ss>
1099 void UnknownArrayHandle::AsArrayHandle(
1102 bool converted =
false;
1104 detail::UnknownArrayHandleMultiplexerCastTry{},
vtkm::List<Ss...>{}, *
this, array, converted);
1116 struct UnknownArrayHandleTry
1118 template <
typename T,
typename S,
typename Functor,
typename... Args>
1123 Args&&... args)
const
1126 if (!called && unknownArray.
CanConvert<DerivedArrayType>())
1129 DerivedArrayType derivedArray;
1141 f(derivedArray, std::forward<Args>(args)...);
1154 template <
typename T>
1155 struct IsUndefinedArrayType
1158 template <
typename T,
typename S>
1159 struct IsUndefinedArrayType<
vtkm::List<T, S>> : vtkm::cont::internal::IsInvalidArrayHandle<T, S>
1165 template <
typename ValueTypeList,
typename StorageTypeList>
1166 using ListAllArrayTypes =
1170 const std::type_info&);
1174 template <
typename TypeList,
typename StorageTagList,
typename Functor,
typename... Args>
1175 inline void UnknownArrayHandle::CastAndCallForTypes(Functor&& f, Args&&... args)
const
1177 using crossProduct = internal::ListAllArrayTypes<TypeList, StorageTagList>;
1179 bool called =
false;
1182 std::forward<Functor>(f),
1185 std::forward<Args>(args)...);
1190 internal::ThrowCastAndCallException(*
this,
typeid(TypeList));
1194 template <
typename TypeList,
typename StorageTagList,
typename Functor,
typename... Args>
1195 VTKM_CONT void UnknownArrayHandle::CastAndCallForTypesWithFloatFallback(Functor&& functor,
1196 Args&&... args)
const
1198 using crossProduct = internal::ListAllArrayTypes<TypeList, StorageTagList>;
1200 bool called =
false;
1203 std::forward<Functor>(functor),
1206 std::forward<Args>(args)...);
1211 "Cast and call from %s failed. Copying to basic float array.",
1212 this->GetArrayTypeName().c_str());
1217 std::forward<Functor>(functor),
1220 std::forward<Args>(args)...);
1226 internal::ThrowCastAndCallException(*
this,
typeid(TypeList));
1235 template <
typename ArrayHandleType>
1238 return array.template IsType<ArrayHandleType>();
1245 template <
typename ArrayHandleType>
1248 return array.template AsArrayHandle<ArrayHandleType>();
1254 struct UnknownArrayHandleTryExtract
1256 template <
typename T,
typename Functor,
typename... Args>
1261 Args&&... args)
const
1276 f(extractedArray, std::forward<Args>(args)...);
1283 template <
typename Functor,
typename... Args>
1284 inline void UnknownArrayHandle::CastAndCallWithExtractedArray(Functor&& functor,
1285 Args&&... args)
const
1287 bool called =
false;
1290 std::forward<Functor>(functor),
1293 std::forward<Args>(args)...);
1339 #endif //vtk_m_cont_UnknownArrayHandle_h