From 57fb153c5d000905016d3e2c2a67186397d5066b Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 3 Apr 2020 16:33:45 +0200 Subject: [PATCH] db/Interface: pass std::string_view to GetSong() --- src/db/Interface.hxx | 2 +- src/db/Uri.hxx | 6 ------ src/db/plugins/ProxyDatabasePlugin.cxx | 6 +++--- src/db/plugins/simple/SimpleDatabasePlugin.cxx | 8 +++----- src/db/plugins/simple/SimpleDatabasePlugin.hxx | 2 +- src/db/plugins/upnp/UpnpDatabasePlugin.cxx | 9 +++++---- 6 files changed, 13 insertions(+), 20 deletions(-) diff --git a/src/db/Interface.hxx b/src/db/Interface.hxx index 708eb240c..24500fbdb 100644 --- a/src/db/Interface.hxx +++ b/src/db/Interface.hxx @@ -75,7 +75,7 @@ public: * directory (UTF-8) * @return a pointer that must be released with ReturnSong() */ - virtual const LightSong *GetSong(const char *uri_utf8) const = 0; + virtual const LightSong *GetSong(std::string_view uri_utf8) const = 0; /** * Mark the song object as "unused". Call this on objects diff --git a/src/db/Uri.hxx b/src/db/Uri.hxx index cb85d0ff2..d5b99e0c7 100644 --- a/src/db/Uri.hxx +++ b/src/db/Uri.hxx @@ -22,12 +22,6 @@ #include -static inline bool -isRootDirectory(const char *name) -{ - return name[0] == 0 || (name[0] == '/' && name[1] == 0); -} - static inline bool isRootDirectory(std::string_view name) noexcept { diff --git a/src/db/plugins/ProxyDatabasePlugin.cxx b/src/db/plugins/ProxyDatabasePlugin.cxx index 0bc318b6e..df62041a8 100644 --- a/src/db/plugins/ProxyDatabasePlugin.cxx +++ b/src/db/plugins/ProxyDatabasePlugin.cxx @@ -121,7 +121,7 @@ public: void Open() override; void Close() noexcept override; - const LightSong *GetSong(const char *uri_utf8) const override; + const LightSong *GetSong(std::string_view uri_utf8) const override; void ReturnSong(const LightSong *song) const noexcept override; void Visit(const DatabaseSelection &selection, @@ -641,12 +641,12 @@ ProxyDatabase::OnIdle() noexcept } const LightSong * -ProxyDatabase::GetSong(const char *uri) const +ProxyDatabase::GetSong(std::string_view uri) const { // TODO: eliminate the const_cast const_cast(this)->EnsureConnected(); - if (!mpd_send_list_meta(connection, uri)) + if (!mpd_send_list_meta(connection, std::string(uri).c_str())) ThrowError(connection); struct mpd_song *song = mpd_recv_song(connection); diff --git a/src/db/plugins/simple/SimpleDatabasePlugin.cxx b/src/db/plugins/simple/SimpleDatabasePlugin.cxx index 71c40c55e..b9bdeff55 100644 --- a/src/db/plugins/simple/SimpleDatabasePlugin.cxx +++ b/src/db/plugins/simple/SimpleDatabasePlugin.cxx @@ -197,7 +197,7 @@ SimpleDatabase::Close() noexcept } const LightSong * -SimpleDatabase::GetSong(const char *uri) const +SimpleDatabase::GetSong(std::string_view uri) const { assert(root != nullptr); assert(prefixed_light_song == nullptr); @@ -211,10 +211,8 @@ SimpleDatabase::GetSong(const char *uri) const /* pass the request to the mounted database */ protect.unlock(); - /* note: r.rest.data() is actually null-terminated - because it points inside the "uri" parameter */ const LightSong *song = - r.directory->mounted_database->GetSong(r.rest.data()); + r.directory->mounted_database->GetSong(r.rest); if (song == nullptr) return nullptr; @@ -224,7 +222,7 @@ SimpleDatabase::GetSong(const char *uri) const return prefixed_light_song; } - if (r.rest.data() == nullptr) + if (r.rest.empty()) /* it's a directory */ throw DatabaseError(DatabaseErrorCode::NOT_FOUND, "No such song"); diff --git a/src/db/plugins/simple/SimpleDatabasePlugin.hxx b/src/db/plugins/simple/SimpleDatabasePlugin.hxx index 73cc901d4..ac4f067d1 100644 --- a/src/db/plugins/simple/SimpleDatabasePlugin.hxx +++ b/src/db/plugins/simple/SimpleDatabasePlugin.hxx @@ -114,7 +114,7 @@ public: void Open() override; void Close() noexcept override; - const LightSong *GetSong(const char *uri_utf8) const override; + const LightSong *GetSong(std::string_view uri_utf8) const override; void ReturnSong(const LightSong *song) const noexcept override; void Visit(const DatabaseSelection &selection, diff --git a/src/db/plugins/upnp/UpnpDatabasePlugin.cxx b/src/db/plugins/upnp/UpnpDatabasePlugin.cxx index ac6240893..1fa54dad3 100644 --- a/src/db/plugins/upnp/UpnpDatabasePlugin.cxx +++ b/src/db/plugins/upnp/UpnpDatabasePlugin.cxx @@ -62,8 +62,9 @@ class UpnpSong : UpnpSongData, public LightSong { std::string real_uri2; public: - UpnpSong(UPnPDirObject &&object, std::string &&_uri) noexcept - :UpnpSongData(std::move(_uri), std::move(object.tag)), + template + UpnpSong(UPnPDirObject &&object, U &&_uri) noexcept + :UpnpSongData(std::forward(_uri), std::move(object.tag)), LightSong(UpnpSongData::uri.c_str(), UpnpSongData::tag), real_uri2(std::move(object.url)) { real_uri = real_uri2.c_str(); @@ -87,7 +88,7 @@ public: void Open() override; void Close() noexcept override; - const LightSong *GetSong(const char *uri_utf8) const override; + const LightSong *GetSong(std::string_view uri_utf8) const override; void ReturnSong(const LightSong *song) const noexcept override; void Visit(const DatabaseSelection &selection, @@ -185,7 +186,7 @@ UpnpDatabase::ReturnSong(const LightSong *_song) const noexcept // Get song info by path. We can receive either the id path, or the titles // one const LightSong * -UpnpDatabase::GetSong(const char *uri) const +UpnpDatabase::GetSong(std::string_view uri) const { auto vpath = SplitString(uri, '/'); if (vpath.empty())