From 5588291a352b5ec7c2499a6abcb865b6c17afa71 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sat, 12 Feb 2022 08:19:36 +0100 Subject: [PATCH] queue/Selection: wrap SongFilter in a new struct --- meson.build | 1 + src/PlaylistPrint.cxx | 4 ++-- src/PlaylistPrint.hxx | 4 ++-- src/command/QueueCommands.cxx | 6 +++++- src/queue/Print.cxx | 9 ++++----- src/queue/Print.hxx | 6 +++--- src/queue/Selection.cxx | 37 ++++++++++++++++++++++++++++++++++ src/queue/Selection.hxx | 38 +++++++++++++++++++++++++++++++++++ 8 files changed, 92 insertions(+), 13 deletions(-) create mode 100644 src/queue/Selection.cxx create mode 100644 src/queue/Selection.hxx diff --git a/meson.build b/meson.build index af6bac822..a5ae6f996 100644 --- a/meson.build +++ b/meson.build @@ -332,6 +332,7 @@ sources = [ 'src/queue/Queue.cxx', 'src/queue/Print.cxx', 'src/queue/Save.cxx', + 'src/queue/Selection.cxx', 'src/queue/Playlist.cxx', 'src/queue/PlaylistControl.cxx', 'src/queue/PlaylistEdit.cxx', diff --git a/src/PlaylistPrint.cxx b/src/PlaylistPrint.cxx index d01718f05..a1c033f46 100644 --- a/src/PlaylistPrint.cxx +++ b/src/PlaylistPrint.cxx @@ -77,9 +77,9 @@ playlist_print_current(Response &r, const playlist &playlist) void playlist_print_find(Response &r, const playlist &playlist, - const SongFilter &filter) + const QueueSelection &selection) { - queue_find(r, playlist.queue, filter); + PrintQueue(r, playlist.queue, selection); } void diff --git a/src/PlaylistPrint.hxx b/src/PlaylistPrint.hxx index d970289ea..85b3dc374 100644 --- a/src/PlaylistPrint.hxx +++ b/src/PlaylistPrint.hxx @@ -24,7 +24,7 @@ struct playlist; struct RangeArg; -class SongFilter; +struct QueueSelection; class Response; /** @@ -65,7 +65,7 @@ playlist_print_current(Response &r, const playlist &playlist); */ void playlist_print_find(Response &r, const playlist &playlist, - const SongFilter &filter); + const QueueSelection &selection); /** * Print detailed changes since the specified playlist version. diff --git a/src/command/QueueCommands.cxx b/src/command/QueueCommands.cxx index 2d70e208e..140dc72ca 100644 --- a/src/command/QueueCommands.cxx +++ b/src/command/QueueCommands.cxx @@ -29,6 +29,7 @@ #include "song/DetachedSong.hxx" #include "LocateUri.hxx" #include "queue/Playlist.hxx" +#include "queue/Selection.hxx" #include "PlaylistPrint.hxx" #include "client/Client.hxx" #include "client/Response.hxx" @@ -299,7 +300,10 @@ handle_playlist_match(Client &client, Request args, Response &r, } filter.Optimize(); - playlist_print_find(r, client.GetPlaylist(), filter); + QueueSelection selection; + selection.filter = &filter; + + playlist_print_find(r, client.GetPlaylist(), selection); return CommandResult::OK; } diff --git a/src/queue/Print.cxx b/src/queue/Print.cxx index c2b455acd..6fb8598ee 100644 --- a/src/queue/Print.cxx +++ b/src/queue/Print.cxx @@ -19,6 +19,7 @@ #include "Print.hxx" #include "Queue.hxx" +#include "Selection.hxx" #include "song/Filter.hxx" #include "SongPrint.hxx" #include "song/DetachedSong.hxx" @@ -100,13 +101,11 @@ queue_print_changes_position(Response &r, const Queue &queue, } void -queue_find(Response &r, const Queue &queue, - const SongFilter &filter) +PrintQueue(Response &r, const Queue &queue, + const QueueSelection &selection) { for (unsigned i = 0; i < queue.GetLength(); i++) { - const LightSong song{queue.Get(i)}; - - if (filter.Match(song)) + if (selection.MatchPosition(queue, i)) queue_print_song_info(r, queue, i); } } diff --git a/src/queue/Print.hxx b/src/queue/Print.hxx index cd597d638..727e41bf2 100644 --- a/src/queue/Print.hxx +++ b/src/queue/Print.hxx @@ -27,7 +27,7 @@ #include struct Queue; -class SongFilter; +struct QueueSelection; class Response; void @@ -49,5 +49,5 @@ queue_print_changes_position(Response &r, const Queue &queue, unsigned start, unsigned end); void -queue_find(Response &response, const Queue &queue, - const SongFilter &filter); +PrintQueue(Response &response, const Queue &queue, + const QueueSelection &selection); diff --git a/src/queue/Selection.cxx b/src/queue/Selection.cxx new file mode 100644 index 000000000..090ad0274 --- /dev/null +++ b/src/queue/Selection.cxx @@ -0,0 +1,37 @@ +/* + * Copyright 2003-2021 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. + */ + +#include "Selection.hxx" +#include "Queue.hxx" +#include "song/DetachedSong.hxx" +#include "song/Filter.hxx" +#include "song/LightSong.hxx" + +bool +QueueSelection::MatchPosition(const Queue &queue, + unsigned position) const noexcept +{ + if (filter != nullptr) { + const LightSong song{queue.Get(position)}; + if (!filter->Match(song)) + return false; + } + + return true; +} diff --git a/src/queue/Selection.hxx b/src/queue/Selection.hxx new file mode 100644 index 000000000..0b2ffa6b1 --- /dev/null +++ b/src/queue/Selection.hxx @@ -0,0 +1,38 @@ +/* + * Copyright 2003-2021 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. + */ + +#pragma once + +struct Queue; +class SongFilter; + +/** + * Describes what part of and how the client wishes to see the queue. + */ +struct QueueSelection { + /** + * An optional pointer to a #SongFilter (not owned by this + * object). + */ + const SongFilter *filter = nullptr; + + [[gnu::pure]] + bool MatchPosition(const Queue &queue, + unsigned position) const noexcept; +};