util/ScopeExit: convert the function from base class to member

This allows using `final` callables.
This commit is contained in:
Max Kellermann 2023-05-22 21:20:41 +02:00
parent 7bb251dad8
commit 6496c1b806

View File

@ -10,14 +10,18 @@
* Internal class. Do not use directly. * Internal class. Do not use directly.
*/ */
template<typename F> template<typename F>
class ScopeExitGuard : F { class ScopeExitGuard {
[[no_unique_address]]
F function;
bool enabled = true; bool enabled = true;
public: public:
explicit ScopeExitGuard(F &&f) noexcept:F(std::forward<F>(f)) {} explicit ScopeExitGuard(F &&f) noexcept
:function(std::forward<F>(f)) {}
ScopeExitGuard(ScopeExitGuard &&src) noexcept ScopeExitGuard(ScopeExitGuard &&src) noexcept
:F(std::move(src)), :function(std::move(src.function)),
enabled(std::exchange(src.enabled, false)) {} enabled(std::exchange(src.enabled, false)) {}
/* destructors are "noexcept" by default; this explicit /* destructors are "noexcept" by default; this explicit
@ -26,7 +30,7 @@ public:
would std::terminate() */ would std::terminate() */
~ScopeExitGuard() noexcept(noexcept(std::declval<F>()())) { ~ScopeExitGuard() noexcept(noexcept(std::declval<F>()())) {
if (enabled) if (enabled)
F::operator()(); function();
} }
ScopeExitGuard(const ScopeExitGuard &) = delete; ScopeExitGuard(const ScopeExitGuard &) = delete;