From 2d801249bd708457a8e9d02d5a7752cdaa658d35 Mon Sep 17 00:00:00 2001 From: Javier Peletier Date: Thu, 31 Oct 2024 15:41:58 +0100 Subject: [PATCH] #1414 Notify on enum modifications --- distr/flecs.c | 1 + src/addons/meta/meta.c | 1 + test/meta/project.json | 3 ++- test/meta/src/EnumTypes.c | 48 +++++++++++++++++++++++++++++++++++++++ test/meta/src/main.c | 7 +++++- 5 files changed, 58 insertions(+), 2 deletions(-) diff --git a/distr/flecs.c b/distr/flecs.c index ab0c925acb..5c718e12aa 100644 --- a/distr/flecs.c +++ b/distr/flecs.c @@ -50168,6 +50168,7 @@ int flecs_add_constant_to_enum( cptr = ecs_ensure_id(world, e, type); cptr[0] = value; + ecs_modified(world, type, EcsEnum); return 0; } diff --git a/src/addons/meta/meta.c b/src/addons/meta/meta.c index a0e9af38c1..45660a4392 100644 --- a/src/addons/meta/meta.c +++ b/src/addons/meta/meta.c @@ -638,6 +638,7 @@ int flecs_add_constant_to_enum( cptr = ecs_ensure_id(world, e, type); cptr[0] = value; + ecs_modified(world, type, EcsEnum); return 0; } diff --git a/test/meta/project.json b/test/meta/project.json index 8609a38fd8..df98e2a79e 100644 --- a/test/meta/project.json +++ b/test/meta/project.json @@ -81,7 +81,8 @@ "struct_w_enum", "zero_initialized", "enum_relation", - "enum_w_short_notation" + "enum_w_short_notation", + "enum_modified_event" ] }, { "id": "BitmaskTypes", diff --git a/test/meta/src/EnumTypes.c b/test/meta/src/EnumTypes.c index f040c45a57..86650d9343 100644 --- a/test/meta/src/EnumTypes.c +++ b/test/meta/src/EnumTypes.c @@ -263,3 +263,51 @@ void EnumTypes_constant_w_type_prefix(void) { void EnumTypes_constant_w_name_type_prefix(void) { // Implement testcase } + + +int enum_modified_calls = 0; + +static +void enum_modified(ecs_iter_t *it) { + enum_modified_calls ++; +} + +/* Checks that observers watching enum changes are notified */ +void EnumTypes_enum_modified_event(void) { + ecs_world_t *world = ecs_init(); + + ecs_observer(world, { + .query.terms[0] = { .id = ecs_id(EcsEnum) }, + .events = {EcsOnSet}, + .callback = enum_modified + }); + + ecs_entity_t e = ecs_enum_init(world, &(ecs_enum_desc_t){ + .constants = { + {"Red"}, {"Blue"} + } + }); + + test_assert(e != 0); + /* must receive two calls, one for each enum member added */ + test_int(enum_modified_calls, 2); + + /* run-time add a new member constant to the enum: */ + ecs_entity_t old_scope = ecs_set_scope(world, e); + ecs_entity_t c = ecs_entity(world, { + .name = "Orange" + }); + ecs_add_id(world, c, EcsConstant); + ecs_set_scope(world, old_scope); + + /* check if observer was called after adding */ + /* a new member constant */ + test_int(enum_modified_calls, 3); + + meta_test_enum(world, e, 3); + meta_test_constant(world, e, "Red", 0); + meta_test_constant(world, e, "Blue", 1); + meta_test_constant(world, e, "Orange", 2); + + ecs_fini(world); +} \ No newline at end of file diff --git a/test/meta/src/main.c b/test/meta/src/main.c index a17c93c890..e65dd4a340 100644 --- a/test/meta/src/main.c +++ b/test/meta/src/main.c @@ -75,6 +75,7 @@ void EnumTypes_struct_w_enum(void); void EnumTypes_zero_initialized(void); void EnumTypes_enum_relation(void); void EnumTypes_enum_w_short_notation(void); +void EnumTypes_enum_modified_event(void); // Testsuite 'BitmaskTypes' void BitmaskTypes_bitmask_1_constant(void); @@ -1257,6 +1258,10 @@ bake_test_case EnumTypes_testcases[] = { { "enum_w_short_notation", EnumTypes_enum_w_short_notation + }, + { + "enum_modified_event", + EnumTypes_enum_modified_event } }; @@ -4866,7 +4871,7 @@ static bake_test_suite suites[] = { "EnumTypes", NULL, NULL, - 8, + 9, EnumTypes_testcases }, {