Skip to content

Commit

Permalink
registry: storage reset function - close #1173
Browse files Browse the repository at this point in the history
  • Loading branch information
skypjack committed Sep 9, 2024
1 parent 6f7dc05 commit 8fd4cf7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/entt/entity/registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,16 @@ class basic_registry {
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.
Expand Down
32 changes: 32 additions & 0 deletions test/entt/entity/registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>;

Expand Down

0 comments on commit 8fd4cf7

Please sign in to comment.