瀏覽代碼

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

main
offa 7 年之前
父節點
當前提交
89e0b8116b
共有 2 個文件被更改,包括 44 次插入1 次删除
  1. +6
    -1
      include/scope_guard.h
  2. +38
    -0
      test/ScopeGuardTest.cpp

+ 6
- 1
include/scope_guard.h 查看文件

@@ -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 查看文件

@@ -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…
取消
儲存