//============================================================================ // 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. //============================================================================ #ifndef vtk_m_cont_ArrayHandleConstant_h #define vtk_m_cont_ArrayHandleConstant_h #include namespace vtkm { namespace cont { struct VTKM_ALWAYS_EXPORT StorageTagConstant { }; namespace internal { template struct VTKM_ALWAYS_EXPORT ConstantFunctor { VTKM_EXEC_CONT ConstantFunctor(const ValueType& value = ValueType()) : Value(value) { } VTKM_EXEC_CONT ValueType operator()(vtkm::Id vtkmNotUsed(index)) const { return this->Value; } private: ValueType Value; }; template using StorageTagConstantSuperclass = typename vtkm::cont::ArrayHandleImplicit>::StorageTag; template struct Storage : Storage> { }; } // namespace internal /// \brief An array handle with a constant value. /// /// ArrayHandleConstant is an implicit array handle with a constant value. A /// constant array handle is constructed by giving a value and an array length. /// The resulting array is of the given size with each entry the same value /// given in the constructor. The array is defined implicitly, so there it /// takes (almost) no memory. /// template class ArrayHandleConstant : public vtkm::cont::ArrayHandle { public: VTKM_ARRAY_HANDLE_SUBCLASS(ArrayHandleConstant, (ArrayHandleConstant), (vtkm::cont::ArrayHandle)); VTKM_CONT ArrayHandleConstant(T value, vtkm::Id numberOfValues = 0) : Superclass(internal::FunctorToArrayHandleImplicitBuffers(internal::ConstantFunctor(value), numberOfValues)) { } /// \brief Returns the constant value stored in this array. /// /// The value set in the constructor of this array is returned even if the number of values is 0. /// VTKM_CONT T GetValue() const { return this->ReadPortal().GetFunctor()(0); } }; /// make_ArrayHandleConstant is convenience function to generate an /// ArrayHandleImplicit. It takes a functor and the virtual length of the /// array. /// template vtkm::cont::ArrayHandleConstant make_ArrayHandleConstant(T value, vtkm::Id numberOfValues) { return vtkm::cont::ArrayHandleConstant(value, numberOfValues); } } } // vtkm::cont //============================================================================= // Specializations of serialization related classes /// @cond SERIALIZATION namespace vtkm { namespace cont { template struct SerializableTypeString> { static VTKM_CONT const std::string& Get() { static std::string name = "AH_Constant<" + SerializableTypeString::Get() + ">"; return name; } }; template struct SerializableTypeString> : SerializableTypeString> { }; } } // vtkm::cont namespace mangled_diy_namespace { template struct Serialization> { private: using Type = vtkm::cont::ArrayHandleConstant; using BaseType = vtkm::cont::ArrayHandle; public: static VTKM_CONT void save(BinaryBuffer& bb, const BaseType& obj) { vtkmdiy::save(bb, obj.GetNumberOfValues()); vtkmdiy::save(bb, obj.ReadPortal().Get(0)); } static VTKM_CONT void load(BinaryBuffer& bb, BaseType& obj) { vtkm::Id count = 0; vtkmdiy::load(bb, count); T value; vtkmdiy::load(bb, value); obj = vtkm::cont::make_ArrayHandleConstant(value, count); } }; template struct Serialization> : Serialization> { }; } // diy /// @endcond SERIALIZATION #endif //vtk_m_cont_ArrayHandleConstant_h