Skip to content

ENH: Add operator==, operator!= to MetaDataObject and MetaDataDictionary#2246

Merged
dzenanz merged 1 commit intoInsightSoftwareConsortium:masterfrom
N-Dekker:Equal-MetaDataObject
Apr 28, 2021
Merged

ENH: Add operator==, operator!= to MetaDataObject and MetaDataDictionary#2246
dzenanz merged 1 commit intoInsightSoftwareConsortium:masterfrom
N-Dekker:Equal-MetaDataObject

Conversation

@N-Dekker
Copy link
Copy Markdown
Contributor

Follow-up to "ENH: Add operator== and operator!= to itk::Image"
pull request #2188
commit 7f3337d

@dzenanz dzenanz requested a review from blowekamp January 11, 2021 14:12
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.

Looks good to me, aside from compile warnings.

Comment thread Modules/Core/Common/include/itkMetaDataDictionary.h Outdated
bool
Equal(const MetaDataObjectBase & metaDataObject) const override
{
return (typeid(metaDataObject) == typeid(Self)) && (*this == static_cast<const Self &>(metaDataObject));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why is this typeid comparison done over using a dynamic_cast?

Beware there are monsters here. There have been issues related to shared libraries, linkage and duplicate symbols ( typeids ) not being equal on some operating symbols (OS X), which cause these types of comparisons or dynamic_cast to fail which the object refer to the same class/lines of code but the type information is from difference complications units. I hope we have addressed they will not reoccur. However, giving this history I need to and some checks to help if this does reoccur.

Please break up this compound statement. When the typeid or dynamic_cast fails we should add an additional check to see if the objects are from the same class. The type_info's name method may provide this check. So add if the first comparison fails: if ( typeid(metaDataObject).name() == typeid(Self).name() ) itkWarningMacro("..."). This will be of great use if the linkage issue reoccurs.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@blowekamp Thanks for your feedback. I used typeid, to state explicitly that two meta-data-objects only compare equal when their types are equal. I think the intension of typeid(metaDataObject) == typeid(Self) is most clear.

However, if you prefer dynamic_cast, that would be fine to me as well. I could certainly make it:

const auto metaDataObjectAsSelf = dynamic_cast<const Self *>(&metaDataObject);
return (metaDataObjectAsSelf != nullptr) && (*this == *metaDataObjectAsSelf);

Would you like that better?

Regarding those possible issues related to shared libraries, linkage and duplicate symbols, does it help that MetaDataObject<T> is explicitly instantiated for a specific set of types? In https://github.com/InsightSoftwareConsortium/ITK/blob/v5.2rc01/Modules/Core/Common/src/itkMetaDataObject.cxx

I would rather not put a itkWarningMacro in there, as I think by definitions, operator== should not have any side effects.

Copy link
Copy Markdown
Member

@blowekamp blowekamp Jan 11, 2021

Choose a reason for hiding this comment

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

const auto metaDataObjectAsSelf = dynamic_cast<const Self *>(&metaDataObject);
return (metaDataObjectAsSelf != nullptr) && (*this == *metaDataObjectAsSelf);

Would you like that better?
@N-Dekker Yes. It is more consistent with ITK, and less likely to be detected as improper use of static_cast.

I would rather not put a itkWarningMacro in there, as I think by definitions, operator== should not have any side effects.

It will improve robustness for a problematic situation that likely can still occur in some configuration of ITK. It is important that there are checks in ITK to ensure the code is robust and errouneous states and able to be tracked down. It will not have side effect ( display warning ) unless the problem is encountered, and it will make the issue be able to tracked down.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

My idea with this PR is that (metaDataObjectBase1 == metaDataObjectBase2) would return false, and (metaDataObjectBase1 != metaDataObjectBase2) would return true, when they refer to objects whose value type is different. The == and != comparisons themselves would then still be valid. That's why I think a warning or error message is not necessary. OK?

Follow-up to "ENH: Add operator== and operator!= to itk::Image"
pull request InsightSoftwareConsortium#2188
commit 7f3337d
@N-Dekker N-Dekker force-pushed the Equal-MetaDataObject branch from ce8cf43 to 9cb80f0 Compare January 11, 2021 20:20
@N-Dekker
Copy link
Copy Markdown
Contributor Author

This pull request is just a nice-to-have to me. It would be nice to be able to compare meta-data this way. But it isn't something I need right away.

@dzenanz
Copy link
Copy Markdown
Member

dzenanz commented Apr 15, 2021

Well, now is a good time to merge this. Brad?

@dzenanz dzenanz marked this pull request as ready for review April 15, 2021 12:39
@blowekamp blowekamp self-requested a review April 15, 2021 13:07
@dzenanz dzenanz merged commit 694bbc0 into InsightSoftwareConsortium:master Apr 28, 2021
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Mar 17, 2022
Follow-up to:
pull request InsightSoftwareConsortium#2246
commit 694bbc0
"ENH: Add operator==, operator!= to MetaDataObject and MetaDataDictionary"
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Mar 17, 2022
Follow-up to:
pull request InsightSoftwareConsortium#2246
commit 694bbc0
"ENH: Add operator==, operator!= to MetaDataObject and MetaDataDictionary"
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Mar 17, 2022
Follow-up to:
pull request InsightSoftwareConsortium#2246
commit 694bbc0
"ENH: Add operator==, operator!= to MetaDataObject and MetaDataDictionary"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants