Selaa lähdekoodia

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

main
offa 7 vuotta sitten
vanhempi
commit
89e0b8116b
2 muutettua tiedostoa jossa 44 lisäystä ja 1 poistoa
  1. +6
    -1
      include/scope_guard.h
  2. +38
    -0
      test/ScopeGuardTest.cpp

+ 6
- 1
include/scope_guard.h Näytä tiedosto

@@ -16,7 +16,12 @@ namespace sg
{
}

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()

+ 38
- 0
test/ScopeGuardTest.cpp Näytä tiedosto

@@ -26,3 +26,41 @@ TEST_CASE("deleter is not called if released", "[ScopeGuard]")
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…
Peruuta
Tallenna