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

BUG: Fix printing of values in MetaDataObject #4368

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

phcerdan
Copy link
Contributor

@phcerdan phcerdan commented Dec 14, 2023

Also removes dead code related to METADATAPRINT

Fix #1454

PR Checklist

  • No API changes were made (or the changes have been approved)
  • No major design changes were made (or the changes have been approved)
  • Added test (or behavior not changed)
  • Updated API documentation (or API not changed)
  • [NA] Added Python wrapping to new files (if any) as described in ITK Software Guide Section 9.5

@github-actions github-actions bot added type:Bug Inconsistencies or issues which will cause an incorrect result under some or all circumstances 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 labels Dec 14, 2023
@phcerdan phcerdan added the action:ApplyClangFormat Add this label to a pull request to apply `clang-format` to the branch label Dec 14, 2023
Also removes dead code related to METADATAPRINT

Fix InsightSoftwareConsortium#1454

namespace itk
{
template <typename TIterable>
void
printIterable(std::ostream & os, const TIterable & iterable)
Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting pull request, thanks @phcerdan

Can you possibly hide printIterable (or whatever its name will be) from the public API? Because it isn't intended to be exposed directly to the end user, right? I guess it should be possible to make it a private (preferably static) member function of MetaDataObject. Otherwise, it might be possible to define it as a lambda, hidden locally inside MetaDataObject::PrintValue, but that seems more challenging to do.

Copy link
Contributor

Choose a reason for hiding this comment

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

If you can make it private, you might as well just name the function something like PrintHelper, rather than printIterable. My 2 cent!

Comment on lines +34 to +40
template <typename T, typename = void>
inline constexpr bool is_iterable_print_v = false;
// Specialize for std::vector<T> and std::vector<std::vector<T>>
template <typename T>
inline constexpr bool is_iterable_print_v<std::vector<T>, std::void_t<>> = true;
template <typename T>
inline constexpr bool is_iterable_print_v<std::vector<std::vector<T>>, std::void_t<>> = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice to see a use case for variable templates, thanks @phcerdan. Unfortunately "itkMetaDataObject.hxx" is usually #include'd (indirectly) by end users, and I guess it's not the intention to add is_iterable_print_v to the global namespace of the user, right? If you agree, you may possibly make is_iterable_print_v a private static member of MetaDataObject. Or otherwise, maybe it's simpler to just add overloaded private static MetaDataObject::IsIterablePrint(const T&) member functions. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good points @N-Dekker, I will make static private members

Copy link
Contributor Author

@phcerdan phcerdan Dec 14, 2023

Choose a reason for hiding this comment

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

GCC doesn't like it... (moving is_iterable_print inside the class).
https://stackoverflow.com/questions/72190700/explicit-template-argument-list-not-allowed-with-g-but-compiles-with-clang

I will explore further.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems fixed in gcc14, but we cannot afford that.

Copy link
Contributor

@N-Dekker N-Dekker Dec 14, 2023

Choose a reason for hiding this comment

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

Another option I guess would be to have constexpr static member functions IsIterablePrint(const T&) overloaded (rather than "specialized") for std::vector<T> and std::vector<std::vector<T>>, right?

You could then use it like:

if constexpr (IsIterablePrint(iterable))

I guess 😃

Copy link
Contributor

Choose a reason for hiding this comment

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

Regarding the name of the boolean, maybe something like ShouldBePrintedElementWise ?

Copy link
Member

@hjmjohnson hjmjohnson left a comment

Choose a reason for hiding this comment

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

I am very supportive of these efforts! Modern C++ can accomplish the desired task much more cleanly and robustly.

I appreciate @N-Dekker comments to fortify the API (avoid leaky API) and would like to see those comments addressed before finalizing this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
action:ApplyClangFormat Add this label to a pull request to apply `clang-format` to the branch area:Core Issues affecting the Core module type:Bug Inconsistencies or issues which will cause an incorrect result under some or all circumstances 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.

MetaDataDictionary::Print only prints [UNKNOWN_PRINT_CHARACTERISTICS]
4 participants