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
1 changed files with 8 additions and 4 deletions

View File

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