Browse Source

release() implemented for scope_guard (fixes #4).

main
offa 7 years ago
parent
commit
494a662243
2 changed files with 25 additions and 2 deletions
  1. +13
    -2
      include/scope_guard.h
  2. +12
    -0
      test/ScopeGuardTest.cpp

+ 13
- 2
include/scope_guard.h View File

@@ -11,7 +11,8 @@ namespace sg
{
public:

explicit scope_guard_t(Deleter&& deleter) noexcept : m_deleter(std::move(deleter))
explicit scope_guard_t(Deleter&& deleter) noexcept : m_deleter(std::move(deleter)),
m_execute_on_destruction(true)
{
}

@@ -20,7 +21,16 @@ namespace sg

~scope_guard_t()
{
m_deleter();
if( m_execute_on_destruction == true )
{
m_deleter();
}
}


void release()
{
m_execute_on_destruction = false;
}


@@ -31,6 +41,7 @@ namespace sg
private:

Deleter m_deleter;
bool m_execute_on_destruction;
};



+ 12
- 0
test/ScopeGuardTest.cpp View File

@@ -14,3 +14,15 @@ TEST_CASE("deleter called on destruction", "[ScopeGuard]")
REQUIRE(executed == true);
}

TEST_CASE("deleter is not called if released", "[ScopeGuard]")
{
bool executed = false;

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

REQUIRE(executed == false);
}


Loading…
Cancel
Save