Browse Source

Move-Ctor implemented for scope guard (fixes #6).

main
offa 7 years ago
parent
commit
89e0b8116b
2 changed files with 44 additions and 1 deletions
  1. +6
    -1
      include/scope_guard.h
  2. +38
    -0
      test/ScopeGuardTest.cpp

+ 6
- 1
include/scope_guard.h View File

{ {
} }


scope_guard_t(scope_guard_t&&)= default;
scope_guard_t(scope_guard_t&& other) : m_deleter(std::move(other.m_deleter)),
m_execute_on_destruction(other.m_execute_on_destruction)
{
other.release();
}

scope_guard_t(const scope_guard_t&) = delete; scope_guard_t(const scope_guard_t&) = delete;


~scope_guard_t() ~scope_guard_t()

+ 38
- 0
test/ScopeGuardTest.cpp View File

REQUIRE(executed == false); REQUIRE(executed == false);
} }


TEST_CASE("move releases moved-from object", "[ScopeGuard]")
{
std::size_t calls{0};

{
auto movedFrom = sg::scope_guard([&calls] { ++calls; });
auto guard = std::move(movedFrom);

}

REQUIRE(calls == 1);
}

TEST_CASE("move transfers state", "[ScopeGuard]")
{
bool executed = false;

{
auto movedFrom = sg::scope_guard([&executed] { executed = true; });
auto guard = std::move(movedFrom);
}

REQUIRE(executed == true);
}

TEST_CASE("move transfers state if released", "[ScopeGuard]")
{
bool executed = false;

{
auto movedFrom = sg::scope_guard([&executed] { executed = true; });
movedFrom.release();
auto guard = std::move(movedFrom);
}

REQUIRE(executed == false);
}


Loading…
Cancel
Save