فهرست منبع

registry: storage reset function - close #1173

Michele Caini 1 سال پیش
والد
کامیت
8fd4cf7c45
2فایلهای تغییر یافته به همراه42 افزوده شده و 0 حذف شده
  1. 10 0
      src/entt/entity/registry.hpp
  2. 32 0
      test/entt/entity/registry.cpp

+ 10 - 0
src/entt/entity/registry.hpp

@@ -463,6 +463,16 @@ public:
         return assure<Type>(id);
     }
 
+    /**
+     * @brief Discards the storage associated with a given name, if any.
+     * @param id Name used to map the storage within the registry.
+     * @return True in case of success, false otherwise.
+     */
+    bool reset(const id_type id) {
+        ENTT_ASSERT(id != type_hash<entity_type>::value(), "Cannot reset entity storage");
+        return !(pools.erase(id) == 0u);
+    }
+
     /**
      * @brief Checks if an identifier refers to a valid entity.
      * @param entt An identifier, either valid or not.

+ 32 - 0
test/entt/entity/registry.cpp

@@ -531,6 +531,38 @@ ENTT_DEBUG_TEST(RegistryDeathTest, Storage) {
     ASSERT_DEATH([[maybe_unused]] const auto *storage = std::as_const(registry).storage<entt::entity>("other"_hs), "");
 }
 
+TEST(Registry, StorageReset) {
+    using namespace entt::literals;
+
+    entt::registry registry{};
+    auto &storage = registry.storage<int>();
+    auto &other = registry.storage<int>("other"_hs);
+
+    ASSERT_NE(std::as_const(registry).storage<int>(), nullptr);
+    ASSERT_NE(registry.storage("other"_hs), nullptr);
+
+    ASSERT_EQ(registry.reset("other"_hs), 1u);
+
+    ASSERT_NE(std::as_const(registry).storage<int>(), nullptr);
+    ASSERT_EQ(registry.storage("other"_hs), nullptr);
+
+    ASSERT_EQ(registry.reset("other"_hs), 0u);
+    ASSERT_EQ(registry.reset(entt::type_id<int>().hash()), 1u);
+    ASSERT_EQ(registry.reset(entt::type_id<int>().hash()), 0u);
+
+    ASSERT_EQ(std::as_const(registry).storage<int>(), nullptr);
+    ASSERT_EQ(registry.storage("other"_hs), nullptr);
+}
+
+ENTT_DEBUG_TEST(RegistryDeathTest, StorageReset) {
+    entt::registry registry{};
+    const entt::entity entity = registry.create();
+
+    ASSERT_TRUE(registry.valid(entity));
+    ASSERT_DEATH(registry.reset(entt::type_id<entt::entity>().hash()), "");
+    ASSERT_TRUE(registry.valid(entity));
+}
+
 TEST(Registry, Identifiers) {
     using traits_type = entt::entt_traits<entt::entity>;