Skip to content

Commit

Permalink
registry: fixed signal race on groups (close #320)
Browse files Browse the repository at this point in the history
  • Loading branch information
skypjack committed Sep 19, 2019
1 parent 9c71b53 commit 5e6cda7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
1 change: 1 addition & 0 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ TODO
* range based registry::remove and some others?
* nested groups: AB/ABC/ABCD/... (hints: sort, check functions)
- sink::before and ordered calls
* ::size_type - c'mon
12 changes: 8 additions & 4 deletions src/entt/entity/registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,15 @@ class basic_registry {
void maybe_valid_if(const Entity entt) {
if constexpr(std::disjunction_v<std::is_same<Get, Component>...>) {
if(((std::is_same_v<Component, Get> || std::get<pool_type<Get> *>(cpools)->has(entt)) && ...)
&& (!std::get<pool_type<Exclude> *>(cpools)->has(entt) && ...))
&& (!std::get<pool_type<Exclude> *>(cpools)->has(entt) && ...)
&& !set.has(entt))
{
set.construct(entt);
}
} else if constexpr(std::disjunction_v<std::is_same<Exclude, Component>...>) {
if((std::get<pool_type<Get> *>(cpools)->has(entt) && ...)
&& ((std::is_same_v<Exclude, Component> || !std::get<pool_type<Exclude> *>(cpools)->has(entt)) && ...))
&& ((std::is_same_v<Exclude, Component> || !std::get<pool_type<Exclude> *>(cpools)->has(entt)) && ...)
&& !set.has(entt))
{
set.construct(entt);
}
Expand All @@ -164,15 +166,17 @@ class basic_registry {
if constexpr(std::disjunction_v<std::is_same<Owned, Component>..., std::is_same<Get, Component>...>) {
if(((std::is_same_v<Component, Owned> || std::get<pool_type<Owned> *>(cpools)->has(entt)) && ...)
&& ((std::is_same_v<Component, Get> || std::get<pool_type<Get> *>(cpools)->has(entt)) && ...)
&& (!std::get<pool_type<Exclude> *>(cpools)->has(entt) && ...))
&& (!std::get<pool_type<Exclude> *>(cpools)->has(entt) && ...)
&& !(std::get<0>(cpools)->index(entt) < owned))
{
const auto pos = owned++;
(std::get<pool_type<Owned> *>(cpools)->swap(std::get<pool_type<Owned> *>(cpools)->data()[pos], entt), ...);
}
} else if constexpr(std::disjunction_v<std::is_same<Exclude, Component>...>) {
if((std::get<pool_type<Owned> *>(cpools)->has(entt) && ...)
&& (std::get<pool_type<Get> *>(cpools)->has(entt) && ...)
&& ((std::is_same_v<Exclude, Component> || !std::get<pool_type<Exclude> *>(cpools)->has(entt)) && ...))
&& ((std::is_same_v<Exclude, Component> || !std::get<pool_type<Exclude> *>(cpools)->has(entt)) && ...)
&& !(std::get<0>(cpools)->index(entt) < owned))
{
const auto pos = owned++;
(std::get<pool_type<Owned> *>(cpools)->swap(std::get<pool_type<Owned> *>(cpools)->data()[pos], entt), ...);
Expand Down
22 changes: 22 additions & 0 deletions test/entt/entity/group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,17 @@ TEST(NonOwningGroup, Less) {
});
}

TEST(NonOwningGroup, SignalRace) {
entt::registry registry;
registry.on_construct<double>().connect<&entt::registry::assign_or_replace<int>>(registry);
registry.group(entt::get<int, double>);

auto entity = registry.create();
registry.assign<double>(entity);

ASSERT_EQ(registry.group(entt::get<int, double>).size(), 1u);
}

TEST(OwningGroup, Functionalities) {
entt::registry registry;
auto group = registry.group<int>(entt::get<char>);
Expand Down Expand Up @@ -983,3 +994,14 @@ TEST(OwningGroup, Less) {
ASSERT_EQ(entity, entt);
});
}

TEST(OwningGroup, SignalRace) {
entt::registry registry;
registry.on_construct<double>().connect<&entt::registry::assign_or_replace<int>>(registry);
registry.group<int>(entt::get<double>);

auto entity = registry.create();
registry.assign<double>(entity);

ASSERT_EQ(registry.group<int>(entt::get<double>).size(), 1u);
}

0 comments on commit 5e6cda7

Please sign in to comment.