mpd: use unique_ptr for storage of mpd_connection

This commit is contained in:
Andrzej Rybczak
2014-11-01 22:09:31 +01:00
parent 72726831ca
commit cb6d8c69cd
2 changed files with 25 additions and 24 deletions

View File

@@ -45,10 +45,7 @@ void Connection::Connect()
assert(!m_connection);
try
{
m_connection = std::shared_ptr<mpd_connection>(
mpd_connection_new(m_host.c_str(), m_port, m_timeout * 1000),
mpd_connection_free
);
m_connection.reset(mpd_connection_new(m_host.c_str(), m_port, m_timeout * 1000));
checkErrors();
if (!m_password.empty())
SendPassword();
@@ -298,7 +295,7 @@ SongIterator Connection::GetPlaylistChanges(unsigned version)
prechecksNoCommandsList();
mpd_send_queue_changes_meta(m_connection.get(), version);
checkErrors();
return SongIterator(m_connection, mpd_recv_song);
return SongIterator(m_connection.get(), mpd_recv_song);
}
Song Connection::GetCurrentSong()
@@ -325,7 +322,7 @@ SongIterator Connection::GetPlaylistContent(const std::string &path)
{
prechecksNoCommandsList();
mpd_send_list_playlist_meta(m_connection.get(), path.c_str());
SongIterator result(m_connection, mpd_recv_song);
SongIterator result(m_connection.get(), mpd_recv_song);
checkErrors();
return result;
}
@@ -334,7 +331,7 @@ SongIterator Connection::GetPlaylistContentNoInfo(const std::string &path)
{
prechecksNoCommandsList();
mpd_send_list_playlist(m_connection.get(), path.c_str());
SongIterator result(m_connection, mpd_recv_song);
SongIterator result(m_connection.get(), mpd_recv_song);
checkErrors();
return result;
}
@@ -664,7 +661,7 @@ SongIterator Connection::CommitSearchSongs()
prechecksNoCommandsList();
mpd_search_commit(m_connection.get());
checkErrors();
return SongIterator(m_connection, mpd_recv_song);
return SongIterator(m_connection.get(), mpd_recv_song);
}
void Connection::CommitSearchTags(StringConsumer f)
@@ -742,7 +739,7 @@ SongIterator Connection::GetSongs(const std::string &directory)
prechecksNoCommandsList();
mpd_send_list_meta(m_connection.get(), directory.c_str());
checkErrors();
return SongIterator(m_connection, mpd_recv_song);
return SongIterator(m_connection.get(), mpd_recv_song);
}
OutputIterator Connection::GetOutputs()
@@ -750,7 +747,7 @@ OutputIterator Connection::GetOutputs()
prechecksNoCommandsList();
mpd_send_outputs(m_connection.get());
checkErrors();
return OutputIterator(m_connection, mpd_recv_output);
return OutputIterator(m_connection.get(), mpd_recv_output);
}
void Connection::EnableOutput(int id)

View File

@@ -21,7 +21,6 @@
#ifndef NCMPCPP_MPDPP_H
#define NCMPCPP_MPDPP_H
#include <boost/noncopyable.hpp>
#include <cassert>
#include <exception>
#include <set>
@@ -148,7 +147,7 @@ struct Output
return true;
else if (!empty() && !rhs.empty())
return id() == rhs.id()
&& std::strcmp(name(), rhs.name()) == 0
&& strcmp(name(), rhs.name()) == 0
&& enabled() == rhs.enabled();
else
return false;
@@ -200,22 +199,22 @@ struct Iterator : std::iterator<std::forward_iterator_tag, DestT>
Iterator() : m_connection(nullptr), m_fetch_source(nullptr) { }
~Iterator()
{
if (m_connection)
if (m_connection != nullptr)
finish();
}
void finish()
{
// clean up
assert(m_connection);
mpd_response_finish(m_connection.get());
assert(m_connection != nullptr);
mpd_response_finish(m_connection);
m_object = DestT();
m_connection = nullptr;
}
DestT &operator*() const
{
assert(m_connection);
assert(m_connection != nullptr);
if (m_object.empty())
throw std::runtime_error("empty object");
return const_cast<DestT &>(m_object);
@@ -227,9 +226,9 @@ struct Iterator : std::iterator<std::forward_iterator_tag, DestT>
Iterator &operator++()
{
assert(m_connection);
assert(m_connection != nullptr);
assert(m_fetch_source != nullptr);
auto src = m_fetch_source(m_connection.get());
auto src = m_fetch_source(m_connection);
if (src != nullptr)
m_object = DestT(src);
else
@@ -254,14 +253,14 @@ struct Iterator : std::iterator<std::forward_iterator_tag, DestT>
}
private:
Iterator(std::shared_ptr<mpd_connection> conn, SourceFetcher fetch_source)
: m_connection(std::move(conn)), m_fetch_source(fetch_source)
Iterator(mpd_connection *connection, SourceFetcher fetch_source)
: m_connection(connection), m_fetch_source(fetch_source)
{
// get the first element
++*this;
}
std::shared_ptr<mpd_connection> m_connection;
mpd_connection *m_connection;
SourceFetcher m_fetch_source;
DestT m_object;
};
@@ -269,7 +268,7 @@ private:
typedef Iterator<Song, mpd_song> SongIterator;
typedef Iterator<Output, mpd_output> OutputIterator;
class Connection : private boost::noncopyable
class Connection
{
typedef std::function<void(Item)> ItemConsumer;
typedef std::function<void(Output)> OutputConsumer;
@@ -381,13 +380,18 @@ public:
int noidle();
private:
struct ConnectionDeleter {
void operator()(mpd_connection *connection) {
mpd_connection_free(connection);
}
};
void checkConnection() const;
void prechecks();
void prechecksNoCommandsList();
void checkErrors() const;
std::shared_ptr<mpd_connection> m_connection;
std::unique_ptr<mpd_connection, ConnectionDeleter> m_connection;
bool m_command_list_active;
int m_fd;