Browse Source

Checked unique resource implemented.

main
offa 7 years ago
parent
commit
9dee2f7ac2
2 changed files with 30 additions and 0 deletions
  1. +6
    -0
      include/unique_resource.h
  2. +24
    -0
      test/UniqueResourceTest.cpp

+ 6
- 0
include/unique_resource.h View File

return unique_resource_t<Ressource, Deleter>{std::move(res), std::move(d), true}; 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 View File

REQUIRE(calls == 0); 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]") TEST_CASE("release returns reference to resource", "[UniqueResource]")
{ {
auto guard = sr::unique_resource(Handle{3}, [](auto) { }); auto guard = sr::unique_resource(Handle{3}, [](auto) { });

Loading…
Cancel
Save