Просмотр исходного кода

registry: assert on user entity storage - close #1143

Michele Caini 1 год назад
Родитель
Сommit
d6d72e6b03
2 измененных файлов с 5 добавлено и 0 удалено
  1. 2 0
      src/entt/entity/registry.hpp
  2. 3 0
      test/entt/entity/registry.cpp

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

@@ -244,6 +244,7 @@ class basic_registry {
         static_assert(std::is_same_v<Type, std::decay_t<Type>>, "Non-decayed types not allowed");
 
         if constexpr(std::is_same_v<Type, entity_type>) {
+            ENTT_ASSERT(id == type_hash<Type>::value(), "User entity storage not allowed");
             return entities;
         } else {
             auto &cpool = pools[id];
@@ -272,6 +273,7 @@ class basic_registry {
         static_assert(std::is_same_v<Type, std::decay_t<Type>>, "Non-decayed types not allowed");
 
         if constexpr(std::is_same_v<Type, entity_type>) {
+            ENTT_ASSERT(id == type_hash<Type>::value(), "User entity storage not allowed");
             return &entities;
         } else {
             if(const auto it = pools.find(id); it != pools.cend()) {

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

@@ -525,6 +525,9 @@ ENTT_DEBUG_TEST(RegistryDeathTest, Storage) {
 
     ASSERT_DEATH(registry.storage<int>("other"_hs), "");
     ASSERT_DEATH(std::as_const(registry).storage<int>("other"_hs), "");
+
+    ASSERT_DEATH(registry.storage<entt::entity>("other"_hs), "");
+    ASSERT_DEATH(std::as_const(registry).storage<entt::entity>("other"_hs), "");
 }
 
 TEST(Registry, Identifiers) {