util: add scope_guard

This custom small implementation avoids adding an extra dependency like
Boost.ScopeExit
This commit is contained in:
Tamino Bauknecht 2023-10-20 22:34:56 +02:00
parent eefd6e8336
commit 8c57756556
No known key found for this signature in database
GPG Key ID: 77837396BE935C6C
1 changed files with 21 additions and 0 deletions

View File

@ -0,0 +1,21 @@
#pragma once
#include <utility>
namespace waybar::util {
template <typename Func>
class scope_guard {
public:
explicit scope_guard(Func&& exit_function) : f{std::forward<Func>(exit_function)} {}
scope_guard(const scope_guard&) = delete;
scope_guard(scope_guard&&) = default;
scope_guard& operator=(const scope_guard&) = delete;
scope_guard& operator=(scope_guard&&) = default;
~scope_guard() { f(); }
private:
Func f;
};
} // namespace waybar::util