Skip to content

Commit

Permalink
utils: add ScopeGuard
Browse files Browse the repository at this point in the history
  • Loading branch information
vaxerski committed Aug 5, 2024
1 parent 5dcbbc1 commit 0252fd1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
17 changes: 17 additions & 0 deletions include/hyprutils/utils/ScopeGuard.hpp
Original file line number Diff line number Diff line change
@@ -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 changes: 12 additions & 0 deletions src/utils/ScopeGuard.cpp
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit 0252fd1

Please sign in to comment.