playlist/SongEnumerator: wrap song in std::unique_ptr

This commit is contained in:
Max Kellermann 2016-02-07 08:23:30 +01:00
parent e2a0fd7a28
commit 1c5f76635a
13 changed files with 33 additions and 38 deletions

View File

@ -20,6 +20,7 @@
#include "config.h"
#include "CloseSongEnumerator.hxx"
#include "input/InputStream.hxx"
#include "DetachedSong.hxx"
CloseSongEnumerator::~CloseSongEnumerator()
{
@ -27,7 +28,7 @@ CloseSongEnumerator::~CloseSongEnumerator()
delete is;
}
DetachedSong *
std::unique_ptr<DetachedSong>
CloseSongEnumerator::NextSong()
{
return other->NextSong();

View File

@ -41,7 +41,7 @@ public:
virtual ~CloseSongEnumerator();
virtual DetachedSong *NextSong() override;
virtual std::unique_ptr<DetachedSong> NextSong() override;
};
#endif

View File

@ -20,13 +20,13 @@
#include "config.h"
#include "MemorySongEnumerator.hxx"
DetachedSong *
std::unique_ptr<DetachedSong>
MemorySongEnumerator::NextSong()
{
if (songs.empty())
return nullptr;
auto result = new DetachedSong(std::move(songs.front()));
std::unique_ptr<DetachedSong> result(new DetachedSong(std::move(songs.front())));
songs.pop_front();
return result;
}

View File

@ -33,7 +33,7 @@ public:
MemorySongEnumerator(std::forward_list<DetachedSong> &&_songs)
:songs(std::move(_songs)) {}
virtual DetachedSong *NextSong() override;
virtual std::unique_ptr<DetachedSong> NextSong() override;
};
#endif

View File

