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:
Maxime Coste 2011-12-20 19:18:00 +00:00
parent 17cab9c7c4
commit 91606850fd

View File

@ -112,6 +112,21 @@ inline std::string str_to_str(const std::string& str)
return 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);
}
} }