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

{ {
public: 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)
{ {
} }




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


void release()
{
m_execute_on_destruction = false;
} }




private: private:


Deleter m_deleter; Deleter m_deleter;
bool m_execute_on_destruction;
}; };





+ 12
- 0
test/ScopeGuardTest.cpp View File

REQUIRE(executed == true); 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