Browse Source

resource_handle: add ::use_count (close #727)

Michele Caini 4 years ago
parent
commit
4171b4ae47
2 changed files with 23 additions and 0 deletions
  1. 11 0
      src/entt/resource/handle.hpp
  2. 12 0
      test/entt/resource/resource.cpp

+ 11 - 0
src/entt/resource/handle.hpp

@@ -31,6 +31,9 @@ class resource_handle {
     friend class resource_handle;
 
 public:
+    /*! @brief Unsigned integer type. */
+    using size_type = long;
+
     /*! @brief Default constructor. */
     resource_handle() ENTT_NOEXCEPT = default;
 
@@ -178,6 +181,14 @@ public:
         return static_cast<bool>(resource);
     }
 
+    /**
+     * @brief Returns the number of handles pointing the same resource.
+     * @return The number of handles pointing the same resource.
+     */
+    [[nodiscard]] size_type use_count() const ENTT_NOEXCEPT {
+        return resource.use_count();
+    }
+
 private:
     std::shared_ptr<Resource> resource;
 };

+ 12 - 0
test/entt/resource/resource.cpp

@@ -44,6 +44,18 @@ TEST(Resource, Functionalities) {
     ASSERT_TRUE(cache.load<loader<resource>>(hs1, 42));
     ASSERT_TRUE(cache.reload<loader<resource>>(hs1, 42));
 
+    ASSERT_EQ(cache.handle(hs1).use_count(), 2);
+
+    auto tmp = cache.handle(hs1);
+
+    ASSERT_EQ(cache.handle(hs1).use_count(), 3);
+    ASSERT_TRUE(static_cast<bool>(tmp));
+
+    tmp = {};
+
+    ASSERT_EQ(cache.handle(hs1).use_count(), 2);
+    ASSERT_FALSE(static_cast<bool>(tmp));
+
     ASSERT_NE(cache.size(), 0u);
     ASSERT_FALSE(cache.empty());
     ASSERT_TRUE(cache.contains(hs1));