Explorar o código

get_if => try_get (#153)

Michele Caini %!s(int64=7) %!d(string=hai) anos
pai
achega
f7905e3bc2

+ 2 - 2
docs/entity.md

@@ -276,8 +276,8 @@ auto &[pos, vel] = registry.get<position, velocity>(entity);
 
 
 The `get` member function template gives direct access to the component of an
 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
 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
 ## Observe changes
 
 

+ 4 - 4
src/entt/entity/actor.hpp

@@ -146,8 +146,8 @@ struct actor {
      * @return Pointers to the components owned by the actor.
      * @return Pointers to the components owned by the actor.
      */
      */
     template<typename... Component>
     template<typename... Component>
-    auto get_if() const ENTT_NOEXCEPT {
-        return std::as_const(*reg).template get_if<Component...>(entt);
+    auto try_get() const ENTT_NOEXCEPT {
+        return std::as_const(*reg).template try_get<Component...>(entt);
     }
     }
 
 
     /**
     /**
@@ -156,8 +156,8 @@ struct actor {
      * @return Pointers to the components owned by the actor.
      * @return Pointers to the components owned by the actor.
      */
      */
     template<typename... Component>
     template<typename... Component>
-    auto get_if() ENTT_NOEXCEPT {
-        return reg->template get_if<Component...>(entt);
+    auto try_get() ENTT_NOEXCEPT {
+        return reg->template try_get<Component...>(entt);
     }
     }
 
 
     /**
     /**

+ 6 - 6
src/entt/entity/prototype.hpp

@@ -217,12 +217,12 @@ public:
      * @return Pointers to the components owned by the prototype.
      * @return Pointers to the components owned by the prototype.
      */
      */
     template<typename... Component>
     template<typename... Component>
-    auto get_if() const ENTT_NOEXCEPT {
+    auto try_get() const ENTT_NOEXCEPT {
         if constexpr(sizeof...(Component) == 1) {
         if constexpr(sizeof...(Component) == 1) {
-            const auto *wrapper = reg->template get_if<component_wrapper<Component...>>(entity);
+            const auto *wrapper = reg->template try_get<component_wrapper<Component...>>(entity);
             return wrapper ? &wrapper->component : nullptr;
             return wrapper ? &wrapper->component : nullptr;
         } else {
         } else {
-            return std::tuple<const Component *...>{get_if<Component>()...};
+            return std::tuple<const Component *...>{try_get<Component>()...};
         }
         }
     }
     }
 
 
@@ -232,11 +232,11 @@ public:
      * @return Pointers to the components owned by the prototype.
      * @return Pointers to the components owned by the prototype.
      */
      */
     template<typename... Component>
     template<typename... Component>
-    inline auto get_if() ENTT_NOEXCEPT {
+    inline auto try_get() ENTT_NOEXCEPT {
         if constexpr(sizeof...(Component) == 1) {
         if constexpr(sizeof...(Component) == 1) {
-            return (const_cast<Component *>(std::as_const(*this).template get_if<Component>()), ...);
+            return (const_cast<Component *>(std::as_const(*this).template try_get<Component>()), ...);
         } else {
         } else {
-            return std::tuple<Component *...>{get_if<Component>()...};
+            return std::tuple<Component *...>{try_get<Component>()...};
         }
         }
     }
     }
 
 

+ 6 - 6
src/entt/entity/registry.hpp

@@ -688,13 +688,13 @@ public:
      * @return Pointers to the components owned by the entity.
      * @return Pointers to the components owned by the entity.
      */
      */
     template<typename... Component>
     template<typename... Component>
-    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));
         assert(valid(entity));
 
 
         if constexpr(sizeof...(Component) == 1) {
         if constexpr(sizeof...(Component) == 1) {
-            return managed<Component...>() ? pool<Component...>().get_if(entity) : nullptr;
+            return managed<Component...>() ? pool<Component...>().try_get(entity) : nullptr;
         } else {
         } else {
-            return std::tuple<const Component *...>{get_if<Component>(entity)...};
+            return std::tuple<const Component *...>{try_get<Component>(entity)...};
         }
         }
     }
     }
 
 
@@ -711,11 +711,11 @@ public:
      * @return Pointers to the components owned by the entity.
      * @return Pointers to the components owned by the entity.
      */
      */
     template<typename... Component>
     template<typename... Component>
-    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) {
         if constexpr(sizeof...(Component) == 1) {
-            return (const_cast<Component *>(std::as_const(*this).template get_if<Component>(entity)), ...);
+            return (const_cast<Component *>(std::as_const(*this).template try_get<Component>(entity)), ...);
         } else {
         } else {
-            return std::tuple<Component *...>{get_if<Component>(entity)...};
+            return std::tuple<Component *...>{try_get<Component>(entity)...};
         }
         }
     }
     }
 
 

+ 3 - 3
src/entt/entity/sparse_set.hpp

@@ -890,7 +890,7 @@ public:
      * @param entity A valid entity identifier.
      * @param entity A valid entity identifier.
      * @return The object associated with the entity, if any.
      * @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;
         return underlying_type::has(entity) ? (instances.data() + underlying_type::get(entity)) : nullptr;
     }
     }
 
 
