diff --git a/src/SongUpdate.cxx b/src/SongUpdate.cxx index 0d22ff1d8..9887da507 100644 --- a/src/SongUpdate.cxx +++ b/src/SongUpdate.cxx @@ -41,18 +41,16 @@ #ifdef ENABLE_DATABASE -Song * +SongPtr Song::LoadFile(Storage &storage, const char *path_utf8, Directory &parent) noexcept { assert(!uri_has_scheme(path_utf8)); assert(strchr(path_utf8, '\n') == nullptr); - Song *song = NewFile(path_utf8, parent); - if (!song->UpdateFile(storage)) { - song->Free(); + auto song = NewFile(path_utf8, parent); + if (!song->UpdateFile(storage)) return nullptr; - } return song; } @@ -102,19 +100,17 @@ Song::UpdateFile(Storage &storage) noexcept #ifdef ENABLE_ARCHIVE -Song * +SongPtr Song::LoadFromArchive(ArchiveFile &archive, const char *name_utf8, Directory &parent) noexcept { assert(!uri_has_scheme(name_utf8)); assert(strchr(name_utf8, '\n') == nullptr); - Song *song = NewFile(name_utf8, parent); + auto song = NewFile(name_utf8, parent); - if (!song->UpdateFileInArchive(archive)) { - song->Free(); + if (!song->UpdateFileInArchive(archive)) return nullptr; - } return song; } diff --git a/src/db/plugins/simple/Directory.cxx b/src/db/plugins/simple/Directory.cxx index 62d3a6075..e4a279dc1 100644 --- a/src/db/plugins/simple/Directory.cxx +++ b/src/db/plugins/simple/Directory.cxx @@ -164,13 +164,13 @@ Directory::LookupDirectory(const char *uri) noexcept } void -Directory::AddSong(Song *song) noexcept +Directory::AddSong(SongPtr song) noexcept { assert(holding_db_lock()); assert(song != nullptr); assert(song->parent == this); - songs.push_back(*song); + songs.push_back(*song.release()); } void diff --git a/src/db/plugins/simple/Directory.hxx b/src/db/plugins/simple/Directory.hxx index c8a429e4f..540cfec3c 100644 --- a/src/db/plugins/simple/Directory.hxx +++ b/src/db/plugins/simple/Directory.hxx @@ -20,6 +20,7 @@ #ifndef MPD_DIRECTORY_HXX #define MPD_DIRECTORY_HXX +#include "Ptr.hxx" #include "util/Compiler.h" #include "db/Visitor.hxx" #include "db/PlaylistVector.hxx" @@ -242,7 +243,7 @@ public: * Add a song object to this directory. Its "parent" attribute must * be set already. */ - void AddSong(Song *song) noexcept; + void AddSong(SongPtr song) noexcept; /** * Remove a song object from this directory (which effectively diff --git a/src/db/plugins/simple/DirectorySave.cxx b/src/db/plugins/simple/DirectorySave.cxx index 40b71f9eb..1349f7b7e 100644 --- a/src/db/plugins/simple/DirectorySave.cxx +++ b/src/db/plugins/simple/DirectorySave.cxx @@ -168,7 +168,7 @@ directory_load(TextFile &file, Directory &directory) directory); song->audio_format = audio_format; - directory.AddSong(song); + directory.AddSong(std::move(song)); } else if ((p = StringAfterPrefix(line, PLAYLIST_META_BEGIN))) { const char *name = p; playlist_metadata_load(file, directory.playlists, name); diff --git a/src/db/plugins/simple/Ptr.hxx b/src/db/plugins/simple/Ptr.hxx new file mode 100644 index 000000000..b649b6395 --- /dev/null +++ b/src/db/plugins/simple/Ptr.hxx @@ -0,0 +1,29 @@ +/* + * Copyright 2003-2019 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_SONG_PTR_HXX +#define MPD_SONG_PTR_HXX + +#include "Disposer.hxx" + +#include + +using SongPtr = std::unique_ptr; + +#endif diff --git a/src/db/plugins/simple/Song.cxx b/src/db/plugins/simple/Song.cxx index 409c2c440..b43ab60f7 100644 --- a/src/db/plugins/simple/Song.cxx +++ b/src/db/plugins/simple/Song.cxx @@ -40,7 +40,7 @@ Song::~Song() noexcept { } -static Song * +static SongPtr song_alloc(const char *uri, Directory &parent) noexcept { size_t uri_length; @@ -49,15 +49,16 @@ song_alloc(const char *uri, Directory &parent) noexcept uri_length = strlen(uri); assert(uri_length); - return NewVarSize(sizeof(Song::uri), - uri_length + 1, - uri, uri_length, parent); + auto *song = NewVarSize(sizeof(Song::uri), + uri_length + 1, + uri, uri_length, parent); + return SongPtr(song); } -Song * +SongPtr Song::NewFrom(DetachedSong &&other, Directory &parent) noexcept { - Song *song = song_alloc(other.GetURI(), parent); + SongPtr song(song_alloc(other.GetURI(), parent)); song->tag = std::move(other.WritableTag()); song->mtime = other.GetLastModified(); song->start_time = other.GetStartTime(); @@ -65,10 +66,10 @@ Song::NewFrom(DetachedSong &&other, Directory &parent) noexcept return song; } -Song * +SongPtr Song::NewFile(const char *path, Directory &parent) noexcept { - return song_alloc(path, parent); + return SongPtr(song_alloc(path, parent)); } void diff --git a/src/db/plugins/simple/Song.hxx b/src/db/plugins/simple/Song.hxx index 34f5161ea..23056cbbf 100644 --- a/src/db/plugins/simple/Song.hxx +++ b/src/db/plugins/simple/Song.hxx @@ -20,6 +20,7 @@ #ifndef MPD_SONG_HXX #define MPD_SONG_HXX +#include "Ptr.hxx" #include "Chrono.hxx" #include "tag/Tag.hxx" #include "AudioFormat.hxx" @@ -95,30 +96,27 @@ struct Song { Song(const char *_uri, size_t uri_length, Directory &parent) noexcept; ~Song() noexcept; - gcc_malloc gcc_returns_nonnull - static Song *NewFrom(DetachedSong &&other, Directory &parent) noexcept; + static SongPtr NewFrom(DetachedSong &&other, Directory &parent) noexcept; /** allocate a new song with a local file name */ - gcc_malloc gcc_returns_nonnull - static Song *NewFile(const char *path_utf8, Directory &parent) noexcept; + static SongPtr NewFile(const char *path_utf8, Directory &parent) noexcept; /** * allocate a new song structure with a local file name and attempt to * load its metadata. If all decoder plugin fail to read its meta * data, nullptr is returned. */ - gcc_malloc - static Song *LoadFile(Storage &storage, const char *name_utf8, - Directory &parent) noexcept; + static SongPtr LoadFile(Storage &storage, const char *name_utf8, + Directory &parent) noexcept; void Free() noexcept; bool UpdateFile(Storage &storage) noexcept; #ifdef ENABLE_ARCHIVE - static Song *LoadFromArchive(ArchiveFile &archive, - const char *name_utf8, - Directory &parent) noexcept; + static SongPtr LoadFromArchive(ArchiveFile &archive, + const char *name_utf8, + Directory &parent) noexcept; bool UpdateFileInArchive(ArchiveFile &archive) noexcept; #endif diff --git a/src/db/update/Archive.cxx b/src/db/update/Archive.cxx index 35a1f0ad9..7e37febcc 100644 --- a/src/db/update/Archive.cxx +++ b/src/db/update/Archive.cxx @@ -82,11 +82,11 @@ UpdateWalk::UpdateArchiveTree(ArchiveFile &archive, Directory &directory, //add file Song *song = LockFindSong(directory, name); if (song == nullptr) { - song = Song::LoadFromArchive(archive, name, directory); - if (song != nullptr) { + auto new_song = Song::LoadFromArchive(archive, name, directory); + if (!new_song) { { const ScopeDatabaseLock protect; - directory.AddSong(song); + directory.AddSong(std::move(new_song)); } modified = true; diff --git a/src/db/update/Container.cxx b/src/db/update/Container.cxx index 35881ac48..9e5f67dc7 100644 --- a/src/db/update/Container.cxx +++ b/src/db/update/Container.cxx @@ -102,8 +102,7 @@ UpdateWalk::UpdateContainerFile(Directory &directory, } for (auto &vtrack : v) { - Song *song = Song::NewFrom(std::move(vtrack), - *contdir); + auto song = Song::NewFrom(std::move(vtrack), *contdir); // shouldn't be necessary but it's there.. song->mtime = info.mtime; @@ -113,7 +112,7 @@ UpdateWalk::UpdateContainerFile(Directory &directory, { const ScopeDatabaseLock protect; - contdir->AddSong(song); + contdir->AddSong(std::move(song)); } modified = true; diff --git a/src/db/update/UpdateSong.cxx b/src/db/update/UpdateSong.cxx index 6b6ff4173..c5c032618 100644 --- a/src/db/update/UpdateSong.cxx +++ b/src/db/update/UpdateSong.cxx @@ -61,8 +61,8 @@ UpdateWalk::UpdateSongFile2(Directory &directory, if (song == nullptr) { FormatDebug(update_domain, "reading %s/%s", directory.GetPath(), name); - song = Song::LoadFile(storage, name, directory); - if (song == nullptr) { + auto new_song = Song::LoadFile(storage, name, directory); + if (!new_song) { FormatDebug(update_domain, "ignoring unrecognized file %s/%s", directory.GetPath(), name); @@ -71,7 +71,7 @@ UpdateWalk::UpdateSongFile2(Directory &directory, { const ScopeDatabaseLock protect; - directory.AddSong(song); + directory.AddSong(std::move(new_song)); } modified = true;