From 5172b73bdb61880765a8f9a16a83be58154ede1a Mon Sep 17 00:00:00 2001 From: Garrett Date: Sun, 25 Feb 2024 22:29:59 -0800 Subject: [PATCH] #1160 Additional overloads for enum entities * enum support for system builder's kind() function * added depends_on overload for enum entity ids * Tests for the previous two functions * Moved the test enum out of the functions so that non-permissive compilers like it. --- .../addons/cpp/mixins/entity/builder.hpp | 12 +++++++ .../addons/cpp/mixins/system/builder_i.hpp | 8 +++++ test/cpp_api/project.json | 4 ++- test/cpp_api/src/System.cpp | 33 +++++++++++++++++++ 4 files changed, 56 insertions(+), 1 deletion(-) diff --git a/include/flecs/addons/cpp/mixins/entity/builder.hpp b/include/flecs/addons/cpp/mixins/entity/builder.hpp index e0a80f850..f09930e1c 100644 --- a/include/flecs/addons/cpp/mixins/entity/builder.hpp +++ b/include/flecs/addons/cpp/mixins/entity/builder.hpp @@ -239,6 +239,18 @@ struct entity_builder : entity_view { return this->add(flecs::DependsOn, second); } + /** Shortcut for add(DependsOn, entity). + * + * @param second The second element of the pair. + */ + template ::value> = 0> + Self& depends_on(E second) + { + const auto& et = enum_type(this->m_world); + flecs::entity_t target = et.entity(second); + return depends_on(target); + } + /** Shortcut for add(SlotOf, entity). * * @param second The second element of the pair. diff --git a/include/flecs/addons/cpp/mixins/system/builder_i.hpp b/include/flecs/addons/cpp/mixins/system/builder_i.hpp index 584e3c339..40d8ad59f 100644 --- a/include/flecs/addons/cpp/mixins/system/builder_i.hpp +++ b/include/flecs/addons/cpp/mixins/system/builder_i.hpp @@ -42,6 +42,14 @@ struct system_builder_i : query_builder_i { return *this; } + template ::value> = 0> + Base& kind(E phase) + { + const auto& et = enum_type(this->world_v()); + flecs::entity_t target = et.entity(phase); + return this->kind(target); + } + /** Specify in which phase the system should run. * * @tparam Phase The phase. diff --git a/test/cpp_api/project.json b/test/cpp_api/project.json index 9baf438b4..e292f88f1 100644 --- a/test/cpp_api/project.json +++ b/test/cpp_api/project.json @@ -487,7 +487,9 @@ "range_get", "randomize_timers", "optional_pair_term", - "singleton_tick_source" + "singleton_tick_source", + "pipeline_step_with_kind_enum", + "pipeline_step_depends_on_pipeline_step_with_enum" ] }, { "id": "Event", diff --git a/test/cpp_api/src/System.cpp b/test/cpp_api/src/System.cpp index 596949b88..c9bd7c97e 100644 --- a/test/cpp_api/src/System.cpp +++ b/test/cpp_api/src/System.cpp @@ -2287,3 +2287,36 @@ void System_singleton_tick_source(void) { ecs.progress(2.0); test_int(1, sys_invoked); } + +enum class PipelineStepEnum +{ + CustomStep, + CustomStep2 +}; + +void System_pipeline_step_with_kind_enum(void) { + flecs::world ecs; + + ecs.entity(PipelineStepEnum::CustomStep).add(flecs::Phase).depends_on(flecs::OnStart); + + bool ran_test = false; + + ecs.system().kind(PipelineStepEnum::CustomStep).iter([&ran_test](flecs::iter& it) { ran_test = true; }); + + ecs.progress(); + test_assert(ran_test); +} + +void System_pipeline_step_depends_on_pipeline_step_with_enum(void) { + flecs::world ecs; + + ecs.entity(PipelineStepEnum::CustomStep).add(flecs::Phase).depends_on(flecs::OnStart); + ecs.entity(PipelineStepEnum::CustomStep2).add(flecs::Phase).depends_on(PipelineStepEnum::CustomStep); + + bool ran_test = false; + + ecs.system().kind(PipelineStepEnum::CustomStep2).iter([&ran_test](flecs::iter& it) { ran_test = true; }); + + ecs.progress(); + test_assert(ran_test); +}