#pragma once #include "common.hpp" #include "components/logger.hpp" LEMONBUDDY_NS namespace scope_util { template class on_exit { public: on_exit(function&& fn, Args... args) : m_callback(bind(fn, args...)) {} virtual ~on_exit() { m_callback(); } protected: function m_callback; }; /** * Creates a wrapper that will trigger given callback when * leaving the object's scope (i.e, when it gets destroyed) * * Example usage: * @code cpp * { * auto handler = scope_util::make_exit_handler([]{ ... }) * ... * } * @endcode */ template , typename... Args> decltype(auto) make_exit_handler(Fn&& fn, Args&&... args) { return make_unique>(forward(fn), forward(args)...); }; } LEMONBUDDY_NS_END