Bläddra i källkod

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

main
offa 7 år sedan
förälder
incheckning
494a662243
2 ändrade filer med 25 tillägg och 2 borttagningar
  1. +13
    -2
      include/scope_guard.h
  2. +12
    -0
      test/ScopeGuardTest.cpp

+ 13
- 2
include/scope_guard.h Visa fil

@@ -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 Visa fil

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


Laddar…
Avbryt
Spara