From f74950f493bb30d33f4f86c72db6c5340dbcb7d3 Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Mon, 6 Nov 2023 16:35:16 +0100 Subject: [PATCH] ENH: Add `get()` member function to itk::SmartPointer Eases code that uses both `itk` and `std` smart pointers. Also serves as a convenient shorter alternative to the somewhat lengthy "GetPointer()". --- Modules/Core/Common/include/itkSmartPointer.h | 9 +++++++++ Modules/Core/Common/test/itkSmartPointerGTest.cxx | 12 ++++++++++++ 2 files changed, 21 insertions(+) diff --git a/Modules/Core/Common/include/itkSmartPointer.h b/Modules/Core/Common/include/itkSmartPointer.h index 250d50c430a..6ed775ac68a 100644 --- a/Modules/Core/Common/include/itkSmartPointer.h +++ b/Modules/Core/Common/include/itkSmartPointer.h @@ -134,6 +134,15 @@ class SmartPointer return m_Pointer; } + /** Returns the stored (raw) pointer. Equivalent to `GetPointer()`, but then following the Standard C++ Library naming + * conversion (like `std::shared_ptr::get()`). */ + ObjectType * + get() const noexcept + { + return m_Pointer; + } + + /** Overload operator assignment. * * This method is also implicitly used for move semantics. diff --git a/Modules/Core/Common/test/itkSmartPointerGTest.cxx b/Modules/Core/Common/test/itkSmartPointerGTest.cxx index 0c4d725a5c7..40e3a373bf9 100644 --- a/Modules/Core/Common/test/itkSmartPointerGTest.cxx +++ b/Modules/Core/Common/test/itkSmartPointerGTest.cxx @@ -357,3 +357,15 @@ TEST(SmartPointer, ConvertingRegisterCount) EXPECT_TRUE(d1ptr.IsNull()); } } + + +// Tests that `smartPointer.get()` is equivalent to `smartPointer.GetPointer()`. +TEST(SmartPointer, GetIsEquivalentToGetPointer) +{ + const auto check = [](auto && smartPointer) { EXPECT_EQ(smartPointer.get(), smartPointer.GetPointer()); }; + + check(itk::Object::New()); + check(itk::Object::Pointer{}); + check(itk::Object::ConstPointer{}); + check(itk::SmartPointer{}); +}