resource_handle: add ::use_count (close #727)

This commit is contained in:
Michele Caini
2021-07-05 14:52:15 +02:00
parent baa4c44632
commit 4171b4ae47
2 changed files with 23 additions and 0 deletions

View File

@@ -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;
};

View File

@@ -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));