Skip to content

Commit 9dd433c

Browse files
committed
Code cleanup
1 parent eaa1e40 commit 9dd433c

10 files changed

Lines changed: 105 additions & 72 deletions

File tree

distr/flecs.c

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -38286,7 +38286,6 @@ int flecs_term_finalize(
3828638286
ecs_term_ref_t *src = &term->src;
3828738287
ecs_term_ref_t *first = &term->first;
3828838288
ecs_term_ref_t *second = &term->second;
38289-
ecs_flags64_t first_flags = ECS_TERM_REF_FLAGS(first);
3829038289
ecs_flags64_t second_flags = ECS_TERM_REF_FLAGS(second);
3829138290

3829238291
if (first->name && (first->id & ~EcsTermRefFlags)) {
@@ -38465,10 +38464,9 @@ int flecs_term_finalize(
3846538464
}
3846638465

3846738466
if (first_entity && !ecs_term_match_0(term)) {
38468-
bool first_is_self = (first_flags & EcsTraverseFlags) == EcsSelf;
3846938467
ecs_record_t *first_record = flecs_entities_get(world, first_entity);
3847038468
ecs_table_t *first_table = first_record ? first_record->table : NULL;
38471-
38469+
3847238470
bool first_can_isa = false;
3847338471
if (first_table) {
3847438472
first_can_isa = (first_table->flags & EcsTableHasIsA) != 0;
@@ -38477,14 +38475,14 @@ int flecs_term_finalize(
3847738475
}
3847838476
}
3847938477

38480-
(void)first_is_self;
3848138478
if (flecs_components_get(world, ecs_pair(EcsIsA, first->id)) ||
3848238479
(cr_flags & EcsIdInheritable) || first_can_isa)
3848338480
{
3848438481
term->flags_ |= EcsTermIdInherited;
3848538482
} else {
3848638483
#ifdef FLECS_DEBUG
38487-
if (!first_is_self) {
38484+
ecs_flags64_t first_flags = ECS_TERM_REF_FLAGS(first);
38485+
if ((first_flags & EcsTraverseFlags) != EcsSelf) {
3848838486
ecs_query_impl_t *q = flecs_query_impl(ctx->query);
3848938487
if (q) {
3849038488
ECS_TERMSET_SET(q->final_terms, 1u << ctx->term_index);
@@ -60221,9 +60219,7 @@ void flecs_rtt_init_default_hooks_struct(
6022160219
ecs_entity_t component,
6022260220
const ecs_type_info_t *ti)
6022360221
{
60224-
/* Obtain struct information to figure out what members it contains: */
60225-
const EcsStruct *struct_info = ecs_get(world, component, EcsStruct);
60226-
ecs_assert(struct_info != NULL, ECS_INTERNAL_ERROR, NULL);
60222+
ecs_assert(ecs_has(world, component, EcsStruct), ECS_INTERNAL_ERROR, NULL);
6022760223

6022860224
/* These flags will be set to true if we determine we need to generate a
6022960225
* hook of a particular type: */
@@ -61330,8 +61326,7 @@ int flecs_meta_serialize_struct(
6133061326
ecs_size_t offset,
6133161327
ecs_vec_t *ops)
6133261328
{
61333-
const EcsStruct *ptr = ecs_get(world, type, EcsStruct);
61334-
ecs_assert(ptr != NULL, ECS_INTERNAL_ERROR, NULL);
61329+
ecs_assert(ecs_has(world, type, EcsStruct), ECS_INTERNAL_ERROR, NULL);
6133561330

6133661331
int32_t cur, first = ecs_vec_count(ops);
6133761332
ecs_meta_op_t *op = flecs_meta_ops_add(ops, EcsOpPushStruct);
@@ -84110,11 +84105,17 @@ bool flecs_query_dispatch(
8411084105
ecs_query_run_ctx_t *ctx);
8411184106

8411284107
static
84113-
bool flecs_query_record_inherited(
84114-
const ecs_table_t *table,
84115-
const ecs_table_record_t *tr)
84108+
int16_t flecs_query_next_column_w_inherited(
84109+
const ecs_world_t *world,
84110+
ecs_table_t *table,
84111+
const ecs_table_record_t *tr,
84112+
ecs_id_t id,
84113+
int32_t column)
8411684114
{
84117-
return !ecs_id_match(table->type.array[tr->index], tr->hdr.cr->id);
84115+
if (!ecs_id_match(table->type.array[tr->index], tr->hdr.cr->id)) {
84116+
return flecs_query_next_inherited_column(world, table, id, column);
84117+
}
84118+
return flecs_query_next_column(table, id, column);
8411884119
}
8411984120

8412084121
bool flecs_query_select_w_id(
@@ -84163,13 +84164,8 @@ bool flecs_query_select_w_id(
8416384164
tr = (const ecs_table_record_t*)op_ctx->it.cur;
8416484165
ecs_assert(tr != NULL, ECS_INTERNAL_ERROR, NULL);
8416584166
table = tr->hdr.table;
84166-
if (flecs_query_record_inherited(table, tr)) {
84167-
op_ctx->column = flecs_query_next_inherited_column(
84168-
ctx->world, table, cr->id, op_ctx->column);
84169-
} else {
84170-
op_ctx->column = flecs_query_next_column(
84171-
table, cr->id, op_ctx->column);
84172-
}
84167+
op_ctx->column = flecs_query_next_column_w_inherited(
84168+
ctx->world, table, tr, cr->id, op_ctx->column);
8417384169
op_ctx->remaining --;
8417484170
}
8417584171

@@ -84234,13 +84230,8 @@ bool flecs_query_with(
8423484230
}
8423584231

8423684232
tr = (const ecs_table_record_t*)op_ctx->it.cur;
84237-
if (flecs_query_record_inherited(table, tr)) {
84238-
op_ctx->column = flecs_query_next_inherited_column(
84239-
ctx->world, table, cr->id, op_ctx->column);
84240-
} else {
84241-
op_ctx->column = flecs_query_next_column(
84242-
table, cr->id, op_ctx->column);
84243-
}
84233+
op_ctx->column = flecs_query_next_column_w_inherited(
84234+
ctx->world, table, tr, cr->id, op_ctx->column);
8424484235
ecs_assert(op_ctx->column != -1, ECS_INTERNAL_ERROR, NULL);
8424584236
}
8424684237

@@ -96234,13 +96225,11 @@ int flecs_expr_member_visit_eval(
9623496225
ecs_os_memcpy(ECS_OFFSET(out->value.ptr, i * size),
9623596226
ECS_OFFSET(expr->value.ptr, node->swizzle[i]), size);
9623696227
}
96237-
out->value.type = node->node.type;
96238-
out->owned = false;
9623996228
} else {
9624096229
out->value.ptr = ECS_OFFSET(expr->value.ptr, node->offset);
96241-
out->value.type = node->node.type;
96242-
out->owned = false;
9624396230
}
96231+
out->value.type = node->node.type;
96232+
out->owned = false;
9624496233

9624596234
flecs_expr_stack_pop(ctx->stack);
9624696235
return 0;

src/addons/meta/rtt_lifecycle.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,7 @@ void flecs_rtt_init_default_hooks_struct(
344344
ecs_entity_t component,
345345
const ecs_type_info_t *ti)
346346
{
347-
/* Obtain struct information to figure out what members it contains: */
348-
const EcsStruct *struct_info = ecs_get(world, component, EcsStruct);
349-
ecs_assert(struct_info != NULL, ECS_INTERNAL_ERROR, NULL);
347+
ecs_assert(ecs_has(world, component, EcsStruct), ECS_INTERNAL_ERROR, NULL);
350348

351349
/* These flags will be set to true if we determine we need to generate a
352350
* hook of a particular type: */

src/addons/meta/serializer.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,7 @@ int flecs_meta_serialize_struct(
448448
ecs_size_t offset,
449449
ecs_vec_t *ops)
450450
{
451-
const EcsStruct *ptr = ecs_get(world, type, EcsStruct);
452-
ecs_assert(ptr != NULL, ECS_INTERNAL_ERROR, NULL);
451+
ecs_assert(ecs_has(world, type, EcsStruct), ECS_INTERNAL_ERROR, NULL);
453452

454453
int32_t cur, first = ecs_vec_count(ops);
455454
ecs_meta_op_t *op = flecs_meta_ops_add(ops, EcsOpPushStruct);

src/addons/script/expr/visit_eval.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -821,13 +821,11 @@ int flecs_expr_member_visit_eval(
821821
ecs_os_memcpy(ECS_OFFSET(out->value.ptr, i * size),
822822
ECS_OFFSET(expr->value.ptr, node->swizzle[i]), size);
823823
}
824-
out->value.type = node->node.type;
825-
out->owned = false;
826824
} else {
827825
out->value.ptr = ECS_OFFSET(expr->value.ptr, node->offset);
828-
out->value.type = node->node.type;
829-
out->owned = false;
830826
}
827+
out->value.type = node->node.type;
828+
out->owned = false;
831829

832830
flecs_expr_stack_pop(ctx->stack);
833831
return 0;

src/query/engine/eval.c

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@ bool flecs_query_dispatch(
1818
ecs_query_run_ctx_t *ctx);
1919

2020
static
21-
bool flecs_query_record_inherited(
22-
const ecs_table_t *table,
23-
const ecs_table_record_t *tr)
21+
int16_t flecs_query_next_column_w_inherited(
22+
const ecs_world_t *world,
23+
ecs_table_t *table,
24+
const ecs_table_record_t *tr,
25+
ecs_id_t id,
26+
int32_t column)
2427
{
25-
return !ecs_id_match(table->type.array[tr->index], tr->hdr.cr->id);
28+
if (!ecs_id_match(table->type.array[tr->index], tr->hdr.cr->id)) {
29+
return flecs_query_next_inherited_column(world, table, id, column);
30+
}
31+
return flecs_query_next_column(table, id, column);
2632
}
2733

2834
bool flecs_query_select_w_id(
@@ -71,13 +77,8 @@ bool flecs_query_select_w_id(
7177
tr = (const ecs_table_record_t*)op_ctx->it.cur;
7278
ecs_assert(tr != NULL, ECS_INTERNAL_ERROR, NULL);
7379
table = tr->hdr.table;
74-
if (flecs_query_record_inherited(table, tr)) {
75-
op_ctx->column = flecs_query_next_inherited_column(
76-
ctx->world, table, cr->id, op_ctx->column);
77-
} else {
78-
op_ctx->column = flecs_query_next_column(
79-
table, cr->id, op_ctx->column);
80-
}
80+
op_ctx->column = flecs_query_next_column_w_inherited(
81+
ctx->world, table, tr, cr->id, op_ctx->column);
8182
op_ctx->remaining --;
8283
}
8384

@@ -142,13 +143,8 @@ bool flecs_query_with(
142143
}
143144

144145
tr = (const ecs_table_record_t*)op_ctx->it.cur;
145-
if (flecs_query_record_inherited(table, tr)) {
146-
op_ctx->column = flecs_query_next_inherited_column(
147-
ctx->world, table, cr->id, op_ctx->column);
148-
} else {
149-
op_ctx->column = flecs_query_next_column(
150-
table, cr->id, op_ctx->column);
151-
}
146+
op_ctx->column = flecs_query_next_column_w_inherited(
147+
ctx->world, table, tr, cr->id, op_ctx->column);
152148
ecs_assert(op_ctx->column != -1, ECS_INTERNAL_ERROR, NULL);
153149
}
154150

src/query/validator.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,6 @@ int flecs_term_finalize(
659659
ecs_term_ref_t *src = &term->src;
660660
ecs_term_ref_t *first = &term->first;
661661
ecs_term_ref_t *second = &term->second;
662-
ecs_flags64_t first_flags = ECS_TERM_REF_FLAGS(first);
663662
ecs_flags64_t second_flags = ECS_TERM_REF_FLAGS(second);
664663

665664
if (first->name && (first->id & ~EcsTermRefFlags)) {
@@ -838,10 +837,9 @@ int flecs_term_finalize(
838837
}
839838

840839
if (first_entity && !ecs_term_match_0(term)) {
841-
bool first_is_self = (first_flags & EcsTraverseFlags) == EcsSelf;
842840
ecs_record_t *first_record = flecs_entities_get(world, first_entity);
843841
ecs_table_t *first_table = first_record ? first_record->table : NULL;
844-
842+
845843
bool first_can_isa = false;
846844
if (first_table) {
847845
first_can_isa = (first_table->flags & EcsTableHasIsA) != 0;
@@ -850,14 +848,14 @@ int flecs_term_finalize(
850848
}
851849
}
852850

853-
(void)first_is_self;
854851
if (flecs_components_get(world, ecs_pair(EcsIsA, first->id)) ||
855852
(cr_flags & EcsIdInheritable) || first_can_isa)
856853
{
857854
term->flags_ |= EcsTermIdInherited;
858855
} else {
859856
#ifdef FLECS_DEBUG
860-
if (!first_is_self) {
857+
ecs_flags64_t first_flags = ECS_TERM_REF_FLAGS(first);
858+
if ((first_flags & EcsTraverseFlags) != EcsSelf) {
861859
ecs_query_impl_t *q = flecs_query_impl(ctx->query);
862860
if (q) {
863861
ECS_TERMSET_SET(q->final_terms, 1u << ctx->term_index);

test/cpp/project.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,8 @@
875875
"component_inheritance_each_multiple_derived",
876876
"component_inheritance_each_entity",
877877
"component_inheritance_base_field",
878-
"component_inheritance_field_asserts"
878+
"component_inheritance_field_asserts",
879+
"component_inheritance_virtual"
879880
]
880881
}, {
881882
"id": "QueryBuilder",

test/cpp/src/Query.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@ struct InheritMage : InheritUnit {
1616
int32_t mana;
1717
};
1818

19+
struct VirtualBase {
20+
int32_t x;
21+
VirtualBase(int32_t x_ = 0) : x(x_) { }
22+
virtual ~VirtualBase() { }
23+
virtual int32_t value() const { return -1; }
24+
};
25+
26+
struct VirtualDerived : VirtualBase {
27+
int32_t y;
28+
VirtualDerived(int32_t x_ = 0, int32_t y_ = 0) : VirtualBase(x_), y(y_) { }
29+
int32_t value() const override { return x + y; }
30+
};
31+
1932
void Query_term_each_component(void) {
2033
flecs::world ecs;
2134

@@ -4111,3 +4124,45 @@ void Query_component_inheritance_field_asserts(void) {
41114124
}
41124125
});
41134126
}
4127+
4128+
void Query_component_inheritance_virtual(void) {
4129+
flecs::world ecs;
4130+
4131+
ecs.component<VirtualBase>();
4132+
ecs.component<VirtualDerived>().is_a<VirtualBase>();
4133+
4134+
auto e1 = ecs.entity().set<VirtualDerived>(VirtualDerived(10, 1));
4135+
auto e2 = ecs.entity().set<VirtualDerived>(VirtualDerived(20, 2));
4136+
auto e3 = ecs.entity().set<VirtualDerived>(VirtualDerived(30, 3));
4137+
4138+
const VirtualBase *b1 = e1.try_get<VirtualBase>();
4139+
const VirtualBase *b2 = e2.try_get<VirtualBase>();
4140+
const VirtualBase *b3 = e3.try_get<VirtualBase>();
4141+
test_assert(b1 != nullptr);
4142+
test_assert(b2 != nullptr);
4143+
test_assert(b3 != nullptr);
4144+
test_int(b1->value(), 11);
4145+
test_int(b2->value(), 22);
4146+
test_int(b3->value(), 33);
4147+
4148+
int32_t sum = 0, count = 0;
4149+
ecs.query<VirtualBase>().each([&](VirtualBase& b) {
4150+
sum += b.value();
4151+
count ++;
4152+
});
4153+
test_int(count, 3);
4154+
test_int(sum, 66);
4155+
4156+
sum = 0; count = 0;
4157+
ecs.query<VirtualBase>().run([&](flecs::iter& it) {
4158+
while (it.next()) {
4159+
auto f = it.base_field<VirtualBase>(0);
4160+
for (auto i : it) {
4161+
sum += f[i].value();
4162+
count ++;
4163+
}
4164+
}
4165+
});
4166+
test_int(count, 3);
4167+
test_int(sum, 66);
4168+
}

test/cpp/src/main.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,7 @@ void Query_component_inheritance_each_multiple_derived(void);
847847
void Query_component_inheritance_each_entity(void);
848848
void Query_component_inheritance_base_field(void);
849849
void Query_component_inheritance_field_asserts(void);
850+
void Query_component_inheritance_virtual(void);
850851

851852
// Testsuite 'QueryBuilder'
852853
void QueryBuilder_setup(void);
@@ -4955,6 +4956,10 @@ bake_test_case Query_testcases[] = {
49554956
{
49564957
"component_inheritance_field_asserts",
49574958
Query_component_inheritance_field_asserts
4959+
},
4960+
{
4961+
"component_inheritance_virtual",
4962+
Query_component_inheritance_virtual
49584963
}
49594964
};
49604965

@@ -8194,7 +8199,7 @@ static bake_test_suite suites[] = {
81948199
"Query",
81958200
NULL,
81968201
NULL,
8197-
146,
8202+
147,
81988203
Query_testcases
81998204
},
82008205
{

test/script/src/Eval.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15966,12 +15966,6 @@ void Eval_struct_inheritance(void) {
1596615966

1596715967
test_assert(ecs_script_run(world, NULL, expr, NULL) == 0);
1596815968

15969-
ecs_entity_t e = ecs_lookup(world, "e");
15970-
test_assert(e != 0);
15971-
15972-
const ecs_f32_t *ptr = ecs_get(world, e, ecs_f32_t);
15973-
test_assert(ptr != NULL);
15974-
test_int(*ptr, 10);
1597515969
ecs_entity_t foo = ecs_lookup(world, "Foo");
1597615970
ecs_entity_t bar = ecs_lookup(world, "Bar");
1597715971
test_assert(foo != 0);

0 commit comments

Comments
 (0)