Skip to content

Commit

Permalink
ENH: Add get() member function to itk::SmartPointer
Browse files Browse the repository at this point in the history
Eases code that uses both `itk` and `std` smart pointers. Also serves as a
convenient shorter alternative to the somewhat lengthy "GetPointer()".
  • Loading branch information
N-Dekker authored and dzenanz committed Nov 8, 2023
1 parent cd6c940 commit f74950f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Modules/Core/Common/include/itkSmartPointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 12 additions & 0 deletions Modules/Core/Common/test/itkSmartPointerGTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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<itk::Object>{});
}

0 comments on commit f74950f

Please sign in to comment.