mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 15:40:19 +00:00
48 lines
679 B
C++
48 lines
679 B
C++
#pragma once
|
|
|
|
#include <AK/StdLibExtras.h>
|
|
|
|
namespace AK {
|
|
|
|
template<typename Callback>
|
|
class ScopeGuard {
|
|
public:
|
|
ScopeGuard(Callback callback)
|
|
: m_callback(move(callback))
|
|
{
|
|
}
|
|
|
|
~ScopeGuard()
|
|
{
|
|
m_callback();
|
|
}
|
|
|
|
private:
|
|
Callback m_callback;
|
|
};
|
|
|
|
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 };
|
|
};
|
|
|
|
}
|
|
|
|
using AK::ScopeGuard;
|
|
using AK::ArmedScopeGuard;
|