Преглед изворни кода

registry: ::storage(id) returns a pointer rather than an utterly annoying iterator

Michele Caini пре 3 година
родитељ
комит
cd9553ca10
3 измењених фајлова са 30 додато и 31 уклоњено
  1. 7 8
      src/entt/entity/registry.hpp
  2. 8 8
      test/entt/entity/registry.cpp
  3. 15 15
      test/example/entity_copy.cpp

+ 7 - 8
src/entt/entity/registry.hpp

@@ -471,21 +471,20 @@ public:
     /**
      * @brief Finds the storage associated with a given name, if any.
      * @param id Name used to map the storage within the registry.
-     * @return An iterator to the given storage if it's found, past the end
-     * iterator otherwise.
+     * @return A pointer to the storage if it exists, a null pointer otherwise.
      */
-    [[nodiscard]] auto storage(const id_type id) {
-        return internal::registry_storage_iterator{pools.find(id)};
+    [[nodiscard]] base_type *storage(const id_type id) {
+        return const_cast<base_type *>(std::as_const(*this).storage(id));
     }
 
     /**
      * @brief Finds the storage associated with a given name, if any.
      * @param id Name used to map the storage within the registry.
-     * @return An iterator to the given storage if it's found, past the end
-     * iterator otherwise.
+     * @return A pointer to the storage if it exists, a null pointer otherwise.
      */
-    [[nodiscard]] auto storage(const id_type id) const {
-        return internal::registry_storage_iterator{pools.find(id)};
+    [[nodiscard]] const base_type *storage(const id_type id) const {
+        const auto it = pools.find(id);
+        return it == pools.cend() ? nullptr : it->second.get();
     }
 
     /**

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

@@ -1487,14 +1487,14 @@ TEST(Registry, SignalWhenDestroying) {
     registry.emplace<double>(entity);
     registry.emplace<int>(entity);
 
-    ASSERT_NE(registry.storage(entt::type_id<double>().hash()), registry.storage().end());
-    ASSERT_NE(registry.storage(entt::type_id<int>().hash()), registry.storage().end());
-    ASSERT_EQ(registry.storage(entt::type_id<char>().hash()), registry.storage().end());
+    ASSERT_NE(registry.storage(entt::type_id<double>().hash()), nullptr);
+    ASSERT_NE(registry.storage(entt::type_id<int>().hash()), nullptr);
+    ASSERT_EQ(registry.storage(entt::type_id<char>().hash()), nullptr);
     ASSERT_TRUE(registry.valid(entity));
 
     registry.destroy(entity);
 
-    ASSERT_NE(registry.storage(entt::type_id<char>().hash()), registry.storage().end());
+    ASSERT_NE(registry.storage(entt::type_id<char>().hash()), nullptr);
     ASSERT_FALSE(registry.valid(entity));
 }
 
@@ -2019,11 +2019,11 @@ TEST(Registry, RuntimePools) {
     static_assert(std::is_same_v<decltype(registry.storage<empty_type>()), entt::storage_type_t<empty_type> &>);
     static_assert(std::is_same_v<decltype(std::as_const(registry).storage<empty_type>()), const entt::storage_type_t<empty_type> &>);
 
-    static_assert(std::is_same_v<decltype(registry.storage("other"_hs)->second), entt::storage_type_t<empty_type>::base_type &>);
-    static_assert(std::is_same_v<decltype(std::as_const(registry).storage("other"_hs)->second), const entt::storage_type_t<empty_type>::base_type &>);
+    static_assert(std::is_same_v<decltype(registry.storage("other"_hs)), entt::storage_type_t<empty_type>::base_type *>);
+    static_assert(std::is_same_v<decltype(std::as_const(registry).storage("other"_hs)), const entt::storage_type_t<empty_type>::base_type *>);
 
-    ASSERT_NE(registry.storage("other"_hs), registry.storage().end());
-    ASSERT_EQ(std::as_const(registry).storage("rehto"_hs), registry.storage().end());
+    ASSERT_NE(registry.storage("other"_hs), nullptr);
+    ASSERT_EQ(std::as_const(registry).storage("rehto"_hs), nullptr);
 
     ASSERT_EQ(&registry.storage<empty_type>("other"_hs), &storage);
     ASSERT_NE(&std::as_const(registry).storage<empty_type>(), &storage);

+ 15 - 15
test/example/entity_copy.cpp

@@ -43,8 +43,8 @@ TEST(Example, EntityCopy) {
 TEST(Example, DifferentRegistryTypes) {
     using namespace entt::literals;
 
-    entt::basic_registry<entt::entity> registry{};
-    entt::basic_registry<my_entity> other{};
+    entt::basic_registry<entt::entity> src{};
+    entt::basic_registry<my_entity> dst{};
 
     /*
         TODO These are currently needed to ensure that the source and
@@ -56,23 +56,23 @@ TEST(Example, DifferentRegistryTypes) {
              lines should be removed when a fix is properly landed.
              https://github.com/skypjack/entt/issues/827
     */
-    static_cast<void>(registry.storage<double>());
-    static_cast<void>(other.storage<int>());
+    static_cast<void>(src.storage<double>());
+    static_cast<void>(dst.storage<int>());
 
-    const auto src = registry.create();
-    const auto dst = other.create();
+    const auto entity = src.create();
+    const auto copy = dst.create();
 
-    registry.emplace<int>(src, 42);
-    registry.emplace<char>(src, 'c');
+    src.emplace<int>(entity, 42);
+    src.emplace<char>(entity, 'c');
 
-    for(auto [id, storage]: registry.storage()) {
-        if(auto it = other.storage(id); it != other.storage().end() && storage.contains(src)) {
-            it->second.emplace(dst, storage.get(src));
+    for(auto [id, storage]: src.storage()) {
+        if(auto *other = dst.storage(id); other && storage.contains(entity)) {
+            other->emplace(copy, storage.get(entity));
         }
     }
 
-    ASSERT_TRUE((registry.all_of<int, char>(src)));
-    ASSERT_FALSE(other.all_of<char>(dst));
-    ASSERT_TRUE(other.all_of<int>(dst));
-    ASSERT_EQ(other.get<int>(dst), 42);
+    ASSERT_TRUE((src.all_of<int, char>(entity)));
+    ASSERT_FALSE(dst.all_of<char>(copy));
+    ASSERT_TRUE(dst.all_of<int>(copy));
+    ASSERT_EQ(dst.get<int>(copy), 42);
 }