Skip to content

Commit

Permalink
Add flecs::iter::other_table()
Browse files Browse the repository at this point in the history
Differential Revision: D62449718

Pull Request resolved: #1352
  • Loading branch information
SanderMertens authored Sep 17, 2024
1 parent 2616e66 commit 5983f23
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 2 deletions.
6 changes: 6 additions & 0 deletions distr/flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -22510,6 +22510,8 @@ struct iter {

flecs::table table() const;

flecs::table other_table() const;

flecs::table_range range() const;

/** Access ctx.
Expand Down Expand Up @@ -32403,6 +32405,10 @@ inline flecs::table iter::table() const {
return flecs::table(iter_->real_world, iter_->table);
}

inline flecs::table iter::other_table() const {
return flecs::table(iter_->real_world, iter_->other_table);
}

inline flecs::table_range iter::range() const {
return flecs::table_range(iter_->real_world, iter_->table,
iter_->offset, iter_->count);
Expand Down
4 changes: 4 additions & 0 deletions include/flecs/addons/cpp/impl/iter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ inline flecs::table iter::table() const {
return flecs::table(iter_->real_world, iter_->table);
}

inline flecs::table iter::other_table() const {
return flecs::table(iter_->real_world, iter_->other_table);
}

inline flecs::table_range iter::range() const {
return flecs::table_range(iter_->real_world, iter_->table,
iter_->offset, iter_->count);
Expand Down
2 changes: 2 additions & 0 deletions include/flecs/addons/cpp/iter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ struct iter {

flecs::table table() const;

flecs::table other_table() const;

flecs::table_range range() const;

/** Access ctx.
Expand Down
5 changes: 4 additions & 1 deletion test/cpp/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,10 @@
"register_twice_w_each",
"register_twice_w_run",
"register_twice_w_run_each",
"register_twice_w_each_run"
"register_twice_w_each_run",
"other_table",
"other_table_w_pair",
"other_table_w_pair_wildcard"
]
}, {
"id": "ComponentLifecycle",
Expand Down
62 changes: 62 additions & 0 deletions test/cpp/src/Observer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1136,3 +1136,65 @@ void Observer_register_twice_w_each_run(void) {
ecs.entity().set(Position{10, 20});
test_int(count2, 1);
}

void Observer_other_table(void) {
flecs::world ecs;

int32_t count = 0;

ecs.observer<Velocity>()
.event(flecs::OnAdd)
.each([&](flecs::iter& it, size_t, Velocity&) {
test_assert(it.table().has<Velocity>());
test_assert(!it.other_table().has<Velocity>());
count ++;
});

flecs::entity e = ecs.entity().add<Position>().add<Velocity>();

test_int(count, 1);
}

void Observer_other_table_w_pair(void) {
flecs::world ecs;

struct Likes {};
struct Apples {};

int32_t count = 0;

ecs.observer()
.with<Likes, Apples>()
.event(flecs::OnAdd)
.each([&](flecs::iter& it, size_t) {
test_assert((it.table().has<Likes, Apples>()));
test_assert((!it.other_table().has<Likes, Apples>()));
count ++;
});

flecs::entity e = ecs.entity().add<Position>().add<Likes, Apples>();

test_int(count, 1);
}

void Observer_other_table_w_pair_wildcard(void) {
flecs::world ecs;

struct Likes {};
struct Apples {};

int32_t count = 0;

ecs.observer()
.with<Likes, Apples>()
.event(flecs::OnAdd)
.each([&](flecs::iter& it, size_t) {
test_assert((it.table().has<Likes>(flecs::Wildcard)));
test_assert((!it.other_table().has<Likes>(flecs::Wildcard)));
count ++;
});

flecs::entity e = ecs.entity().add<Position>().add<Likes, Apples>();

test_int(count, 1);
}
17 changes: 16 additions & 1 deletion test/cpp/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,9 @@ void Observer_register_twice_w_each(void);
void Observer_register_twice_w_run(void);
void Observer_register_twice_w_run_each(void);
void Observer_register_twice_w_each_run(void);
void Observer_other_table(void);
void Observer_other_table_w_pair(void);
void Observer_other_table_w_pair_wildcard(void);

// Testsuite 'ComponentLifecycle'
void ComponentLifecycle_ctor_on_add(void);
Expand Down Expand Up @@ -4789,6 +4792,18 @@ bake_test_case Observer_testcases[] = {
{
"register_twice_w_each_run",
Observer_register_twice_w_each_run
},
{
"other_table",
Observer_other_table
},
{
"other_table_w_pair",
Observer_other_table_w_pair
},
{
"other_table_w_pair_wildcard",
Observer_other_table_w_pair_wildcard
}
};

Expand Down Expand Up @@ -6661,7 +6676,7 @@ static bake_test_suite suites[] = {
"Observer",
NULL,
NULL,
46,
49,
Observer_testcases
},
{
Expand Down

0 comments on commit 5983f23

Please sign in to comment.