Skip to content

Commit

Permalink
#1160 Additional overloads for enum entities
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
RoyAwesome authored Feb 26, 2024
1 parent 39ccc74 commit 5172b73
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
12 changes: 12 additions & 0 deletions include/flecs/addons/cpp/mixins/entity/builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename E, if_t<is_enum<E>::value> = 0>
Self& depends_on(E second)
{
const auto& et = enum_type<E>(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.
Expand Down
8 changes: 8 additions & 0 deletions include/flecs/addons/cpp/mixins/system/builder_i.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ struct system_builder_i : query_builder_i<Base, Components ...> {
return *this;
}

template <typename E, if_t<is_enum<E>::value> = 0>
Base& kind(E phase)
{
const auto& et = enum_type<E>(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.
Expand Down
4 changes: 3 additions & 1 deletion test/cpp_api/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
33 changes: 33 additions & 0 deletions test/cpp_api/src/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 5172b73

Please sign in to comment.