diff --git a/docs/changelog/variant-istype.md b/docs/changelog/variant-istype.md new file mode 100644 index 000000000..1005ca8cd --- /dev/null +++ b/docs/changelog/variant-istype.md @@ -0,0 +1,5 @@ +# Add Variant::IsType + +The `Variant` class was missing a way to check the type. You could do it +indirectly using `variant.GetIndex() == variant.GetIndexOf()`, but +having this convenience function is more clear. diff --git a/vtkm/exec/internal/testing/UnitTestVariant.cxx b/vtkm/exec/internal/testing/UnitTestVariant.cxx index 64834a369..5fbc9d6c5 100644 --- a/vtkm/exec/internal/testing/UnitTestVariant.cxx +++ b/vtkm/exec/internal/testing/UnitTestVariant.cxx @@ -365,6 +365,8 @@ void TestGet() VariantType variant = expectedValue; VTKM_TEST_ASSERT(variant.GetIndex() == 2); + VTKM_TEST_ASSERT(variant.IsType()); + VTKM_TEST_ASSERT(!variant.IsType()); VTKM_TEST_ASSERT(variant.Get<2>() == expectedValue); diff --git a/vtkm/internal/VariantImpl.h b/vtkm/internal/VariantImpl.h index 7e586b6de..64436e58a 100644 --- a/vtkm/internal/VariantImpl.h +++ b/vtkm/internal/VariantImpl.h @@ -352,6 +352,14 @@ public: return (this->Index >= 0) && (this->Index < NumberOfTypes); } + /// Returns true if this `Variant` stores the given type + /// + template + VTK_M_DEVICE bool IsType() const + { + return (this->GetIndex() == this->GetIndexOf()); + } + Variant() = default; ~Variant() = default; Variant(const Variant&) = default; @@ -373,7 +381,7 @@ public: template VTK_M_DEVICE Variant& operator=(const T& src) { - if (this->GetIndex() == this->GetIndexOf()) + if (this->IsType()) { this->Get() = src; } @@ -474,14 +482,14 @@ private: template VTK_M_DEVICE T& GetImpl(std::true_type) { - VTKM_ASSERT(this->GetIndexOf() == this->GetIndex()); + VTKM_ASSERT(this->IsType()); return detail::VariantUnionGet::value>(this->Storage); } template VTK_M_DEVICE const T& GetImpl(std::true_type) const { - VTKM_ASSERT(this->GetIndexOf() == this->GetIndex()); + VTKM_ASSERT(this->IsType()); return detail::VariantUnionGet::value>(this->Storage); }