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");
}
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<font_fc>(cairo, match, offset, dpi_x, dpi_y);
}
} // namespace cairo
} // namespace cairo
POLYBAR_NS_END

View File

@ -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)*>;

View File

@ -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
} // namespace scope_util
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()));
}
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);

View File

@ -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) {

View File

@ -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;