Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Use a macro to print numeric traits values of objects #3909

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Modules/Core/Common/include/itkMacro.h
Original file line number Diff line number Diff line change
Expand Up @@ -1271,6 +1271,14 @@ compilers.
ITK_MACROEND_NOOP_STATEMENT


// A useful macro in the PrintSelf method for printing member variables
// that require to be casted to the corresponding numeric value so that
// their numeric value gets printed.
#define itkPrintSelfNumericTraitsMacro(name, type) \
os << indent << #name << ": " << static_cast<typename NumericTraits<type>::PrintType>(this->m_##name) << std::endl; \
ITK_MACROEND_NOOP_STATEMENT
Comment on lines +1277 to +1279
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for this idea, Jon, but I would prefer to only make limited use of macro's. In general, I think macro's make code harder to understand, and harder to debug. Specifically in this case I would at least suggest to take os and indent out of the macro definition, because it is unclear from the outside that a call to this macro "captures" these two variables.

Last December I already suggested a different approach for the implementation of those PrintSelf member functions (but I did not have time to make a PR in that direction: #3802 (comment)

Copy link
Member Author

Choose a reason for hiding this comment

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

Niels, it is still WIP, but here my few thoughts:

  • After STYLE: Make PrintSelf implementation style consistent #3872, I think it becomes clear that having a macro saves typing/time and allows to print all members with a consistent style: I'd dare to say that most of the times what this macro attempts to replace has not been done because it requires a non-negligible amount of typing for what can be considered to be a low reward effort at first sight, but it pays off when debugging issues, as @blowekamp made it clear in STYLE: Make PrintSelf implementation style consistent #3872 (comment).
  • The macro follows the itkPrintSelfObjectMacro philosophy, and in the same way, the os and indent are not taken as parameters as they do not change. But that is the least of the issues for now.
  • The macro is very easy to understand in this case, as it is in the itkPrintSelfObjectMacro case.



/** Set a decorated output. This defines the Set"name"() and a Set"name"Output() method */
#define itkSetDecoratedOutputMacro(name, type) \
virtual void Set##name##Output(const SimpleDataObjectDecorator<type> * _arg) \
Expand Down