1
0
Fork 0
mirror of https://github.com/polybar/polybar.git synced 2024-10-27 05:23:39 -04:00

Stop using unique_ptr for on_exit

This commit is contained in:
patrick96 2022-03-06 17:02:28 +01:00 committed by Patrick Ziegler
parent b66f920308
commit 8ddf9d2cdf
6 changed files with 23 additions and 31 deletions

View file

@ -307,7 +307,7 @@ namespace cairo {
throw application_error("Could not load FreeType"); 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); FT_Done_FreeType(g_ftlib);
FcFini(); FcFini();
}); });
@ -336,6 +336,6 @@ namespace cairo {
return make_shared<font_fc>(cairo, match, offset, dpi_x, dpi_y); return make_shared<font_fc>(cairo, match, offset, dpi_x, dpi_y);
} }
} // namespace cairo } // namespace cairo
POLYBAR_NS_END POLYBAR_NS_END

View file

@ -6,9 +6,6 @@
POLYBAR_NS POLYBAR_NS
template <typename T>
using malloc_ptr_t = shared_ptr<T>;
template <typename T> template <typename T>
using malloc_unique_ptr = unique_ptr<T, decltype(free)*>; using malloc_unique_ptr = unique_ptr<T, decltype(free)*>;

View file

@ -5,10 +5,21 @@
POLYBAR_NS POLYBAR_NS
namespace scope_util { 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 { class on_exit {
public: 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() { virtual ~on_exit() {
m_callback(); m_callback();
@ -17,23 +28,6 @@ namespace scope_util {
protected: protected:
function<void()> m_callback; function<void()> m_callback;
}; };
} // namespace scope_util
/**
* 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 POLYBAR_NS_END

View file

@ -148,7 +148,7 @@ script_runner::interval script_runner::run_tail() {
throw modules::module_error("Failed to execute command: " + string(err.what())); 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(); m_pid = cmd->get_pid();
int fd = cmd->get_stdout(PIPE_READ); int fd = cmd->get_stdout(PIPE_READ);

View file

@ -170,8 +170,8 @@ void controller::conn_cb() {
return; return;
} }
malloc_ptr_t<xcb_generic_event_t> evt{}; shared_ptr<xcb_generic_event_t> evt{};
while ((evt = malloc_ptr_t<xcb_generic_event_t>(xcb_poll_for_event(m_connection), free)) != nullptr) { while ((evt = shared_ptr<xcb_generic_event_t>(xcb_poll_for_event(m_connection), free)) != nullptr) {
try { try {
m_connection.dispatch_event(evt); m_connection.dispatch_event(evt);
} catch (xpp::connection_error& err) { } catch (xpp::connection_error& err) {

View file

@ -1,16 +1,17 @@
#include "common/test.hpp"
#include "utils/scope.hpp" #include "utils/scope.hpp"
#include "common/test.hpp"
using namespace polybar; using namespace polybar;
TEST(Scope, onExit) { TEST(Scope, onExit) {
auto flag = false; auto flag = false;
{ {
EXPECT_FALSE(flag); EXPECT_FALSE(flag);
auto handler = scope_util::make_exit_handler<>([&] { flag = true; }); scope_util::on_exit handler([&] { flag = true; });
EXPECT_FALSE(flag); EXPECT_FALSE(flag);
{ {
auto handler = scope_util::make_exit_handler<>([&] { flag = true; }); scope_util::on_exit handler([&] { flag = true; });
} }
EXPECT_TRUE(flag); EXPECT_TRUE(flag);
flag = false; flag = false;