Make OnScopeEnd valid even when non-copy elided
OnScopeEnd was relying on copy elision to avoid temporary destructor calls that would run the scope end function too soon.
This commit is contained in:
parent
1fb53ca712
commit
6777c14697
11
src/utils.hh
11
src/utils.hh
|
@ -65,11 +65,18 @@ class OnScopeEnd
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
[[gnu::always_inline]]
|
[[gnu::always_inline]]
|
||||||
OnScopeEnd(T func) : m_func(std::move(func)) {}
|
OnScopeEnd(T func) : m_func{std::move(func)}, m_valid{true} {}
|
||||||
|
|
||||||
[[gnu::always_inline]]
|
[[gnu::always_inline]]
|
||||||
~OnScopeEnd() { m_func(); }
|
OnScopeEnd(OnScopeEnd&& other)
|
||||||
|
: m_func{std::move(other.m_func)}, m_valid{other.m_valid}
|
||||||
|
{ other.m_valid = false; }
|
||||||
|
|
||||||
|
[[gnu::always_inline]]
|
||||||
|
~OnScopeEnd() { if (m_valid) m_func(); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
bool m_valid;
|
||||||
T m_func;
|
T m_func;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user