Skip to content

Commit

Permalink
Add tests for setting it.offset in hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Mar 10, 2023
1 parent 6c89bdf commit 0996bd6
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 66 deletions.
44 changes: 19 additions & 25 deletions flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -2250,6 +2250,18 @@ void* flecs_get_base_component(
ecs_id_record_t *table_index,
int32_t recur_depth);

void flecs_invoke_hook(
ecs_world_t *world,
ecs_table_t *table,
int32_t count,
int32_t row,
ecs_entity_t *entities,
void *ptr,
ecs_id_t id,
const ecs_type_info_t *ti,
ecs_entity_t event,
ecs_iter_action_t hook);

////////////////////////////////////////////////////////////////////////////////
//// Query API
////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -3169,27 +3181,9 @@ void flecs_on_component_callback(
ecs_type_info_t *ti)
{
ecs_assert(ti != NULL, ECS_INTERNAL_ERROR, NULL);
ecs_iter_t it = { .field_count = 1 };
it.entities = entities;

ecs_size_t size = ti->size;
void *ptr = ecs_vec_get(column, size, row);

flecs_iter_init(world, &it, flecs_iter_cache_all);
it.world = world;
it.real_world = world;
it.table = table;
it.ptrs[0] = ptr;
it.sizes[0] = size;
it.ids[0] = id;
it.event = event;
it.event_id = id;
it.ctx = ti->hooks.ctx;
it.binding_ctx = ti->hooks.binding_ctx;
it.count = count;
flecs_iter_validate(&it);
callback(&it);
ecs_iter_fini(&it);
void *ptr = ecs_vec_get(column, ti->size, row);
flecs_invoke_hook(
world, table, count, row, entities, ptr, id, ti, event, callback);
}

