Skip to content

Commit

Permalink
Add parameter to world::lookup/entity::lookup to disable/enable recur…
Browse files Browse the repository at this point in the history
…sive lookups
  • Loading branch information
SanderMertens committed Sep 22, 2023
1 parent f186ff6 commit e97b171
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 14 deletions.
15 changes: 9 additions & 6 deletions flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -19778,8 +19778,10 @@ struct world {
/** Lookup entity by name.
*
* @param name Entity name.
* @param search_path When false, only the current scope is searched.
* @result The entity if found, or 0 if not found.
*/
flecs::entity lookup(const char *name) const;
flecs::entity lookup(const char *name, bool search_path = true) const;

/** Set singleton component.
*/
Expand Down Expand Up @@ -21998,9 +22000,10 @@ struct entity_view : public id {
* contain double colons as scope separators, for example: "Foo::Bar".
*
* @param path The name of the entity to lookup.
* @param search_path When false, only the entity's scope is searched.
* @return The found entity, or entity::null if no entity matched.
*/
flecs::entity lookup(const char *path) const;
flecs::entity lookup(const char *path, bool search_path = false) const;

/** Check if entity has the provided entity.
*
Expand Down Expand Up @@ -26460,9 +26463,9 @@ inline bool entity_view::get(const Func& func) const {
return _::entity_with_invoker<Func>::invoke_get(m_world, m_id, func);
}

inline flecs::entity entity_view::lookup(const char *path) const {
inline flecs::entity entity_view::lookup(const char *path, bool search_path) const {
ecs_assert(m_id != 0, ECS_INVALID_PARAMETER, "invalid lookup from null handle");
auto id = ecs_lookup_path_w_sep(m_world, m_id, path, "::", "::", false);
auto id = ecs_lookup_path_w_sep(m_world, m_id, path, "::", "::", search_path);
return flecs::entity(m_world, id);
}

Expand Down Expand Up @@ -30523,8 +30526,8 @@ inline flecs::entity world::set_scope() const {
return set_scope( _::cpp_type<T>::id(m_world) );
}

inline entity world::lookup(const char *name) const {
auto e = ecs_lookup_path_w_sep(m_world, 0, name, "::", "::", true);
inline entity world::lookup(const char *name, bool search_path) const {
auto e = ecs_lookup_path_w_sep(m_world, 0, name, "::", "::", search_path);
return flecs::entity(*this, e);
}

Expand Down
3 changes: 2 additions & 1 deletion include/flecs/addons/cpp/entity_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,9 +479,10 @@ struct entity_view : public id {
* contain double colons as scope separators, for example: "Foo::Bar".
*
* @param path The name of the entity to lookup.
* @param search_path When false, only the entity's scope is searched.
* @return The found entity, or entity::null if no entity matched.
*/
flecs::entity lookup(const char *path) const;
flecs::entity lookup(const char *path, bool search_path = false) const;

/** Check if entity has the provided entity.
*
Expand Down
4 changes: 2 additions & 2 deletions include/flecs/addons/cpp/impl/world.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ inline flecs::entity world::set_scope() const {
return set_scope( _::cpp_type<T>::id(m_world) );
}

inline entity world::lookup(const char *name) const {
auto e = ecs_lookup_path_w_sep(m_world, 0, name, "::", "::", true);
inline entity world::lookup(const char *name, bool search_path) const {
auto e = ecs_lookup_path_w_sep(m_world, 0, name, "::", "::", search_path);
return flecs::entity(*this, e);
}

Expand Down
4 changes: 2 additions & 2 deletions include/flecs/addons/cpp/mixins/entity/impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ inline bool entity_view::get(const Func& func) const {
return _::entity_with_invoker<Func>::invoke_get(m_world, m_id, func);
}

inline flecs::entity entity_view::lookup(const char *path) const {
inline flecs::entity entity_view::lookup(const char *path, bool search_path) const {
ecs_assert(m_id != 0, ECS_INVALID_PARAMETER, "invalid lookup from null handle");
auto id = ecs_lookup_path_w_sep(m_world, m_id, path, "::", "::", false);
auto id = ecs_lookup_path_w_sep(m_world, m_id, path, "::", "::", search_path);
return flecs::entity(m_world, id);
}

Expand Down
4 changes: 3 additions & 1 deletion include/flecs/addons/cpp/world.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,8 +551,10 @@ struct world {
/** Lookup entity by name.
*
* @param name Entity name.
* @param search_path When false, only the current scope is searched.
* @result The entity if found, or 0 if not found.
*/
flecs::entity lookup(const char *name) const;
flecs::entity lookup(const char *name, bool search_path = true) const;

/** Set singleton component.
*/
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 @@ -265,7 +265,9 @@
"to_view_from_stage",
"set_alias",
"emplace_w_observer",
"scoped_world"
"scoped_world",
"entity_lookup_not_recursive",
"world_lookup_not_recursive"
]
}, {
"id": "Pairs",
Expand Down
22 changes: 22 additions & 0 deletions test/cpp_api/src/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4365,3 +4365,25 @@ void Entity_scoped_world(void) {
flecs::entity child = parent.scope().entity();
test_assert(child.parent() == parent);
}

void Entity_entity_lookup_not_recursive(void) {
flecs::world world;

flecs::entity parent = world.entity("parent");
flecs::entity child = world.scope(parent).entity("child");
flecs::entity foo = world.scope(parent).entity("foo");

test_assert(child.lookup("foo") == 0);
test_assert(child.lookup("foo", true) == foo);
}

void Entity_world_lookup_not_recursive(void) {
flecs::world world;

flecs::entity parent = world.entity("parent");
flecs::entity child = world.scope(parent).entity("child");
flecs::entity foo = world.scope(parent).entity("foo");

test_assert(world.scope(child).lookup("foo") == foo);
test_assert(world.scope(child).lookup("foo", false) == 0);
}
12 changes: 11 additions & 1 deletion test/cpp_api/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ void Entity_to_view_from_stage(void);
void Entity_set_alias(void);
void Entity_emplace_w_observer(void);
void Entity_scoped_world(void);
void Entity_entity_lookup_not_recursive(void);
void Entity_world_lookup_not_recursive(void);

// Testsuite 'Pairs'
void Pairs_add_component_pair(void);
Expand Down Expand Up @@ -2281,6 +2283,14 @@ bake_test_case Entity_testcases[] = {
{
"scoped_world",
Entity_scoped_world
},
{
"entity_lookup_not_recursive",
Entity_entity_lookup_not_recursive
},
{
"world_lookup_not_recursive",
Entity_world_lookup_not_recursive
}
};

Expand Down Expand Up @@ -6286,7 +6296,7 @@ static bake_test_suite suites[] = {
"Entity",
NULL,
NULL,
245,
247,
Entity_testcases
},
{
Expand Down

0 comments on commit e97b171

Please sign in to comment.