mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
AK: Add ArmedScopeGuard, a scope guard that can be disarmed
This commit is contained in:
parent
4cee441279
commit
9681d41bf0
Notes:
sideshowbarker
2024-07-19 10:05:28 +09:00
Author: https://github.com/ADKaster Commit: https://github.com/SerenityOS/serenity/commit/9681d41bf0b Pull-request: https://github.com/SerenityOS/serenity/pull/1048 Reviewed-by: https://github.com/awesomekling ✅
1 changed files with 22 additions and 0 deletions
|
@ -21,6 +21,28 @@ 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;
|
||||
|
|
Loading…
Reference in a new issue