resource: add ::reset functions (with tests)

This commit is contained in:
Michele Caini
2024-04-12 11:17:57 +02:00
parent 0b6e325109
commit aa178dd2cf
2 changed files with 25 additions and 0 deletions

View File

@@ -148,6 +148,19 @@ public:
return static_cast<bool>(value);
}
/*! @brief Releases the ownership of the managed resource. */
void reset() {
value.reset();
}
/**
* @brief Replaces the managed resource.
* @param other A handle to a resource.
*/
void reset(handle_type other) {
value = std::move(other);
}
/**
* @brief Returns the underlying resource handle.
* @return The underlying resource handle.

View File

@@ -56,6 +56,18 @@ TEST(Resource, Functionalities) {
ASSERT_TRUE(copy);
ASSERT_TRUE(move);
ASSERT_EQ(copy, move);
copy.reset(std::make_shared<derived>());
ASSERT_TRUE(copy);
ASSERT_TRUE(move);
ASSERT_NE(copy, move);
move.reset();
ASSERT_TRUE(copy);
ASSERT_FALSE(move);
ASSERT_NE(copy, move);
}
TEST(Resource, DerivedToBase) {