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