Ver código fonte

Checked unique resource implemented.

main
offa 7 anos atrás
pai
commit
9dee2f7ac2
2 arquivos alterados com 30 adições e 0 exclusões
  1. +6
    -0
      include/unique_resource.h
  2. +24
    -0
      test/UniqueResourceTest.cpp

+ 6
- 0
include/unique_resource.h Ver arquivo

@@ -152,6 +152,12 @@ namespace sr
return unique_resource_t<Ressource, Deleter>{std::move(res), std::move(d), true};
}

template<class Ressource, class Deleter>
unique_resource_t<Ressource, Deleter> unique_resource_checked(Ressource res, Ressource invalid, Deleter d) noexcept
{
return unique_resource_t<Ressource, Deleter>{std::move(res), std::move(d), (res != invalid)};
}

}



+ 24
- 0
test/UniqueResourceTest.cpp Ver arquivo

@@ -49,6 +49,30 @@ TEST_CASE("deleter is not called if released", "[UniqueResource]")
REQUIRE(calls == 0);
}

TEST_CASE("deleter not called if checked valid", "[UniqueResource]")
{
std::size_t calls{0};

{
auto guard = sr::unique_resource_checked(Handle{3}, Handle{6}, [&calls](auto) { ++calls; });
static_cast<void>(guard);
}

REQUIRE(calls == 1);
}

TEST_CASE("deleter not called if checked invalid", "[UniqueResource]")
{
std::size_t calls{0};

{
auto guard = sr::unique_resource_checked(Handle{3}, Handle{3}, [&calls](auto) { ++calls; });
static_cast<void>(guard);
}

REQUIRE(calls == 0);
}

TEST_CASE("release returns reference to resource", "[UniqueResource]")
{
auto guard = sr::unique_resource(Handle{3}, [](auto) { });

Carregando…
Cancelar
Salvar