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



void reset(T&& newValue) noexcept(std::is_nothrow_assignable_v<T, decltype(std::move_if_noexcept(newValue))>) 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&>) void reset(const T& newValue) noexcept(std::is_nothrow_assignable_v<T, const T&>)

+ 13
- 2
include/unique_resource.h View File

template<class RR> template<class RR>
void reset(RR&& r) void reset(RR&& r)
{ {
auto se = scope_exit{[this, &r] { get_deleter()(r); }};
reset(); 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; m_execute_on_destruction = true;
se.release(); se.release();
} }

+ 10
- 1
test/UniqueResourceTest.cpp View File

guard.reset(); 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(3));
REQUIRE_CALL(m, deleter(7)); REQUIRE_CALL(m, deleter(7));
guard.reset(Handle{7}); 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]") TEST_CASE("reset handles exception on assignment", "[UniqueResource]")
{ {
REQUIRE_CALL(m, deleter(3)); REQUIRE_CALL(m, deleter(3));

Loading…
Cancel
Save