diff --git a/src/neighbor/Glue.cxx b/src/neighbor/Glue.cxx index ad56acd94..5037ba8ac 100644 --- a/src/neighbor/Glue.cxx +++ b/src/neighbor/Glue.cxx @@ -30,14 +30,10 @@ #include -NeighborGlue::Explorer::~Explorer() noexcept -{ - delete explorer; -} - +NeighborGlue::NeighborGlue() noexcept {} NeighborGlue::~NeighborGlue() noexcept {} -static NeighborExplorer * +static std::unique_ptr CreateNeighborExplorer(EventLoop &loop, NeighborListener &listener, const ConfigBlock &block) { @@ -59,9 +55,9 @@ NeighborGlue::Init(EventLoop &loop, NeighborListener &listener) for (const auto *block = config_get_block(ConfigBlockOption::NEIGHBORS); block != nullptr; block = block->next) { try { - auto *explorer = - CreateNeighborExplorer(loop, listener, *block); - explorers.emplace_front(explorer); + explorers.emplace_front(CreateNeighborExplorer(loop, + listener, + *block)); } catch (...) { std::throw_with_nested(FormatRuntimeError("Line %i: ", block->line)); diff --git a/src/neighbor/Glue.hxx b/src/neighbor/Glue.hxx index e4ca84bf3..914419845 100644 --- a/src/neighbor/Glue.hxx +++ b/src/neighbor/Glue.hxx @@ -25,6 +25,7 @@ #include "thread/Mutex.hxx" #include +#include class EventLoop; class NeighborExplorer; @@ -36,13 +37,13 @@ struct NeighborInfo; */ class NeighborGlue { struct Explorer { - NeighborExplorer *const explorer; + std::unique_ptr explorer; - Explorer(NeighborExplorer *_explorer) noexcept - :explorer(_explorer) {} + template + Explorer(E &&_explorer) noexcept + :explorer(std::forward(_explorer)) {} Explorer(const Explorer &) = delete; - ~Explorer() noexcept; }; Mutex mutex; @@ -52,7 +53,7 @@ class NeighborGlue { public: typedef std::forward_list List; - NeighborGlue() = default; + NeighborGlue() noexcept; NeighborGlue(const NeighborGlue &) = delete; ~NeighborGlue() noexcept; diff --git a/src/neighbor/NeighborPlugin.hxx b/src/neighbor/NeighborPlugin.hxx index a390d2997..4cbc502f5 100644 --- a/src/neighbor/NeighborPlugin.hxx +++ b/src/neighbor/NeighborPlugin.hxx @@ -20,6 +20,8 @@ #ifndef MPD_NEIGHBOR_PLUGIN_HXX #define MPD_NEIGHBOR_PLUGIN_HXX +#include + struct ConfigBlock; class EventLoop; class NeighborListener; @@ -31,8 +33,9 @@ struct NeighborPlugin { /** * Allocates and configures a #NeighborExplorer instance. */ - NeighborExplorer *(*create)(EventLoop &loop, NeighborListener &listener, - const ConfigBlock &block); + std::unique_ptr (*create)(EventLoop &loop, + NeighborListener &listener, + const ConfigBlock &block); }; #endif diff --git a/src/neighbor/plugins/SmbclientNeighborPlugin.cxx b/src/neighbor/plugins/SmbclientNeighborPlugin.cxx index 97437add9..287cc8d07 100644 --- a/src/neighbor/plugins/SmbclientNeighborPlugin.cxx +++ b/src/neighbor/plugins/SmbclientNeighborPlugin.cxx @@ -252,14 +252,14 @@ SmbclientNeighborExplorer::ThreadFunc() mutex.unlock(); } -static NeighborExplorer * +static std::unique_ptr smbclient_neighbor_create(gcc_unused EventLoop &loop, NeighborListener &listener, gcc_unused const ConfigBlock &block) { SmbclientInit(); - return new SmbclientNeighborExplorer(listener); + return std::make_unique(listener); } const NeighborPlugin smbclient_neighbor_plugin = { diff --git a/src/neighbor/plugins/UpnpNeighborPlugin.cxx b/src/neighbor/plugins/UpnpNeighborPlugin.cxx index b034fa96d..1180133d2 100644 --- a/src/neighbor/plugins/UpnpNeighborPlugin.cxx +++ b/src/neighbor/plugins/UpnpNeighborPlugin.cxx @@ -127,12 +127,12 @@ UpnpNeighborExplorer::LostUPnP(const ContentDirectoryService &service) listener.LostNeighbor(n); } -static NeighborExplorer * +static std::unique_ptr upnp_neighbor_create(EventLoop &event_loop, NeighborListener &listener, gcc_unused const ConfigBlock &block) { - return new UpnpNeighborExplorer(event_loop, listener); + return std::make_unique(event_loop, listener); } const NeighborPlugin upnp_neighbor_plugin = {