* Removed free function get_as_tuple
* Added get_as_tuple function at storage level
This commit is contained in:
Michele Caini
2021-07-19 15:22:13 +02:00
parent ae60266477
commit 2d92d8ee67
3 changed files with 43 additions and 30 deletions

View File

@@ -99,7 +99,7 @@ class basic_group<Entity, exclude_t<Exclude...>, get_t<Get...>> final {
[[nodiscard]] reference operator*() const ENTT_NOEXCEPT {
const auto entt = *it;
return std::tuple_cat(std::make_tuple(entt), get_as_tuple(*std::get<storage_type<Get> *>(pools), entt)...);
return std::tuple_cat(std::make_tuple(entt), std::get<storage_type<Get> *>(pools)->get_as_tuple(entt)...);
}
[[nodiscard]] bool operator==(const iterable_iterator &other) const ENTT_NOEXCEPT {
@@ -341,11 +341,11 @@ public:
ENTT_ASSERT(contains(entt), "Group does not contain entity");
if constexpr(sizeof...(Component) == 0) {
return std::tuple_cat(get_as_tuple(*std::get<storage_type<Get> *>(pools), entt)...);
return std::tuple_cat(std::get<storage_type<Get> *>(pools)->get_as_tuple(entt)...);
} else if constexpr(sizeof...(Component) == 1) {
return (std::get<storage_type<Component> *>(pools)->get(entt), ...);
} else {
return std::tuple_cat(get_as_tuple(*std::get<storage_type<Component> *>(pools), entt)...);
return std::tuple_cat(std::get<storage_type<Component> *>(pools)->get_as_tuple(entt)...);
}
}
@@ -570,7 +570,7 @@ class basic_group<Entity, exclude_t<Exclude...>, get_t<Get...>, Owned...> final
return std::tuple_cat(
std::make_tuple(*it),
std::forward_as_tuple(*std::get<OIt>(owned)...),
get_as_tuple(*std::get<storage_type<Get> *>(get), *it)...
std::get<storage_type<Get> *>(get)->get_as_tuple(*it)...
);
}
@@ -836,11 +836,11 @@ public:
ENTT_ASSERT(contains(entt), "Group does not contain entity");
if constexpr(sizeof...(Component) == 0) {
return std::tuple_cat(get_as_tuple(*std::get<storage_type<Owned> *>(pools), entt)..., get_as_tuple(*std::get<storage_type<Get> *>(pools), entt)...);
return std::tuple_cat(std::get<storage_type<Owned> *>(pools)->get_as_tuple(entt)..., std::get<storage_type<Get> *>(pools)->get_as_tuple(entt)...);
} else if constexpr(sizeof...(Component) == 1) {
return (std::get<storage_type<Component> *>(pools)->get(entt), ...);
} else {
return std::tuple_cat(get_as_tuple(*std::get<storage_type<Component> *>(pools), entt)...);
return std::tuple_cat(std::get<storage_type<Component> *>(pools)->get_as_tuple(entt)...);
}
}

View File

@@ -553,6 +553,23 @@ public:
return const_cast<value_type &>(std::as_const(*this).get(entt));
}
/**
* @brief Returns the object assigned to an entity as a tuple.
*
* @sa get
*
* @param entt A valid entity identifier.
* @return The object assigned to the entity as a tuple.
*/
[[nodiscard]] std::tuple<const value_type &> get_as_tuple(const entity_type entt) const ENTT_NOEXCEPT {
return std::forward_as_tuple(get(entt));
}
/*! @copydoc get_as_tuple */
[[nodiscard]] std::tuple<value_type &> get_as_tuple(const entity_type entt) ENTT_NOEXCEPT {
return std::forward_as_tuple(get(entt));
}
/**
* @brief Assigns an entity to a storage and constructs its object.
*
@@ -700,6 +717,21 @@ public:
return allocator_type{underlying_type::get_allocator()};
}
/**
* @brief Returns an empty tuple.
*
* @warning
* Attempting to use an entity that doesn't belong to the storage results in
* undefined behavior.
*
* @param entt A valid entity identifier.
* @return Returns an empty tuple.
*/
[[nodiscard]] std::tuple<> get_as_tuple([[maybe_unused]] const entity_type entt) const ENTT_NOEXCEPT {
ENTT_ASSERT(underlying_type::contains(entt), "Storage does not contain entity");
return std::tuple{};
}
/**
* @brief Assigns an entity to a storage and constructs its object.
*
@@ -975,25 +1007,6 @@ struct storage_traits {
};
/**
* @brief Gets the element assigned to an entity from a storage, if any.
* @tparam Type Storage type.
* @param container A valid instance of a storage class.
* @param entt A valid entity identifier.
* @return A possibly empty tuple containing the requested element.
*/
template<typename Type>
[[nodiscard]] auto get_as_tuple([[maybe_unused]] Type &container, [[maybe_unused]] const typename Type::entity_type entt) {
static_assert(std::is_same_v<std::remove_const_t<Type>, typename storage_traits<typename Type::entity_type, typename Type::value_type>::storage_type>, "Invalid storage");
if constexpr(ignore_as_empty_v<typename Type::value_type>) {
return std::make_tuple();
} else {
return std::forward_as_tuple(container.get(entt));
}
}
}

View File

@@ -38,7 +38,7 @@ class iterable_storage final {
template<typename... It>
struct iterable_storage_iterator final {
using difference_type = std::ptrdiff_t;
using value_type = decltype(std::tuple_cat(std::tuple<Entity>{}, std::declval<decltype(get_as_tuple(std::declval<storage_type &>(), {}))>()));
using value_type = decltype(std::tuple_cat(std::tuple<Entity>{}, std::declval<decltype(std::declval<storage_type &>().get_as_tuple({}))>()));
using pointer = void;
using reference = value_type;
using iterator_category = std::input_iterator_tag;
@@ -366,7 +366,7 @@ class basic_view_impl<Policy, Entity, exclude_t<Exclude...>, Component...> {
if constexpr(std::is_same_v<Comp, Other>) {
return std::forward_as_tuple(std::get<Args>(curr)...);
} else {
return get_as_tuple(*std::get<storage_type<Other> *>(pools), std::get<0>(curr));
return std::get<storage_type<Other> *>(pools)->get_as_tuple(std::get<0>(curr));
}
}
@@ -553,11 +553,11 @@ public:
ENTT_ASSERT(contains(entt), "View does not contain entity");
if constexpr(sizeof...(Comp) == 0) {
return std::tuple_cat(get_as_tuple(*std::get<storage_type<Component> *>(pools), entt)...);
return std::tuple_cat(std::get<storage_type<Component> *>(pools)->get_as_tuple(entt)...);
} else if constexpr(sizeof...(Comp) == 1) {
return (std::get<storage_type<Comp> *>(pools)->get(entt), ...);
} else {
return std::tuple_cat(get_as_tuple(*std::get<storage_type<Comp> *>(pools), entt)...);
return std::tuple_cat(std::get<storage_type<Comp> *>(pools)->get_as_tuple(entt)...);
}
}
@@ -890,7 +890,7 @@ public:
ENTT_ASSERT(contains(entt), "View does not contain entity");
if constexpr(sizeof...(Comp) == 0) {
return get_as_tuple(*std::get<0>(pools), entt);
return std::get<0>(pools)->get_as_tuple(entt);
} else {
static_assert(std::is_same_v<Comp..., Component>, "Invalid component type");
return std::get<0>(pools)->get(entt);