registry: deduce group handler type from group type

This commit is contained in:
Michele Caini
2023-03-10 09:57:23 +01:00
parent 19c4857ef1
commit 1e61204e80
2 changed files with 32 additions and 22 deletions

View File

@@ -122,9 +122,10 @@ struct group_handler<owned_t<Owned...>, get_t<Get...>, exclude_t<Exclude...>> {
}
}
std::size_t current{};
private:
std::tuple<Owned *..., Get *..., Exclude *...> pools;
std::size_t current{};
};
template<typename... Get, typename... Exclude>
@@ -216,10 +217,12 @@ public:
using reverse_iterator = typename base_type::reverse_iterator;
/*! @brief Iterable group type. */
using iterable = iterable_adaptor<internal::extended_group_iterator<iterator, owned_t<>, get_t<Get...>>>;
/*! @brief Group handler type. */
using handler = internal::group_handler<owned_t<>, get_t<std::remove_const_t<Get>...>, exclude_t<std::remove_const_t<Exclude>...>>;
/*! @brief Default constructor to use to create empty, invalid groups. */
basic_group() noexcept
: handler{} {}
: set{} {}
/**
* @brief Constructs a group from a set of storage classes.
@@ -228,7 +231,7 @@ public:
* @param epool The storage for the types used to filter the group.
*/
basic_group(basic_common_type &ref, Get &...gpool, Exclude &...epool) noexcept
: handler{&ref},
: set{&ref},
pools{&gpool...},
filter{&epool...} {}
@@ -237,7 +240,7 @@ public:
* @return A const reference to the underlying handler.
*/
[[nodiscard]] const base_type &handle() const noexcept {
return *handler;
return *set;
}
/**
@@ -271,7 +274,7 @@ public:
* @return Number of entities that are part of the group.
*/
[[nodiscard]] size_type size() const noexcept {
return *this ? handler->size() : size_type{};
return *this ? set->size() : size_type{};
}
/**
@@ -280,13 +283,13 @@ public:
* @return Capacity of the group.
*/
[[nodiscard]] size_type capacity() const noexcept {
return *this ? handler->capacity() : size_type{};
return *this ? set->capacity() : size_type{};
}
/*! @brief Requests the removal of unused capacity. */
void shrink_to_fit() {
if(*this) {
handler->shrink_to_fit();
set->shrink_to_fit();
}
}
@@ -295,7 +298,7 @@ public:
* @return True if the group is empty, false otherwise.
*/
[[nodiscard]] bool empty() const noexcept {
return !*this || handler->empty();
return !*this || set->empty();
}
/**
@@ -307,7 +310,7 @@ public:
* @return An iterator to the first entity of the group.
*/
[[nodiscard]] iterator begin() const noexcept {
return *this ? handler->begin() : iterator{};
return *this ? set->begin() : iterator{};
}
/**
@@ -321,7 +324,7 @@ public:
* group.
*/
[[nodiscard]] iterator end() const noexcept {
return *this ? handler->end() : iterator{};
return *this ? set->end() : iterator{};
}
/**
@@ -333,7 +336,7 @@ public:
* @return An iterator to the first entity of the reversed group.
*/
[[nodiscard]] reverse_iterator rbegin() const noexcept {
return *this ? handler->rbegin() : reverse_iterator{};
return *this ? set->rbegin() : reverse_iterator{};
}
/**
@@ -348,7 +351,7 @@ public:
* reversed group.
*/
[[nodiscard]] reverse_iterator rend() const noexcept {
return *this ? handler->rend() : reverse_iterator{};
return *this ? set->rend() : reverse_iterator{};
}
/**
@@ -378,7 +381,7 @@ public:
* iterator otherwise.
*/
[[nodiscard]] iterator find(const entity_type entt) const noexcept {
const auto it = *this ? handler->find(entt) : iterator{};
const auto it = *this ? set->find(entt) : iterator{};
return it != end() && *it == entt ? it : end();
}
@@ -396,7 +399,7 @@ public:
* @return True if the group is properly initialized, false otherwise.
*/
[[nodiscard]] explicit operator bool() const noexcept {
return handler != nullptr;
return set != nullptr;
}
/**
@@ -405,7 +408,7 @@ public:
* @return True if the group contains the given entity, false otherwise.
*/
[[nodiscard]] bool contains(const entity_type entt) const noexcept {
return *this && handler->contains(entt);
return *this && set->contains(entt);
}
/**
@@ -525,7 +528,7 @@ public:
if(*this) {
if constexpr(sizeof...(Type) == 0) {
static_assert(std::is_invocable_v<Compare, const entity_type, const entity_type>, "Invalid comparison function");
handler->sort(std::move(compare), std::move(algo), std::forward<Args>(args)...);
set->sort(std::move(compare), std::move(algo), std::forward<Args>(args)...);
} else {
auto comp = [this, &compare](const entity_type lhs, const entity_type rhs) {
if constexpr(sizeof...(Type) == 1) {
@@ -535,7 +538,7 @@ public:
}
};
handler->sort(std::move(comp), std::move(algo), std::forward<Args>(args)...);
set->sort(std::move(comp), std::move(algo), std::forward<Args>(args)...);
}
}
}
@@ -559,12 +562,12 @@ public:
template<typename Type>
void sort() const {
if(*this) {
handler->respect(*std::get<index_of<Type>>(pools));
set->respect(*std::get<index_of<Type>>(pools));
}
}
private:
base_type *handler;
base_type *set;
std::tuple<Get *...> pools;
std::tuple<Exclude *...> filter;
};
@@ -621,6 +624,8 @@ public:
using reverse_iterator = typename base_type::reverse_iterator;
/*! @brief Iterable group type. */
using iterable = iterable_adaptor<internal::extended_group_iterator<iterator, owned_t<Owned...>, get_t<Get...>>>;
/*! @brief Group handler type. */
using handler = internal::group_handler<owned_t<std::remove_const_t<Owned>...>, get_t<std::remove_const_t<Get>...>, exclude_t<std::remove_const_t<Exclude>...>>;
/*! @brief Default constructor to use to create empty, invalid groups. */
basic_group() noexcept

View File

@@ -1209,7 +1209,9 @@ public:
template<typename Owned, typename... Other, typename... Get, typename... Exclude>
[[nodiscard]] basic_group<owned_t<storage_for_type<Owned>, storage_for_type<Other>...>, get_t<storage_for_type<Get>...>, exclude_t<storage_for_type<Exclude>...>>
group(get_t<Get...> = {}, exclude_t<Exclude...> = {}) {
using handler_type = internal::group_handler<owned_t<storage_for_type<std::remove_const_t<Owned>>, storage_for_type<std::remove_const_t<Other>>...>, get_t<storage_for_type<std::remove_const_t<Get>>...>, exclude_t<storage_for_type<std::remove_const_t<Exclude>>...>>;
using group_type = basic_group<owned_t<storage_for_type<Owned>, storage_for_type<Other>...>, get_t<storage_for_type<Get>...>, exclude_t<storage_for_type<Exclude>...>>;
using handler_type = typename group_type::handler;
constexpr auto size = 1u + sizeof...(Other) + sizeof...(Get) + sizeof...(Exclude);
handler_type *handler = nullptr;
@@ -1287,7 +1289,9 @@ public:
template<typename Get, typename... Other, typename... Exclude>
[[nodiscard]] basic_group<owned_t<>, get_t<storage_for_type<Get>, storage_for_type<Other>...>, exclude_t<storage_for_type<Exclude>...>>
group(get_t<Get, Other...>, exclude_t<Exclude...> = {}) {
using handler_type = internal::group_handler<owned_t<>, get_t<storage_for_type<std::remove_const_t<Get>>, storage_for_type<std::remove_const_t<Other>>...>, exclude_t<storage_for_type<std::remove_const_t<Exclude>>...>>;
using group_type = basic_group<owned_t<>, get_t<storage_for_type<Get>, storage_for_type<Other>...>, exclude_t<storage_for_type<Exclude>...>>;
using handler_type = typename group_type::handler;
constexpr auto size = 1u + sizeof...(Other) + sizeof...(Exclude);
handler_type *handler = nullptr;
@@ -1342,7 +1346,8 @@ public:
if(it == groups.cend()) {
return {};
} else {
using handler_type = internal::group_handler<owned_t<storage_for_type<std::remove_const_t<Owned>>...>, get_t<storage_for_type<std::remove_const_t<Get>>...>, exclude_t<storage_for_type<std::remove_const_t<Exclude>>...>>;
using group_type = basic_group<owned_t<storage_for_type<const Owned>...>, get_t<storage_for_type<const Get>...>, exclude_t<storage_for_type<const Exclude>...>>;
using handler_type = typename group_type::handler;
if constexpr(sizeof...(Owned) == 0u) {
return {*static_cast<handler_type *>(it->handler.get()), assure<std::remove_const_t<Owned>>()..., assure<std::remove_const_t<Get>>()..., assure<std::remove_const_t<Exclude>>()...};