Browse Source

Move ctor implemented.

main
offa 7 years ago
parent
commit
c4fd938af4
2 changed files with 64 additions and 3 deletions
  1. +12
    -2
      include/unique_resource.h
  2. +52
    -1
      test/UniqueResourceTest.cpp

+ 12
- 2
include/unique_resource.h View File

@@ -36,13 +36,23 @@ namespace sr
}

unique_resource_t(const unique_resource_t&) = delete;
unique_resource_t(unique_resource_t&&) = default;

unique_resource_t(unique_resource_t&& other) noexcept : m_resource(std::move(other.m_resource)),
m_deleter(std::move(other.m_deleter)),
m_execute_on_destruction(other.m_execute_on_destruction)
{
other.release();
}

~unique_resource_t()
{
if( m_execute_on_destruction == true )
{
m_deleter();
try
{
m_deleter();
}
catch( ... ) { /* Empty */ }
}
}


+ 52
- 1
test/UniqueResourceTest.cpp View File

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


TEST_CASE("release returns reference to resource", "[UniqueResource]")
{
constexpr Handle handle{3};
@@ -60,4 +59,56 @@ TEST_CASE("release returns reference to resource", "[UniqueResource]")
REQUIRE(handle == result);
}

TEST_CASE("move releases moved-from object", "[UniqueResource]")
{
std::size_t calls{0};
constexpr Handle handle{3};

{
auto movedFrom = sr::unique_resource(handle, [&calls] { ++calls; });
auto guard = std::move(movedFrom);
static_cast<void>(guard);
}

REQUIRE(calls == 1);
}

TEST_CASE("move transfers state", "[UniqueResource]")
{
std::size_t calls{0};
constexpr Handle handle{3};

{
auto movedFrom = sr::unique_resource(handle, [&calls] { ++calls; });
auto guard = std::move(movedFrom);
static_cast<void>(guard);
}

REQUIRE(calls == 1);
}

TEST_CASE("move transfers state if released", "[UniqueResource]")
{
std::size_t calls{0};
constexpr Handle handle{3};

{
auto movedFrom = sr::unique_resource(handle, [&calls] { ++calls; });
movedFrom.release();
auto guard = std::move(movedFrom);
static_cast<void>(guard);
}

REQUIRE(calls == 0);
}

TEST_CASE("no exception propagation from deleter", "[UniqueResource]")
{
constexpr Handle handle{3};

REQUIRE_NOTHROW([] {
auto guard = sr::unique_resource(handle, [] { throw "Don't propagate this!"; });
static_cast<void>(guard);
}());
}


Loading…
Cancel
Save