Skip to content

ENH: Replace PrintSelf NumericTraits-cast boilerplate with itk::print_helper::PrintNumericTrait helper (supersedes #3909)#6234

Merged
hjmjohnson merged 2 commits into
InsightSoftwareConsortium:mainfrom
hjmjohnson:print-numeric-trait
May 10, 2026
Merged

ENH: Replace PrintSelf NumericTraits-cast boilerplate with itk::print_helper::PrintNumericTrait helper (supersedes #3909)#6234
hjmjohnson merged 2 commits into
InsightSoftwareConsortium:mainfrom
hjmjohnson:print-numeric-trait

Conversation

@hjmjohnson
Copy link
Copy Markdown
Member

@hjmjohnson hjmjohnson commented May 8, 2026

Replace the os << indent << "Name: " << static_cast<typename NumericTraits<T>::PrintType>(this->m_Name) << std::endl; boilerplate in 513 PrintSelf overrides across 221 files with itk::print_helper::PrintNumericTrait(os, indent, "Name", this->m_Name);. Supersedes #3909 (which proposed a macro).

How #3909's review concerns are resolved
#3909 concern Resolution
@N-Dekker: prefer named template over macro Pure template function in itk::print_helper; no macro
@N-Dekker: take os and indent out as explicit params They are explicit parameters
@N-Dekker: PR #3802 chained-<< co-location Helper lives next to those overloads in itkPrintHelper.h
@issakomi: skip useless cast when types match if constexpr (std::is_same_v<T, NumericTraits<T>::PrintType>) skips it
@issakomi: handle std::complex<T> (PrintType = Self) Identity-cast branch covers it; unit-tested
WIP: no in-tree adoption Commit 2 converts 513 sites
Commits
  1. ENH: Add itk::print_helper::PrintNumericTrait helper (helper + 6 tests for double / int / unsigned char / signed char / complex<double> / indented output)
  2. ENH: Adopt PrintNumericTrait helper toolkit-wide (mechanical regex conversion; 1 chained-statement leftover left for follow-up — Modules/Filtering/ImageFusion/include/itkLabelOverlayImageFilter.hxx:103)

Co-authored with @jhlegarreta.

Local validation
  • pre-commit run --all-files: clean
  • Toolkit-wide build: 4448/4448 ninja targets in build-python
  • ctest -R PrintHelper: 17/17 (incl. 6 new PrintNumericTrait cases)
  • Cross-module sample (ImageRegistrationMethodv4|MeanSquares|FastMarching|ImageStatistics|MultiResolution): 63/63

Closes #3909.

@github-actions github-actions Bot added type:Enhancement Improvement of existing methods or implementation area:Python wrapping Python bindings for a class type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct area:Core Issues affecting the Core module area:Filtering Issues affecting the Filtering module area:IO Issues affecting the IO module area:Registration Issues affecting the Registration module area:Segmentation Issues affecting the Segmentation module area:Video Issues affecting the Video module area:Numerics Issues affecting the Numerics module labels May 8, 2026
@hjmjohnson hjmjohnson marked this pull request as ready for review May 8, 2026 10:29
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 8, 2026

Too many files changed for review. (223 files found, 100 file limit)

Copy link
Copy Markdown
Member

@dzenanz dzenanz left a comment

Choose a reason for hiding this comment

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

Mostly looks good on a glance.

Comment thread Modules/Core/Common/include/itkAnnulusOperator.h Outdated
Comment thread Modules/Core/Common/include/itkPrintHelper.h Outdated
Comment thread Modules/Core/Common/test/itkPrintHelperGTest.cxx
Non-macro replacement for the recurring PrintSelf boilerplate
  os << indent << "Name: "
     << static_cast<typename NumericTraits<T>::PrintType>(this->m_Name)
     << std::endl;

Explicit os/indent parameters (no implicit capture). constexpr
dispatch elides the cast when PrintType == T. Supersedes InsightSoftwareConsortium#3909.

Co-Authored-By: Jon Haitz Legarreta Gorroño <jhlegarreta@users.noreply.github.com>
@hjmjohnson hjmjohnson force-pushed the print-numeric-trait branch 2 times, most recently from 9f9fee6 to 0074c8e Compare May 9, 2026 14:29
@hjmjohnson hjmjohnson requested a review from dzenanz May 9, 2026 14:33
Comment thread Modules/Core/Common/include/itkPrintHelper.h Outdated
Comment thread Modules/Core/Common/include/itkPrintHelper.h Outdated
Convert 513 PrintSelf call sites across 221 files in Modules/ to
itk::print_helper::PrintNumericTrait. Handles 1/2-line forms,
typename and itk:: variants, member and method-call expressions.
Chained-statement form intentionally untouched (1 leftover at
Modules/Filtering/ImageFusion/include/itkLabelOverlayImageFilter.hxx:103).

No observable runtime change.

Co-Authored-By: Jon Haitz Legarreta Gorroño <jhlegarreta@users.noreply.github.com>
@hjmjohnson hjmjohnson force-pushed the print-numeric-trait branch from 0074c8e to 17a76eb Compare May 10, 2026 12:06
Copy link
Copy Markdown
Member

@jhlegarreta jhlegarreta left a comment

Choose a reason for hiding this comment

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

Thanks for your relentless efforts, Hans.

@hjmjohnson hjmjohnson merged commit 59176b1 into InsightSoftwareConsortium:main May 10, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:Core Issues affecting the Core module area:Filtering Issues affecting the Filtering module area:IO Issues affecting the IO module area:Numerics Issues affecting the Numerics module area:Python wrapping Python bindings for a class area:Registration Issues affecting the Registration module area:Segmentation Issues affecting the Segmentation module area:Video Issues affecting the Video module type:Enhancement Improvement of existing methods or implementation type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants