utils: add ScopeGuard

This commit is contained in:
Vaxry 2024-08-05 16:45:41 +02:00
parent 5dcbbc1e3d
commit 0252fd13e7
2 changed files with 29 additions and 0 deletions

View 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
View 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();
}