-
-
Notifications
You must be signed in to change notification settings - Fork 715
BUG: Fix printing of values in MetaDataObject #4368
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,9 +28,79 @@ | |
| #ifndef itkMetaDataObject_hxx | ||
| #define itkMetaDataObject_hxx | ||
|
|
||
| #include <type_traits> | ||
| #include <iterator> | ||
|
|
||
| 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; | ||
|
|
||
| namespace itk | ||
| { | ||
| template <typename TIterable> | ||
| void | ||
| printIterable(std::ostream & os, const TIterable & iterable) | ||
jhlegarreta marked this conversation as resolved.
Show resolved
Hide resolved
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting pull request, thanks @phcerdan Can you possibly hide
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| { | ||
| if constexpr (is_iterable_print_v<TIterable>) | ||
| { | ||
| os << "["; | ||
| auto begin = std::begin(iterable); | ||
| auto end = std::end(iterable); | ||
| for (auto it = begin; it != end; ++it) | ||
| { | ||
| if (it != begin) | ||
| { | ||
| os << ", "; | ||
| } | ||
| printIterable(os, *it); | ||
| } | ||
| os << "]"; | ||
| } | ||
| else | ||
| { | ||
| // Handle non-iterable types | ||
| os << iterable; | ||
| } | ||
| } | ||
|
|
||
| template <typename MetaDataObjectType> | ||
| void | ||
| MetaDataObject<MetaDataObjectType>::PrintValue(std::ostream & os) const | ||
| { | ||
| if constexpr (is_iterable_print_v<MetaDataObjectType>) | ||
| { | ||
| os << "["; | ||
| auto begin = std::begin(m_MetaDataObjectValue); | ||
| auto end = std::end(m_MetaDataObjectValue); | ||
| for (auto it = begin; it != end; ++it) | ||
| { | ||
| if (it != begin) | ||
| { | ||
| os << ", "; | ||
| } | ||
| printIterable(os, *it); | ||
| } | ||
| os << "]"; | ||
| } | ||
| else | ||
| { | ||
| os << m_MetaDataObjectValue; | ||
| } | ||
| } | ||
|
|
||
| template <typename MetaDataObjectType> | ||
| void | ||
| MetaDataObject<MetaDataObjectType>::PrintSelf(std::ostream & os, Indent indent) const | ||
| { | ||
| os << indent; | ||
| this->PrintValue(os); | ||
| os << std::endl; | ||
| } | ||
|
|
||
| template <typename MetaDataObjectType> | ||
| const char * | ||
| MetaDataObject<MetaDataObjectType>::GetMetaDataObjectTypeName() const | ||
|
|
@@ -59,12 +129,6 @@ MetaDataObject<MetaDataObjectType>::SetMetaDataObjectValue(const MetaDataObjectT | |
| Self::Assign(m_MetaDataObjectValue, newValue); | ||
| } | ||
|
|
||
| template <typename MetaDataObjectType> | ||
| void | ||
| MetaDataObject<MetaDataObjectType>::Print(std::ostream & os) const | ||
| { | ||
| Superclass::Print(os); | ||
| } | ||
|
|
||
| } // end namespace itk | ||
|
|
||
|
|
||
There was a problem hiding this comment.
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 addis_iterable_print_vto the global namespace of the user, right? If you agree, you may possibly makeis_iterable_print_va private static member ofMetaDataObject. Or otherwise, maybe it's simpler to just add overloaded private staticMetaDataObject::IsIterablePrint(const T&)member functions. What do you think?There was a problem hiding this comment.
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
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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_printinside the class).https://stackoverflow.com/questions/72190700/explicit-template-argument-list-not-allowed-with-g-but-compiles-with-clang
I will explore further.
There was a problem hiding this comment.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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") forstd::vector<T>andstd::vector<std::vector<T>>, right?You could then use it like:
I guess 😃
There was a problem hiding this comment.
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?