Pārlūkot izejas kodu

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

main
offa pirms 7 gadiem
vecāks
revīzija
89e0b8116b
2 mainītis faili ar 44 papildinājumiem un 1 dzēšanām
  1. +6
    -1
      include/scope_guard.h
  2. +38
    -0
      test/ScopeGuardTest.cpp

+ 6
- 1
include/scope_guard.h Parādīt failu

@@ -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 Parādīt failu

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


Notiek ielāde…
Atcelt
Saglabāt