Skip to content

COMP: Use std::unique_ptr in FEMLinearSystemWrapperDenseVNL#5995

Merged
hjmjohnson merged 4 commits intoInsightSoftwareConsortium:mainfrom
hjmjohnson:cpp-cg-fem-unique-ptr
Apr 2, 2026
Merged

COMP: Use std::unique_ptr in FEMLinearSystemWrapperDenseVNL#5995
hjmjohnson merged 4 commits intoInsightSoftwareConsortium:mainfrom
hjmjohnson:cpp-cg-fem-unique-ptr

Conversation

@hjmjohnson
Copy link
Copy Markdown
Member

Summary

Replace raw new/delete of matrix and vector holders in FEMLinearSystemWrapperDenseVNL with std::unique_ptr and std::make_unique, following C++ Core Guidelines R.11 and R.20.

  • Replace raw new/delete of vnl_matrix and vnl_vector holder objects with std::unique_ptr and std::make_unique
  • Use std::swap for all swap methods instead of manual pointer shuffling
  • Default the destructor (no manual delete needed)

Test plan

  • CI builds pass on all platforms
  • FEM module tests pass (ctest -R FEM)

Part of a series splitting out #5993

🤖 Generated with Claude Code

@github-actions github-actions Bot added type:Compiler Compiler support or related warnings area:Numerics Issues affecting the Numerics module labels Mar 31, 2026
@hjmjohnson hjmjohnson force-pushed the cpp-cg-fem-unique-ptr branch 2 times, most recently from be010a1 to 7769a5c Compare April 1, 2026 12:41
@dzenanz dzenanz requested a review from N-Dekker April 1, 2026 13:05
Comment thread Modules/Numerics/FEM/include/itkFEMLinearSystemWrapperDenseVNL.h Outdated
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Apr 1, 2026

Greptile Summary

This PR modernizes LinearSystemWrapperDenseVNL by replacing raw new/delete memory management with std::unique_ptr and std::make_unique, following C++ Core Guidelines R.11 and R.20. The changes are correct and well-scoped: the three private holder members (m_Matrices, m_Vectors, m_Solutions) are now std::unique_ptr-managed, the destructor is safely defaulted (relying on unique_ptr RAII), and all swap operations are simplified via std::swap on unique_ptrs.

Key changes:

  • MatrixHolder element type upgraded from raw MatrixRepresentation* to std::unique_ptr<MatrixRepresentation>; outer holder wrapped in std::unique_ptr<MatrixHolder>
  • m_Vectors and m_Solutions similarly wrapped; all new/delete calls replaced with std::make_unique and .reset()
  • SwapMatrices, SwapVectors, and SwapSolutions all consolidated to std::swap on unique_ptrs — note that SwapVectors previously did a deep value copy-swap while the new code does a pointer swap, but observable behavior through the class interface is identical
  • Manual destructor (which explicitly looped through all indices and called delete) is replaced by = default, correctly relying on unique_ptr chain destruction
  • Minor: m_Vectors/m_Solutions inline types are verbose and repeated, while MatrixHolder has a named alias — a VectorHolder alias would improve symmetry (style suggestion only)

Confidence Score: 5/5

Safe to merge — all changes are semantically equivalent, memory management is correct, and no regressions are introduced.

All findings are P2 style suggestions (a missing VectorHolder type alias). No logic errors, no semantic regressions, and the unique_ptr-based RAII pattern is strictly safer than the old manual new/delete loop. The only behavioural difference in SwapVectors (pointer swap vs. value swap) is equivalent through the class's public interface.

No files require special attention.

Important Files Changed

Filename Overview
Modules/Numerics/FEM/include/itkFEMLinearSystemWrapperDenseVNL.h Modernized member types from raw pointers to std::unique_ptr; added #include ; defaulted destructor. Minor style note: VectorHolder alias inconsistency.
Modules/Numerics/FEM/src/itkFEMLinearSystemWrapperDenseVNL.cxx Replaced all new/delete with std::make_unique/reset, manual pointer swaps with std::swap, and removed the manual destructor body. All changes are semantically correct.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[LinearSystemWrapperDenseVNL] --> B["m_Matrices\nunique_ptr&lt;MatrixHolder&gt;"]
    A --> C["m_Vectors\nunique_ptr&lt;vector&lt;unique_ptr&lt;vnl_vector&gt;&gt;&gt;"]
    A --> D["m_Solutions\nunique_ptr&lt;vector&lt;unique_ptr&lt;vnl_vector&gt;&gt;&gt;"]
    B --> B1["MatrixHolder\nvector&lt;unique_ptr&lt;vnl_matrix&lt;Float&gt;&gt;&gt;"]
    B1 --> B2["unique_ptr&lt;MatrixRepresentation&gt; [0..N]"]
    C --> C1["unique_ptr&lt;vnl_vector&lt;Float&gt;&gt; [0..N]"]
    D --> D1["unique_ptr&lt;vnl_vector&lt;Float&gt;&gt; [0..N]"]
    style A fill:#4a90d9,color:#fff
    style B fill:#7ec8a0,color:#000
    style C fill:#7ec8a0,color:#000
    style D fill:#7ec8a0,color:#000
