Skip to content

Commit

Permalink
#525 fix issue where entity::each passes stage to ecs_type_match
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Nov 4, 2021
1 parent d807ad1 commit 8ed305f
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 4 deletions.
4 changes: 4 additions & 0 deletions flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -33137,6 +33137,8 @@ bool ecs_type_has_id(
ecs_id_t id,
bool owned)
{
ecs_poly_assert(world, ecs_world_t);

return search_type(world, NULL, type, 0, id, owned ? 0 : EcsIsA, 0, 0, 0,
NULL, NULL) != -1;
}
Expand All @@ -33161,6 +33163,8 @@ int32_t ecs_type_match(
ecs_entity_t *subject_out,
int32_t *count_out)
{
ecs_poly_assert(world, ecs_world_t);

if (subject_out) {
*subject_out = 0;
}
Expand Down
5 changes: 4 additions & 1 deletion flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -17889,6 +17889,9 @@ inline void entity_view::each(const Func& func) const {

template <typename Func>
inline void entity_view::each(flecs::id_t pred, flecs::id_t obj, const Func& func) const {
flecs::world_t *real_world = std::const_cast<flecs::world_t*>(
ecs_get_world(m_world));

const ecs_table_t *table = ecs_get_table(m_world, m_id);
if (!table) {
return;
Expand All @@ -17909,7 +17912,7 @@ inline void entity_view::each(flecs::id_t pred, flecs::id_t obj, const Func& fun
_ecs_vector_first(type, ECS_VECTOR_T(ecs_id_t)));

while (-1 != (cur = ecs_type_match(
m_world, table, type, cur, pattern, 0, 0, 0, NULL, NULL)))
real_world, table, type, cur, pattern, 0, 0, 0, NULL, NULL)))
{
flecs::id ent(m_world, ids[cur]);
func(ent);
Expand Down
5 changes: 4 additions & 1 deletion include/flecs/addons/cpp/impl/entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ inline void entity_view::each(const Func& func) const {

template <typename Func>
inline void entity_view::each(flecs::id_t pred, flecs::id_t obj, const Func& func) const {
flecs::world_t *real_world = std::const_cast<flecs::world_t*>(
ecs_get_world(m_world));

const ecs_table_t *table = ecs_get_table(m_world, m_id);
if (!table) {
return;
Expand All @@ -162,7 +165,7 @@ inline void entity_view::each(flecs::id_t pred, flecs::id_t obj, const Func& fun
_ecs_vector_first(type, ECS_VECTOR_T(ecs_id_t)));

while (-1 != (cur = ecs_type_match(
m_world, table, type, cur, pattern, 0, 0, 0, NULL, NULL)))
real_world, table, type, cur, pattern, 0, 0, 0, NULL, NULL)))
{
flecs::id ent(m_world, ids[cur]);
func(ent);
Expand Down
4 changes: 4 additions & 0 deletions src/type.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ bool ecs_type_has_id(
ecs_id_t id,
bool owned)
{
ecs_poly_assert(world, ecs_world_t);

return search_type(world, NULL, type, 0, id, owned ? 0 : EcsIsA, 0, 0, 0,
NULL, NULL) != -1;
}
Expand All @@ -152,6 +154,8 @@ int32_t ecs_type_match(
ecs_entity_t *subject_out,
int32_t *count_out)
{
ecs_poly_assert(world, ecs_world_t);

if (subject_out) {
*subject_out = 0;
}
Expand Down
3 changes: 2 additions & 1 deletion test/cpp_api/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@
"child_of",
"child_of_w_type",
"id_get_entity",
"id_get_invalid_entity"
"id_get_invalid_entity",
"each_in_stage"
]
}, {
"id": "Pairs",
Expand Down
27 changes: 27 additions & 0 deletions test/cpp_api/src/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2963,3 +2963,30 @@ void Entity_id_get_invalid_entity() {

id.entity();
}

void Entity_each_in_stage() {
flecs::world world;

struct Rel { };
struct Obj { };

auto e = world.entity().add<Rel, Obj>();
test_assert((e.has<Rel, Obj>()));

world.staging_begin();

auto s = world.get_stage(0);
auto em = e.mut(s);
test_assert((em.has<Rel, Obj>()));

int count = 0;

em.each<Rel>([&](flecs::entity obj) {
count ++;
test_assert(obj == world.id<Obj>());
});

test_int(count, 1);

world.staging_end();
}
7 changes: 6 additions & 1 deletion test/cpp_api/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ void Entity_child_of(void);
void Entity_child_of_w_type(void);
void Entity_id_get_entity(void);
void Entity_id_get_invalid_entity(void);
void Entity_each_in_stage(void);

// Testsuite 'Pairs'
void Pairs_add_component_pair(void);
Expand Down Expand Up @@ -1349,6 +1350,10 @@ bake_test_case Entity_testcases[] = {
{
"id_get_invalid_entity",
Entity_id_get_invalid_entity
},
{
"each_in_stage",
Entity_each_in_stage
}
};

Expand Down Expand Up @@ -3444,7 +3449,7 @@ static bake_test_suite suites[] = {
"Entity",
NULL,
NULL,
158,
159,
Entity_testcases
},
{
Expand Down

0 comments on commit 8ed305f

Please sign in to comment.