@ -44,24 +44,21 @@ playlist_load_into_queue(const char *uri, SongEnumerator &e,
? PathTraitsUTF8::GetParent(uri)
: std::string(".");
DetachedSong *song;
std::unique_ptr<DetachedSong> song;
for (unsigned i = 0;
i < end_index && (song = e.NextSong()) != nullptr;
++i) {
if (i < start_index) {
/* skip songs before the start index */
delete song;
continue;
}
if (!playlist_check_translate_song(*song, base_uri.c_str(),
loader)) {
delete song;
continue;
}
unsigned id = dest.AppendSong(pc, std::move(*song), error);
delete song;
if (id == 0)
return false;
}

View File

@ -41,7 +41,7 @@ playlist_provider_print(Response &r, Partition &partition,
? PathTraitsUTF8::GetParent(uri)
: std::string(".");
DetachedSong *song;
std::unique_ptr<DetachedSong> song;
while ((song = e.NextSong()) != nullptr) {
if (playlist_check_translate_song(*song, base_uri.c_str(),
loader) &&
@ -51,8 +51,6 @@ playlist_provider_print(Response &r, Partition &partition,
/* fallback if no detail was requested or no
detail was available */
song_print_uri(r, partition, *song);
delete song;
}
}

View File

@ -20,6 +20,8 @@
#ifndef MPD_SONG_ENUMERATOR_HXX
#define MPD_SONG_ENUMERATOR_HXX
#include <memory>
class DetachedSong;
/**
@ -31,11 +33,10 @@ public:
virtual ~SongEnumerator() {}
/**
* Obtain the next song. The caller is responsible for
* freeing the returned #Song object. Returns nullptr if
* there are no more songs.
* Obtain the next song. Returns nullptr if there are no more
* songs.
*/
virtual DetachedSong *NextSong() = 0;
virtual std::unique_ptr<DetachedSong> NextSong() = 0;
};
#endif

View File

@ -36,7 +36,7 @@ class CuePlaylist final : public SongEnumerator {
:is(_is), tis(is) {
}
virtual DetachedSong *NextSong() override;
virtual std::unique_ptr<DetachedSong> NextSong() override;
};
static SongEnumerator *
@ -45,23 +45,23 @@ cue_playlist_open_stream(InputStream &is)
return new CuePlaylist(is);
}
DetachedSong *
std::unique_ptr<DetachedSong>
CuePlaylist::NextSong()
{
auto song = parser.Get();
if (song != nullptr)
return song.release();
return song;
const char *line;
while ((line = tis.ReadLine()) != nullptr) {
parser.Feed(line);
song = parser.Get();
if (song != nullptr)
return song.release();
return song;
}
parser.Finish();
return parser.Get().release();
return parser.Get();
}
static const char *const cue_playlist_suffixes[] = {

View File

@ -69,7 +69,7 @@ public:
delete parser;
}
virtual DetachedSong *NextSong() override;
virtual std::unique_ptr<DetachedSong> NextSong() override;
};
static void
@ -124,13 +124,13 @@ embcue_playlist_open_uri(const char *uri,
return playlist;
}
DetachedSong *
std::unique_ptr<DetachedSong>
EmbeddedCuePlaylist::NextSong()
{
auto song = parser->Get();
if (song != nullptr) {
song->SetURI(filename);
return song.release();
return song;
}
while (*next != 0) {
@ -149,7 +149,7 @@ EmbeddedCuePlaylist::NextSong()
song = parser->Get();
if (song != nullptr) {
song->SetURI(filename);
return song.release();
return song;
}
}
@ -157,7 +157,7 @@ EmbeddedCuePlaylist::NextSong()
song = parser->Get();
if (song != nullptr)
song->SetURI(filename);
return song.release();
return song;
}
static const char *const embcue_playlist_suffixes[] = {

View File

@ -48,7 +48,7 @@ public:
return strcmp(line, "#EXTM3U") == 0;
}
virtual DetachedSong *NextSong() override;
virtual std::unique_ptr<DetachedSong> NextSong() override;
};
static SongEnumerator *
@ -105,7 +105,7 @@ extm3u_parse_tag(const char *line)
return tag.Commit();
}
DetachedSong *
std::unique_ptr<DetachedSong>
ExtM3uPlaylist::NextSong()
{
Tag tag;
@ -126,7 +126,7 @@ ExtM3uPlaylist::NextSong()
line_s = StripLeft(line_s);
} while (line_s[0] == '#' || *line_s == 0);
return new DetachedSong(line_s, std::move(tag));
return std::unique_ptr<DetachedSong>(new DetachedSong(line_s, std::move(tag)));
}
static const char *const extm3u_suffixes[] = {

View File

@ -58,10 +58,10 @@ public:
FLAC__metadata_object_delete(cuesheet);
}
virtual DetachedSong *NextSong() override;
virtual std::unique_ptr<DetachedSong> NextSong() override;
};
DetachedSong *
std::unique_ptr<DetachedSong>
FlacPlaylist::NextSong()
{
const FLAC__StreamMetadata_CueSheet &c = cuesheet->data.cue_sheet;
@ -82,7 +82,7 @@ FlacPlaylist::NextSong()
? c.tracks[next_track].offset
: total_samples;
auto *song = new DetachedSong(uri);
std::unique_ptr<DetachedSong> song(new DetachedSong(uri));
song->SetStartTime(SongTime::FromScale(start, sample_rate));
song->SetEndTime(SongTime::FromScale(end, sample_rate));
return song;

View File

@ -33,7 +33,7 @@ public:
:tis(is) {
}
virtual DetachedSong *NextSong() override;
virtual std::unique_ptr<DetachedSong> NextSong() override;
};
static SongEnumerator *
@ -42,7 +42,7 @@ m3u_open_stream(InputStream &is)
return new M3uPlaylist(is);
}
DetachedSong *
std::unique_ptr<DetachedSong>
M3uPlaylist::NextSong()
{
char *line_s;
@ -55,7 +55,7 @@ M3uPlaylist::NextSong()
line_s = Strip(line_s);
} while (line_s[0] == '#' || *line_s == 0);
return new DetachedSong(line_s);
return std::unique_ptr<DetachedSong>(new DetachedSong(line_s));
}
static const char *const m3u_suffixes[] = {

View File

@ -108,7 +108,7 @@ try {
/* dump the playlist */
DetachedSong *song;
std::unique_ptr<DetachedSong> song;
while ((song = playlist->NextSong()) != NULL) {
printf("%s\n", song->GetURI());
@ -127,8 +127,6 @@ try {
(start_ms / 1000) % 60);
tag_save(stdout, song->GetTag());
delete song;
}
/* deinitialize everything */