Luna/luna/include/luna/ScopeGuard.h

28 lines
484 B
C
Raw Normal View History

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