SongLoader: return instance, not pointer

This commit is contained in:
Max Kellermann 2017-02-08 10:02:08 +01:00
parent d184231169
commit f689e28958
6 changed files with 23 additions and 35 deletions

View File

@ -359,8 +359,7 @@ void
spl_append_uri(const char *utf8file, spl_append_uri(const char *utf8file,
const SongLoader &loader, const char *url) const SongLoader &loader, const char *url)
{ {
std::unique_ptr<DetachedSong> song(loader.LoadSong(url)); spl_append_song(utf8file, loader.LoadSong(url));
spl_append_song(utf8file, *song);
} }
static void static void

View File

@ -36,12 +36,12 @@ SongLoader::SongLoader(const Client &_client)
#endif #endif
DetachedSong * DetachedSong
SongLoader::LoadFromDatabase(const char *uri) const SongLoader::LoadFromDatabase(const char *uri) const
{ {
#ifdef ENABLE_DATABASE #ifdef ENABLE_DATABASE
if (db != nullptr) if (db != nullptr)
return new DetachedSong(DatabaseDetachSong(*db, *storage, uri)); return DatabaseDetachSong(*db, *storage, uri);
#else #else
(void)uri; (void)uri;
#endif #endif
@ -49,7 +49,7 @@ SongLoader::LoadFromDatabase(const char *uri) const
throw PlaylistError(PlaylistResult::NO_SUCH_SONG, "No database"); throw PlaylistError(PlaylistResult::NO_SUCH_SONG, "No database");
} }
DetachedSong * DetachedSong
SongLoader::LoadFile(const char *path_utf8, Path path_fs) const SongLoader::LoadFile(const char *path_utf8, Path path_fs) const
{ {
#ifdef ENABLE_DATABASE #ifdef ENABLE_DATABASE
@ -66,15 +66,15 @@ SongLoader::LoadFile(const char *path_utf8, Path path_fs) const
if (!song.LoadFile(path_fs)) if (!song.LoadFile(path_fs))
throw PlaylistError::NoSuchSong(); throw PlaylistError::NoSuchSong();
return new DetachedSong(std::move(song)); return song;
} }
DetachedSong * DetachedSong
SongLoader::LoadSong(const LocatedUri &located_uri) const SongLoader::LoadSong(const LocatedUri &located_uri) const
{ {
switch (located_uri.type) { switch (located_uri.type) {
case LocatedUri::Type::ABSOLUTE: case LocatedUri::Type::ABSOLUTE:
return new DetachedSong(located_uri.canonical_uri); return DetachedSong(located_uri.canonical_uri);
case LocatedUri::Type::RELATIVE: case LocatedUri::Type::RELATIVE:
return LoadFromDatabase(located_uri.canonical_uri); return LoadFromDatabase(located_uri.canonical_uri);
@ -86,7 +86,7 @@ SongLoader::LoadSong(const LocatedUri &located_uri) const
gcc_unreachable(); gcc_unreachable();
} }
DetachedSong * DetachedSong
SongLoader::LoadSong(const char *uri_utf8) const SongLoader::LoadSong(const char *uri_utf8) const
{ {
#if !CLANG_CHECK_VERSION(3,6) #if !CLANG_CHECK_VERSION(3,6)

View File

@ -67,20 +67,20 @@ public:
} }
#endif #endif
DetachedSong *LoadSong(const LocatedUri &uri) const; DetachedSong LoadSong(const LocatedUri &uri) const;
/** /**
* Throws #std::runtime_error on error. * Throws #std::runtime_error on error.
*/ */
gcc_nonnull_all gcc_nonnull_all
DetachedSong *LoadSong(const char *uri_utf8) const; DetachedSong LoadSong(const char *uri_utf8) const;
private: private:
gcc_nonnull_all gcc_nonnull_all
DetachedSong *LoadFromDatabase(const char *uri) const; DetachedSong LoadFromDatabase(const char *uri) const;
gcc_nonnull_all gcc_nonnull_all
DetachedSong *LoadFile(const char *path_utf8, Path path_fs) const; DetachedSong LoadFile(const char *path_utf8, Path path_fs) const;
}; };
#endif #endif

View File

@ -43,11 +43,9 @@
static void static void
AddUri(Client &client, const LocatedUri &uri) AddUri(Client &client, const LocatedUri &uri)
{ {
std::unique_ptr<DetachedSong> song(SongLoader(client).LoadSong(uri));
assert(song);
auto &partition = client.partition; auto &partition = client.partition;
partition.playlist.AppendSong(partition.pc, std::move(*song)); partition.playlist.AppendSong(partition.pc,
SongLoader(client).LoadSong(uri));
} }
static CommandResult static CommandResult

View File

@ -44,25 +44,17 @@ merge_song_metadata(DetachedSong &add, const DetachedSong &base)
static bool static bool
playlist_check_load_song(DetachedSong &song, const SongLoader &loader) playlist_check_load_song(DetachedSong &song, const SongLoader &loader)
{ try {
DetachedSong *tmp; DetachedSong tmp = loader.LoadSong(song.GetURI());
try { song.SetURI(tmp.GetURI());
tmp = loader.LoadSong(song.GetURI()); if (!song.HasRealURI() && tmp.HasRealURI())
} catch (const std::runtime_error &) { song.SetRealURI(tmp.GetRealURI());
return false;
}
if (tmp == nullptr) merge_song_metadata(song, tmp);
return false;
song.SetURI(tmp->GetURI());
if (!song.HasRealURI() && tmp->HasRealURI())
song.SetRealURI(tmp->GetRealURI());
merge_song_metadata(song, *tmp);
delete tmp;
return true; return true;
} catch (const std::runtime_error &) {
return false;
} }
bool bool

View File

@ -125,8 +125,7 @@ unsigned
playlist::AppendURI(PlayerControl &pc, const SongLoader &loader, playlist::AppendURI(PlayerControl &pc, const SongLoader &loader,
const char *uri) const char *uri)
{ {
std::unique_ptr<DetachedSong> song(loader.LoadSong(uri)); return AppendSong(pc, loader.LoadSong(uri));
return AppendSong(pc, std::move(*song));
} }
void void