From 2d92d8ee67ee5beb35ec81342804c8c40167df52 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Mon, 19 Jul 2021 15:22:13 +0200 Subject: [PATCH] storage: * Removed free function get_as_tuple * Added get_as_tuple function at storage level --- src/entt/entity/group.hpp | 12 ++++----- src/entt/entity/storage.hpp | 51 +++++++++++++++++++++++-------------- src/entt/entity/view.hpp | 10 ++++---- 3 files changed, 43 insertions(+), 30 deletions(-) diff --git a/src/entt/entity/group.hpp b/src/entt/entity/group.hpp index ed3540bdf..fae351c9d 100644 --- a/src/entt/entity/group.hpp +++ b/src/entt/entity/group.hpp @@ -99,7 +99,7 @@ class basic_group, get_t> final { [[nodiscard]] reference operator*() const ENTT_NOEXCEPT { const auto entt = *it; - return std::tuple_cat(std::make_tuple(entt), get_as_tuple(*std::get *>(pools), entt)...); + return std::tuple_cat(std::make_tuple(entt), std::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 *>(pools), entt)...); + return std::tuple_cat(std::get *>(pools)->get_as_tuple(entt)...); } else if constexpr(sizeof...(Component) == 1) { return (std::get *>(pools)->get(entt), ...); } else { - return std::tuple_cat(get_as_tuple(*std::get *>(pools), entt)...); + return std::tuple_cat(std::get *>(pools)->get_as_tuple(entt)...); } } @@ -570,7 +570,7 @@ class basic_group, get_t, Owned...> final return std::tuple_cat( std::make_tuple(*it), std::forward_as_tuple(*std::get(owned)...), - get_as_tuple(*std::get *>(get), *it)... + std::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 *>(pools), entt)..., get_as_tuple(*std::get *>(pools), entt)...); + return std::tuple_cat(std::get *>(pools)->get_as_tuple(entt)..., std::get *>(pools)->get_as_tuple(entt)...); } else if constexpr(sizeof...(Component) == 1) { return (std::get *>(pools)->get(entt), ...); } else { - return std::tuple_cat(get_as_tuple(*std::get *>(pools), entt)...); + return std::tuple_cat(std::get *>(pools)->get_as_tuple(entt)...); } } diff --git a/src/entt/entity/storage.hpp b/src/entt/entity/storage.hpp index 6b524021f..52eefc6b9 100644 --- a/src/entt/entity/storage.hpp +++ b/src/entt/entity/storage.hpp @@ -553,6 +553,23 @@ public: return const_cast(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 get_as_tuple(const entity_type entt) const ENTT_NOEXCEPT { + return std::forward_as_tuple(get(entt)); + } + + /*! @copydoc get_as_tuple */ + [[nodiscard]] std::tuple 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 -[[nodiscard]] auto get_as_tuple([[maybe_unused]] Type &container, [[maybe_unused]] const typename Type::entity_type entt) { - static_assert(std::is_same_v, typename storage_traits::storage_type>, "Invalid storage"); - - if constexpr(ignore_as_empty_v) { - return std::make_tuple(); - } else { - return std::forward_as_tuple(container.get(entt)); - } -} - - } diff --git a/src/entt/entity/view.hpp b/src/entt/entity/view.hpp index 495eb3d96..3c4c37104 100644 --- a/src/entt/entity/view.hpp +++ b/src/entt/entity/view.hpp @@ -38,7 +38,7 @@ class iterable_storage final { template struct iterable_storage_iterator final { using difference_type = std::ptrdiff_t; - using value_type = decltype(std::tuple_cat(std::tuple{}, std::declval(), {}))>())); + using value_type = decltype(std::tuple_cat(std::tuple{}, std::declval().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, Component...> { if constexpr(std::is_same_v) { return std::forward_as_tuple(std::get(curr)...); } else { - return get_as_tuple(*std::get *>(pools), std::get<0>(curr)); + return std::get *>(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 *>(pools), entt)...); + return std::tuple_cat(std::get *>(pools)->get_as_tuple(entt)...); } else if constexpr(sizeof...(Comp) == 1) { return (std::get *>(pools)->get(entt), ...); } else { - return std::tuple_cat(get_as_tuple(*std::get *>(pools), entt)...); + return std::tuple_cat(std::get *>(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, "Invalid component type"); return std::get<0>(pools)->get(entt);