resource: swap

This commit is contained in:
Michele Caini
2024-09-09 12:02:33 +02:00
parent 7ad5eb8c17
commit eabe7be7da
3 changed files with 34 additions and 1 deletions

1
TODO
View File

@@ -45,4 +45,3 @@ TODO:
* view and view iterator specializations for multi, single and filtered elements
* organizer support to groups
* meta range: move id to meta objects and return plain types (?), then remove id from meta base and meta ctor too
* resource: swap support

View File

@@ -116,6 +116,15 @@ public:
return *this;
}
/**
* @brief Exchanges the content with that of a given resource.
* @param other Resource to exchange the content with.
*/
void swap(resource &other) {
using std::swap;
swap(value, other.value);
}
/**
* @brief Returns a reference to the managed resource.
*

View File

@@ -70,6 +70,31 @@ TEST(Resource, Functionalities) {
ASSERT_NE(copy, move);
}
TEST(Resource, Swap) {
entt::resource<int> resource{};
entt::resource<int> other{};
ASSERT_FALSE(resource);
ASSERT_FALSE(other);
resource.swap(other);
ASSERT_FALSE(resource);
ASSERT_FALSE(other);
resource.reset(std::make_shared<int>(1));
ASSERT_TRUE(resource);
ASSERT_EQ(*resource, 1);
ASSERT_FALSE(other);
resource.swap(other);
ASSERT_FALSE(resource);
ASSERT_TRUE(other);
ASSERT_EQ(*other, 1);
}
TEST(Resource, DerivedToBase) {
const entt::resource<derived> resource{std::make_shared<derived>()};
entt::resource<base> other{resource};