neighbor/Plugin: std::unique_ptr<NeighborExplorer>

This commit is contained in:
Max Kellermann 2018-01-02 17:00:27 +01:00
parent cd6de3b24e
commit 201210cfe1
5 changed files with 20 additions and 20 deletions

View File

@ -30,14 +30,10 @@
#include <stdexcept>
NeighborGlue::Explorer::~Explorer() noexcept
{
delete explorer;
}
NeighborGlue::NeighborGlue() noexcept {}
NeighborGlue::~NeighborGlue() noexcept {}
static NeighborExplorer *
static std::unique_ptr<NeighborExplorer>
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));

View File

@ -25,6 +25,7 @@
#include "thread/Mutex.hxx"
#include <forward_list>
#include <memory>
class EventLoop;
class NeighborExplorer;
@ -36,13 +37,13 @@ struct NeighborInfo;
*/
class NeighborGlue {
struct Explorer {
NeighborExplorer *const explorer;
std::unique_ptr<NeighborExplorer> explorer;
Explorer(NeighborExplorer *_explorer) noexcept
:explorer(_explorer) {}
template<typename E>
Explorer(E &&_explorer) noexcept
:explorer(std::forward<E>(_explorer)) {}
Explorer(const Explorer &) = delete;
~Explorer() noexcept;
};
Mutex mutex;
@ -52,7 +53,7 @@ class NeighborGlue {
public:
typedef std::forward_list<NeighborInfo> List;
NeighborGlue() = default;
NeighborGlue() noexcept;
NeighborGlue(const NeighborGlue &) = delete;
~NeighborGlue() noexcept;

View File

@ -20,6 +20,8 @@
#ifndef MPD_NEIGHBOR_PLUGIN_HXX
#define MPD_NEIGHBOR_PLUGIN_HXX
#include <memory>
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<NeighborExplorer> (*create)(EventLoop &loop,
NeighborListener &listener,
const ConfigBlock &block);
};
#endif

View File

@ -252,14 +252,14 @@ SmbclientNeighborExplorer::ThreadFunc()
mutex.unlock();
}
static NeighborExplorer *
static std::unique_ptr<NeighborExplorer>
smbclient_neighbor_create(gcc_unused EventLoop &loop,
NeighborListener &listener,
gcc_unused const ConfigBlock &block)
{
SmbclientInit();
return new SmbclientNeighborExplorer(listener);
return std::make_unique<SmbclientNeighborExplorer>(listener);
}
const NeighborPlugin smbclient_neighbor_plugin = {

View File

@ -127,12 +127,12 @@ UpnpNeighborExplorer::LostUPnP(const ContentDirectoryService &service)
listener.LostNeighbor(n);
}
static NeighborExplorer *
static std::unique_ptr<NeighborExplorer>
upnp_neighbor_create(EventLoop &event_loop,
NeighborListener &listener,
gcc_unused const ConfigBlock &block)
{
return new UpnpNeighborExplorer(event_loop, listener);
return std::make_unique<UpnpNeighborExplorer>(event_loop, listener);
}
const NeighborPlugin upnp_neighbor_plugin = {