Skip to content

Commit 30d16b8

Browse files
committed
view: operator-> for single type views
1 parent 81b878d commit 30d16b8

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/entt/entity/view.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,14 @@ class basic_view<get_t<Get>, exclude_t<>, std::void_t<std::enable_if_t<!componen
843843
this->leading = &elem;
844844
}
845845

846+
/**
847+
* @brief Returns a pointer to the underlying storage.
848+
* @return A pointer to the underlying storage.
849+
*/
850+
[[nodiscard]] Get *operator->() const noexcept {
851+
return storage();
852+
}
853+
846854
/**
847855
* @brief Returns the element assigned to the given entity.
848856
* @param entt A valid identifier.

test/entt/entity/view.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,37 @@ TEST(SingleComponentView, Storage) {
531531
ASSERT_EQ(cview.storage<const char>(), nullptr);
532532
}
533533

534+
TEST(SingleComponentView, ArrowOperator) {
535+
entt::registry registry;
536+
const auto entity = registry.create();
537+
auto view = registry.view<int>();
538+
auto cview = registry.view<const char>();
539+
540+
testing::StaticAssertTypeEq<decltype(view.operator->()), entt::storage_type_t<int> *>();
541+
testing::StaticAssertTypeEq<decltype(cview.operator->()), const entt::storage_type_t<char> *>();
542+
543+
ASSERT_TRUE(view);
544+
ASSERT_TRUE(cview);
545+
546+
ASSERT_NE(view.operator->(), nullptr);
547+
ASSERT_NE(cview.operator->(), nullptr);
548+
549+
view->emplace(entity);
550+
registry.emplace<char>(entity);
551+
552+
ASSERT_EQ(view.operator->(), &registry.storage<int>());
553+
ASSERT_EQ(cview.operator->(), &registry.storage<char>());
554+
555+
ASSERT_EQ(view.operator->(), view.storage());
556+
ASSERT_EQ(cview.operator->(), cview.storage());
557+
558+
view = {};
559+
cview = {};
560+
561+
ASSERT_EQ(view.operator->(), nullptr);
562+
ASSERT_EQ(cview.operator->(), nullptr);
563+
}
564+
534565
TEST(SingleComponentView, SwapStorage) {
535566
using namespace entt::literals;
536567

0 commit comments

Comments
 (0)