registry: storage reset function - close #1173

This commit is contained in:
Michele Caini
2024-09-09 12:42:52 +02:00
parent 6f7dc0558b
commit 8fd4cf7c45
2 changed files with 42 additions and 0 deletions

View File

@@ -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.

View File

@@ -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>;