registry: template ::storage support to const qualified types

This commit is contained in:
Michele Caini
2022-01-21 11:37:19 +01:00
parent e6006663ec
commit 26ba137424
2 changed files with 10 additions and 4 deletions

View File

@@ -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);
}
/**

View File

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