浏览代码

Factory fucntion make_unique_resource_checked() added.

main
offa 7 年前
父节点
当前提交
027a53b1a3
共有 2 个文件被更改,包括 31 次插入1 次删除
  1. +18
    -1
      include/unique_resource.h
  2. +13
    -0
      test/UniqueResourceTest.cpp

+ 18
- 1
include/unique_resource.h 查看文件

@@ -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 查看文件

@@ -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);
}


正在加载...
取消
保存