Ver Fonte

storage: added .get_as_tuple(entity)

Michele Caini há 5 anos atrás
pai
commit
902dc49de2
2 ficheiros alterados com 49 adições e 2 exclusões
  1. 44 2
      src/entt/entity/storage.hpp
  2. 5 0
      test/entt/entity/storage.cpp

+ 44 - 2
src/entt/entity/storage.hpp

@@ -3,11 +3,12 @@
 
 
 #include <algorithm>
+#include <cstddef>
 #include <iterator>
+#include <tuple>
+#include <type_traits>
 #include <utility>
 #include <vector>
-#include <cstddef>
-#include <type_traits>
 #include "../config/config.h"
 #include "../core/algorithm.hpp"
 #include "../core/type_traits.hpp"
@@ -339,6 +340,26 @@ public:
         return const_cast<value_type &>(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<const value_type &> get_as_tuple(const entity_type entt) const {
+        return { instances[underlying_type::index(entt)] };
+    }
+
+    /*! @copydoc get_as_tuple */
+    [[nodiscard]] std::tuple<value_type &> 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.
      *

+ 5 - 0
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) {