瀏覽代碼

Reset and get implemented.

main
offa 7 年之前
父節點
當前提交
7988145317
共有 2 個文件被更改,包括 49 次插入0 次删除
  1. +11
    -0
      include/unique_resource.h
  2. +38
    -0
      test/UniqueResourceTest.cpp

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

@@ -74,6 +74,17 @@ namespace sr
return m_resource;
}

void reset(Ressource&& res) noexcept
{
invoke(invoke_it::again);
m_resource = std::move(res);
}

const Ressource& get() const noexcept
{
return m_resource;
}


unique_resource_t& operator=(unique_resource_t&& other) noexcept
{

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

@@ -211,3 +211,41 @@ TEST_CASE("invoke does not propagate exception", "[UniqueResource]")
REQUIRE_NOTHROW(guard.invoke());
}

TEST_CASE("reset releases old ressource", "[UniqueResource]")
{
std::size_t calls{0};

{
auto d = [&calls](auto v)
{
if( calls == 0 )
{
REQUIRE(v == 3);
}
else
{
REQUIRE(v == 7);
}
++calls;
};

auto guard = sr::unique_resource(Handle{3}, d);
guard.reset(Handle{7});
}

REQUIRE(calls == 2);
}

TEST_CASE("reset sets ressource", "[UniqueResource]")
{
auto guard = sr::unique_resource(Handle{3}, [](auto) { });
guard.reset(Handle{7});
REQUIRE(guard.get() == 7);
}

TEST_CASE("get returns ressource", "[UniqueResource]")
{
auto guard = sr::unique_resource(Handle{3}, [](auto) { });
REQUIRE(guard.get() == 3);
}


Loading…
取消
儲存