From 1c5f76635ab4bb4ad95da5d8d995b4ba84077307 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sun, 7 Feb 2016 08:23:30 +0100 Subject: [PATCH] playlist/SongEnumerator: wrap song in std::unique_ptr --- src/playlist/CloseSongEnumerator.cxx | 3 ++- src/playlist/CloseSongEnumerator.hxx | 2 +- src/playlist/MemorySongEnumerator.cxx | 4 ++-- src/playlist/MemorySongEnumerator.hxx | 2 +- src/playlist/PlaylistQueue.cxx | 5 +---- src/playlist/Print.cxx | 4 +--- src/playlist/SongEnumerator.hxx | 9 +++++---- src/playlist/plugins/CuePlaylistPlugin.cxx | 10 +++++----- src/playlist/plugins/EmbeddedCuePlaylistPlugin.cxx | 10 +++++----- src/playlist/plugins/ExtM3uPlaylistPlugin.cxx | 6 +++--- src/playlist/plugins/FlacPlaylistPlugin.cxx | 6 +++--- src/playlist/plugins/M3uPlaylistPlugin.cxx | 6 +++--- test/dump_playlist.cxx | 4 +--- 13 files changed, 33 insertions(+), 38 deletions(-) diff --git a/src/playlist/CloseSongEnumerator.cxx b/src/playlist/CloseSongEnumerator.cxx index 6a95fd66e..1637cfb0b 100644 --- a/src/playlist/CloseSongEnumerator.cxx +++ b/src/playlist/CloseSongEnumerator.cxx @@ -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 CloseSongEnumerator::NextSong() { return other->NextSong(); diff --git a/src/playlist/CloseSongEnumerator.hxx b/src/playlist/CloseSongEnumerator.hxx index f8e352f9b..adcd622eb 100644 --- a/src/playlist/CloseSongEnumerator.hxx +++ b/src/playlist/CloseSongEnumerator.hxx @@ -41,7 +41,7 @@ public: virtual ~CloseSongEnumerator(); - virtual DetachedSong *NextSong() override; + virtual std::unique_ptr NextSong() override; }; #endif diff --git a/src/playlist/MemorySongEnumerator.cxx b/src/playlist/MemorySongEnumerator.cxx index e34a8d628..0bbadad01 100644 --- a/src/playlist/MemorySongEnumerator.cxx +++ b/src/playlist/MemorySongEnumerator.cxx @@ -20,13 +20,13 @@ #include "config.h" #include "MemorySongEnumerator.hxx" -DetachedSong * +std::unique_ptr MemorySongEnumerator::NextSong() { if (songs.empty()) return nullptr; - auto result = new DetachedSong(std::move(songs.front())); + std::unique_ptr result(new DetachedSong(std::move(songs.front()))); songs.pop_front(); return result; } diff --git a/src/playlist/MemorySongEnumerator.hxx b/src/playlist/MemorySongEnumerator.hxx index 5a1493810..f8981046b 100644 --- a/src/playlist/MemorySongEnumerator.hxx +++ b/src/playlist/MemorySongEnumerator.hxx @@ -33,7 +33,7 @@ public: MemorySongEnumerator(std::forward_list &&_songs) :songs(std::move(_songs)) {} - virtual DetachedSong *NextSong() override; + virtual std::unique_ptr NextSong() override; }; #endif diff --git a/src/playlist/PlaylistQueue.cxx b/src/playlist/PlaylistQueue.cxx index b6f85f586..f6e4c8da6 100644 --- a/src/playlist/PlaylistQueue.cxx +++ b/src/playlist/PlaylistQueue.cxx @@ -44,24 +44,21 @@ playlist_load_into_queue(const char *uri, SongEnumerator &e, ? PathTraitsUTF8::GetParent(uri) : std::string("."); - DetachedSong *song; + std::unique_ptr 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; } diff --git a/src/playlist/Print.cxx b/src/playlist/Print.cxx index 13e45d160..b1222dc63 100644 --- a/src/playlist/Print.cxx +++ b/src/playlist/Print.cxx @@ -41,7 +41,7 @@ playlist_provider_print(Response &r, Partition &partition, ? PathTraitsUTF8::GetParent(uri) : std::string("."); - DetachedSong *song; + std::unique_ptr 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; } } diff --git a/src/playlist/SongEnumerator.hxx b/src/playlist/SongEnumerator.hxx index 0f6997785..9a14c74b4 100644 --- a/src/playlist/SongEnumerator.hxx +++ b/src/playlist/SongEnumerator.hxx @@ -20,6 +20,8 @@ #ifndef MPD_SONG_ENUMERATOR_HXX #define MPD_SONG_ENUMERATOR_HXX +#include + 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 NextSong() = 0; }; #endif diff --git a/src/playlist/plugins/CuePlaylistPlugin.cxx b/src/playlist/plugins/CuePlaylistPlugin.cxx index 4b635f71f..4aa253528 100644 --- a/src/playlist/plugins/CuePlaylistPlugin.cxx +++ b/src/playlist/plugins/CuePlaylistPlugin.cxx @@ -36,7 +36,7 @@ class CuePlaylist final : public SongEnumerator { :is(_is), tis(is) { } - virtual DetachedSong *NextSong() override; + virtual std::unique_ptr NextSong() override; }; static SongEnumerator * @@ -45,23 +45,23 @@ cue_playlist_open_stream(InputStream &is) return new CuePlaylist(is); } -DetachedSong * +std::unique_ptr 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[] = { diff --git a/src/playlist/plugins/EmbeddedCuePlaylistPlugin.cxx b/src/playlist/plugins/EmbeddedCuePlaylistPlugin.cxx index 72fb75253..a7c683c13 100644 --- a/src/playlist/plugins/EmbeddedCuePlaylistPlugin.cxx +++ b/src/playlist/plugins/EmbeddedCuePlaylistPlugin.cxx @@ -69,7 +69,7 @@ public: delete parser; } - virtual DetachedSong *NextSong() override; + virtual std::unique_ptr NextSong() override; }; static void @@ -124,13 +124,13 @@ embcue_playlist_open_uri(const char *uri, return playlist; } -DetachedSong * +std::unique_ptr 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[] = { diff --git a/src/playlist/plugins/ExtM3uPlaylistPlugin.cxx b/src/playlist/plugins/ExtM3uPlaylistPlugin.cxx index 37f975dda..53f863650 100644 --- a/src/playlist/plugins/ExtM3uPlaylistPlugin.cxx +++ b/src/playlist/plugins/ExtM3uPlaylistPlugin.cxx @@ -48,7 +48,7 @@ public: return strcmp(line, "#EXTM3U") == 0; } - virtual DetachedSong *NextSong() override; + virtual std::unique_ptr NextSong() override; }; static SongEnumerator * @@ -105,7 +105,7 @@ extm3u_parse_tag(const char *line) return tag.Commit(); } -DetachedSong * +std::unique_ptr 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(new DetachedSong(line_s, std::move(tag))); } static const char *const extm3u_suffixes[] = { diff --git a/src/playlist/plugins/FlacPlaylistPlugin.cxx b/src/playlist/plugins/FlacPlaylistPlugin.cxx index 19b77ef32..591312af9 100644 --- a/src/playlist/plugins/FlacPlaylistPlugin.cxx +++ b/src/playlist/plugins/FlacPlaylistPlugin.cxx @@ -58,10 +58,10 @@ public: FLAC__metadata_object_delete(cuesheet); } - virtual DetachedSong *NextSong() override; + virtual std::unique_ptr NextSong() override; }; -DetachedSong * +std::unique_ptr 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 song(new DetachedSong(uri)); song->SetStartTime(SongTime::FromScale(start, sample_rate)); song->SetEndTime(SongTime::FromScale(end, sample_rate)); return song; diff --git a/src/playlist/plugins/M3uPlaylistPlugin.cxx b/src/playlist/plugins/M3uPlaylistPlugin.cxx index 9e7647dd7..0b1bfda1b 100644 --- a/src/playlist/plugins/M3uPlaylistPlugin.cxx +++ b/src/playlist/plugins/M3uPlaylistPlugin.cxx @@ -33,7 +33,7 @@ public: :tis(is) { } - virtual DetachedSong *NextSong() override; + virtual std::unique_ptr NextSong() override; }; static SongEnumerator * @@ -42,7 +42,7 @@ m3u_open_stream(InputStream &is) return new M3uPlaylist(is); } -DetachedSong * +std::unique_ptr 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(new DetachedSong(line_s)); } static const char *const m3u_suffixes[] = { diff --git a/test/dump_playlist.cxx b/test/dump_playlist.cxx index b6659849a..7ae06eacf 100644 --- a/test/dump_playlist.cxx +++ b/test/dump_playlist.cxx @@ -108,7 +108,7 @@ try { /* dump the playlist */ - DetachedSong *song; + std::unique_ptr 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 */