util/ScopeExit: convert the function from base class to member
This allows using `final` callables.
This commit is contained in:
parent
7bb251dad8
commit
6496c1b806
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue