2016-06-14 23:32:35 -04:00
|
|
|
#pragma once
|
|
|
|
|
2016-11-20 17:04:31 -05:00
|
|
|
// TODO: move to functional.hpp
|
|
|
|
|
2016-06-14 23:32:35 -04:00
|
|
|
#include "common.hpp"
|
|
|
|
|
|
|
|
#include "components/logger.hpp"
|
2016-12-09 03:02:47 -05:00
|
|
|
#include "utils/factory.hpp"
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
POLYBAR_NS
|
2016-06-14 23:32:35 -04:00
|
|
|
|
|
|
|
namespace scope_util {
|
|
|
|
template <typename... Args>
|
|
|
|
class on_exit {
|
|
|
|
public:
|
|
|
|
on_exit(function<void(Args...)>&& fn, Args... args) : m_callback(bind(fn, args...)) {}
|
|
|
|
|
|
|
|
virtual ~on_exit() {
|
|
|
|
m_callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
function<void()> m_callback;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a wrapper that will trigger given callback when
|
|
|
|
* leaving the object's scope (i.e, when it gets destroyed)
|
|
|
|
*
|
|
|
|
* Example usage:
|
2018-11-04 10:08:54 -05:00
|
|
|
* \code cpp
|
2016-06-14 23:32:35 -04:00
|
|
|
* {
|
|
|
|
* auto handler = scope_util::make_exit_handler([]{ ... })
|
|
|
|
* ...
|
|
|
|
* }
|
2018-11-04 10:08:54 -05:00
|
|
|
* \endcode
|
2016-06-14 23:32:35 -04:00
|
|
|
*/
|
|
|
|
template <typename Fn = function<void()>, typename... Args>
|
2016-10-11 06:38:15 -04:00
|
|
|
decltype(auto) make_exit_handler(Fn&& fn, Args&&... args) {
|
2016-12-09 03:02:47 -05:00
|
|
|
return factory_util::unique<on_exit<Args...>>(forward<Fn>(fn), forward<Args>(args)...);
|
2016-10-25 14:14:44 -04:00
|
|
|
}
|
2016-06-14 23:32:35 -04:00
|
|
|
}
|
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
POLYBAR_NS_END
|