11 template<
typename TFunction>
23 bool engaged =
true) noexcept(std::is_nothrow_move_constructible_v<TFunction>)
24 : m_function{std::move(func)}, m_engaged{engaged} {
25 static_assert(
noexcept(func()),
"scope_guard function must not throw exceptions");
31 if (m_engaged) m_function();
36 void engage() noexcept { m_engaged =
true; }
41 [[nodiscard]]
bool is_engaged() const noexcept {
return m_engaged; }
43 TFunction&
function() noexcept {
return m_function; }
45 const TFunction&
function() const noexcept {
return m_function; }
Execute a callback function when this scope gets destroyed.
Definition scope_guard.h:12
void engage() noexcept
Engage the guard, making sure the callback is executed on destruction.
Definition scope_guard.h:36
const TFunction & function() const noexcept
Get a const reference to the contained function.
Definition scope_guard.h:45
~scope_guard()
Destructor.
Definition scope_guard.h:30
scope_guard(TFunction func, bool engaged=true) noexcept(std::is_nothrow_move_constructible_v< TFunction >)
Construct a new scope guard.
Definition scope_guard.h:22
TFunction & function() noexcept
Get a reference to the contained function.
Definition scope_guard.h:43
bool is_engaged() const noexcept
Check if the guard is engaged.
Definition scope_guard.h:41
void disengage() noexcept
Disengage the guard, making sure the callback is not executed on destruction.
Definition scope_guard.h:34