Skip to content

Commit

Permalink
#1414 Notify on enum modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
jpeletier authored Oct 31, 2024
1 parent 54bf4ae commit 2d80124
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
1 change: 1 addition & 0 deletions distr/flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions src/addons/meta/meta.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion test/meta/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@
"struct_w_enum",
"zero_initialized",
"enum_relation",
"enum_w_short_notation"
"enum_w_short_notation",
"enum_modified_event"
]
}, {
"id": "BitmaskTypes",
Expand Down
48 changes: 48 additions & 0 deletions test/meta/src/EnumTypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
7 changes: 6 additions & 1 deletion test/meta/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
}
};

Expand Down Expand Up @@ -4866,7 +4871,7 @@ static bake_test_suite suites[] = {
"EnumTypes",
NULL,
NULL,
8,
9,
EnumTypes_testcases
},
{
Expand Down

0 comments on commit 2d80124

Please sign in to comment.