Skip to content

Commit

Permalink
util: add scope_guard
Browse files Browse the repository at this point in the history
This custom small implementation avoids adding an extra dependency like
Boost.ScopeExit
  • Loading branch information
taminob committed Oct 21, 2023
1 parent eefd6e8 commit 8c57756
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions include/util/scope_guard.hpp
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8c57756

Please sign in to comment.