From 902dc49de2ee33e4822ef88aa62c056a2fc641f9 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Thu, 5 Nov 2020 14:29:45 +0100 Subject: [PATCH] storage: added .get_as_tuple(entity) --- src/entt/entity/storage.hpp | 46 ++++++++++++++++++++++++++++++++++-- test/entt/entity/storage.cpp | 5 ++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/entt/entity/storage.hpp b/src/entt/entity/storage.hpp index 11d6f8527..e970f8dcd 100644 --- a/src/entt/entity/storage.hpp +++ b/src/entt/entity/storage.hpp @@ -3,11 +3,12 @@ #include +#include #include +#include +#include #include #include -#include -#include #include "../config/config.h" #include "../core/algorithm.hpp" #include "../core/type_traits.hpp" @@ -339,6 +340,26 @@ public: return const_cast(std::as_const(*this).get(entt)); } + /** + * @brief Returns the object associated with an entity as a tuple suitable + * for merging in a multi-type get. + * + * @warning + * Attempting to use an entity that doesn't belong to the storage results in + * undefined behavior. + * + * @param entt A valid entity identifier. + * @return The object associated with the entity as a tuple. + */ + [[nodiscard]] std::tuple get_as_tuple(const entity_type entt) const { + return { instances[underlying_type::index(entt)] }; + } + + /*! @copydoc get_as_tuple */ + [[nodiscard]] std::tuple get_as_tuple(const entity_type entt) { + return { instances[underlying_type::index(entt)] }; + } + /** * @brief Assigns an entity to a storage and constructs its object. * @@ -489,6 +510,27 @@ public: /*! @brief Unsigned integer type. */ using size_type = std::size_t; + /** + * @brief Returns the object associated with an entity as a tuple suitable + * for merging in a multi-type get. + * + * @warning + * Attempting to use an entity that doesn't belong to the storage results in + * undefined behavior. + * + * @param entt A valid entity identifier. + * @return The object associated with the entity as a tuple. + */ + [[nodiscard]] std::tuple<> get_as_tuple([[maybe_unused]] const entity_type entt) const { + ENTT_ASSERT(contains(entt)); + return {}; + } + + /*! @copydoc get_as_tuple */ + [[nodiscard]] std::tuple<> get_as_tuple(const entity_type entt) { + return std::as_const(*this).get_as_tuple(entt); + } + /** * @brief Assigns an entity to a storage and constructs its object. * diff --git a/test/entt/entity/storage.cpp b/test/entt/entity/storage.cpp index ff5fc958e..7abf1ac63 100644 --- a/test/entt/entity/storage.cpp +++ b/test/entt/entity/storage.cpp @@ -46,6 +46,8 @@ TEST(Storage, Functionalities) { ASSERT_FALSE(pool.contains(entt::entity{0})); ASSERT_TRUE(pool.contains(entt::entity{41})); ASSERT_EQ(pool.get(entt::entity{41}), 3); + ASSERT_EQ(pool.get_as_tuple(entt::entity{41}), std::make_tuple(3)); + ASSERT_EQ(std::as_const(pool).get_as_tuple(entt::entity{41}), std::make_tuple(3)); pool.remove(entt::entity{41}); @@ -59,6 +61,8 @@ TEST(Storage, Functionalities) { pool.emplace(entt::entity{41}, 12); ASSERT_EQ(pool.get(entt::entity{41}), 12); + ASSERT_EQ(pool.get_as_tuple(entt::entity{41}), std::make_tuple(12)); + ASSERT_EQ(std::as_const(pool).get_as_tuple(entt::entity{41}), std::make_tuple(12)); pool.clear(); @@ -85,6 +89,7 @@ TEST(Storage, EmptyType) { pool.emplace(entt::entity{99}); ASSERT_TRUE(pool.contains(entt::entity{99})); + ASSERT_EQ(pool.get_as_tuple(entt::entity{99}), std::make_tuple()); } TEST(Storage, Insert) {