Bladeren bron

Factory fucntion make_unique_resource_checked() added.

main
offa 7 jaren geleden
bovenliggende
commit
027a53b1a3
2 gewijzigde bestanden met toevoegingen van 31 en 1 verwijderingen
  1. +18
    -1
      include/unique_resource.h
  2. +13
    -0
      test/UniqueResourceTest.cpp

+ 18
- 1
include/unique_resource.h Bestand weergeven

@@ -199,6 +199,7 @@ namespace sr
};



template<class R, class D>
unique_resource<std::decay_t<R>, std::decay_t<D>> make_unique_resource(R&& r, D&& d)
noexcept(std::is_nothrow_constructible<std::decay_t<R>, R>::value
@@ -209,11 +210,27 @@ namespace sr

template<class R, class D>
unique_resource<R&, std::decay_t<D>> make_unique_resource(std::reference_wrapper<R> r, D d)
noexcept(std::is_nothrow_constructible<std::decay_t<D>, D>::value)
noexcept(std::is_nothrow_constructible<std::decay_t<D>, D>::value)
{
return unique_resource<R&, std::decay_t<D>>(r.get(), std::forward<D>(d));
}

template<class R, class D, class S = R>
unique_resource<std::decay_t<R>, std::decay_t<D>> make_unique_resource_checked(R&& r, const S& invalid, D&& d)
noexcept(std::is_nothrow_constructible<std::decay_t<R>, R>::value
&& std::is_nothrow_constructible<std::decay_t<D>, D>::value)
{
const bool mustRelease{r == invalid};
auto ur = make_unique_resource(r, d);

if( mustRelease == true )
{
ur.release();
}

return ur;
}

}



+ 13
- 0
test/UniqueResourceTest.cpp Bestand weergeven

@@ -273,3 +273,16 @@ TEST_CASE("make unique resource with reference wrapper", "[UniqueResource]")
static_cast<void>(guard);
}

TEST_CASE("make unique resource checked", "[UniqueResource]")
{
REQUIRE_CALL(m, deleter(4));
auto guard = sr::make_unique_resource_checked(Handle{4}, Handle{-1}, deleter);
static_cast<void>(guard);
}

TEST_CASE("make unique resource checked releases if invalid", "[UniqueResource]")
{
auto guard = sr::make_unique_resource_checked(Handle{-1}, Handle{-1}, deleter);
static_cast<void>(guard);
}


Laden…
Annuleren
Opslaan