Browse Source

sparse_set: public ::policy method for opaque runtime detection

Michele Caini 4 years ago
parent
commit
cdf67a1421
2 changed files with 17 additions and 5 deletions
  1. 13 5
      src/entt/entity/sparse_set.hpp
  2. 4 0
      test/entt/entity/sparse_set.cpp

+ 13 - 5
src/entt/entity/sparse_set.hpp

@@ -330,7 +330,7 @@ public:
           count{0u},
           reserved{0u},
           free_list{tombstone},
-          policy{pol}
+          mode{pol}
     {}
 
     /**
@@ -354,7 +354,7 @@ public:
           count{std::exchange(other.count, 0u)},
           reserved{std::exchange(other.reserved, 0u)},
           free_list{std::exchange(other.free_list, tombstone)},
-          policy{other.policy}
+          mode{other.mode}
     {}
 
     /*! @brief Default destructor. */
@@ -378,11 +378,19 @@ public:
         count = std::exchange(other.count, 0u);
         reserved = std::exchange(other.reserved, 0u);
         free_list = std::exchange(other.free_list, tombstone);
-        policy = other.policy;
+        mode = other.mode;
 
         return *this;
     }
 
+    /**
+     * @brief Returns the deletion policy of a sparse set.
+     * @return The deletion policy of the sparse set.
+     */
+    deletion_policy policy() const ENTT_NOEXCEPT {
+        return mode;
+    }
+
     /**
      * @brief Increases the capacity of a sparse set.
      *
@@ -630,7 +638,7 @@ public:
      */
     void erase(const entity_type entt, void *ud = nullptr) {
         ENTT_ASSERT(contains(entt), "Set does not contain entity");
-        (policy == deletion_policy::in_place) ? stable_erase(entt, ud) : unstable_erase(entt, ud);
+        (mode == deletion_policy::in_place) ? stable_erase(entt, ud) : unstable_erase(entt, ud);
     }
 
     /**
@@ -854,7 +862,7 @@ private:
     std::size_t count;
     std::size_t reserved;
     entity_type free_list;
-    deletion_policy policy;
+    deletion_policy mode;
 };
 
 

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

@@ -222,6 +222,7 @@ TEST(SparseSet, Erase) {
     entities[1u] = entt::entity{42};
     entities[2u] = entt::entity{9};
 
+    ASSERT_EQ(set.policy(), entt::deletion_policy::swap_and_pop);
     ASSERT_TRUE(set.empty());
 
     ASSERT_DEATH(set.erase(std::begin(entities), std::end(entities)), "");
@@ -261,6 +262,7 @@ TEST(SparseSet, StableErase) {
     entities[1u] = entt::entity{42};
     entities[2u] = entt::entity{9};
 
+    ASSERT_EQ(set.policy(), entt::deletion_policy::in_place);
     ASSERT_TRUE(set.empty());
     ASSERT_EQ(set.size(), 0u);
 
@@ -350,6 +352,7 @@ TEST(SparseSet, Remove) {
     entities[1u] = entt::entity{42};
     entities[2u] = entt::entity{9};
 
+    ASSERT_EQ(set.policy(), entt::deletion_policy::swap_and_pop);
     ASSERT_TRUE(set.empty());
 
     ASSERT_EQ(set.remove(std::begin(entities), std::end(entities)), 0u);
@@ -393,6 +396,7 @@ TEST(SparseSet, StableRemove) {
     entities[1u] = entt::entity{42};
     entities[2u] = entt::entity{9};
 
+    ASSERT_EQ(set.policy(), entt::deletion_policy::in_place);
     ASSERT_TRUE(set.empty());
     ASSERT_EQ(set.size(), 0u);