Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fixup! src: do not use std::function for OnScopeLeave
  • Loading branch information
addaleax committed Nov 6, 2019
commit e3aa3b6ad958ff5f77904f17b935e159dc8e05d6
18 changes: 17 additions & 1 deletion src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,23 @@ template <typename T> inline void USE(T&&) {}
template <typename Fn>
struct OnScopeLeaveImpl {
Fn fn_;
~OnScopeLeaveImpl() { fn_(); }
bool active_;

explicit OnScopeLeaveImpl(Fn&& fn) : fn_(std::move(fn)), active_(true) {}
~OnScopeLeaveImpl() { if (active_) fn_(); }

OnScopeLeaveImpl(const OnScopeLeaveImpl& other) = delete;
OnScopeLeaveImpl& operator=(const OnScopeLeaveImpl& other) = delete;
OnScopeLeaveImpl(OnScopeLeaveImpl&& other)
: fn_(std::move(other.fn_)), active_(other.active_) {
other.active_ = false;
}
OnScopeLeaveImpl& operator=(OnScopeLeaveImpl&& other) {
if (this == &other) return *this;
this->~OnScopeLeave();
new (this)OnScopeLeaveImpl(std::move(other));
return *this;
}
};

// Run a function when exiting the current scope. Used like this:
Expand Down