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

[Tf] Update TF_FOR_ALL to C++11 iterator #3520

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from

Conversation

Cewein
Copy link

@Cewein Cewein commented Feb 5, 2025

Description of Change(s)

Update files under pxr/base/tf by replacing TF_FOR_ALL with C++11-style range-based for each loop.
Additionally, updated pointer usage to align with the new iterator approach, since we now handle references directly rather than raw pointers.

No modification to iterator.h was done nether to the part of bits.h and compressedBits.h that provide support for TF_FOR_ALL as it break the compilation and because the modification of the iterators regarding bits and compressedBits is out of scope of this PR.

File in sub folder have not been touch yet except testenv/bits.

Ongoing Issue(s)

This PR addresses issue #80, though many more instances still need to be resolved.

Checklist

When the type name was easy to write or short enough change it back to the type name rather than auto.

Long type name and namespace where excluded.
@nvmkuruc
Copy link
Collaborator

nvmkuruc commented Feb 5, 2025

Minor note-- It looks like your change description is referencing pxr/base/plug instead of pxr/base/tf.

@Cewein
Copy link
Author

Cewein commented Feb 5, 2025

Minor note-- It looks like your change description is referencing pxr/base/plug instead of pxr/base/tf.

Thank you for noticing, updated the description, added also that the file under pxr/base/tf/testenv where almost not modified. I reserve that for another PR since it would have pilled up to too many file modified at the same time.

If you said this is alright would be happy to push the modification here.

@jesschimein
Copy link
Collaborator

Filed as internal issue #USD-10653

❗ Please make sure that a signed CLA has been submitted!

(This is an automated message. See here for more information.)

@jesschimein
Copy link
Collaborator

/AzurePipelines run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

We already have access to the wanted value with the reference, thus no need for the * operator to access the value.
@jesschimein
Copy link
Collaborator

/AzurePipelines run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@@ -190,8 +190,8 @@ Tf_MallocTagStringMatchTable::SetMatchList(const std::string& matchList)
{
_matchStrings.clear();
std::vector<std::string> items = TfStringTokenize(matchList, ",\t\n");
TF_FOR_ALL(i, items) {
_matchStrings.push_back(_MatchString(TfStringTrim(*i, " ")));
for(std::string i: items) {
Copy link
Member

Choose a reason for hiding this comment

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

could be a reference here, std::string&. could reserve _matchStrings to matchList.size even though the existing code didn't do it. emplace_back would allow construction in place. Another optimization that didn't originally exist, but since we're touching the loop :)

@@ -109,11 +109,11 @@ TfReportActiveErrorMarks()
localStacks = TfErrorMark_GetActiveMarkStacks();
}

TF_FOR_ALL(i, localStacks) {
for(const auto& i: localStacks) {
Copy link
Member

Choose a reason for hiding this comment

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

Throughout the diff, let's match the style of the surrounding code, with a space between for and the paren.

In general the convention with auto is to only use it when the type is visible in the context. Like if std::vector<float> was declared on a previous line, auto would obvious from context. In the case of the _ActiveMarkStacksMap we have to go digging to discover that the iteration will be on a series of vector<uintptr_t> so it would help readability if the line read

for (const vector::<uintptr_t>& i: localStacks) {
Since the goal of this PR is to improve readability by expanding the TF_FOR_ALL macro, I think it's worthwhile to think about each replacement.

Copy link
Author

@Cewein Cewein Feb 14, 2025

Choose a reason for hiding this comment

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

this make sense, I was not sure on how to update with auto or the actual type since sometime the auto as sometime it might translate to lengthy types.

@meshula
Copy link
Member

meshula commented Feb 13, 2025

Thanks for addressing the compile issue. Since we are using this PR to figure out the best way to update the TF_FOR_ALL macros, I've left a couple of suggestions in line to help iterate ideas.

if (_CastFunction *castFunc = _info->GetCastFunc(it->GetTypeid()))
for(const auto& it: _info->baseTypes) {
if (void* tmp = it.CastFromAncestor(ancestor, addr)) {
if (_CastFunction *castFunc = _info->GetCastFunc(it.GetTypeid()))
Copy link
Author

@Cewein Cewein Feb 14, 2025

Choose a reason for hiding this comment

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

Since recent discussion, this is a case where TF_FOR_ALL should be keep as of now in my opinion.

it->GetTypeid() require direct access to the iterator and will loose when switching to range based iterator

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.

4 participants