view: uniform design (prepare for chaining)

This commit is contained in:
Michele Caini
2021-03-16 17:14:20 +01:00
parent a40ac1c46c
commit cabcc761c6

View File

@@ -669,7 +669,7 @@ public:
/*! @brief Default constructor to use to create empty, invalid views. */
basic_view() ENTT_NOEXCEPT
: pool{}
: pools{}
{}
/**
@@ -677,7 +677,7 @@ public:
* @param ref The storage for the type to iterate.
*/
basic_view(storage_type &ref) ENTT_NOEXCEPT
: pool{&ref}
: pools{&ref}
{}
/**
@@ -685,7 +685,7 @@ public:
* @return Number of entities that have the given component.
*/
[[nodiscard]] size_type size() const ENTT_NOEXCEPT {
return pool->size();
return std::get<0>(pools)->size();
}
/**
@@ -693,7 +693,7 @@ public:
* @return True if the view is empty, false otherwise.
*/
[[nodiscard]] bool empty() const ENTT_NOEXCEPT {
return pool->empty();
return std::get<0>(pools)->empty();
}
/**
@@ -705,7 +705,7 @@ public:
* @return A pointer to the array of components.
*/
[[nodiscard]] raw_type * raw() const ENTT_NOEXCEPT {
return pool->raw();
return std::get<0>(pools)->raw();
}
/**
@@ -717,7 +717,7 @@ public:
* @return A pointer to the array of entities.
*/
[[nodiscard]] const entity_type * data() const ENTT_NOEXCEPT {
return pool->data();
return std::get<0>(pools)->data();
}
/**
@@ -729,7 +729,7 @@ public:
* @return An iterator to the first entity of the view.
*/
[[nodiscard]] iterator begin() const ENTT_NOEXCEPT {
return pool->basic_sparse_set<entity_type>::begin();
return std::get<0>(pools)->basic_sparse_set<entity_type>::begin();
}
/**
@@ -742,7 +742,7 @@ public:
* @return An iterator to the entity following the last entity of the view.
*/
[[nodiscard]] iterator end() const ENTT_NOEXCEPT {
return pool->basic_sparse_set<entity_type>::end();
return std::get<0>(pools)->basic_sparse_set<entity_type>::end();
}
/**
@@ -754,7 +754,7 @@ public:
* @return An iterator to the first entity of the reversed view.
*/
[[nodiscard]] reverse_iterator rbegin() const ENTT_NOEXCEPT {
return pool->basic_sparse_set<entity_type>::rbegin();
return std::get<0>(pools)->basic_sparse_set<entity_type>::rbegin();
}
/**
@@ -769,7 +769,7 @@ public:
* reversed view.
*/
[[nodiscard]] reverse_iterator rend() const ENTT_NOEXCEPT {
return pool->basic_sparse_set<entity_type>::rend();
return std::get<0>(pools)->basic_sparse_set<entity_type>::rend();
}
/**
@@ -799,7 +799,7 @@ public:
* iterator otherwise.
*/
[[nodiscard]] iterator find(const entity_type entt) const {
const auto it = pool->find(entt);
const auto it = std::get<0>(pools)->find(entt);
return it != end() && *it == entt ? it : end();
}
@@ -817,7 +817,7 @@ public:
* @return True if the view is properly initialized, false otherwise.
*/
[[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
return pool != nullptr;
return std::get<0>(pools) != nullptr;
}
/**
@@ -826,7 +826,7 @@ public:
* @return True if the view contains the given entity, false otherwise.
*/
[[nodiscard]] bool contains(const entity_type entt) const {
return pool->contains(entt);
return std::get<0>(pools)->contains(entt);
}
/**
@@ -849,10 +849,10 @@ public:
ENTT_ASSERT(contains(entt));
if constexpr(sizeof...(Comp) == 0) {
return get_as_tuple(*pool, entt);
return get_as_tuple(*std::get<0>(pools), entt);
} else {
static_assert(std::is_same_v<Comp..., Component>, "Invalid component type");
return pool->get(entt);
return std::get<0>(pools)->get(entt);
}
}
@@ -880,7 +880,7 @@ public:
*/
template<typename Func>
void each(Func func) const {
if constexpr(std::is_void_v<decltype(pool->get({}))>) {
if constexpr(std::is_void_v<decltype(std::get<0>(pools)->get({}))>) {
if constexpr(std::is_invocable_v<Func>) {
for(auto pos = size(); pos; --pos) {
func();
@@ -896,7 +896,7 @@ public:
std::apply(func, pack);
}
} else {
for(auto &&component: *pool) {
for(auto &&component: *std::get<0>(pools)) {
func(component);
}
}
@@ -917,11 +917,12 @@ public:
* @return An iterable object to use to _visit_ the view.
*/
[[nodiscard]] iterable_view each() const ENTT_NOEXCEPT {
return iterable_view{*pool};
return iterable_view{*std::get<0>(pools)};
}
private:
storage_type * const pool;
const std::tuple<storage_type *> pools;
const std::tuple<> filter;
};