Parcourir la source

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

main
offa il y a 7 ans
Parent
révision
89e0b8116b
2 fichiers modifiés avec 44 ajouts et 1 suppressions
  1. +6
    -1
      include/scope_guard.h
  2. +38
    -0
      test/ScopeGuardTest.cpp

+ 6
- 1
include/scope_guard.h Voir le fichier

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

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


Chargement…
Annuler
Enregistrer