Explorar el Código

Checked unique resource implemented.

main
offa hace 7 años
padre
commit
9dee2f7ac2
Se han modificado 2 ficheros con 30 adiciones y 0 borrados
  1. +6
    -0
      include/unique_resource.h
  2. +24
    -0
      test/UniqueResourceTest.cpp

+ 6
- 0
include/unique_resource.h Ver fichero

@@ -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 fichero

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

Cargando…
Cancelar
Guardar