28 lines
484 B
C
28 lines
484 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};
|
||
|
}
|