瀏覽代碼

Implementation of swap() added. As the specification of the

noexcept-value is missing in the document it is implemented as for
std::swap(). Fixes #73.
main
offa 7 年之前
父節點
當前提交
0b264e8f80
共有 2 個文件被更改,包括 30 次插入0 次删除
  1. +16
    -0
      include/unique_resource.h
  2. +14
    -0
      test/UniqueResourceTest.cpp

+ 16
- 0
include/unique_resource.h 查看文件

@@ -37,6 +37,12 @@ namespace sr
constexpr auto is_nothrow_move_or_copy_constructible_from_v = is_ntmocp_constructible<T, TT>::value;


template<class T>
constexpr auto is_nothrow_swappable_v = std::is_nothrow_move_constructible<T>::value
&& std::is_nothrow_move_assignable<T>::value;



template<class T, class U = std::conditional_t<std::is_nothrow_move_constructible<T>::value, T&&, const T&>>
constexpr U forward_if_nothrow_move_constructible(T&& value) noexcept
{
@@ -162,6 +168,16 @@ namespace sr
}


void swap(unique_resource& other) noexcept(is_nothrow_swappable_v<R>
&& is_nothrow_swappable_v<D>
&& is_nothrow_swappable_v<bool>)
{
using std::swap;
swap(m_resource.get(), other.m_resource.get());
swap(m_deleter.get(), other.m_deleter.get());
swap(m_execute_on_destruction, other.m_execute_on_destruction);
}

void reset()
{
if( m_execute_on_destruction == true )

+ 14
- 0
test/UniqueResourceTest.cpp 查看文件

@@ -283,6 +283,20 @@ TEST_CASE("deleter access", "[UniqueResource]")
}
}

TEST_CASE("swap", "[UniqueResource]")
{
REQUIRE_CALL(m, deleter(7));
auto guard1 = sr::make_unique_resource(Handle{3}, deleter);

{
REQUIRE_CALL(m, deleter(3));
auto guard2 = sr::make_unique_resource(Handle{7}, deleter);
guard2.swap(guard1);
REQUIRE(guard1.get() == 7);
REQUIRE(guard2.get() == 3);
}
}

TEST_CASE("make unique resource", "[UniqueResource]")
{
REQUIRE_CALL(m, deleter(7));

Loading…
取消
儲存