@@ -899,8 +899,8 @@ public:
      * @param entity A valid entity identifier.
      * @param entity A valid entity identifier.
      * @return The object associated with the entity, if any.
      * @return The object associated with the entity, if any.
      */
      */
-    inline object_type * get_if(const entity_type entity) ENTT_NOEXCEPT {
-        return const_cast<object_type *>(std::as_const(*this).get_if(entity));
+    inline object_type * try_get(const entity_type entity) ENTT_NOEXCEPT {
+        return const_cast<object_type *>(std::as_const(*this).try_get(entity));
     }
     }
 
 
     /**
     /**

+ 6 - 6
test/entt/entity/actor.cpp

@@ -21,12 +21,12 @@ TEST(Actor, Component) {
     ASSERT_EQ(&cchar, &cactor.get<char>());
     ASSERT_EQ(&cchar, &cactor.get<char>());
     ASSERT_EQ(&cint, &std::get<0>(actor.get<int, char>()));
     ASSERT_EQ(&cint, &std::get<0>(actor.get<int, char>()));
     ASSERT_EQ(&cchar, &std::get<1>(actor.get<int, char>()));
     ASSERT_EQ(&cchar, &std::get<1>(actor.get<int, char>()));
-    ASSERT_EQ(&cint, std::get<0>(actor.get_if<int, char, double>()));
-    ASSERT_EQ(&cchar, std::get<1>(actor.get_if<int, char, double>()));
-    ASSERT_EQ(nullptr, std::get<2>(actor.get_if<int, char, double>()));
-    ASSERT_EQ(nullptr, actor.get_if<double>());
-    ASSERT_EQ(&cchar, actor.get_if<char>());
-    ASSERT_EQ(&cint, actor.get_if<int>());
+    ASSERT_EQ(&cint, std::get<0>(actor.try_get<int, char, double>()));
+    ASSERT_EQ(&cchar, std::get<1>(actor.try_get<int, char, double>()));
+    ASSERT_EQ(nullptr, std::get<2>(actor.try_get<int, char, double>()));
+    ASSERT_EQ(nullptr, actor.try_get<double>());
+    ASSERT_EQ(&cchar, actor.try_get<char>());
+    ASSERT_EQ(&cint, actor.try_get<int>());
 
 
     ASSERT_FALSE(registry.empty<int>());
     ASSERT_FALSE(registry.empty<int>());
     ASSERT_FALSE(registry.empty());
     ASSERT_FALSE(registry.empty());

+ 7 - 7
test/entt/entity/prototype.cpp

@@ -21,13 +21,13 @@ TEST(Prototype, SameRegistry) {
     ASSERT_EQ(std::get<0>(prototype.get<int, char>()), 3);
     ASSERT_EQ(std::get<0>(prototype.get<int, char>()), 3);
     ASSERT_EQ(std::get<1>(cprototype.get<int, char>()), 'c');
     ASSERT_EQ(std::get<1>(cprototype.get<int, char>()), 'c');
 
 
-    ASSERT_NE(prototype.get_if<int>(), nullptr);
-    ASSERT_NE(prototype.get_if<char>(), nullptr);
-    ASSERT_EQ(prototype.get_if<double>(), nullptr);
-    ASSERT_EQ(*prototype.get_if<int>(), 3);
-    ASSERT_EQ(*cprototype.get_if<char>(), 'c');
-    ASSERT_EQ(*std::get<0>(prototype.get_if<int, char, double>()), 3);
-    ASSERT_EQ(*std::get<1>(cprototype.get_if<int, char, double>()), 'c');
+    ASSERT_NE(prototype.try_get<int>(), nullptr);
+    ASSERT_NE(prototype.try_get<char>(), nullptr);
+    ASSERT_EQ(prototype.try_get<double>(), nullptr);
+    ASSERT_EQ(*prototype.try_get<int>(), 3);
+    ASSERT_EQ(*cprototype.try_get<char>(), 'c');
+    ASSERT_EQ(*std::get<0>(prototype.try_get<int, char, double>()), 3);
+    ASSERT_EQ(*std::get<1>(cprototype.try_get<int, char, double>()), 'c');
 
 
     const auto e0 = prototype.create();
     const auto e0 = prototype.create();
 
 

+ 13 - 13
test/entt/entity/registry.cpp

@@ -75,12 +75,12 @@ TEST(Registry, Functionalities) {
     ASSERT_FALSE((registry.has<int, char>(e0)));
     ASSERT_FALSE((registry.has<int, char>(e0)));
     ASSERT_TRUE((registry.has<int, char>(e1)));
     ASSERT_TRUE((registry.has<int, char>(e1)));
 
 
-    ASSERT_EQ(registry.get_if<int>(e0), nullptr);
-    ASSERT_NE(registry.get_if<int>(e1), nullptr);
-    ASSERT_EQ(registry.get_if<char>(e0), nullptr);
-    ASSERT_NE(registry.get_if<char>(e1), nullptr);
-    ASSERT_EQ(registry.get_if<double>(e0), nullptr);
-    ASSERT_EQ(registry.get_if<double>(e1), nullptr);
+    ASSERT_EQ(registry.try_get<int>(e0), nullptr);
+    ASSERT_NE(registry.try_get<int>(e1), nullptr);
+    ASSERT_EQ(registry.try_get<char>(e0), nullptr);
+    ASSERT_NE(registry.try_get<char>(e1), nullptr);
+    ASSERT_EQ(registry.try_get<double>(e0), nullptr);
+    ASSERT_EQ(registry.try_get<double>(e1), nullptr);
 
 
     ASSERT_EQ(registry.assign<int>(e0, 42), 42);
     ASSERT_EQ(registry.assign<int>(e0, 42), 42);
     ASSERT_EQ(registry.assign<char>(e0, 'c'), 'c');
     ASSERT_EQ(registry.assign<char>(e0, 'c'), 'c');
@@ -104,16 +104,16 @@ TEST(Registry, Functionalities) {
     ASSERT_EQ(registry.get<int>(e0), 42);
     ASSERT_EQ(registry.get<int>(e0), 42);
     ASSERT_EQ(registry.get<char>(e0), 'c');
     ASSERT_EQ(registry.get<char>(e0), 'c');
 
 
-    ASSERT_NE(registry.get_if<int>(e0), nullptr);
-    ASSERT_NE(registry.get_if<char>(e0), nullptr);
-    ASSERT_EQ(registry.get_if<double>(e0), nullptr);
-    ASSERT_EQ(*registry.get_if<int>(e0), 42);
-    ASSERT_EQ(*registry.get_if<char>(e0), 'c');
+    ASSERT_NE(registry.try_get<int>(e0), nullptr);
+    ASSERT_NE(registry.try_get<char>(e0), nullptr);
+    ASSERT_EQ(registry.try_get<double>(e0), nullptr);
+    ASSERT_EQ(*registry.try_get<int>(e0), 42);
+    ASSERT_EQ(*registry.try_get<char>(e0), 'c');
 
 
     ASSERT_EQ(std::get<0>(registry.get<int, char>(e0)), 42);
     ASSERT_EQ(std::get<0>(registry.get<int, char>(e0)), 42);
-    ASSERT_EQ(*std::get<0>(registry.get_if<int, char, double>(e0)), 42);
+    ASSERT_EQ(*std::get<0>(registry.try_get<int, char, double>(e0)), 42);
     ASSERT_EQ(std::get<1>(static_cast<const entt::registry<> &>(registry).get<int, char>(e0)), 'c');
     ASSERT_EQ(std::get<1>(static_cast<const entt::registry<> &>(registry).get<int, char>(e0)), 'c');
-    ASSERT_EQ(*std::get<1>(static_cast<const entt::registry<> &>(registry).get_if<int, char, double>(e0)), 'c');
+    ASSERT_EQ(*std::get<1>(static_cast<const entt::registry<> &>(registry).try_get<int, char, double>(e0)), 'c');
 
 
     ASSERT_EQ(registry.get<int>(e0), registry.get<int>(e2));
     ASSERT_EQ(registry.get<int>(e0), registry.get<int>(e2));
     ASSERT_EQ(registry.get<char>(e0), registry.get<char>(e2));
     ASSERT_EQ(registry.get<char>(e0), registry.get<char>(e2));

+ 5 - 1
test/entt/entity/sparse_set.cpp

@@ -364,6 +364,8 @@ TEST(SparseSetWithType, Functionalities) {
     ASSERT_TRUE(set.has(42));
     ASSERT_TRUE(set.has(42));
     ASSERT_TRUE(set.fast(42));
     ASSERT_TRUE(set.fast(42));
     ASSERT_EQ(set.get(42), 3);
     ASSERT_EQ(set.get(42), 3);
+    ASSERT_EQ(*set.try_get(42), 3);
+    ASSERT_EQ(set.try_get(99), nullptr);
 
 
     set.destroy(42);
     set.destroy(42);
 
 
@@ -377,6 +379,8 @@ TEST(SparseSetWithType, Functionalities) {
     set.construct(42, 12);
     set.construct(42, 12);
 
 
     ASSERT_EQ(set.get(42), 12);
     ASSERT_EQ(set.get(42), 12);
+    ASSERT_EQ(*set.try_get(42), 12);
+    ASSERT_EQ(set.try_get(99), nullptr);
 
 
     set.reset();
     set.reset();
 
 
@@ -392,7 +396,7 @@ TEST(SparseSetWithType, Functionalities) {
     other = std::move(set);
     other = std::move(set);
 }
 }
 
 
-    TEST(SparseSetWithType, AggregatesMustWork) {
+TEST(SparseSetWithType, AggregatesMustWork) {
     struct aggregate_type { int value; };
     struct aggregate_type { int value; };
     // the goal of this test is to enforce the requirements for aggregate types
     // the goal of this test is to enforce the requirements for aggregate types
     entt::sparse_set<std::uint64_t, aggregate_type>{}.construct(0, 42);
     entt::sparse_set<std::uint64_t, aggregate_type>{}.construct(0, 42);