29 lines
490 B
C++
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 };
|
|
}
|