Skip to content

Commit

Permalink
support enums
Browse files Browse the repository at this point in the history
  • Loading branch information
jpeletier committed Dec 5, 2024
1 parent f0f65f2 commit 38dce50
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 6 deletions.
11 changes: 9 additions & 2 deletions distr/flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -50635,8 +50635,15 @@ int flecs_init_type(
* serializers on uninitialized values. For runtime types (rtt), the default hooks are set
by flecs_meta_rtt_init_default_hooks */
ecs_type_info_t *ti = flecs_type_info_ensure(world, type);
if (meta_type->existing && !ti->hooks.ctor) {
ti->hooks.ctor = flecs_default_ctor;
if (meta_type->existing) {
if(!ti->hooks.ctor) {
ti->hooks.ctor = flecs_default_ctor;
}
if(kind == EcsEnumType) {
ti->hooks.cmp = ecs_compare_i32;
ti->hooks.equals = ecs_equals_i32;
ti->hooks.flags &= ~(ECS_TYPE_HOOK_CMP_ILLEGAL|ECS_TYPE_HOOK_EQUALS_ILLEGAL);
}
}
} else {
if (meta_type->kind != kind) {
Expand Down
12 changes: 10 additions & 2 deletions src/addons/meta/meta.c
Original file line number Diff line number Diff line change
Expand Up @@ -646,8 +646,16 @@ int flecs_init_type(
* serializers on uninitialized values. For runtime types (rtt), the default hooks are set
by flecs_meta_rtt_init_default_hooks */
ecs_type_info_t *ti = flecs_type_info_ensure(world, type);
if (meta_type->existing && !ti->hooks.ctor) {
ti->hooks.ctor = flecs_default_ctor;
if (meta_type->existing) {
if(!ti->hooks.ctor) {
ti->hooks.ctor = flecs_default_ctor;
}
if(kind == EcsEnumType) {
ti->hooks.cmp = ecs_compare_i32;
ti->hooks.equals = ecs_equals_i32;
ti->hooks.flags &= ~(ECS_TYPE_HOOK_CMP_ILLEGAL|ECS_TYPE_HOOK_EQUALS_ILLEGAL);
ti->hooks.flags |= ECS_TYPE_HOOK_CMP|ECS_TYPE_HOOK_EQUALS;
}
}
} else {
if (meta_type->kind != kind) {
Expand Down
3 changes: 2 additions & 1 deletion test/cpp/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,8 @@
"compare_WithEqualsAndGreaterThan",
"compare_WithEqualsAndLessThan",
"compare_WithEqualsOnly",
"compare_WithoutOperators"
"compare_WithoutOperators",
"compare_Enum"
]
}, {
"id": "Refs",
Expand Down
29 changes: 29 additions & 0 deletions test/cpp/src/ComponentLifecycle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2604,3 +2604,32 @@ void ComponentLifecycle_compare_WithoutOperators(void) {
test_assert(compare(ecs, component, &b, &b) == 0);
}


enum Color {
Red = 1,
Yellow = 2,
Blue = 3
};

void ComponentLifecycle_compare_Enum(void) {

flecs::world ecs;

auto component = ecs.component<Color>();

const ecs_type_hooks_t* hooks = ecs_get_hooks_id(ecs, component);

test_assert(!(hooks->flags & ECS_TYPE_HOOK_CMP_ILLEGAL));
test_assert(!(hooks->flags & ECS_TYPE_HOOK_EQUALS_ILLEGAL));

Color a = Color::Red;
Color b = Color::Yellow;
Color c = Color::Red;

test_assert(compare(ecs, component, &a, &b) < 0);
test_assert(compare(ecs, component, &b, &a) > 0);
test_assert(compare(ecs, component, &a, &c) == 0);
test_assert(compare(ecs, component, &b, &c) > 0);
test_assert(compare(ecs, component, &c, &b) < 0);
test_assert(compare(ecs, component, &b, &b) == 0);
}
7 changes: 6 additions & 1 deletion test/cpp/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,7 @@ void ComponentLifecycle_compare_WithEqualsAndGreaterThan(void);
void ComponentLifecycle_compare_WithEqualsAndLessThan(void);
void ComponentLifecycle_compare_WithEqualsOnly(void);
void ComponentLifecycle_compare_WithoutOperators(void);
void ComponentLifecycle_compare_Enum(void);

// Testsuite 'Refs'
void Refs_get_ref_by_ptr(void);
Expand Down Expand Up @@ -5411,6 +5412,10 @@ bake_test_case ComponentLifecycle_testcases[] = {
{
"compare_WithoutOperators",
ComponentLifecycle_compare_WithoutOperators
},
{
"compare_Enum",
ComponentLifecycle_compare_Enum
}
};

Expand Down Expand Up @@ -7003,7 +7008,7 @@ static bake_test_suite suites[] = {
"ComponentLifecycle",
NULL,
NULL,
96,
97,
ComponentLifecycle_testcases
},
{
Expand Down

0 comments on commit 38dce50

Please sign in to comment.