From e97b171c01988ac124a8c198473dedc84c89dc62 Mon Sep 17 00:00:00 2001 From: Sander Mertens Date: Fri, 22 Sep 2023 11:20:53 -0700 Subject: [PATCH] Add parameter to world::lookup/entity::lookup to disable/enable recursive lookups --- flecs.h | 15 ++++++++----- include/flecs/addons/cpp/entity_view.hpp | 3 ++- include/flecs/addons/cpp/impl/world.hpp | 4 ++-- .../flecs/addons/cpp/mixins/entity/impl.hpp | 4 ++-- include/flecs/addons/cpp/world.hpp | 4 +++- test/cpp_api/project.json | 4 +++- test/cpp_api/src/Entity.cpp | 22 +++++++++++++++++++ test/cpp_api/src/main.cpp | 12 +++++++++- 8 files changed, 54 insertions(+), 14 deletions(-) diff --git a/flecs.h b/flecs.h index d4b8d32514..4350bf69ed 100644 --- a/flecs.h +++ b/flecs.h @@ -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. */ @@ -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. * @@ -26460,9 +26463,9 @@ inline bool entity_view::get(const Func& func) const { return _::entity_with_invoker::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); } @@ -30523,8 +30526,8 @@ inline flecs::entity world::set_scope() const { return set_scope( _::cpp_type::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); } diff --git a/include/flecs/addons/cpp/entity_view.hpp b/include/flecs/addons/cpp/entity_view.hpp index a41cf10168..8a9a2ad509 100644 --- a/include/flecs/addons/cpp/entity_view.hpp +++ b/include/flecs/addons/cpp/entity_view.hpp @@ -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. * diff --git a/include/flecs/addons/cpp/impl/world.hpp b/include/flecs/addons/cpp/impl/world.hpp index 4e75922b8e..f0fa16da4d 100644 --- a/include/flecs/addons/cpp/impl/world.hpp +++ b/include/flecs/addons/cpp/impl/world.hpp @@ -69,8 +69,8 @@ inline flecs::entity world::set_scope() const { return set_scope( _::cpp_type::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); } diff --git a/include/flecs/addons/cpp/mixins/entity/impl.hpp b/include/flecs/addons/cpp/mixins/entity/impl.hpp index 7221122ae0..df0e970986 100644 --- a/include/flecs/addons/cpp/mixins/entity/impl.hpp +++ b/include/flecs/addons/cpp/mixins/entity/impl.hpp @@ -181,9 +181,9 @@ inline bool entity_view::get(const Func& func) const { return _::entity_with_invoker::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); } diff --git a/include/flecs/addons/cpp/world.hpp b/include/flecs/addons/cpp/world.hpp index 9b64476702..f7025d8425 100644 --- a/include/flecs/addons/cpp/world.hpp +++ b/include/flecs/addons/cpp/world.hpp @@ -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. */ diff --git a/test/cpp_api/project.json b/test/cpp_api/project.json index 3ea5675440..27f1f23342 100644 --- a/test/cpp_api/project.json +++ b/test/cpp_api/project.json @@ -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", diff --git a/test/cpp_api/src/Entity.cpp b/test/cpp_api/src/Entity.cpp index b8a1109e14..67d492dfb2 100644 --- a/test/cpp_api/src/Entity.cpp +++ b/test/cpp_api/src/Entity.cpp @@ -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); +} diff --git a/test/cpp_api/src/main.cpp b/test/cpp_api/src/main.cpp index a04a70a5b7..a079b340f6 100644 --- a/test/cpp_api/src/main.cpp +++ b/test/cpp_api/src/main.cpp @@ -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); @@ -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 } }; @@ -6286,7 +6296,7 @@ static bake_test_suite suites[] = { "Entity", NULL, NULL, - 245, + 247, Entity_testcases }, {