Stop using unique_ptr for on_exit
This commit is contained in:
parent
b66f920308
commit
8ddf9d2cdf
@ -307,7 +307,7 @@ namespace cairo {
|
||||
throw application_error("Could not load FreeType");
|
||||
}
|
||||
|
||||
static auto fc_cleanup = scope_util::make_exit_handler([] {
|
||||
static scope_util::on_exit fc_cleanup([] {
|
||||
FT_Done_FreeType(g_ftlib);
|
||||
FcFini();
|
||||
});
|
||||
|
@ -6,9 +6,6 @@
|
||||
|
||||
POLYBAR_NS
|
||||
|
||||
template <typename T>
|
||||
using malloc_ptr_t = shared_ptr<T>;
|
||||
|
||||
template <typename T>
|
||||
using malloc_unique_ptr = unique_ptr<T, decltype(free)*>;
|
||||
|
||||
|
@ -5,10 +5,21 @@
|
||||
POLYBAR_NS
|
||||
|
||||
namespace scope_util {
|
||||
template <typename... Args>
|
||||
/**
|
||||
* Creates a wrapper that will trigger given callback when
|
||||
* leaving the object's scope (i.e, when it gets destroyed)
|
||||
*
|
||||
* Example usage:
|
||||
* @code cpp
|
||||
* {
|
||||
* on_exit handler([]{ ... });
|
||||
* ...
|
||||
* }
|
||||
* @endcode
|
||||
*/
|
||||
class on_exit {
|
||||
public:
|
||||
on_exit(function<void(Args...)>&& fn, Args... args) : m_callback(bind(fn, args...)) {}
|
||||
on_exit(const function<void(void)>& fn) : m_callback(fn) {}
|
||||
|
||||
virtual ~on_exit() {
|
||||
m_callback();
|
||||
@ -17,23 +28,6 @@ namespace scope_util {
|
||||
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:
|
||||
* @code cpp
|
||||
* {
|
||||
* auto handler = scope_util::make_exit_handler([]{ ... })
|
||||
* ...
|
||||
* }
|
||||
* @endcode
|
||||
*/
|
||||
template <typename Fn = function<void()>, typename... Args>
|
||||
decltype(auto) make_exit_handler(Fn&& fn, Args&&... args) {
|
||||
return std::make_unique<on_exit<Args...>>(forward<Fn>(fn), forward<Args>(args)...);
|
||||
}
|
||||
} // namespace scope_util
|
||||
|
||||
POLYBAR_NS_END
|
||||
|
@ -148,7 +148,7 @@ script_runner::interval script_runner::run_tail() {
|
||||
throw modules::module_error("Failed to execute command: " + string(err.what()));
|
||||
}
|
||||
|
||||
auto pid_guard = scope_util::make_exit_handler([this]() { m_pid = -1; });
|
||||
scope_util::on_exit pid_guard([this]() { m_pid = -1; });
|
||||
m_pid = cmd->get_pid();
|
||||
|
||||
int fd = cmd->get_stdout(PIPE_READ);
|
||||
|
@ -170,8 +170,8 @@ void controller::conn_cb() {
|
||||
return;
|
||||
}
|
||||
|
||||
malloc_ptr_t<xcb_generic_event_t> evt{};
|
||||
while ((evt = malloc_ptr_t<xcb_generic_event_t>(xcb_poll_for_event(m_connection), free)) != nullptr) {
|
||||
shared_ptr<xcb_generic_event_t> evt{};
|
||||
while ((evt = shared_ptr<xcb_generic_event_t>(xcb_poll_for_event(m_connection), free)) != nullptr) {
|
||||
try {
|
||||
m_connection.dispatch_event(evt);
|
||||
} catch (xpp::connection_error& err) {
|
||||
|
@ -1,16 +1,17 @@
|
||||
#include "common/test.hpp"
|
||||
#include "utils/scope.hpp"
|
||||
|
||||
#include "common/test.hpp"
|
||||
|
||||
using namespace polybar;
|
||||
|
||||
TEST(Scope, onExit) {
|
||||
auto flag = false;
|
||||
{
|
||||
EXPECT_FALSE(flag);
|
||||
auto handler = scope_util::make_exit_handler<>([&] { flag = true; });
|
||||
scope_util::on_exit handler([&] { flag = true; });
|
||||
EXPECT_FALSE(flag);
|
||||
{
|
||||
auto handler = scope_util::make_exit_handler<>([&] { flag = true; });
|
||||
scope_util::on_exit handler([&] { flag = true; });
|
||||
}
|
||||
EXPECT_TRUE(flag);
|
||||
flag = false;
|
||||
|
Loading…
Reference in New Issue
Block a user