diff --git a/docs/entity.md b/docs/entity.md index b774afc7e..ce909a54b 100644 --- a/docs/entity.md +++ b/docs/entity.md @@ -276,8 +276,8 @@ auto &[pos, vel] = registry.get(entity); The `get` member function template gives direct access to the component of an entity stored in the underlying data structures of the registry. There exists -also an alternative member function named `get_if` that returns a pointer to the -component owned by an entity if any, a null pointer otherwise. +also an alternative member function named `try_get` that returns a pointer to +the component owned by an entity if any, a null pointer otherwise. ## Observe changes diff --git a/src/entt/entity/actor.hpp b/src/entt/entity/actor.hpp index 00d0d1e81..de85cbb36 100644 --- a/src/entt/entity/actor.hpp +++ b/src/entt/entity/actor.hpp @@ -146,8 +146,8 @@ struct actor { * @return Pointers to the components owned by the actor. */ template - auto get_if() const ENTT_NOEXCEPT { - return std::as_const(*reg).template get_if(entt); + auto try_get() const ENTT_NOEXCEPT { + return std::as_const(*reg).template try_get(entt); } /** @@ -156,8 +156,8 @@ struct actor { * @return Pointers to the components owned by the actor. */ template - auto get_if() ENTT_NOEXCEPT { - return reg->template get_if(entt); + auto try_get() ENTT_NOEXCEPT { + return reg->template try_get(entt); } /** diff --git a/src/entt/entity/prototype.hpp b/src/entt/entity/prototype.hpp index 190be6f1a..da678422c 100644 --- a/src/entt/entity/prototype.hpp +++ b/src/entt/entity/prototype.hpp @@ -217,12 +217,12 @@ public: * @return Pointers to the components owned by the prototype. */ template - auto get_if() const ENTT_NOEXCEPT { + auto try_get() const ENTT_NOEXCEPT { if constexpr(sizeof...(Component) == 1) { - const auto *wrapper = reg->template get_if>(entity); + const auto *wrapper = reg->template try_get>(entity); return wrapper ? &wrapper->component : nullptr; } else { - return std::tuple{get_if()...}; + return std::tuple{try_get()...}; } } @@ -232,11 +232,11 @@ public: * @return Pointers to the components owned by the prototype. */ template - inline auto get_if() ENTT_NOEXCEPT { + inline auto try_get() ENTT_NOEXCEPT { if constexpr(sizeof...(Component) == 1) { - return (const_cast(std::as_const(*this).template get_if()), ...); + return (const_cast(std::as_const(*this).template try_get()), ...); } else { - return std::tuple{get_if()...}; + return std::tuple{try_get()...}; } } diff --git a/src/entt/entity/registry.hpp b/src/entt/entity/registry.hpp index c1a4f3048..070eeb4c6 100644 --- a/src/entt/entity/registry.hpp +++ b/src/entt/entity/registry.hpp @@ -688,13 +688,13 @@ public: * @return Pointers to the components owned by the entity. */ template - auto get_if([[maybe_unused]] const entity_type entity) const ENTT_NOEXCEPT { + auto try_get([[maybe_unused]] const entity_type entity) const ENTT_NOEXCEPT { assert(valid(entity)); if constexpr(sizeof...(Component) == 1) { - return managed() ? pool().get_if(entity) : nullptr; + return managed() ? pool().try_get(entity) : nullptr; } else { - return std::tuple{get_if(entity)...}; + return std::tuple{try_get(entity)...}; } } @@ -711,11 +711,11 @@ public: * @return Pointers to the components owned by the entity. */ template - inline auto get_if([[maybe_unused]] const entity_type entity) ENTT_NOEXCEPT { + inline auto try_get([[maybe_unused]] const entity_type entity) ENTT_NOEXCEPT { if constexpr(sizeof...(Component) == 1) { - return (const_cast(std::as_const(*this).template get_if(entity)), ...); + return (const_cast(std::as_const(*this).template try_get(entity)), ...); } else { - return std::tuple{get_if(entity)...}; + return std::tuple{try_get(entity)...}; } } diff --git a/src/entt/entity/sparse_set.hpp b/src/entt/entity/sparse_set.hpp index 2f29f5752..bdc665a0a 100644 --- a/src/entt/entity/sparse_set.hpp +++ b/src/entt/entity/sparse_set.hpp @@ -890,7 +890,7 @@ public: * @param entity A valid entity identifier. * @return The object associated with the entity, if any. */ - const object_type * get_if(const entity_type entity) const ENTT_NOEXCEPT { + const object_type * try_get(const entity_type entity) const ENTT_NOEXCEPT { return underlying_type::has(entity) ? (instances.data() + underlying_type::get(entity)) : nullptr; } @@ -899,8 +899,8 @@ public: * @param entity A valid entity identifier. * @return The object associated with the entity, if any. */ - inline object_type * get_if(const entity_type entity) ENTT_NOEXCEPT { - return const_cast(std::as_const(*this).get_if(entity)); + inline object_type * try_get(const entity_type entity) ENTT_NOEXCEPT { + return const_cast(std::as_const(*this).try_get(entity)); } /** diff --git a/test/entt/entity/actor.cpp b/test/entt/entity/actor.cpp index 14100757a..e63949830 100644 --- a/test/entt/entity/actor.cpp +++ b/test/entt/entity/actor.cpp @@ -21,12 +21,12 @@ TEST(Actor, Component) { ASSERT_EQ(&cchar, &cactor.get()); ASSERT_EQ(&cint, &std::get<0>(actor.get())); ASSERT_EQ(&cchar, &std::get<1>(actor.get())); - ASSERT_EQ(&cint, std::get<0>(actor.get_if())); - ASSERT_EQ(&cchar, std::get<1>(actor.get_if())); - ASSERT_EQ(nullptr, std::get<2>(actor.get_if())); - ASSERT_EQ(nullptr, actor.get_if()); - ASSERT_EQ(&cchar, actor.get_if()); - ASSERT_EQ(&cint, actor.get_if()); + ASSERT_EQ(&cint, std::get<0>(actor.try_get())); + ASSERT_EQ(&cchar, std::get<1>(actor.try_get())); + ASSERT_EQ(nullptr, std::get<2>(actor.try_get())); + ASSERT_EQ(nullptr, actor.try_get()); + ASSERT_EQ(&cchar, actor.try_get()); + ASSERT_EQ(&cint, actor.try_get()); ASSERT_FALSE(registry.empty()); ASSERT_FALSE(registry.empty()); diff --git a/test/entt/entity/prototype.cpp b/test/entt/entity/prototype.cpp index d21a95205..65a8dac4e 100644 --- a/test/entt/entity/prototype.cpp +++ b/test/entt/entity/prototype.cpp @@ -21,13 +21,13 @@ TEST(Prototype, SameRegistry) { ASSERT_EQ(std::get<0>(prototype.get()), 3); ASSERT_EQ(std::get<1>(cprototype.get()), 'c'); - ASSERT_NE(prototype.get_if(), nullptr); - ASSERT_NE(prototype.get_if(), nullptr); - ASSERT_EQ(prototype.get_if(), nullptr); - ASSERT_EQ(*prototype.get_if(), 3); - ASSERT_EQ(*cprototype.get_if(), 'c'); - ASSERT_EQ(*std::get<0>(prototype.get_if()), 3); - ASSERT_EQ(*std::get<1>(cprototype.get_if()), 'c'); + ASSERT_NE(prototype.try_get(), nullptr); + ASSERT_NE(prototype.try_get(), nullptr); + ASSERT_EQ(prototype.try_get(), nullptr); + ASSERT_EQ(*prototype.try_get(), 3); + ASSERT_EQ(*cprototype.try_get(), 'c'); + ASSERT_EQ(*std::get<0>(prototype.try_get()), 3); + ASSERT_EQ(*std::get<1>(cprototype.try_get()), 'c'); const auto e0 = prototype.create(); diff --git a/test/entt/entity/registry.cpp b/test/entt/entity/registry.cpp index 4fe752502..19e591727 100644 --- a/test/entt/entity/registry.cpp +++ b/test/entt/entity/registry.cpp @@ -75,12 +75,12 @@ TEST(Registry, Functionalities) { ASSERT_FALSE((registry.has(e0))); ASSERT_TRUE((registry.has(e1))); - ASSERT_EQ(registry.get_if(e0), nullptr); - ASSERT_NE(registry.get_if(e1), nullptr); - ASSERT_EQ(registry.get_if(e0), nullptr); - ASSERT_NE(registry.get_if(e1), nullptr); - ASSERT_EQ(registry.get_if(e0), nullptr); - ASSERT_EQ(registry.get_if(e1), nullptr); + ASSERT_EQ(registry.try_get(e0), nullptr); + ASSERT_NE(registry.try_get(e1), nullptr); + ASSERT_EQ(registry.try_get(e0), nullptr); + ASSERT_NE(registry.try_get(e1), nullptr); + ASSERT_EQ(registry.try_get(e0), nullptr); + ASSERT_EQ(registry.try_get(e1), nullptr); ASSERT_EQ(registry.assign(e0, 42), 42); ASSERT_EQ(registry.assign(e0, 'c'), 'c'); @@ -104,16 +104,16 @@ TEST(Registry, Functionalities) { ASSERT_EQ(registry.get(e0), 42); ASSERT_EQ(registry.get(e0), 'c'); - ASSERT_NE(registry.get_if(e0), nullptr); - ASSERT_NE(registry.get_if(e0), nullptr); - ASSERT_EQ(registry.get_if(e0), nullptr); - ASSERT_EQ(*registry.get_if(e0), 42); - ASSERT_EQ(*registry.get_if(e0), 'c'); + ASSERT_NE(registry.try_get(e0), nullptr); + ASSERT_NE(registry.try_get(e0), nullptr); + ASSERT_EQ(registry.try_get(e0), nullptr); + ASSERT_EQ(*registry.try_get(e0), 42); + ASSERT_EQ(*registry.try_get(e0), 'c'); ASSERT_EQ(std::get<0>(registry.get(e0)), 42); - ASSERT_EQ(*std::get<0>(registry.get_if(e0)), 42); + ASSERT_EQ(*std::get<0>(registry.try_get(e0)), 42); ASSERT_EQ(std::get<1>(static_cast &>(registry).get(e0)), 'c'); - ASSERT_EQ(*std::get<1>(static_cast &>(registry).get_if(e0)), 'c'); + ASSERT_EQ(*std::get<1>(static_cast &>(registry).try_get(e0)), 'c'); ASSERT_EQ(registry.get(e0), registry.get(e2)); ASSERT_EQ(registry.get(e0), registry.get(e2)); diff --git a/test/entt/entity/sparse_set.cpp b/test/entt/entity/sparse_set.cpp index 0d130a3da..3dd899474 100644 --- a/test/entt/entity/sparse_set.cpp +++ b/test/entt/entity/sparse_set.cpp @@ -364,6 +364,8 @@ TEST(SparseSetWithType, Functionalities) { ASSERT_TRUE(set.has(42)); ASSERT_TRUE(set.fast(42)); ASSERT_EQ(set.get(42), 3); + ASSERT_EQ(*set.try_get(42), 3); + ASSERT_EQ(set.try_get(99), nullptr); set.destroy(42); @@ -377,6 +379,8 @@ TEST(SparseSetWithType, Functionalities) { set.construct(42, 12); ASSERT_EQ(set.get(42), 12); + ASSERT_EQ(*set.try_get(42), 12); + ASSERT_EQ(set.try_get(99), nullptr); set.reset(); @@ -392,7 +396,7 @@ TEST(SparseSetWithType, Functionalities) { other = std::move(set); } - TEST(SparseSetWithType, AggregatesMustWork) { +TEST(SparseSetWithType, AggregatesMustWork) { struct aggregate_type { int value; }; // the goal of this test is to enforce the requirements for aggregate types entt::sparse_set{}.construct(0, 42);