Browse Source

sparse_set: added opaque payload (mainly meant to attach owners for augmented storage mixins)

Michele Caini 5 years ago
parent
commit
0eb6d7de3c
2 changed files with 26 additions and 0 deletions
  1. 18 0
      src/entt/entity/sparse_set.hpp
  2. 8 0
      test/entt/entity/sparse_set.cpp

+ 18 - 0
src/entt/entity/sparse_set.hpp

@@ -588,9 +588,27 @@ public:
         remove(begin(), end());
     }
 
+    /**
+     * @brief Returns the opaque payload associated with the sparse set, if any.
+     * @return The opaque payload associated with the sparse set, if any.
+     */
+    void * payload() const ENTT_NOEXCEPT {
+        return user_data;
+    }
+
+    /**
+     * @brief Set an opaque payload, typically used to attach information that
+     * are useful to storage mixins.
+     * @param ud Opaque payload, a sparse set won't use this data in any case.
+     */
+    void payload(void *ud) ENTT_NOEXCEPT {
+        user_data = ud;
+    }
+
 private:
     std::vector<page_type> sparse;
     std::vector<entity_type> packed;
+    void *user_data{};
 };
 
 

+ 8 - 0
test/entt/entity/sparse_set.cpp

@@ -14,6 +14,12 @@ struct boxed_int { int value; };
 TEST(SparseSet, Functionalities) {
     entt::sparse_set set;
 
+    ASSERT_EQ(set.payload(), nullptr);
+
+    set.payload(&set);
+
+    ASSERT_EQ(set.payload(), &set);
+
     set.reserve(42);
 
     ASSERT_EQ(set.capacity(), 42u);
@@ -78,6 +84,8 @@ TEST(SparseSet, Functionalities) {
     ASSERT_EQ(other.begin(), other.end());
     ASSERT_FALSE(other.contains(entt::entity{0}));
     ASSERT_FALSE(other.contains(entt::entity{42}));
+
+    ASSERT_EQ(set.payload(), &set);
 }
 
 TEST(SparseSet, Pagination) {