Browse Source

Implementation of unique_resource reset() updated (fixes #99).

main
offa 7 years ago
parent
commit
816573778d
3 changed files with 24 additions and 4 deletions
  1. +1
    -1
      include/detail/wrapper.h
  2. +13
    -2
      include/unique_resource.h
  3. +10
    -1
      test/UniqueResourceTest.cpp

+ 1
- 1
include/detail/wrapper.h View File

@@ -50,7 +50,7 @@ namespace sr::detail

void reset(T&& newValue) noexcept(std::is_nothrow_assignable_v<T, decltype(std::move_if_noexcept(newValue))>)
{
m_value = std::move_if_noexcept(newValue);
m_value = std::forward<T>(newValue);
}

void reset(const T& newValue) noexcept(std::is_nothrow_assignable_v<T, const T&>)

+ 13
- 2
include/unique_resource.h View File

@@ -100,9 +100,20 @@ namespace sr
template<class RR>
void reset(RR&& r)
{
auto se = scope_exit{[this, &r] { get_deleter()(r); }};
reset();
m_resource.reset(std::forward<RR>(r));

using R1 = typename detail::Wrapper<R>::type;
auto se = scope_exit{[this, &r] { get_deleter()(r); }};

if constexpr( std::is_nothrow_assignable_v<R1&, RR> == true )
{
m_resource.reset(std::forward<RR>(r));
}
else
{
m_resource = std::as_const(r);
}

m_execute_on_destruction = true;
se.release();
}

+ 10
- 1
test/UniqueResourceTest.cpp View File

@@ -120,7 +120,7 @@ TEST_CASE("reset does not call deleter if released", "[UniqueResource]")
guard.reset();
}

TEST_CASE("reset sets new value and calls deleter on previous", "[UniqueResource]")
TEST_CASE("reset sets new rvalue and calls deleter on previous", "[UniqueResource]")
{
REQUIRE_CALL(m, deleter(3));
REQUIRE_CALL(m, deleter(7));
@@ -128,6 +128,15 @@ TEST_CASE("reset sets new value and calls deleter on previous", "[UniqueResource
guard.reset(Handle{7});
}

TEST_CASE("reset sets new lvalue and calls deleter on previous", "[UniqueResource]")
{
REQUIRE_CALL(m, deleter(3));
REQUIRE_CALL(m, deleter(7));
auto guard = sr::unique_resource{Handle{3}, deleter};
const Handle h{7};
guard.reset(h);
}

TEST_CASE("reset handles exception on assignment", "[UniqueResource]")
{
REQUIRE_CALL(m, deleter(3));

Loading…
Cancel
Save