Skip to content

Commit

Permalink
registry: try_get should not create storage
Browse files Browse the repository at this point in the history
  • Loading branch information
skypjack committed Nov 3, 2023
1 parent 4dcc0e8 commit 62a1352
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/entt/entity/registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -929,8 +929,7 @@ class basic_registry {
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)...);
}
Expand Down
17 changes: 17 additions & 0 deletions test/entt/entity/registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 62a1352

Please sign in to comment.