Async++ unknown
Async (co_await/co_return) code for C++
Loading...
Searching...
No Matches
scope_guard.h
1#pragma once
2#include <type_traits>
3#include <utility>
4
5namespace asyncpp {
11 template<typename TFunction>
13 TFunction m_function;
14 bool m_engaged;
15
16 public:
22 explicit scope_guard(TFunction func,
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");
26 }
27 scope_guard(const scope_guard&) = delete;
28 scope_guard& operator=(const scope_guard&) = delete;
31 if (m_engaged) m_function();
32 }
34 void disengage() noexcept { m_engaged = false; }
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; }
46 };
47} // namespace asyncpp
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