2019-06-07 03:18:21 -04:00
|
|
|
#pragma once
|
|
|
|
|
2019-06-18 03:31:14 -04:00
|
|
|
#include <AK/StdLibExtras.h>
|
|
|
|
|
2019-06-07 03:18:21 -04:00
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
class ScopeGuard {
|
|
|
|
public:
|
|
|
|
ScopeGuard(Callback callback)
|
|
|
|
: m_callback(move(callback))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~ScopeGuard()
|
|
|
|
{
|
|
|
|
m_callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Callback m_callback;
|
|
|
|
};
|
|
|
|
|
2020-01-09 02:35:32 -05:00
|
|
|
template<typename Callback>
|
|
|
|
class ArmedScopeGuard {
|
|
|
|
public:
|
|
|
|
ArmedScopeGuard(Callback callback)
|
|
|
|
: m_callback(move(callback))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~ArmedScopeGuard()
|
|
|
|
{
|
|
|
|
if (m_armed)
|
|
|
|
m_callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
void disarm() { m_armed = false; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Callback m_callback;
|
|
|
|
bool m_armed { true };
|
|
|
|
};
|
|
|
|
|
2019-06-07 03:18:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
using AK::ScopeGuard;
|
2020-01-09 02:35:32 -05:00
|
|
|
using AK::ArmedScopeGuard;
|