Commit 201210cf authored by Max Kellermann's avatar Max Kellermann

neighbor/Plugin: std::unique_ptr<NeighborExplorer>

parent cd6de3b2
...@@ -30,14 +30,10 @@ ...@@ -30,14 +30,10 @@
#include <stdexcept> #include <stdexcept>
NeighborGlue::Explorer::~Explorer() noexcept NeighborGlue::NeighborGlue() noexcept {}
{
delete explorer;
}
NeighborGlue::~NeighborGlue() noexcept {} NeighborGlue::~NeighborGlue() noexcept {}
static NeighborExplorer * static std::unique_ptr<NeighborExplorer>
CreateNeighborExplorer(EventLoop &loop, NeighborListener &listener, CreateNeighborExplorer(EventLoop &loop, NeighborListener &listener,
const ConfigBlock &block) const ConfigBlock &block)
{ {
...@@ -59,9 +55,9 @@ NeighborGlue::Init(EventLoop &loop, NeighborListener &listener) ...@@ -59,9 +55,9 @@ NeighborGlue::Init(EventLoop &loop, NeighborListener &listener)
for (const auto *block = config_get_block(ConfigBlockOption::NEIGHBORS); for (const auto *block = config_get_block(ConfigBlockOption::NEIGHBORS);
block != nullptr; block = block->next) { block != nullptr; block = block->next) {
try { try {
auto *explorer = explorers.emplace_front(CreateNeighborExplorer(loop,
CreateNeighborExplorer(loop, listener, *block); listener,
explorers.emplace_front(explorer); *block));
} catch (...) { } catch (...) {
std::throw_with_nested(FormatRuntimeError("Line %i: ", std::throw_with_nested(FormatRuntimeError("Line %i: ",
block->line)); block->line));
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include "thread/Mutex.hxx" #include "thread/Mutex.hxx"
#include <forward_list> #include <forward_list>
#include <memory>
class EventLoop; class EventLoop;
class NeighborExplorer; class NeighborExplorer;
...@@ -36,13 +37,13 @@ struct NeighborInfo; ...@@ -36,13 +37,13 @@ struct NeighborInfo;
*/ */
class NeighborGlue { class NeighborGlue {
struct Explorer { struct Explorer {
NeighborExplorer *const explorer; std::unique_ptr<NeighborExplorer> explorer;
Explorer(NeighborExplorer *_explorer) noexcept template<typename E>
:explorer(_explorer) {} Explorer(E &&_explorer) noexcept
:explorer(std::forward<E>(_explorer)) {}
Explorer(const Explorer &) = delete; Explorer(const Explorer &) = delete;
~Explorer() noexcept;
}; };
Mutex mutex; Mutex mutex;
...@@ -52,7 +53,7 @@ class NeighborGlue { ...@@ -52,7 +53,7 @@ class NeighborGlue {
public: public:
typedef std::forward_list<NeighborInfo> List; typedef std::forward_list<NeighborInfo> List;
NeighborGlue() = default; NeighborGlue() noexcept;
NeighborGlue(const NeighborGlue &) = delete; NeighborGlue(const NeighborGlue &) = delete;
~NeighborGlue() noexcept; ~NeighborGlue() noexcept;
......
...@@ -20,6 +20,8 @@ ...@@ -20,6 +20,8 @@
#ifndef MPD_NEIGHBOR_PLUGIN_HXX #ifndef MPD_NEIGHBOR_PLUGIN_HXX
#define MPD_NEIGHBOR_PLUGIN_HXX #define MPD_NEIGHBOR_PLUGIN_HXX
#include <memory>
struct ConfigBlock; struct ConfigBlock;
class EventLoop; class EventLoop;
class NeighborListener; class NeighborListener;
...@@ -31,8 +33,9 @@ struct NeighborPlugin { ...@@ -31,8 +33,9 @@ struct NeighborPlugin {
/** /**
* Allocates and configures a #NeighborExplorer instance. * Allocates and configures a #NeighborExplorer instance.
*/ */
NeighborExplorer *(*create)(EventLoop &loop, NeighborListener &listener, std::unique_ptr<NeighborExplorer> (*create)(EventLoop &loop,
const ConfigBlock &block); NeighborListener &listener,
const ConfigBlock &block);
}; };
#endif #endif
...@@ -252,14 +252,14 @@ SmbclientNeighborExplorer::ThreadFunc() ...@@ -252,14 +252,14 @@ SmbclientNeighborExplorer::ThreadFunc()
mutex.unlock(); mutex.unlock();
} }
static NeighborExplorer * static std::unique_ptr<NeighborExplorer>
smbclient_neighbor_create(gcc_unused EventLoop &loop, smbclient_neighbor_create(gcc_unused EventLoop &loop,
NeighborListener &listener, NeighborListener &listener,
gcc_unused const ConfigBlock &block) gcc_unused const ConfigBlock &block)
{ {
SmbclientInit(); SmbclientInit();
return new SmbclientNeighborExplorer(listener); return std::make_unique<SmbclientNeighborExplorer>(listener);
} }
const NeighborPlugin smbclient_neighbor_plugin = { const NeighborPlugin smbclient_neighbor_plugin = {
......
...@@ -127,12 +127,12 @@ UpnpNeighborExplorer::LostUPnP(const ContentDirectoryService &service) ...@@ -127,12 +127,12 @@ UpnpNeighborExplorer::LostUPnP(const ContentDirectoryService &service)
listener.LostNeighbor(n); listener.LostNeighbor(n);
} }
static NeighborExplorer * static std::unique_ptr<NeighborExplorer>
upnp_neighbor_create(EventLoop &event_loop, upnp_neighbor_create(EventLoop &event_loop,
NeighborListener &listener, NeighborListener &listener,
gcc_unused const ConfigBlock &block) gcc_unused const ConfigBlock &block)
{ {
return new UpnpNeighborExplorer(event_loop, listener); return std::make_unique<UpnpNeighborExplorer>(event_loop, listener);
} }
const NeighborPlugin upnp_neighbor_plugin = { const NeighborPlugin upnp_neighbor_plugin = {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment