From 2c416eaec28e43660e4eac14bd603f0869695bcb Mon Sep 17 00:00:00 2001 From: Javier Peletier Date: Thu, 5 Dec 2024 18:01:29 +0100 Subject: [PATCH] support enums --- distr/flecs.c | 12 ++++++++++-- src/addons/meta/meta.c | 12 ++++++++++-- test/cpp/project.json | 3 ++- test/cpp/src/ComponentLifecycle.cpp | 29 +++++++++++++++++++++++++++++ test/cpp/src/main.cpp | 7 ++++++- 5 files changed, 57 insertions(+), 6 deletions(-) diff --git a/distr/flecs.c b/distr/flecs.c index 7e682e525..b367560ce 100644 --- a/distr/flecs.c +++ b/distr/flecs.c @@ -50635,8 +50635,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) { diff --git a/src/addons/meta/meta.c b/src/addons/meta/meta.c index 780debf1f..d38bb6306 100644 --- a/src/addons/meta/meta.c +++ b/src/addons/meta/meta.c @@ -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) { diff --git a/test/cpp/project.json b/test/cpp/project.json index 0c9bb867a..a569010b6 100644 --- a/test/cpp/project.json +++ b/test/cpp/project.json @@ -1060,7 +1060,8 @@ "compare_WithEqualsAndGreaterThan", "compare_WithEqualsAndLessThan", "compare_WithEqualsOnly", - "compare_WithoutOperators" + "compare_WithoutOperators", + "compare_Enum" ] }, { "id": "Refs", diff --git a/test/cpp/src/ComponentLifecycle.cpp b/test/cpp/src/ComponentLifecycle.cpp index 5bc9053e9..e84d41b4d 100644 --- a/test/cpp/src/ComponentLifecycle.cpp +++ b/test/cpp/src/ComponentLifecycle.cpp @@ -2604,3 +2604,32 @@ void ComponentLifecycle_compare_WithoutOperators(void) { test_assert(compare(ecs, component, &b, &b) == 0); } + +enum TestColorEnum { + Red = 1, + Yellow = 2, + Blue = 3 +}; + +void ComponentLifecycle_compare_Enum(void) { + + flecs::world ecs; + + auto component = ecs.component(); + + 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)); + + TestColorEnum a = TestColorEnum::Red; + TestColorEnum b = TestColorEnum::Yellow; + TestColorEnum c = TestColorEnum::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); +} diff --git a/test/cpp/src/main.cpp b/test/cpp/src/main.cpp index 4bbdbcb81..78107acf5 100644 --- a/test/cpp/src/main.cpp +++ b/test/cpp/src/main.cpp @@ -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); @@ -5411,6 +5412,10 @@ bake_test_case ComponentLifecycle_testcases[] = { { "compare_WithoutOperators", ComponentLifecycle_compare_WithoutOperators + }, + { + "compare_Enum", + ComponentLifecycle_compare_Enum } }; @@ -7003,7 +7008,7 @@ static bake_test_suite suites[] = { "ComponentLifecycle", NULL, NULL, - 96, + 97, ComponentLifecycle_testcases }, {