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

WIP: STYLE: Use ImageRegion auto [index, size] = region structured bindings #4379

Conversation

N-Dekker
Copy link
Contributor

@N-Dekker N-Dekker commented Dec 27, 2023

Replaced initializations of the form:

SizeType size = region.GetSize();
IndexType index = region.GetIndex();

By using structured binding of the form auto [index, size] = region, as was introduced by pull request #4367 commit 72aa9a6 "ENH: ImageRegion support C++17 structured binding"

Did so by Notepad++ Replace in Files, using regular expressions:

Find what: ^([ ]+)const (.*)\w.+[ ]+(\w+) = (.+)\.GetIndex\(\);\r\n\1const \2\w+[ ]+(\w+) = \4\.GetSize\(\);$
Replace with: $1const auto [$3, $5] = $4;

Find what: ^([ ]+)const (.*)\w.+[ ]+(\w+) = (.+)\.GetSize\(\);\r\n\1const \2\w+[ ]+(\w+) = \4\.GetIndex\(\);$
Replace with: $1const auto [$5, $3] = $4;

Find what: ^([ ]+)(.*)\w.+[ ]+(\w+) = (.+)\.GetIndex\(\);\r\n\1\2\w+[ ]+(\w+) = \4\.GetSize\(\);$
Replace with: $1auto [$3, $5] = $4;

Find what: ^([ ]+)(.*)\w.+[ ]+(\w+) = (.+)\.GetSize\(\);\r\n\1\2\w+[ ]+(\w+) = \4\.GetIndex\(\);$
Replace with: $1auto [$5, $3] = $4;

Manually added const whenever the values of the bindings were not modified, excluded ImageIORegion, and removed Index and Size type aliases that are no longer used after this commit.


When reviewing, it may be helpful to ignore whitespace changes: https://github.com/InsightSoftwareConsortium/ITK/pull/4379/files?w=1

@github-actions github-actions bot added type:Infrastructure Infrastructure/ecosystem related changes, such as CMake or buildbots type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct area:Bridge Issues affecting the Bridge module 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 type:Style Style changes: no logic impact (indentation, comments, naming) area:Numerics Issues affecting the Numerics module labels Dec 27, 2023
Copy link
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. Remove now unused type definitions to get rid of those warnings for a clean CI.

@N-Dekker N-Dekker force-pushed the Use-structured-binding-ImageRegion branch from 8ed2f28 to 9d78904 Compare January 1, 2024 23:03
@N-Dekker N-Dekker changed the title STYLE: Use ImageRegion auto [index, size] = region structured bindings WIP: STYLE: Use ImageRegion auto [index, size] = region structured bindings Jan 1, 2024
@N-Dekker N-Dekker force-pushed the Use-structured-binding-ImageRegion branch from 9d78904 to 7d1e437 Compare January 2, 2024 09:27
@github-actions github-actions bot removed the type:Style Style changes: no logic impact (indentation, comments, naming) label Jan 2, 2024
@N-Dekker
Copy link
Contributor Author

N-Dekker commented Jan 2, 2024

FYI, I'm considering to postpone this pull request, for the moment. I think it's a nice "proof of principle" on how to use structured bindings with ImageRegion. It looks nice. And it does significantly reduce the amount of boilerplate code. But it appears that the PR will trigger some warnings that I find hard to deal with! Like Microsoft's lnt-accidental-copy, "auto doesn't deduce references. A possibly unintended copy is being made" and Coverity's "AUTO_CAUSES_COPY".

For example, the following line may trigger such a warning (lnt-accidental-copy, AUTO_CAUSES_COPY):

const auto [index, size] = region; // copying

The warning would suggest doing the structured bindings "by reference" instead:

const auto & [index, size] = region; // referencing

But I do not blindly want to follow their suggestion to replace copying with referencing. For a rather small object like ImageRegion, copying is pretty fast already, and it will be even faster when ImageRegion has become trivially copyable (pull request #4344). Whereas referencing potentially makes the code more complicated, as it will potentially introduce more dependencies between variables.

I'm considering a workaround for those warnings, but I'm not sure yet. 🤷


Update, 3 January, 2024: A workaround against those potential warnings might be coming from the following pull request

Which would allow: const auto [index, size] = itk::Copy(region) without a warning.

Replaced initializations of the form:

    SizeType size = region.GetSize();
    IndexType index = region.GetIndex();

By using structured binding of the form `auto [index, size] = region`, as was
introduced by pull request InsightSoftwareConsortium#4367
commit 72aa9a6 "ENH: `ImageRegion` support C++17
structured binding"

Did so by Notepad++ Replace in Files, using regular expressions:

    Find what: ^([ ]+)const (.*)\w.+[ ]+(\w+) = (.+)\.GetIndex\(\);\r\n\1const \2\w+[ ]+(\w+) = \4\.GetSize\(\);$
    Replace with: $1const auto [$3, $5] = $4;

    Find what: ^([ ]+)const (.*)\w.+[ ]+(\w+) = (.+)\.GetSize\(\);\r\n\1const \2\w+[ ]+(\w+) = \4\.GetIndex\(\);$
    Replace with: $1const auto [$5, $3] = $4;

    Find what: ^([ ]+)(.*)\w.+[ ]+(\w+) = (.+)\.GetIndex\(\);\r\n\1\2\w+[ ]+(\w+) = \4\.GetSize\(\);$
    Replace with: $1auto [$3, $5] = $4;

    Find what: ^([ ]+)(.*)\w.+[ ]+(\w+) = (.+)\.GetSize\(\);\r\n\1\2\w+[ ]+(\w+) = \4\.GetIndex\(\);$
    Replace with: $1auto [$5, $3] = $4;

Manually added `const` to bindings in `SetBound(const SizeType &)` member
functions, excluded `ImageIORegion`, and removed `Index` and `Size` type aliases
that are no longer used after this commit.
Added `const`, whenever the values of the bindings were not modified, after the
declaration.
@N-Dekker N-Dekker force-pushed the Use-structured-binding-ImageRegion branch from 7d1e437 to b4efee3 Compare January 14, 2024 15:29
Removed those local "region" variables, as they appear that appear no longer
useful.
@N-Dekker
Copy link
Contributor Author

N-Dekker commented Feb 3, 2024

Basically I think this pull request is OK, but if it gets merged, we might still get into a discussion on const auto [index, size] = region (copy) versus const auto & [index, size] = region (reference). I do prefer copying, because an ImageRegion is quite a lightweight object, but my position in favor of copying will be stronger once itk::ImageRegion has become trivially-copyable.

itk::ImageRegion should be trivially-copyable in the future: pull request #4344 commit 1f3bbb6 So maybe we can reconsider using the const auto [index, size] = region syntax in the future! For the time being, I'll just close this pull request.

@N-Dekker N-Dekker closed this Feb 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:Bridge Issues affecting the Bridge module 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:Registration Issues affecting the Registration module area:Segmentation Issues affecting the Segmentation module area:Video Issues affecting the Video module type:Infrastructure Infrastructure/ecosystem related changes, such as CMake or buildbots 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.

2 participants