static
Expand All @@ -3200,7 +3194,6 @@ void flecs_ctor_component(
int32_t count)
{
ecs_assert(ti != NULL, ECS_INTERNAL_ERROR, NULL);

ecs_xtor_t ctor = ti->hooks.ctor;
if (ctor) {
void *ptr = ecs_vec_get(column, ti->size, row);
Expand Down Expand Up @@ -6360,11 +6353,11 @@ flecs_component_ptr_t flecs_get_mut(
}

/* -- Private functions -- */
static
void flecs_invoke_hook(
ecs_world_t *world,
ecs_table_t *table,
int32_t count,
int32_t row,
ecs_entity_t *entities,
void *ptr,
ecs_id_t id,
Expand All @@ -6389,6 +6382,7 @@ void flecs_invoke_hook(
it.ctx = ti->hooks.ctx;
it.binding_ctx = ti->hooks.binding_ctx;
it.count = count;
it.offset = row;
flecs_iter_validate(&it);
hook(&it);
ecs_iter_fini(&it);
Expand Down Expand Up @@ -6432,7 +6426,7 @@ void flecs_notify_on_set(
if (on_set) {
ecs_vec_t *c = &table->data.columns[column];
void *ptr = ecs_vec_get(c, ti->size, row);
flecs_invoke_hook(world, table, count, entities, ptr, id,
flecs_invoke_hook(world, table, count, row, entities, ptr, id,
ti, EcsOnSet, on_set);
}
}
Expand Down Expand Up @@ -9576,7 +9570,7 @@ void flecs_cmd_batch_for_entity(
ecs_type_info_t *ti = ptr.ti;
ecs_iter_action_t on_set;
if ((on_set = ti->hooks.on_set)) {
flecs_invoke_hook(world, start_table, 1, &entity,
flecs_invoke_hook(world, start_table, 1, row, &entity,
ptr.ptr, cmd->id, ptr.ti, EcsOnSet, on_set);
}
}
Expand Down
1 change: 0 additions & 1 deletion src/entity.c
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,6 @@ flecs_component_ptr_t flecs_get_mut(
}

/* -- Private functions -- */
static
void flecs_invoke_hook(
ecs_world_t *world,
ecs_table_t *table,
Expand Down
12 changes: 12 additions & 0 deletions src/private_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ void* flecs_get_base_component(
ecs_id_record_t *table_index,
int32_t recur_depth);

void flecs_invoke_hook(
ecs_world_t *world,
ecs_table_t *table,
int32_t count,
int32_t row,
ecs_entity_t *entities,
void *ptr,
ecs_id_t id,
const ecs_type_info_t *ti,
ecs_entity_t event,
ecs_iter_action_t hook);

////////////////////////////////////////////////////////////////////////////////
//// Query API
////////////////////////////////////////////////////////////////////////////////
Expand Down
57 changes: 19 additions & 38 deletions src/table.c
Original file line number Diff line number Diff line change
Expand Up @@ -725,27 +725,9 @@ void flecs_on_component_callback(
ecs_type_info_t *ti)
{
ecs_assert(ti != NULL, ECS_INTERNAL_ERROR, NULL);
ecs_iter_t it = { .field_count = 1 };
it.entities = entities;

ecs_size_t size = ti->size;
void *ptr = ecs_vec_get(column, size, row);

flecs_iter_init(world, &it, flecs_iter_cache_all);
it.world = world;
it.real_world = world;
it.table = table;
it.ptrs[0] = ptr;
it.sizes[0] = size;
it.ids[0] = id;
it.event = event;
it.event_id = id;
it.ctx = ti->hooks.ctx;
it.binding_ctx = ti->hooks.binding_ctx;
it.count = count;
flecs_iter_validate(&it);
callback(&it);
ecs_iter_fini(&it);
void *ptr = ecs_vec_get(column, ti->size, row);
flecs_invoke_hook(
world, table, count, row, entities, ptr, id, ti, event, callback);
}

static
Expand All @@ -756,14 +738,29 @@ void flecs_ctor_component(
int32_t count)
{
ecs_assert(ti != NULL, ECS_INTERNAL_ERROR, NULL);

ecs_xtor_t ctor = ti->hooks.ctor;
if (ctor) {
void *ptr = ecs_vec_get(column, ti->size, row);
ctor(ptr, count, ti);
}
}

static
void flecs_dtor_component(
ecs_type_info_t *ti,
ecs_vec_t *column,
int32_t row,
int32_t count)
{
ecs_assert(ti != NULL, ECS_INTERNAL_ERROR, NULL);

ecs_xtor_t dtor = ti->hooks.dtor;
if (dtor) {
void *ptr = ecs_vec_get(column, ti->size, row);
dtor(ptr, count, ti);
}
}

static
void flecs_run_add_hooks(
ecs_world_t *world,
Expand All @@ -789,22 +786,6 @@ void flecs_run_add_hooks(
}
}

static
void flecs_dtor_component(
ecs_type_info_t *ti,
ecs_vec_t *column,
int32_t row,
int32_t count)
{
ecs_assert(ti != NULL, ECS_INTERNAL_ERROR, NULL);

ecs_xtor_t dtor = ti->hooks.dtor;
if (dtor) {
void *ptr = ecs_vec_get(column, ti->size, row);
dtor(ptr, count, ti);
}
}

static
void flecs_run_remove_hooks(
ecs_world_t *world,
Expand Down
5 changes: 4 additions & 1 deletion test/api/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,10 @@
"with_before_hooks",
"move_ctor_on_move",
"ptr_to_self",
"ctor_move_dtor_from_move_ctor"
"ctor_move_dtor_from_move_ctor",
"on_add_hook_check_offset",
"on_remove_hook_check_offset",
"on_set_hook_check_offset"
]
}, {
"id": "Sorting",
Expand Down
102 changes: 102 additions & 0 deletions test/api/src/ComponentLifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -2898,3 +2898,105 @@ void ComponentLifecycle_ctor_move_dtor_from_move_ctor() {

ecs_fini(world);
}

static int hook_w_offset_invoked = 0;
static int hook_w_offset_offset = 0;
static Position hook_w_offset_position;

static
void hook_w_offset(ecs_iter_t *it) {
Position *p = ecs_field(it, Position, 1);
test_int(it->count, 1);
hook_w_offset_offset = it->offset;
hook_w_offset_invoked ++;
hook_w_offset_position = *p;
}

void ComponentLifecycle_on_add_hook_check_offset() {
ecs_world_t *world = ecs_mini();

ECS_COMPONENT(world, Position);

ecs_set_hooks(world, Position, {
.on_add = hook_w_offset
});

ecs_set(world, 0, Position, {10, 20});
test_int(hook_w_offset_invoked, 1);
test_int(hook_w_offset_offset, 0);

ecs_set(world, 0, Position, {30, 40});
test_int(hook_w_offset_invoked, 2);
test_int(hook_w_offset_offset, 1);

ecs_set(world, 0, Position, {50, 60});
test_int(hook_w_offset_invoked, 3);
test_int(hook_w_offset_offset, 2);

ecs_fini(world);
}

void ComponentLifecycle_on_remove_hook_check_offset() {
ecs_world_t *world = ecs_mini();

ECS_COMPONENT(world, Position);

ecs_set_hooks(world, Position, {
.on_remove = hook_w_offset
});

ecs_entity_t e1 = ecs_set(world, 0, Position, {10, 20});
ecs_entity_t e2 = ecs_set(world, 0, Position, {30, 40});
ecs_entity_t e3 = ecs_set(world, 0, Position, {50, 60});
test_int(hook_w_offset_invoked, 0);

ecs_remove(world, e3, Position);
test_int(hook_w_offset_invoked, 1);
test_int(hook_w_offset_offset, 2);
test_int(hook_w_offset_position.x, 50);
test_int(hook_w_offset_position.y, 60);

ecs_remove(world, e2, Position);
test_int(hook_w_offset_invoked, 2);
test_int(hook_w_offset_offset, 1);
test_int(hook_w_offset_position.x, 30);
test_int(hook_w_offset_position.y, 40);

ecs_remove(world, e1, Position);
test_int(hook_w_offset_invoked, 3);
test_int(hook_w_offset_offset, 0);
test_int(hook_w_offset_position.x, 10);
test_int(hook_w_offset_position.y, 20);

ecs_fini(world);
}

void ComponentLifecycle_on_set_hook_check_offset() {
ecs_world_t *world = ecs_mini();

ECS_COMPONENT(world, Position);

ecs_set_hooks(world, Position, {
.on_set = hook_w_offset
});

ecs_set(world, 0, Position, {10, 20});
test_int(hook_w_offset_invoked, 1);
test_int(hook_w_offset_offset, 0);
test_int(hook_w_offset_position.x, 10);
test_int(hook_w_offset_position.y, 20);

ecs_set(world, 0, Position, {30, 40});
test_int(hook_w_offset_invoked, 2);
test_int(hook_w_offset_offset, 1);
test_int(hook_w_offset_position.x, 30);
test_int(hook_w_offset_position.y, 40);

ecs_set(world, 0, Position, {50, 60});
test_int(hook_w_offset_invoked, 3);
test_int(hook_w_offset_offset, 2);
test_int(hook_w_offset_position.x, 50);
test_int(hook_w_offset_position.y, 60);

ecs_fini(world);
}
17 changes: 16 additions & 1 deletion test/api/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,9 @@ void ComponentLifecycle_with_before_hooks(void);
void ComponentLifecycle_move_ctor_on_move(void);
void ComponentLifecycle_ptr_to_self(void);
void ComponentLifecycle_ctor_move_dtor_from_move_ctor(void);
void ComponentLifecycle_on_add_hook_check_offset(void);
void ComponentLifecycle_on_remove_hook_check_offset(void);
void ComponentLifecycle_on_set_hook_check_offset(void);

// Testsuite 'Sorting'
void Sorting_sort_by_component(void);
Expand Down Expand Up @@ -5679,6 +5682,18 @@ bake_test_case ComponentLifecycle_testcases[] = {
{
"ctor_move_dtor_from_move_ctor",
ComponentLifecycle_ctor_move_dtor_from_move_ctor
},
{
"on_add_hook_check_offset",
ComponentLifecycle_on_add_hook_check_offset
},
{
"on_remove_hook_check_offset",
ComponentLifecycle_on_remove_hook_check_offset
},
{
"on_set_hook_check_offset",
ComponentLifecycle_on_set_hook_check_offset
}
};

Expand Down Expand Up @@ -11979,7 +11994,7 @@ static bake_test_suite suites[] = {
"ComponentLifecycle",
ComponentLifecycle_setup,
NULL,
79,
82,
ComponentLifecycle_testcases
},
{
Expand Down

0 comments on commit 0996bd6

Please sign in to comment.