queue/Selection: wrap SongFilter in a new struct

This commit is contained in:
Max Kellermann 2022-02-12 08:19:36 +01:00
parent 4b41e766c6
commit 5588291a35
8 changed files with 92 additions and 13 deletions

View File

@ -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',

View File

@ -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

View File

@ -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.

View File

@ -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;
}

View File

@ -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);
}
}

View File

@ -27,7 +27,7 @@
#include <cstdint>
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);

37
src/queue/Selection.cxx Normal file
View File

@ -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;
}

38
src/queue/Selection.hxx Normal file
View File

@ -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;
};