From 0252fd13e78e60fb0da512a212e56007515a49f7 Mon Sep 17 00:00:00 2001 From: Vaxry Date: Mon, 5 Aug 2024 16:45:41 +0200 Subject: [PATCH] utils: add ScopeGuard --- include/hyprutils/utils/ScopeGuard.hpp | 17 +++++++++++++++++ src/utils/ScopeGuard.cpp | 12 ++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 include/hyprutils/utils/ScopeGuard.hpp create mode 100644 src/utils/ScopeGuard.cpp diff --git a/include/hyprutils/utils/ScopeGuard.hpp b/include/hyprutils/utils/ScopeGuard.hpp new file mode 100644 index 0000000..af4feb7 --- /dev/null +++ b/include/hyprutils/utils/ScopeGuard.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include + +namespace Hyprutils { + namespace Utils { + // calls a function when it goes out of scope + class CScopeGuard { + public: + CScopeGuard(const std::function& fn_); + ~CScopeGuard(); + + private: + std::function fn; + }; + }; +}; diff --git a/src/utils/ScopeGuard.cpp b/src/utils/ScopeGuard.cpp new file mode 100644 index 0000000..fa8bbd8 --- /dev/null +++ b/src/utils/ScopeGuard.cpp @@ -0,0 +1,12 @@ +#include + +using namespace Hyprutils::Utils; + +Hyprutils::Utils::CScopeGuard::CScopeGuard(const std::function& fn_) : fn(fn_) { + ; +} + +Hyprutils::Utils::CScopeGuard::~CScopeGuard() { + if (fn) + fn(); +}