From 8ddf9d2cdf9f8ad4afda320f1901b6fcfabc3af0 Mon Sep 17 00:00:00 2001 From: patrick96 Date: Sun, 6 Mar 2022 17:02:28 +0100 Subject: [PATCH] Stop using unique_ptr for on_exit --- include/cairo/font.hpp | 4 ++-- include/utils/memory.hpp | 3 --- include/utils/scope.hpp | 34 +++++++++++++------------------- src/adapters/script_runner.cpp | 2 +- src/components/controller.cpp | 4 ++-- tests/unit_tests/utils/scope.cpp | 7 ++++--- 6 files changed, 23 insertions(+), 31 deletions(-) diff --git a/include/cairo/font.hpp b/include/cairo/font.hpp index 67fe8f29..488a0875 100644 --- a/include/cairo/font.hpp +++ b/include/cairo/font.hpp @@ -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(); }); @@ -336,6 +336,6 @@ namespace cairo { return make_shared(cairo, match, offset, dpi_x, dpi_y); } -} // namespace cairo +} // namespace cairo POLYBAR_NS_END diff --git a/include/utils/memory.hpp b/include/utils/memory.hpp index 88f8a280..57f9fc36 100644 --- a/include/utils/memory.hpp +++ b/include/utils/memory.hpp @@ -6,9 +6,6 @@ POLYBAR_NS -template -using malloc_ptr_t = shared_ptr; - template using malloc_unique_ptr = unique_ptr; diff --git a/include/utils/scope.hpp b/include/utils/scope.hpp index ed8a7136..c274609b 100644 --- a/include/utils/scope.hpp +++ b/include/utils/scope.hpp @@ -5,10 +5,21 @@ POLYBAR_NS namespace scope_util { - template + /** + * 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&& fn, Args... args) : m_callback(bind(fn, args...)) {} + on_exit(const function& fn) : m_callback(fn) {} virtual ~on_exit() { m_callback(); @@ -17,23 +28,6 @@ namespace scope_util { 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 std::make_unique>(forward(fn), forward(args)...); - } -} // namespace scope_util +} // namespace scope_util POLYBAR_NS_END diff --git a/src/adapters/script_runner.cpp b/src/adapters/script_runner.cpp index ce8fef73..d532d611 100644 --- a/src/adapters/script_runner.cpp +++ b/src/adapters/script_runner.cpp @@ -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); diff --git a/src/components/controller.cpp b/src/components/controller.cpp index 54117f97..cb6651b6 100644 --- a/src/components/controller.cpp +++ b/src/components/controller.cpp @@ -170,8 +170,8 @@ void controller::conn_cb() { return; } - malloc_ptr_t evt{}; - while ((evt = malloc_ptr_t(xcb_poll_for_event(m_connection), free)) != nullptr) { + shared_ptr evt{}; + while ((evt = shared_ptr(xcb_poll_for_event(m_connection), free)) != nullptr) { try { m_connection.dispatch_event(evt); } catch (xpp::connection_error& err) { diff --git a/tests/unit_tests/utils/scope.cpp b/tests/unit_tests/utils/scope.cpp index 69ab87da..9f60dd7d 100644 --- a/tests/unit_tests/utils/scope.cpp +++ b/tests/unit_tests/utils/scope.cpp @@ -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;