Selaa lähdekoodia

Reset and get implemented.

main
offa 7 vuotta sitten
vanhempi
commit
7988145317
2 muutettua tiedostoa jossa 49 lisäystä ja 0 poistoa
  1. +11
    -0
      include/unique_resource.h
  2. +38
    -0
      test/UniqueResourceTest.cpp

+ 11
- 0
include/unique_resource.h Näytä tiedosto

@@ -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 Näytä tiedosto

@@ -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…
Peruuta
Tallenna