2022-12-08 13:56:11 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
template <typename Callback> class ScopeGuard
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ScopeGuard(const Callback& callback) : m_callback(callback)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void deactivate()
|
|
|
|
{
|
|
|
|
m_activated = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
~ScopeGuard()
|
|
|
|
{
|
|
|
|
if (m_activated) m_callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-12-21 19:22:44 +00:00
|
|
|
bool m_activated { true };
|
2022-12-08 13:56:11 +00:00
|
|
|
Callback m_callback;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Callback> [[nodiscard]] ScopeGuard<Callback> make_scope_guard(const Callback& callback)
|
|
|
|
{
|
2022-12-21 19:22:44 +00:00
|
|
|
return { callback };
|
2023-01-02 12:07:29 +00:00
|
|
|
}
|