Skip to content

Commit

Permalink
view: drop unnecessary data member from single type views
Browse files Browse the repository at this point in the history
  • Loading branch information
skypjack committed Jul 7, 2023
1 parent 4dcae18 commit 3b3deaa
Showing 1 changed file with 21 additions and 24 deletions.
45 changes: 21 additions & 24 deletions src/entt/entity/view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,17 +612,15 @@ class basic_view<get_t<Get>, exclude_t<>, std::void_t<std::enable_if_t<!Get::tra
/*! @brief Default constructor to use to create empty, invalid views. */
basic_view() noexcept
: pools{},
filter{},
view{} {}
filter{} {}

/**
* @brief Constructs a single-type view from a storage class.
* @param value The storage for the type to iterate.
*/
basic_view(Get &value) noexcept
: pools{&value},
filter{},
view{&value} {}
filter{} {}

/**
* @brief Constructs a single-type view from a storage class.
Expand All @@ -636,7 +634,7 @@ class basic_view<get_t<Get>, exclude_t<>, std::void_t<std::enable_if_t<!Get::tra
* @return The leading storage of the view.
*/
[[nodiscard]] const common_type *handle() const noexcept {
return view;
return storage();
}

/**
Expand Down Expand Up @@ -675,23 +673,23 @@ class basic_view<get_t<Get>, exclude_t<>, std::void_t<std::enable_if_t<!Get::tra
*/
template<std::size_t Index>
void storage(Get &elem) noexcept {
view = std::get<Index>(pools) = &elem;
std::get<Index>(pools) = &elem;
}

/**
* @brief Returns the number of entities that have the given component.
* @return Number of entities that have the given component.
*/
[[nodiscard]] size_type size() const noexcept {
return view ? view->size() : size_type{};
return *this ? handle()->size() : size_type{};
}

/**
* @brief Checks whether a view is empty.
* @return True if the view is empty, false otherwise.
*/
[[nodiscard]] bool empty() const noexcept {
return !view || view->empty();
return !*this || handle()->empty();
}

/**
Expand All @@ -702,15 +700,15 @@ class basic_view<get_t<Get>, exclude_t<>, std::void_t<std::enable_if_t<!Get::tra
* @return An iterator to the first entity of the view.
*/
[[nodiscard]] iterator begin() const noexcept {
return view ? view->begin() : iterator{};
return *this ? handle()->begin() : iterator{};
}

/**
* @brief Returns an iterator that is past the last entity of the view.
* @return An iterator to the entity following the last entity of the view.
*/
[[nodiscard]] iterator end() const noexcept {
return view ? view->end() : iterator{};
return *this ? handle()->end() : iterator{};
}

/**
Expand All @@ -721,7 +719,7 @@ class basic_view<get_t<Get>, exclude_t<>, std::void_t<std::enable_if_t<!Get::tra
* @return An iterator to the first entity of the reversed view.
*/
[[nodiscard]] reverse_iterator rbegin() const noexcept {
return view ? view->rbegin() : reverse_iterator{};
return *this ? handle()->rbegin() : reverse_iterator{};
}

/**
Expand All @@ -731,7 +729,7 @@ class basic_view<get_t<Get>, exclude_t<>, std::void_t<std::enable_if_t<!Get::tra
* reversed view.
*/
[[nodiscard]] reverse_iterator rend() const noexcept {
return view ? view->rend() : reverse_iterator{};
return *this ? handle()->rend() : reverse_iterator{};
}

/**
Expand All @@ -740,7 +738,7 @@ class basic_view<get_t<Get>, exclude_t<>, std::void_t<std::enable_if_t<!Get::tra
* otherwise.
*/
[[nodiscard]] entity_type front() const noexcept {
return empty() ? null : *view->begin();
return empty() ? null : *handle()->begin();
}

/**
Expand All @@ -749,7 +747,7 @@ class basic_view<get_t<Get>, exclude_t<>, std::void_t<std::enable_if_t<!Get::tra
* otherwise.
*/
[[nodiscard]] entity_type back() const noexcept {
return empty() ? null : *view->rbegin();
return empty() ? null : *handle()->rbegin();
}

/**
Expand All @@ -759,7 +757,7 @@ class basic_view<get_t<Get>, exclude_t<>, std::void_t<std::enable_if_t<!Get::tra
* iterator otherwise.
*/
[[nodiscard]] iterator find(const entity_type entt) const noexcept {
return view ? view->find(entt) : iterator{};
return *this ? handle()->find(entt) : iterator{};
}

/**
Expand All @@ -777,15 +775,15 @@ class basic_view<get_t<Get>, exclude_t<>, std::void_t<std::enable_if_t<!Get::tra
* @return The component assigned to the given entity.
*/
[[nodiscard]] decltype(auto) operator[](const entity_type entt) const {
return std::get<0>(pools)->get(entt);
return storage()->get(entt);
}

/**
* @brief Checks if a view is fully initialized.
* @return True if the view is fully initialized, false otherwise.
*/
[[nodiscard]] explicit operator bool() const noexcept {
return (std::get<0>(pools) != nullptr);
return (handle() != nullptr);
}

/**
Expand All @@ -794,7 +792,7 @@ class basic_view<get_t<Get>, exclude_t<>, std::void_t<std::enable_if_t<!Get::tra
* @return True if the view contains the given entity, false otherwise.
*/
[[nodiscard]] bool contains(const entity_type entt) const noexcept {
return view && view->contains(entt);
return *this && handle()->contains(entt);
}

/**
Expand All @@ -813,9 +811,9 @@ class basic_view<get_t<Get>, exclude_t<>, std::void_t<std::enable_if_t<!Get::tra
template<std::size_t... Elem>
[[nodiscard]] decltype(auto) get(const entity_type entt) const {
if constexpr(sizeof...(Elem) == 0) {
return std::get<0>(pools)->get_as_tuple(entt);
return storage()->get_as_tuple(entt);
} else {
return std::get<Elem...>(pools)->get(entt);
return storage<Elem...>()->get(entt);
}
}

Expand Down Expand Up @@ -843,7 +841,7 @@ class basic_view<get_t<Get>, exclude_t<>, std::void_t<std::enable_if_t<!Get::tra
*/
template<typename Func>
void each(Func func) const {
if(view) {
if(*this) {
if constexpr(is_applicable_v<Func, decltype(*each().begin())>) {
for(const auto pack: each()) {
std::apply(func, pack);
Expand All @@ -853,7 +851,7 @@ class basic_view<get_t<Get>, exclude_t<>, std::void_t<std::enable_if_t<!Get::tra
func();
}
} else {
for(auto &&component: *std::get<0>(pools)) {
for(auto &&component: *storage()) {
func(component);
}
}
Expand All @@ -870,7 +868,7 @@ class basic_view<get_t<Get>, exclude_t<>, std::void_t<std::enable_if_t<!Get::tra
* @return An iterable object to use to _visit_ the view.
*/
[[nodiscard]] iterable each() const noexcept {
return view ? std::get<0>(pools)->each() : iterable{};
return *this ? storage()->each() : iterable{};
}

/**
Expand All @@ -891,7 +889,6 @@ class basic_view<get_t<Get>, exclude_t<>, std::void_t<std::enable_if_t<!Get::tra
private:
std::tuple<Get *> pools;
std::array<const common_type *, 0u> filter;
const common_type *view;
};

/**
Expand Down

0 comments on commit 3b3deaa

Please sign in to comment.