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