Utils: add on_scope_end utility
on_scope_end permits to register a functor to be called at scope end (either exception thrown or normal scope end). this is usefull for cleanup code that must be run. usage: auto cleaner = on_scope_end([]() { cleanup(); });
This commit is contained in:
parent
17cab9c7c4
commit
91606850fd
15
src/utils.hh
15
src/utils.hh
|
@ -112,6 +112,21 @@ inline std::string str_to_str(const std::string& str)
|
|||
return str;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
class OnScopeEnd
|
||||
{
|
||||
public:
|
||||
OnScopeEnd(T func) : m_func(func) {}
|
||||
~OnScopeEnd() { m_func(); }
|
||||
private:
|
||||
T m_func;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
OnScopeEnd<T> on_scope_end(T t)
|
||||
{
|
||||
return OnScopeEnd<T>(t);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user