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

Joining views with storages #1196

Merged
merged 5 commits into from
Dec 18, 2024
Merged

Conversation

TerensTare
Copy link
Contributor

Views are pretty much a core utility of EnTT already and there is good support for many common operations. One of them is joining with other views, especially useful when one view might have named pools. However, the syntax becomes a little noisy when you need to join a view with one storage, which most often is a named void storage or a storage out of the registry (from my experience). To do this, we currently have to do something like:

entt::storage_for_t<void> dirty_entities;
// ...
auto view = registry.view<C1, const C2>() | entt::basic_view{dirty_entities};
for (auto id : view) {
    // ...
}

To address this, I propose to add a join function to basic_view, which works similarly to the existing operator|, except that it only accepts a storage as a parameter instead of a view. So the example before would now look like this:

entt::storage_for_t<void> dirty_entities;
// ...
auto view = registry.view<C1, const C2>().join(dirty_entities);
for (auto id : view)
{
    // ...
}

Now the function returns a new view, so you can chain .join() calls one after another. This is something of preference but I would advise to use operator| in that case.

@codecov-commenter
Copy link

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (cadf755) to head (ddf49ad).

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##            master     #1196    +/-   ##
==========================================
  Coverage   100.00%   100.00%            
==========================================
  Files          158       158            
  Lines        26792     27027   +235     
==========================================
+ Hits         26792     27027   +235     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@TerensTare TerensTare changed the base branch from master to wip December 1, 2024 21:59
@skypjack
Copy link
Owner

skypjack commented Dec 2, 2024

Out of curiosity, why not:

auto view = registry.view<C1, const C2>() | dirty_entities;

It seems more on the line of the already existing functionality. Any particular reasons for using a dedicate function instead?

@skypjack skypjack self-assigned this Dec 2, 2024
@skypjack skypjack self-requested a review December 2, 2024 16:23
@TerensTare
Copy link
Contributor Author

Any particular reasons for using a dedicate function instead?

Purely a matter of semantics, since storages are a different type from views. Nothing against operator| except that. Also, I just realized that the implementation does not support joining a view with a const-storages,I will have to add that too.

* @sa operator|
*/
template<typename OGet, typename = std::enable_if_t<std::is_lvalue_reference_v<OGet>>>
[[nodiscard]] auto join(OGet &&other) const noexcept {
Copy link
Owner

Choose a reason for hiding this comment

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

Ehy, sorry for the late reply. I've been busy recently.
I think you can have OGet &other and remove the is_lvalue_reference_v check as a consequence, no? remove_reference_t below is also useless then.
Same for the other join. Am I missing something? 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Huh I'd swear I had a problem with the const not propagating from storage to view earlier, I probably did something wrong... Anyway I just reverted the changes, let me know if you need anything else.

@skypjack
Copy link
Owner

Looks like my playing with branches other than master messed up here. I'll merge the PR to a yet another branch and clean it up. 👍

@skypjack skypjack changed the base branch from wip to join_storage December 18, 2024 15:46
@skypjack skypjack added the enhancement accepted requests, sooner or later I'll do it label Dec 18, 2024
@skypjack skypjack merged commit 832ff4a into skypjack:join_storage Dec 18, 2024
35 of 36 checks passed
@TerensTare TerensTare deleted the view_join branch December 18, 2024 16:40
@TerensTare TerensTare restored the view_join branch December 18, 2024 17:44
@TerensTare TerensTare deleted the view_join branch December 18, 2024 17:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement accepted requests, sooner or later I'll do it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants