Skip to content

Commit

Permalink
view: operator-> for single type views
Browse files Browse the repository at this point in the history
  • Loading branch information
skypjack committed May 7, 2024
1 parent 81b878d commit 30d16b8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/entt/entity/view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,14 @@ class basic_view<get_t<Get>, exclude_t<>, std::void_t<std::enable_if_t<!componen
this->leading = &elem;
}

/**
* @brief Returns a pointer to the underlying storage.
* @return A pointer to the underlying storage.
*/
[[nodiscard]] Get *operator->() const noexcept {
return storage();
}

/**
* @brief Returns the element assigned to the given entity.
* @param entt A valid identifier.
Expand Down
31 changes: 31 additions & 0 deletions test/entt/entity/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,37 @@ TEST(SingleComponentView, Storage) {
ASSERT_EQ(cview.storage<const char>(), nullptr);
}

TEST(SingleComponentView, ArrowOperator) {
entt::registry registry;
const auto entity = registry.create();
auto view = registry.view<int>();
auto cview = registry.view<const char>();

testing::StaticAssertTypeEq<decltype(view.operator->()), entt::storage_type_t<int> *>();
testing::StaticAssertTypeEq<decltype(cview.operator->()), const entt::storage_type_t<char> *>();

ASSERT_TRUE(view);
ASSERT_TRUE(cview);

ASSERT_NE(view.operator->(), nullptr);
ASSERT_NE(cview.operator->(), nullptr);

view->emplace(entity);
registry.emplace<char>(entity);

ASSERT_EQ(view.operator->(), &registry.storage<int>());
ASSERT_EQ(cview.operator->(), &registry.storage<char>());

ASSERT_EQ(view.operator->(), view.storage());
ASSERT_EQ(cview.operator->(), cview.storage());

view = {};
cview = {};

ASSERT_EQ(view.operator->(), nullptr);
ASSERT_EQ(cview.operator->(), nullptr);
}

TEST(SingleComponentView, SwapStorage) {
using namespace entt::literals;

Expand Down

0 comments on commit 30d16b8

Please sign in to comment.