mirror of
https://github.com/hyprwm/hyprutils.git
synced 2024-11-17 00:15:58 +01:00
utils: add ScopeGuard
This commit is contained in:
parent
5dcbbc1e3d
commit
0252fd13e7
2 changed files with 29 additions and 0 deletions
17
include/hyprutils/utils/ScopeGuard.hpp
Normal file
17
include/hyprutils/utils/ScopeGuard.hpp
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
namespace Hyprutils {
|
||||||
|
namespace Utils {
|
||||||
|
// calls a function when it goes out of scope
|
||||||
|
class CScopeGuard {
|
||||||
|
public:
|
||||||
|
CScopeGuard(const std::function<void()>& fn_);
|
||||||
|
~CScopeGuard();
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::function<void()> fn;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
12
src/utils/ScopeGuard.cpp
Normal file
12
src/utils/ScopeGuard.cpp
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
#include <hyprutils/utils/ScopeGuard.hpp>
|
||||||
|
|
||||||
|
using namespace Hyprutils::Utils;
|
||||||
|
|
||||||
|
Hyprutils::Utils::CScopeGuard::CScopeGuard(const std::function<void()>& fn_) : fn(fn_) {
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
Hyprutils::Utils::CScopeGuard::~CScopeGuard() {
|
||||||
|
if (fn)
|
||||||
|
fn();
|
||||||
|
}
|
Loading…
Reference in a new issue