Parcourir la source

Checked unique resource implemented.

main
offa il y a 7 ans
Parent
révision
9dee2f7ac2
2 fichiers modifiés avec 30 ajouts et 0 suppressions
  1. +6
    -0
      include/unique_resource.h
  2. +24
    -0
      test/UniqueResourceTest.cpp

+ 6
- 0
include/unique_resource.h Voir le fichier

@@ -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 Voir le fichier

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

Chargement…
Annuler
Enregistrer