ソースを参照

registry:: non-template storage(id) returns an iterator rather than a naked pointer

Michele Caini 4 年 前
コミット
2196db562e
3 ファイル変更26 行追加25 行削除
  1. 20 19
      src/entt/entity/registry.hpp
  2. 4 4
      test/entt/entity/registry.cpp
  3. 2 2
      test/example/entity_copy.cpp

+ 20 - 19
src/entt/entity/registry.hpp

@@ -336,6 +336,26 @@ public:
         return iterable_adaptor{internal::storage_proxy_iterator{pools.cbegin()}, internal::storage_proxy_iterator{pools.cend()}};
         return iterable_adaptor{internal::storage_proxy_iterator{pools.cbegin()}, internal::storage_proxy_iterator{pools.cend()}};
     }
     }
 
 
+    /**
+     * @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.
+     */
+    [[nodiscard]] auto storage(const id_type id) {
+        return internal::storage_proxy_iterator{pools.find(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.
+     */
+    [[nodiscard]] auto storage(const id_type id) const {
+        return internal::storage_proxy_iterator{pools.find(id)};
+    }
+
     /**
     /**
      * @brief Returns the storage for a given component type.
      * @brief Returns the storage for a given component type.
      * @tparam Component Type of component of which to return the storage.
      * @tparam Component Type of component of which to return the storage.
@@ -363,25 +383,6 @@ public:
         return assure<Component>(id);
         return assure<Component>(id);
     }
     }
 
 
-    /**
-     * @brief Returns the storage associated with a given name, if any.
-     * @param id Name used to map the storage within the registry.
-     * @return The requested storage if it exists, a null pointer otherwise.
-     */
-    [[nodiscard]] base_type *storage(const id_type id) {
-        return const_cast<base_type *>(std::as_const(*this).storage(id));
-    }
-
-    /**
-     * @brief Returns the storage associated with a given name, if any.
-     * @param id Name used to map the storage within the registry.
-     * @return The requested storage if it exists, a null pointer otherwise.
-     */
-    [[nodiscard]] const base_type *storage(const id_type id) const {
-        const auto it = pools.find(id);
-        return it == pools.end() ? nullptr : it->second.get();
-    }
-
     /**
     /**
      * @brief Returns the number of entities created so far.
      * @brief Returns the number of entities created so far.
      * @return Number of entities created so far.
      * @return Number of entities created so far.

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

@@ -1881,14 +1881,14 @@ TEST(Registry, RuntimePools) {
     static_assert(std::is_same_v<decltype(registry.storage<empty_type>()), typename entt::storage_traits<entt::entity, empty_type>::storage_type &>);
     static_assert(std::is_same_v<decltype(registry.storage<empty_type>()), typename entt::storage_traits<entt::entity, empty_type>::storage_type &>);
     static_assert(std::is_same_v<decltype(std::as_const(registry).storage<empty_type>()), const typename entt::storage_traits<entt::entity, empty_type>::storage_type &>);
     static_assert(std::is_same_v<decltype(std::as_const(registry).storage<empty_type>()), const typename entt::storage_traits<entt::entity, empty_type>::storage_type &>);
 
 
-    static_assert(std::is_same_v<decltype(registry.storage("other"_hs)), typename entt::storage_traits<entt::entity, empty_type>::storage_type::base_type *>);
-    static_assert(std::is_same_v<decltype(std::as_const(registry).storage("other"_hs)), const typename entt::storage_traits<entt::entity, empty_type>::storage_type::base_type *>);
+    static_assert(std::is_same_v<decltype(registry.storage("other"_hs)->second), typename entt::storage_traits<entt::entity, empty_type>::storage_type::base_type &>);
+    static_assert(std::is_same_v<decltype(std::as_const(registry).storage("other"_hs)->second), const typename entt::storage_traits<entt::entity, empty_type>::storage_type::base_type &>);
 
 
     ASSERT_DEATH([[maybe_unused]] auto &&unused = registry.storage<int>("other"_hs), "");
     ASSERT_DEATH([[maybe_unused]] auto &&unused = registry.storage<int>("other"_hs), "");
     ASSERT_DEATH([[maybe_unused]] auto &&unused = std::as_const(registry).storage<int>("other"_hs), "");
     ASSERT_DEATH([[maybe_unused]] auto &&unused = std::as_const(registry).storage<int>("other"_hs), "");
 
 
-    ASSERT_EQ(registry.storage("other"_hs), &storage);
-    ASSERT_EQ(std::as_const(registry).storage("rehto"_hs), nullptr);
+    ASSERT_NE(registry.storage("other"_hs), registry.storage().end());
+    ASSERT_EQ(std::as_const(registry).storage("rehto"_hs), registry.storage().end());
 
 
     ASSERT_EQ(&registry.storage<empty_type>("other"_hs), &storage);
     ASSERT_EQ(&registry.storage<empty_type>("other"_hs), &storage);
     ASSERT_NE(&std::as_const(registry).storage<empty_type>(), &storage);
     ASSERT_NE(&std::as_const(registry).storage<empty_type>(), &storage);

+ 2 - 2
test/example/entity_copy.cpp

@@ -56,8 +56,8 @@ TEST(Example, DifferentRegistryTypes) {
     registry.emplace<char>(src, 'c');
     registry.emplace<char>(src, 'c');
 
 
     for(auto [id, storage]: registry.storage()) {
     for(auto [id, storage]: registry.storage()) {
-        if(auto *pool = other.storage(id); pool && storage.contains(src)) {
-            pool->emplace(dst, storage.get(src));
+        if(auto it = other.storage(id); it != other.storage().end() && storage.contains(src)) {
+            it->second.emplace(dst, storage.get(src));
         }
         }
     }
     }