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

registry: strict check on storage types to prevent subtle bugs in case of errors

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

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

@@ -236,6 +236,7 @@ class basic_registry {
             cpool->bind(forward_as_any(*this));
         }
 
+        ENTT_ASSERT(cpool->type() == type_id<Component>(), "Unexpected type");
         return static_cast<storage_type<Component> &>(*cpool);
     }
 
@@ -244,6 +245,7 @@ class basic_registry {
         static_assert(std::is_same_v<Component, std::decay_t<Component>>, "Non-decayed types not allowed");
 
         if(const auto it = pools.find(id); it != pools.cend()) {
+            ENTT_ASSERT(it->second->type() == type_id<Component>(), "Unexpected type");
             return static_cast<const storage_type<Component> &>(*it->second);
         }
 

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

@@ -1884,11 +1884,14 @@ TEST(Registry, RuntimePools) {
     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 *>);
 
+    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_EQ(registry.storage("other"_hs), &storage);
     ASSERT_EQ(std::as_const(registry).storage("rehto"_hs), nullptr);
 
     ASSERT_EQ(&registry.storage<empty_type>("other"_hs), &storage);
-    ASSERT_NE(&registry.storage<empty_type>(), &storage);
+    ASSERT_NE(&std::as_const(registry).storage<empty_type>(), &storage);
 
     ASSERT_FALSE(registry.any_of<empty_type>(entity));
     ASSERT_FALSE(storage.contains(entity));