diff --git a/vtkm/cont/CMakeLists.txt b/vtkm/cont/CMakeLists.txt index 6543c9893..c6c009dee 100644 --- a/vtkm/cont/CMakeLists.txt +++ b/vtkm/cont/CMakeLists.txt @@ -138,7 +138,6 @@ set(sources internal/ArrayHandleBasicImpl.cxx internal/ArrayManagerExecutionShareWithControl.cxx internal/DeviceAdapterTag.cxx - internal/SimplePolymorphicContainer.cxx internal/TransferInfo.cxx internal/VirtualObjectTransfer.cxx Initialize.cxx diff --git a/vtkm/cont/DynamicCellSet.h b/vtkm/cont/DynamicCellSet.h index c2f4515ea..5a0508eff 100644 --- a/vtkm/cont/DynamicCellSet.h +++ b/vtkm/cont/DynamicCellSet.h @@ -26,63 +26,11 @@ #include #include -#include - namespace vtkm { namespace cont { -// Forward declaration. -template -class DynamicCellSetBase; - -namespace detail -{ - -// One instance of a template class cannot access the private members of -// another instance of a template class. However, I want to be able to copy -// construct a DynamicCellSet from another DynamicCellSet of any other type. -// Since you cannot partially specialize friendship, use this accessor class to -// get at the internals for the copy constructor. -struct DynamicCellSetCopyHelper -{ - template - VTKM_CONT static const std::shared_ptr& - GetCellSetContainer(const vtkm::cont::DynamicCellSetBase& src) - { - return src.CellSetContainer; - } -}; - -// A simple function to downcast a CellSet encapsulated in a -// SimplePolymorphicContainerBase to the given subclass of CellSet. If the -// conversion cannot be done, nullptr is returned. -template -VTKM_CONT CellSetType* DynamicCellSetTryCast( - vtkm::cont::internal::SimplePolymorphicContainerBase* cellSetContainer) -{ - vtkm::cont::internal::SimplePolymorphicContainer* downcastContainer = - dynamic_cast*>(cellSetContainer); - if (downcastContainer != nullptr) - { - return &downcastContainer->Item; - } - else - { - return nullptr; - } -} - -template -VTKM_CONT CellSetType* DynamicCellSetTryCast( - const std::shared_ptr& cellSetContainer) -{ - return detail::DynamicCellSetTryCast(cellSetContainer.get()); -} - -} // namespace detail - /// \brief Holds a cell set without having to specify concrete type. /// /// \c DynamicCellSet holds a \c CellSet object using runtime polymorphism to @@ -119,36 +67,24 @@ class VTKM_ALWAYS_EXPORT DynamicCellSetBase public: VTKM_CONT - DynamicCellSetBase() {} + DynamicCellSetBase() = default; template VTKM_CONT DynamicCellSetBase(const CellSetType& cellSet) - : CellSetContainer(new vtkm::cont::internal::SimplePolymorphicContainer(cellSet)) + : CellSet(std::make_shared(cellSet)) { VTKM_IS_CELL_SET(CellSetType); } - VTKM_CONT - DynamicCellSetBase(const DynamicCellSetBase& src) - : CellSetContainer(src.CellSetContainer) - { - } - template VTKM_CONT explicit DynamicCellSetBase(const DynamicCellSetBase& src) - : CellSetContainer(detail::DynamicCellSetCopyHelper::GetCellSetContainer(src)) + : CellSet(src.CellSet) { } - VTKM_CONT - ~DynamicCellSetBase() {} - - VTKM_CONT - vtkm::cont::DynamicCellSetBase& operator=( - const vtkm::cont::DynamicCellSetBase& src) + VTKM_CONT explicit DynamicCellSetBase(const std::shared_ptr& cs) + : CellSet(cs) { - this->CellSetContainer = src.CellSetContainer; - return *this; } /// Returns true if this cell set is of the provided type. @@ -156,7 +92,7 @@ public: template VTKM_CONT bool IsType() const { - return (detail::DynamicCellSetTryCast(this->CellSetContainer) != nullptr); + return (dynamic_cast(this->CellSet.get()) != nullptr); } /// Returns true if this cell set is the same (or equivalent) type as the @@ -168,14 +104,6 @@ public: return this->IsType(); } - /// Returns the contained cell set as the abstract \c CellSet type. - /// - VTKM_CONT - const vtkm::cont::CellSet& CastToBase() const - { - return *reinterpret_cast(this->CellSetContainer->GetVoidPointer()); - } - /// Returns this cell set cast to the given \c CellSet type. Throws \c /// ErrorBadType if the cast does not work. Use \c IsType to check if /// the cast can happen. @@ -183,8 +111,7 @@ public: template VTKM_CONT CellSetType& Cast() const { - CellSetType* cellSetPointer = - detail::DynamicCellSetTryCast(this->CellSetContainer); + auto cellSetPointer = dynamic_cast(this->CellSet.get()); if (cellSetPointer == nullptr) { VTKM_LOG_CAST_FAIL(*this, CellSetType); @@ -235,7 +162,7 @@ public: /// \brief Create a new cell set of the same type as this cell set. /// - /// This method creates a new cell setthat is the same type as this one and + /// This method creates a new cell set that is the same type as this one and /// returns a new dynamic cell set for it. This method is convenient when /// creating output data sets that should be the same type as some input cell /// set. @@ -244,32 +171,39 @@ public: DynamicCellSetBase NewInstance() const { DynamicCellSetBase newCellSet; - newCellSet.CellSetContainer = this->CellSetContainer->NewInstance(); + newCellSet.CellSet = this->CellSet->NewInstance(); return newCellSet; } VTKM_CONT - std::string GetName() const { return this->CastToBase().GetName(); } + vtkm::cont::CellSet* GetCellSetBase() { return this->CellSet.get(); } VTKM_CONT - vtkm::Id GetNumberOfCells() const { return this->CastToBase().GetNumberOfCells(); } + const vtkm::cont::CellSet* GetCellSetBase() const { return this->CellSet.get(); } VTKM_CONT - vtkm::Id GetNumberOfFaces() const { return this->CastToBase().GetNumberOfFaces(); } + std::string GetName() const { return this->CellSet->GetName(); } VTKM_CONT - vtkm::Id GetNumberOfEdges() const { return this->CastToBase().GetNumberOfEdges(); } + vtkm::Id GetNumberOfCells() const { return this->CellSet->GetNumberOfCells(); } VTKM_CONT - vtkm::Id GetNumberOfPoints() const { return this->CastToBase().GetNumberOfPoints(); } + vtkm::Id GetNumberOfFaces() const { return this->CellSet->GetNumberOfFaces(); } VTKM_CONT - void PrintSummary(std::ostream& stream) const { return this->CastToBase().PrintSummary(stream); } + vtkm::Id GetNumberOfEdges() const { return this->CellSet->GetNumberOfEdges(); } + + VTKM_CONT + vtkm::Id GetNumberOfPoints() const { return this->CellSet->GetNumberOfPoints(); } + + VTKM_CONT + void PrintSummary(std::ostream& stream) const { return this->CellSet->PrintSummary(stream); } private: - std::shared_ptr CellSetContainer; + std::shared_ptr CellSet; - friend struct detail::DynamicCellSetCopyHelper; + template + friend class DynamicCellSetBase; }; //============================================================================= @@ -298,29 +232,27 @@ namespace detail struct DynamicCellSetTry { - DynamicCellSetTry( - const vtkm::cont::internal::SimplePolymorphicContainerBase* const cellSetContainer) - : Container(cellSetContainer) + DynamicCellSetTry(const vtkm::cont::CellSet* const cellSetBase) + : CellSetBase(cellSetBase) { } template void operator()(CellSetType, Functor&& f, bool& called, Args&&... args) const { - using downcastType = const vtkm::cont::internal::SimplePolymorphicContainer* const; if (!called) { - downcastType downcastContainer = dynamic_cast(this->Container); - if (downcastContainer) + auto* cellset = dynamic_cast(this->CellSetBase); + if (cellset) { - VTKM_LOG_CAST_SUCC(*this->Container, *downcastContainer); - f(downcastContainer->Item, std::forward(args)...); + VTKM_LOG_CAST_SUCC(*this->CellSetBase, *cellset); + f(*cellset, std::forward(args)...); called = true; } } } - const vtkm::cont::internal::SimplePolymorphicContainerBase* const Container; + const vtkm::cont::CellSet* const CellSetBase; }; } // namespace detail @@ -330,7 +262,7 @@ template VTKM_CONT void DynamicCellSetBase::CastAndCall(Functor&& f, Args&&... args) const { bool called = false; - detail::DynamicCellSetTry tryCellSet(this->CellSetContainer.get()); + detail::DynamicCellSetTry tryCellSet(this->CellSet.get()); vtkm::ListForEach( tryCellSet, CellSetList{}, std::forward(f), called, std::forward(args)...); if (!called) diff --git a/vtkm/cont/internal/CMakeLists.txt b/vtkm/cont/internal/CMakeLists.txt index ba338e8bf..ba8fb56c8 100644 --- a/vtkm/cont/internal/CMakeLists.txt +++ b/vtkm/cont/internal/CMakeLists.txt @@ -42,7 +42,6 @@ set(headers ParallelRadixSort.h ParallelRadixSortInterface.h ReverseConnectivityBuilder.h - SimplePolymorphicContainer.h StorageError.h TransferInfo.h VariantArrayHandleContainer.h diff --git a/vtkm/cont/internal/SimplePolymorphicContainer.cxx b/vtkm/cont/internal/SimplePolymorphicContainer.cxx deleted file mode 100644 index 566ef422b..000000000 --- a/vtkm/cont/internal/SimplePolymorphicContainer.cxx +++ /dev/null @@ -1,39 +0,0 @@ -//============================================================================ -// 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. -// -// Copyright 2015 National Technology & Engineering Solutions of Sandia, LLC (NTESS). -// Copyright 2015 UT-Battelle, LLC. -// Copyright 2015 Los Alamos National Security. -// -// Under the terms of Contract DE-NA0003525 with NTESS, -// the U.S. Government retains certain rights in this software. -// -// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National -// Laboratory (LANL), the U.S. Government retains certain rights in -// this software. -//============================================================================ - -#include - -namespace vtkm -{ -namespace cont -{ -namespace internal -{ - -SimplePolymorphicContainerBase::SimplePolymorphicContainerBase() -{ -} - -SimplePolymorphicContainerBase::~SimplePolymorphicContainerBase() -{ -} -} -} -} // namespace vtkm::cont::internal diff --git a/vtkm/cont/internal/SimplePolymorphicContainer.h b/vtkm/cont/internal/SimplePolymorphicContainer.h deleted file mode 100644 index a01e7a5e0..000000000 --- a/vtkm/cont/internal/SimplePolymorphicContainer.h +++ /dev/null @@ -1,86 +0,0 @@ -//============================================================================ -// 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. -// -// Copyright 2014 National Technology & Engineering Solutions of Sandia, LLC (NTESS). -// Copyright 2014 UT-Battelle, LLC. -// Copyright 2014 Los Alamos National Security. -// -// Under the terms of Contract DE-NA0003525 with NTESS, -// the U.S. Government retains certain rights in this software. -// -// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National -// Laboratory (LANL), the U.S. Government retains certain rights in -// this software. -//============================================================================ -#ifndef vtk_m_cont_internal_SimplePolymorphicContainer_h -#define vtk_m_cont_internal_SimplePolymorphicContainer_h - -#include - -#include -#include - -namespace vtkm -{ -namespace cont -{ -namespace internal -{ - -/// \brief Base class for SimplePolymorphicContainer -/// -struct VTKM_CONT_EXPORT SimplePolymorphicContainerBase -{ - SimplePolymorphicContainerBase(); - - // This must exist so that subclasses are destroyed correctly. - virtual ~SimplePolymorphicContainerBase(); - - virtual std::shared_ptr NewInstance() const = 0; - - virtual const void* GetVoidPointer() const = 0; -}; - -/// \brief Simple object container that can use C++ run-time type information. -/// -/// The SimplePolymorphicContainer is a trivial structure that contains a -/// single object. The intention is to be able to pass around a pointer to the -/// superclass SimplePolymorphicContainerBase to methods that cannot know the -/// full type of the object at run-time. This is roughly equivalent to passing -/// around a void* except that C++ will capture run-time type information that -/// allows for safer dynamic downcasts. -/// -template -struct VTKM_ALWAYS_EXPORT SimplePolymorphicContainer : public SimplePolymorphicContainerBase -{ - T Item; - - VTKM_CONT - SimplePolymorphicContainer() - : Item() - { - } - - VTKM_CONT - SimplePolymorphicContainer(const T& src) - : Item(src) - { - } - - virtual std::shared_ptr NewInstance() const - { - return std::shared_ptr(new SimplePolymorphicContainer()); - } - - virtual const void* GetVoidPointer() const { return &this->Item; } -}; -} -} -} // namespace vtkm::cont::internal - -#endif //vtk_m_cont_internal_SimplePolymorphicContainer_h diff --git a/vtkm/cont/internal/VariantArrayHandleContainer.h b/vtkm/cont/internal/VariantArrayHandleContainer.h index a8a571a1b..062ab7077 100644 --- a/vtkm/cont/internal/VariantArrayHandleContainer.h +++ b/vtkm/cont/internal/VariantArrayHandleContainer.h @@ -70,9 +70,7 @@ struct VTKM_CONT_EXPORT VariantArrayHandleContainerBase /// \brief ArrayHandle container that can use C++ run-time type information. /// -/// The \c VariantArrayHandleContainer is similar to the -/// \c SimplePolymorphicContainer in that it can contain an object of an -/// unknown type. However, this class specifically holds ArrayHandle objects +/// The \c VariantArrayHandleContainer holds ArrayHandle objects /// (with different template parameters) so that it can polymorphically answer /// simple questions about the object. /// diff --git a/vtkm/cont/testing/UnitTestDynamicCellSet.cxx b/vtkm/cont/testing/UnitTestDynamicCellSet.cxx index ff4e93fa2..9e146f428 100644 --- a/vtkm/cont/testing/UnitTestDynamicCellSet.cxx +++ b/vtkm/cont/testing/UnitTestDynamicCellSet.cxx @@ -47,6 +47,10 @@ struct CheckFunctor } }; +class DummyCellSet : public vtkm::cont::CellSet +{ +}; + template void CheckDynamicCellSet(const CellSetType& cellSet, vtkm::cont::DynamicCellSetBase dynamicCellSet) @@ -54,7 +58,7 @@ void CheckDynamicCellSet(const CellSetType& cellSet, VTKM_TEST_ASSERT(dynamicCellSet.template IsType(), "DynamicCellSet reports wrong type."); VTKM_TEST_ASSERT(dynamicCellSet.IsSameType(cellSet), "DynamicCellSet reports wrong type."); - VTKM_TEST_ASSERT(!dynamicCellSet.template IsType(), + VTKM_TEST_ASSERT(!dynamicCellSet.template IsType(), "DynamicCellSet reports wrong type."); dynamicCellSet.template Cast(); @@ -79,7 +83,7 @@ void TryNewInstance(CellSetType, vtkm::cont::DynamicCellSetBase& or VTKM_TEST_ASSERT(newCellSet.template IsType(), "New cell set wrong type."); - VTKM_TEST_ASSERT(&originalCellSet.CastToBase() != &newCellSet.CastToBase(), + VTKM_TEST_ASSERT(originalCellSet.GetCellSetBase() != newCellSet.GetCellSetBase(), "NewInstance did not make a copy."); }