registry: try_get should not create storage

This commit is contained in:
Michele Caini
2023-11-03 09:36:29 +01:00
parent 4dcc0e8666
commit 62a13526c9
2 changed files with 18 additions and 2 deletions

View File

@@ -929,8 +929,7 @@ public:
template<typename... Type>
[[nodiscard]] auto try_get([[maybe_unused]] const entity_type entt) {
if constexpr(sizeof...(Type) == 1u) {
auto &cpool = assure<std::remove_const_t<Type>...>();
return (static_cast<Type *>(cpool.contains(entt) ? std::addressof(cpool.get(entt)) : nullptr), ...);
return (const_cast<Type *>(std::as_const(*this).template try_get<Type>(entt)), ...);
} else {
return std::make_tuple(try_get<Type>(entt)...);
}

View File

@@ -1893,6 +1893,23 @@ TEST(Registry, GetOrEmplace) {
ASSERT_EQ(registry.get<int>(entity), 3);
}
TEST(Registry, TryGet) {
entt::registry registry;
const auto entity = registry.create();
ASSERT_EQ(registry.try_get<int>(entity), nullptr);
ASSERT_EQ(std::as_const(registry).try_get<int>(entity), nullptr);
ASSERT_EQ(std::as_const(registry).storage<int>(), nullptr);
const int &elem = registry.emplace<int>(entity);
ASSERT_NE(std::as_const(registry).storage<int>(), nullptr);
ASSERT_EQ(registry.try_get<int>(entity), &elem);
ASSERT_EQ(std::as_const(registry).try_get<int>(entity), &elem);
}
TEST(Registry, Constness) {
entt::registry registry;