Loading

Reviews (1): Last reviewed commit: "COMP: Use std::unique_ptr in FEMLinearSy..." | Re-trigger Greptile

Comment thread Modules/Numerics/FEM/include/itkFEMLinearSystemWrapperDenseVNL.h Outdated
Replace raw pointer members and new/delete with std::unique_ptr
and std::make_unique. Use std::swap for all Swap* methods.
Destructor is defaulted since unique_ptr handles cleanup.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Comment thread Modules/Numerics/FEM/include/itkFEMLinearSystemWrapperDenseVNL.h Outdated
Copy link
Copy Markdown
Contributor

@N-Dekker N-Dekker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Hans! Almost perfect. 😇 Just one change request: Please move VectorHolder to the private section of the class.

@N-Dekker
Copy link
Copy Markdown
Contributor

N-Dekker commented Apr 2, 2026

This PR is clearly an improvement. However, I wonder if the extra pointer indirection is really necessary! Why not simply using std::vector<vnl_matrix<Float>> and std::vector<vnl_vector<Float>> ?

@hjmjohnson
Copy link
Copy Markdown
Member Author

This PR is clearly an improvement. However, I wonder if the extra pointer indirection is really necessary! Why not simply using std::vector<vnl_matrix<Float>> and std::vector<vnl_vector<Float>> ?

That change should be investigated and performed as a separate PR that builds upon this PR. It is a change outside the scope of what was being worked on here.

hjmjohnson and others added 2 commits April 2, 2026 05:35
VectorHolder is only used internally by m_Vectors and m_Solutions
members, so it should not be part of the public API.

Addresses review comment from @N-Dekker on PR InsightSoftwareConsortium#5995.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
These type aliases are only used internally by private member variables
and implementation details. Moving them to private keeps the public API
minimal and consistent with the VectorHolder change.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@hjmjohnson
Copy link
Copy Markdown
Member Author

Addressed @N-Dekker's review: moved VectorHolder to the private section. Also moved MatrixRepresentation and MatrixHolder to private in a follow-on commit, since they are equally internal-only and not part of the public API.

@github-actions github-actions Bot added the area:Documentation Issues affecting the Documentation module label Apr 2, 2026
@hjmjohnson
Copy link
Copy Markdown
Member Author

@N-Dekker I had a change of heart. In the pursuit to make this perfect, I want to keep the entire conversation together. When moving the VectorHolder (new alias) to private, it raised the question about MatrixRepresentation and MatrixHolder (ITKv5 public API aliases). I had a migration guide entry generated and moved those two items to private as well. I would guess that those were not intended to be part of the public interface.

@hjmjohnson hjmjohnson requested a review from N-Dekker April 2, 2026 10:47
Comment thread Documentation/docs/migration_guides/itk_6_migration_guide.md Outdated
@N-Dekker N-Dekker self-requested a review April 2, 2026 12:56
@N-Dekker
Copy link
Copy Markdown
Contributor

N-Dekker commented Apr 2, 2026

Thanks Hans, I agree with this change: making MatrixRepresentation and MatrixHolder private. Note that the "official way" would be to deprecate and future-legacy-remove them. But these two typedefs were clearly never meant to be part of the API.

Comment thread Documentation/docs/migration_guides/itk_6_migration_guide.md Outdated
Comment thread Documentation/docs/migration_guides/itk_6_migration_guide.md
@N-Dekker N-Dekker dismissed their stale review April 2, 2026 13:25

My change request to make VectorHolder private is addressed

Document the removal of MatrixRepresentation and MatrixHolder type
aliases from the public interface of LinearSystemWrapperDenseVNL.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@hjmjohnson hjmjohnson force-pushed the cpp-cg-fem-unique-ptr branch from 4ab00d8 to 7d94b93 Compare April 2, 2026 13:31
@hjmjohnson hjmjohnson requested a review from N-Dekker April 2, 2026 13:32
Copy link
Copy Markdown
Contributor

@N-Dekker N-Dekker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect 💯

@hjmjohnson hjmjohnson merged commit 19eb086 into InsightSoftwareConsortium:main Apr 2, 2026
11 of 16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:Documentation Issues affecting the Documentation module area:Numerics Issues affecting the Numerics module type:Compiler Compiler support or related warnings

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants