10 #ifndef vtk_m_cont_ArrayHandleMultiplexer_h
11 #define vtk_m_cont_ArrayHandleMultiplexer_h
34 struct ArrayPortalMultiplexerGetNumberOfValuesFunctor
36 template <
typename PortalType>
39 return portal.GetNumberOfValues();
43 struct ArrayPortalMultiplexerGetFunctor
46 template <
typename PortalType>
47 VTKM_EXEC_CONT typename PortalType::ValueType operator()(
const PortalType& portal,
50 return portal.Get(index);
54 struct ArrayPortalMultiplexerSetFunctor
56 template <
typename PortalType>
59 const typename PortalType::ValueType& value)
const noexcept
62 portal, index, value,
typename vtkm::internal::PortalSupportsSets<PortalType>::type{});
67 template <
typename PortalType>
70 const typename PortalType::ValueType& value,
71 std::true_type)
const noexcept
73 portal.Set(index, value);
77 template <
typename PortalType>
80 const typename PortalType::ValueType&,
81 std::false_type)
const noexcept
84 VTKM_ASSERT(
false &&
"Calling Set on a portal that does not support it.");
90 template <
typename... PortalTypes>
91 struct ArrayPortalMultiplexer
93 using PortalVariantType = vtkm::exec::internal::Variant<PortalTypes...>;
94 PortalVariantType PortalVariant;
96 using ValueType =
typename PortalVariantType::template TypeAt<0>::ValueType;
98 ArrayPortalMultiplexer() =
default;
99 ~ArrayPortalMultiplexer() =
default;
100 ArrayPortalMultiplexer(ArrayPortalMultiplexer&&) =
default;
101 ArrayPortalMultiplexer(
const ArrayPortalMultiplexer&) =
default;
102 ArrayPortalMultiplexer& operator=(ArrayPortalMultiplexer&&) =
default;
103 ArrayPortalMultiplexer& operator=(
const ArrayPortalMultiplexer&) =
default;
105 template <
typename Portal>
111 template <
typename Portal>
112 VTKM_EXEC_CONT ArrayPortalMultiplexer& operator=(
const Portal& src) noexcept
114 this->PortalVariant = src;
120 return this->PortalVariant.CastAndCall(
121 detail::ArrayPortalMultiplexerGetNumberOfValuesFunctor{});
126 return this->PortalVariant.CastAndCall(detail::ArrayPortalMultiplexerGetFunctor{}, index);
131 this->PortalVariant.CastAndCall(detail::ArrayPortalMultiplexerSetFunctor{}, index, value);
140 template <
typename... StorageTags>
151 struct MultiplexerGetNumberOfValuesFunctor
153 template <
typename StorageType>
154 VTKM_CONT vtkm::Id operator()(StorageType,
const vtkm::cont::internal::Buffer* buffers)
const
156 return StorageType::GetNumberOfValues(buffers);
160 struct MultiplexerResizeBuffersFunctor
162 template <
typename StorageType>
165 vtkm::cont::internal::Buffer* buffers,
169 StorageType::ResizeBuffers(numValues, buffers, preserve, token);
173 struct MultiplexerFillFunctor
175 template <
typename ValueType,
typename StorageType>
177 vtkm::cont::internal::Buffer* buffers,
178 const ValueType& fillValue,
183 StorageType::Fill(buffers, fillValue, startIndex, endIndex, token);
187 template <
typename ReadPortalType>
188 struct MultiplexerCreateReadPortalFunctor
190 template <
typename StorageType>
191 VTKM_CONT ReadPortalType operator()(StorageType,
192 const vtkm::cont::internal::Buffer* buffers,
196 return ReadPortalType(StorageType::CreateReadPortal(buffers, device, token));
200 template <
typename WritePortalType>
201 struct MultiplexerCreateWritePortalFunctor
203 template <
typename StorageType>
204 VTKM_CONT WritePortalType operator()(StorageType,
205 vtkm::cont::internal::Buffer* buffers,
209 return WritePortalType(StorageType::CreateWritePortal(buffers, device, token));
213 template <
typename T,
typename... Ss>
214 struct MultiplexerArrayHandleVariantFunctor
216 using VariantType = vtkm::cont::internal::Variant<vtkm::cont::ArrayHandle<T, Ss>...>;
218 template <
typename StorageTag>
219 VTKM_CONT VariantType operator()(vtkm::cont::internal::Storage<T, StorageTag>,
220 const vtkm::cont::internal::Buffer* buffers)
228 template <
typename ValueType,
typename... StorageTags>
229 class Storage<ValueType, StorageTagMultiplexer<StorageTags...>>
231 template <
typename S>
232 using StorageFor = vtkm::cont::internal::Storage<ValueType, S>;
234 using StorageVariant = vtkm::cont::internal::Variant<StorageFor<StorageTags>...>;
236 VTKM_CONT static StorageVariant Variant(
const vtkm::cont::internal::Buffer* buffers)
238 return buffers[0].GetMetaData<StorageVariant>();
241 template <
typename Buff>
242 VTKM_CONT static Buff* ArrayBuffers(Buff* buffers)
248 using ReadPortalType =
249 vtkm::internal::ArrayPortalMultiplexer<typename StorageFor<StorageTags>::ReadPortalType...>;
250 using WritePortalType =
251 vtkm::internal::ArrayPortalMultiplexer<typename StorageFor<StorageTags>::WritePortalType...>;
255 return std::max({ StorageFor<StorageTags>::GetNumberOfBuffers()... }) + 1;
258 VTKM_CONT static vtkm::Id GetNumberOfValues(
const vtkm::cont::internal::Buffer* buffers)
260 return Variant(buffers).CastAndCall(detail::MultiplexerGetNumberOfValuesFunctor{},
261 ArrayBuffers(buffers));
265 vtkm::cont::internal::Buffer* buffers,
269 Variant(buffers).CastAndCall(
270 detail::MultiplexerResizeBuffersFunctor{}, numValues, ArrayBuffers(buffers), preserve, token);
273 VTKM_CONT static void Fill(vtkm::cont::internal::Buffer* buffers,
274 const ValueType& fillValue,
279 Variant(buffers).CastAndCall(detail::MultiplexerFillFunctor{},
280 ArrayBuffers(buffers),
287 VTKM_CONT static ReadPortalType CreateReadPortal(
const vtkm::cont::internal::Buffer* buffers,
291 return Variant(buffers).CastAndCall(
292 detail::MultiplexerCreateReadPortalFunctor<ReadPortalType>{},
293 ArrayBuffers(buffers),
298 VTKM_CONT static WritePortalType CreateWritePortal(vtkm::cont::internal::Buffer* buffers,
302 return Variant(buffers).CastAndCall(
303 detail::MultiplexerCreateWritePortalFunctor<WritePortalType>{},
304 ArrayBuffers(buffers),
309 VTKM_CONT static bool IsValid(
const vtkm::cont::internal::Buffer* buffers)
311 return Variant(buffers).IsValid();
314 template <
typename ArrayType>
315 VTKM_CONT static std::vector<vtkm::cont::internal::Buffer> CreateBuffers(
const ArrayType& array)
318 std::vector<vtkm::cont::internal::Buffer> buffers =
319 vtkm::cont::internal::CreateBuffers(StorageVariant{ array.GetStorage() }, array);
323 std::size_t numBuffers =
static_cast<std::size_t
>(GetNumberOfBuffers());
325 buffers.resize(numBuffers);
331 typename detail::MultiplexerArrayHandleVariantFunctor<ValueType, StorageTags...>::VariantType
332 GetArrayHandleVariant(
const vtkm::cont::internal::Buffer* buffers)
334 return Variant(buffers).CastAndCall(
335 detail::MultiplexerArrayHandleVariantFunctor<ValueType, StorageTags...>{},
336 ArrayBuffers(buffers));
345 template <
typename... ArrayHandleTypes>
346 struct ArrayHandleMultiplexerTraits
350 using ValueType =
typename ArrayHandleType0::ValueType;
356 template <
typename ArrayHandle>
357 struct CheckArrayHandleTransform
360 VTKM_STATIC_ASSERT((std::is_same<ValueType, typename ArrayHandle::ValueType>::value));
369 template <
typename ArrayHandle>
370 struct ArrayHandleToStorageTagImpl
374 template <
typename ArrayHandle>
375 using ArrayHandleToStorageTag =
typename ArrayHandleToStorageTagImpl<ArrayHandle>::Type;
379 using StorageType = vtkm::cont::internal::Storage<ValueType, StorageTag>;
402 template <
typename... ArrayHandleTypes>
405 typename detail::ArrayHandleMultiplexerTraits<ArrayHandleTypes...>::ValueType,
406 typename detail::ArrayHandleMultiplexerTraits<ArrayHandleTypes...>::StorageTag>
408 using Traits = detail::ArrayHandleMultiplexerTraits<ArrayHandleTypes...>;
417 using StorageType = vtkm::cont::internal::Storage<ValueType, StorageTag>;
420 template <
typename RealStorageTag>
428 template <
typename S>
431 this->
SetBuffers(StorageType::CreateBuffers(src));
437 return StorageType::GetArrayHandleVariant(this->GetBuffers());
449 template <
typename ListTag>
452 "vtkm::ListTag is no longer supported. Use vtkm::List instead.") =
461 template <
typename List>
470 struct ArrayExtractComponentMultiplexerFunctor
472 template <
typename ArrayType>
473 auto operator()(
const ArrayType& array,
478 return vtkm::cont::internal::ArrayExtractComponentImpl<typename ArrayType::StorageTag>{}(
479 array, componentIndex, allowCopy);
485 template <
typename... Ss>
488 template <
typename T>
495 return array.GetArrayHandleVariant().CastAndCall(
496 detail::ArrayExtractComponentMultiplexerFunctor{}, componentIndex, allowCopy);
506 #endif //vtk_m_cont_ArrayHandleMultiplexer_h