Skip to content

Commit 8ed305f

Browse files
committed
#525 fix issue where entity::each passes stage to ecs_type_match
1 parent d807ad1 commit 8ed305f

File tree

7 files changed

+51
-4
lines changed

7 files changed

+51
-4
lines changed

flecs.c

+4
Original file line numberDiff line numberDiff line change
@@ -33137,6 +33137,8 @@ bool ecs_type_has_id(
3313733137
ecs_id_t id,
3313833138
bool owned)
3313933139
{
33140+
ecs_poly_assert(world, ecs_world_t);
33141+
3314033142
return search_type(world, NULL, type, 0, id, owned ? 0 : EcsIsA, 0, 0, 0,
3314133143
NULL, NULL) != -1;
3314233144
}
@@ -33161,6 +33163,8 @@ int32_t ecs_type_match(
3316133163
ecs_entity_t *subject_out,
3316233164
int32_t *count_out)
3316333165
{
33166+
ecs_poly_assert(world, ecs_world_t);
33167+
3316433168
if (subject_out) {
3316533169
*subject_out = 0;
3316633170
}

flecs.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -17889,6 +17889,9 @@ inline void entity_view::each(const Func& func) const {
1788917889

1789017890
template <typename Func>
1789117891
inline void entity_view::each(flecs::id_t pred, flecs::id_t obj, const Func& func) const {
17892+
flecs::world_t *real_world = std::const_cast<flecs::world_t*>(
17893+
ecs_get_world(m_world));
17894+
1789217895
const ecs_table_t *table = ecs_get_table(m_world, m_id);
1789317896
if (!table) {
1789417897
return;
@@ -17909,7 +17912,7 @@ inline void entity_view::each(flecs::id_t pred, flecs::id_t obj, const Func& fun
1790917912
_ecs_vector_first(type, ECS_VECTOR_T(ecs_id_t)));
1791017913

1791117914
while (-1 != (cur = ecs_type_match(
17912-
m_world, table, type, cur, pattern, 0, 0, 0, NULL, NULL)))
17915+
real_world, table, type, cur, pattern, 0, 0, 0, NULL, NULL)))
1791317916
{
1791417917
flecs::id ent(m_world, ids[cur]);
1791517918
func(ent);

include/flecs/addons/cpp/impl/entity.hpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ inline void entity_view::each(const Func& func) const {
142142

143143
template <typename Func>
144144
inline void entity_view::each(flecs::id_t pred, flecs::id_t obj, const Func& func) const {
145+
flecs::world_t *real_world = std::const_cast<flecs::world_t*>(
146+
ecs_get_world(m_world));
147+
145148
const ecs_table_t *table = ecs_get_table(m_world, m_id);
146149
if (!table) {
147150
return;
@@ -162,7 +165,7 @@ inline void entity_view::each(flecs::id_t pred, flecs::id_t obj, const Func& fun
162165
_ecs_vector_first(type, ECS_VECTOR_T(ecs_id_t)));
163166

164167
while (-1 != (cur = ecs_type_match(
165-
m_world, table, type, cur, pattern, 0, 0, 0, NULL, NULL)))
168+
real_world, table, type, cur, pattern, 0, 0, 0, NULL, NULL)))
166169
{
167170
flecs::id ent(m_world, ids[cur]);
168171
func(ent);

src/type.c

+4
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ bool ecs_type_has_id(
128128
ecs_id_t id,
129129
bool owned)
130130
{
131+
ecs_poly_assert(world, ecs_world_t);
132+
131133
return search_type(world, NULL, type, 0, id, owned ? 0 : EcsIsA, 0, 0, 0,
132134
NULL, NULL) != -1;
133135
}
@@ -152,6 +154,8 @@ int32_t ecs_type_match(
152154
ecs_entity_t *subject_out,
153155
int32_t *count_out)
154156
{
157+
ecs_poly_assert(world, ecs_world_t);
158+
155159
if (subject_out) {
156160
*subject_out = 0;
157161
}

test/cpp_api/project.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@
172172
"child_of",
173173
"child_of_w_type",
174174
"id_get_entity",
175-
"id_get_invalid_entity"
175+
"id_get_invalid_entity",
176+
"each_in_stage"
176177
]
177178
}, {
178179
"id": "Pairs",

test/cpp_api/src/Entity.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -2963,3 +2963,30 @@ void Entity_id_get_invalid_entity() {
29632963

29642964
id.entity();
29652965
}
2966+
2967+
void Entity_each_in_stage() {
2968+
flecs::world world;
2969+
2970+
struct Rel { };
2971+
struct Obj { };
2972+
2973+
auto e = world.entity().add<Rel, Obj>();
2974+
test_assert((e.has<Rel, Obj>()));
2975+
2976+
world.staging_begin();
2977+
2978+
auto s = world.get_stage(0);
2979+
auto em = e.mut(s);
2980+
test_assert((em.has<Rel, Obj>()));
2981+
2982+
int count = 0;
2983+
2984+
em.each<Rel>([&](flecs::entity obj) {
2985+
count ++;
2986+
test_assert(obj == world.id<Obj>());
2987+
});
2988+
2989+
test_int(count, 1);
2990+
2991+
world.staging_end();
2992+
}

test/cpp_api/src/main.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ void Entity_child_of(void);
167167
void Entity_child_of_w_type(void);
168168
void Entity_id_get_entity(void);
169169
void Entity_id_get_invalid_entity(void);
170+
void Entity_each_in_stage(void);
170171

171172
// Testsuite 'Pairs'
172173
void Pairs_add_component_pair(void);
@@ -1349,6 +1350,10 @@ bake_test_case Entity_testcases[] = {
13491350
{
13501351
"id_get_invalid_entity",
13511352
Entity_id_get_invalid_entity
1353+
},
1354+
{
1355+
"each_in_stage",
1356+
Entity_each_in_stage
13521357
}
13531358
};
13541359

@@ -3444,7 +3449,7 @@ static bake_test_suite suites[] = {
34443449
"Entity",
34453450
NULL,
34463451
NULL,
3447-
158,
3452+
159,
34483453
Entity_testcases
34493454
},
34503455
{

0 commit comments

Comments
 (0)