Luna/libluna/include/luna/ScopeGuard.h
apio 77887eed80
All checks were successful
continuous-integration/drone/push Build is passing
luna -> libluna
2023-02-27 15:22:39 +01:00

29 lines
490 B
C++

#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:
bool m_activated { true };
Callback m_callback;
};
template <typename Callback> [[nodiscard]] ScopeGuard<Callback> make_scope_guard(const Callback& callback)
{
return { callback };
}