Browse Source

registry: template ::storage support to const qualified types

Michele Caini 4 years ago
parent
commit
26ba137424
2 changed files with 10 additions and 4 deletions
  1. 8 4
      src/entt/entity/registry.hpp
  2. 2 0
      test/entt/entity/registry.cpp

+ 8 - 4
src/entt/entity/registry.hpp

@@ -413,8 +413,12 @@ public:
      * @return The storage for the given component type.
      */
     template<typename Component>
-    decltype(auto) storage(const id_type id = type_hash<Component>::value()) {
-        return assure<Component>(id);
+    decltype(auto) storage(const id_type id = type_hash<std::remove_const_t<Component>>::value()) {
+        if constexpr(std::is_const_v<Component>) {
+            return std::as_const(*this).template storage<std::remove_const_t<Component>>(id);
+        } else {
+            return assure<Component>(id);
+        }
     }
 
     /**
@@ -429,8 +433,8 @@ public:
      * @return The storage for the given component type.
      */
     template<typename Component>
-    decltype(auto) storage(const id_type id = type_hash<Component>::value()) const {
-        return assure<Component>(id);
+    decltype(auto) storage(const id_type id = type_hash<std::remove_const_t<Component>>::value()) const {
+        return assure<std::remove_const_t<Component>>(id);
     }
 
     /**

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

@@ -1868,7 +1868,9 @@ TEST(Registry, RuntimePools) {
     const auto entity = registry.create();
 
     static_assert(std::is_same_v<decltype(registry.storage<empty_type>()), typename entt::storage_traits<entt::entity, empty_type>::storage_type &>);
+    static_assert(std::is_same_v<decltype(registry.storage<const empty_type>()), const typename entt::storage_traits<entt::entity, empty_type>::storage_type &>);
     static_assert(std::is_same_v<decltype(std::as_const(registry).storage<empty_type>()), const typename entt::storage_traits<entt::entity, empty_type>::storage_type &>);
+    static_assert(std::is_same_v<decltype(std::as_const(registry).storage<const empty_type>()), const typename entt::storage_traits<entt::entity, empty_type>::storage_type &>);
 
     static_assert(std::is_same_v<decltype(registry.storage("other"_hs)->second), 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)->second), const typename entt::storage_traits<entt::entity, empty_type>::storage_type::base_type &>);