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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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