From b9ff6383a4df993abeba2fe3d0cab3dee3760f40 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 6 Jul 2018 16:43:11 +0200 Subject: [PATCH] db/LightSong: make Tag a reference This enforces the "not nullptr" rule. --- src/DetachedSong.cxx | 5 ++--- src/SongFilter.cxx | 2 +- src/SongPrint.cxx | 2 +- src/db/Count.cxx | 4 +--- src/db/DatabasePrint.cxx | 4 ++-- src/db/Helpers.cxx | 2 +- src/db/LightSong.cxx | 6 +++--- src/db/LightSong.hxx | 7 +++++-- src/db/UniqueTags.cxx | 3 +-- src/db/plugins/ProxyDatabasePlugin.cxx | 2 +- src/db/plugins/simple/Song.cxx | 3 +-- src/db/plugins/upnp/UpnpDatabasePlugin.cxx | 7 +++---- src/queue/PlaylistUpdate.cxx | 2 +- 13 files changed, 23 insertions(+), 26 deletions(-) diff --git a/src/DetachedSong.cxx b/src/DetachedSong.cxx index e1d82c113..3952ef1a8 100644 --- a/src/DetachedSong.cxx +++ b/src/DetachedSong.cxx @@ -26,18 +26,17 @@ DetachedSong::DetachedSong(const LightSong &other) :uri(other.GetURI()), real_uri(other.real_uri != nullptr ? other.real_uri : ""), - tag(*other.tag), + tag(other.tag), mtime(other.mtime), start_time(other.start_time), end_time(other.end_time) {} DetachedSong::operator LightSong() const noexcept { - LightSong result; + LightSong result(tag); result.directory = nullptr; result.uri = uri.c_str(); result.real_uri = real_uri.empty() ? nullptr : real_uri.c_str(); - result.tag = &tag; result.mtime = mtime; result.start_time = start_time; result.end_time = end_time; diff --git a/src/SongFilter.cxx b/src/SongFilter.cxx index 2d8068ace..de43b2fad 100644 --- a/src/SongFilter.cxx +++ b/src/SongFilter.cxx @@ -164,7 +164,7 @@ SongFilter::Item::Match(const LightSong &song) const noexcept return StringMatch(uri.c_str()); } - return Match(*song.tag); + return Match(song.tag); } SongFilter::SongFilter(unsigned tag, const char *value, bool fold_case) diff --git a/src/SongPrint.cxx b/src/SongPrint.cxx index c7c670bdd..9db113f80 100644 --- a/src/SongPrint.cxx +++ b/src/SongPrint.cxx @@ -92,7 +92,7 @@ song_print_info(Response &r, const LightSong &song, bool base) noexcept if (!IsNegative(song.mtime)) time_print(r, "Last-Modified", song.mtime); - tag_print(r, *song.tag); + tag_print(r, song.tag); } void diff --git a/src/db/Count.cxx b/src/db/Count.cxx index dd2b0a22b..08e1fae97 100644 --- a/src/db/Count.cxx +++ b/src/db/Count.cxx @@ -97,9 +97,7 @@ static void GroupCountVisitor(TagCountMap &map, TagType group, const LightSong &song) noexcept { - assert(song.tag != nullptr); - - const Tag &tag = *song.tag; + const Tag &tag = song.tag; if (!CollectGroupCounts(map, group, tag) && group == TAG_ALBUM_ARTIST) /* fall back to "Artist" if no "AlbumArtist" was found */ CollectGroupCounts(map, TAG_ARTIST, tag); diff --git a/src/db/DatabasePrint.cxx b/src/db/DatabasePrint.cxx index b8bd979b8..f166e4a17 100644 --- a/src/db/DatabasePrint.cxx +++ b/src/db/DatabasePrint.cxx @@ -105,7 +105,7 @@ PrintSongBrief(Response &r, bool base, const LightSong &song) noexcept { song_print_uri(r, song, base); - if (song.tag->has_playlist) + if (song.tag.has_playlist) /* this song file has an embedded CUE sheet */ print_playlist_in_directory(r, base, song.directory, song.uri); @@ -116,7 +116,7 @@ PrintSongFull(Response &r, bool base, const LightSong &song) noexcept { song_print_info(r, song, base); - if (song.tag->has_playlist) + if (song.tag.has_playlist) /* this song file has an embedded CUE sheet */ print_playlist_in_directory(r, base, song.directory, song.uri); diff --git a/src/db/Helpers.cxx b/src/db/Helpers.cxx index 92cd1cbe9..dec2d0177 100644 --- a/src/db/Helpers.cxx +++ b/src/db/Helpers.cxx @@ -65,7 +65,7 @@ StatsVisitSong(DatabaseStats &stats, StringSet &artists, StringSet &albums, { ++stats.song_count; - StatsVisitTag(stats, artists, albums, *song.tag); + StatsVisitTag(stats, artists, albums, song.tag); } DatabaseStats diff --git a/src/db/LightSong.cxx b/src/db/LightSong.cxx index 98e8a1001..b1a219e0b 100644 --- a/src/db/LightSong.cxx +++ b/src/db/LightSong.cxx @@ -25,10 +25,10 @@ LightSong::GetDuration() const noexcept { SongTime a = start_time, b = end_time; if (!b.IsPositive()) { - if (tag->duration.IsNegative()) - return tag->duration; + if (tag.duration.IsNegative()) + return tag.duration; - b = SongTime(tag->duration); + b = SongTime(tag.duration); } return SignedSongTime(b - a); diff --git a/src/db/LightSong.hxx b/src/db/LightSong.hxx index 1b89fc9ad..73329f9fd 100644 --- a/src/db/LightSong.hxx +++ b/src/db/LightSong.hxx @@ -57,9 +57,9 @@ struct LightSong { const char *real_uri; /** - * Must not be nullptr. + * Metadata. */ - const Tag *tag; + const Tag &tag; /** * The time stamp of the last file modification. A negative @@ -78,6 +78,9 @@ struct LightSong { */ SongTime end_time; + explicit LightSong(const Tag &_tag) noexcept + :tag(_tag) {} + gcc_pure std::string GetURI() const noexcept { if (directory == nullptr) diff --git a/src/db/UniqueTags.cxx b/src/db/UniqueTags.cxx index e755ac12c..4820f53ae 100644 --- a/src/db/UniqueTags.cxx +++ b/src/db/UniqueTags.cxx @@ -31,8 +31,7 @@ static void CollectTags(TagSet &set, TagType tag_type, TagMask group_mask, const LightSong &song) { - assert(song.tag != nullptr); - const Tag &tag = *song.tag; + const Tag &tag = song.tag; set.InsertUnique(tag, tag_type, group_mask); } diff --git a/src/db/plugins/ProxyDatabasePlugin.cxx b/src/db/plugins/ProxyDatabasePlugin.cxx index 6181110ab..d82d2fcd4 100644 --- a/src/db/plugins/ProxyDatabasePlugin.cxx +++ b/src/db/plugins/ProxyDatabasePlugin.cxx @@ -203,11 +203,11 @@ Copy(TagBuilder &tag, TagType d_tag, } ProxySong::ProxySong(const mpd_song *song) + :LightSong(tag2) { directory = nullptr; uri = mpd_song_get_uri(song); real_uri = nullptr; - tag = &tag2; const auto _mtime = mpd_song_get_last_modified(song); mtime = _mtime > 0 diff --git a/src/db/plugins/simple/Song.cxx b/src/db/plugins/simple/Song.cxx index 49ba6e2c9..9f516cde8 100644 --- a/src/db/plugins/simple/Song.cxx +++ b/src/db/plugins/simple/Song.cxx @@ -98,12 +98,11 @@ Song::GetURI() const noexcept LightSong Song::Export() const noexcept { - LightSong dest; + LightSong dest(tag); dest.directory = parent->IsRoot() ? nullptr : parent->GetPath(); dest.uri = uri; dest.real_uri = nullptr; - dest.tag = &tag; dest.mtime = mtime; dest.start_time = start_time; dest.end_time = end_time; diff --git a/src/db/plugins/upnp/UpnpDatabasePlugin.cxx b/src/db/plugins/upnp/UpnpDatabasePlugin.cxx index f0b0789a7..4b93bd975 100644 --- a/src/db/plugins/upnp/UpnpDatabasePlugin.cxx +++ b/src/db/plugins/upnp/UpnpDatabasePlugin.cxx @@ -55,13 +55,13 @@ class UpnpSong : public LightSong { public: UpnpSong(UPnPDirObject &&object, std::string &&_uri) - :uri2(std::move(_uri)), + :LightSong(tag2), + uri2(std::move(_uri)), real_uri2(std::move(object.url)), tag2(std::move(object.tag)) { directory = nullptr; uri = uri2.c_str(); real_uri = real_uri2.c_str(); - tag = &tag2; mtime = std::chrono::system_clock::time_point::min(); start_time = end_time = SongTime::zero(); } @@ -321,11 +321,10 @@ visitSong(const UPnPDirObject &meta, const char *path, if (!visit_song) return; - LightSong song; + LightSong song(meta.tag); song.directory = nullptr; song.uri = path; song.real_uri = meta.url.c_str(); - song.tag = &meta.tag; song.mtime = std::chrono::system_clock::time_point::min(); song.start_time = song.end_time = SongTime::zero(); diff --git a/src/queue/PlaylistUpdate.cxx b/src/queue/PlaylistUpdate.cxx index a710a0c36..84e1e1b71 100644 --- a/src/queue/PlaylistUpdate.cxx +++ b/src/queue/PlaylistUpdate.cxx @@ -50,7 +50,7 @@ UpdatePlaylistSong(const Database &db, DetachedSong &song) } song.SetLastModified(original->mtime); - song.SetTag(*original->tag); + song.SetTag(original->tag); db.ReturnSong(original); return true;