playlist/Registry: replace playlist_plugins_for_each() with a container class

This commit is contained in:
Max Kellermann 2024-07-12 15:24:53 +02:00
parent c64b4838dc
commit 29747a213f
3 changed files with 32 additions and 27 deletions

View File

@ -218,8 +218,9 @@ static void version()
fmt::print("\n\n" fmt::print("\n\n"
"Playlist plugins:\n"); "Playlist plugins:\n");
playlist_plugins_for_each(plugin) for (const auto &plugin : GetAllPlaylistPlugins()) {
fmt::print(" {}", plugin->name); fmt::print(" {}", plugin.name);
}
fmt::print("\n\n" fmt::print("\n\n"
"Protocols:\n"); "Protocols:\n");

View File

@ -18,6 +18,7 @@
#include "plugins/EmbeddedCuePlaylistPlugin.hxx" #include "plugins/EmbeddedCuePlaylistPlugin.hxx"
#include "decoder/Features.h" #include "decoder/Features.h"
#include "input/InputStream.hxx" #include "input/InputStream.hxx"
#include "util/FilteredContainer.hxx"
#include "util/MimeType.hxx" #include "util/MimeType.hxx"
#include "util/UriExtract.hxx" #include "util/UriExtract.hxx"
#include "config/Data.hxx" #include "config/Data.hxx"
@ -57,9 +58,12 @@ static bool playlist_plugins_enabled[n_playlist_plugins];
/** which plugins have the "as_folder" option enabled? */ /** which plugins have the "as_folder" option enabled? */
static bool playlist_plugins_as_folder[n_playlist_plugins]; static bool playlist_plugins_as_folder[n_playlist_plugins];
#define playlist_plugins_for_each_enabled(plugin) \ static inline auto
playlist_plugins_for_each(plugin) \ GetEnabledPlaylistPlugins() noexcept
if (playlist_plugins_enabled[playlist_plugin_iterator - playlist_plugins]) {
const auto all = GetAllPlaylistPlugins();
return FilteredContainer{all.begin(), all.end(), playlist_plugins_enabled};
}
void void
playlist_list_global_init(const ConfigData &config) playlist_list_global_init(const ConfigData &config)
@ -92,8 +96,9 @@ playlist_list_global_init(const ConfigData &config)
void void
playlist_list_global_finish() noexcept playlist_list_global_finish() noexcept
{ {
playlist_plugins_for_each_enabled(plugin) for (const auto &plugin : GetEnabledPlaylistPlugins()) {
playlist_plugin_finish(plugin); playlist_plugin_finish(&plugin);
}
} }
bool bool
@ -179,9 +184,9 @@ playlist_list_open_uri(const char *uri, Mutex &mutex)
static std::unique_ptr<SongEnumerator> static std::unique_ptr<SongEnumerator>
playlist_list_open_stream_mime2(InputStreamPtr &&is, std::string_view mime) playlist_list_open_stream_mime2(InputStreamPtr &&is, std::string_view mime)
{ {
playlist_plugins_for_each_enabled(plugin) { for (const auto &plugin : GetEnabledPlaylistPlugins()) {
if (plugin->open_stream != nullptr && if (plugin.open_stream != nullptr &&
plugin->SupportsMimeType(mime)) { plugin.SupportsMimeType(mime)) {
/* rewind the stream, so each plugin gets a /* rewind the stream, so each plugin gets a
fresh start */ fresh start */
try { try {
@ -189,7 +194,7 @@ playlist_list_open_stream_mime2(InputStreamPtr &&is, std::string_view mime)
} catch (...) { } catch (...) {
} }
auto playlist = plugin->open_stream(std::move(is)); auto playlist = plugin.open_stream(std::move(is));
if (playlist != nullptr) if (playlist != nullptr)
return playlist; return playlist;
} }
@ -209,9 +214,9 @@ playlist_list_open_stream_mime(InputStreamPtr &&is, std::string_view mime)
std::unique_ptr<SongEnumerator> std::unique_ptr<SongEnumerator>
playlist_list_open_stream_suffix(InputStreamPtr &&is, std::string_view suffix) playlist_list_open_stream_suffix(InputStreamPtr &&is, std::string_view suffix)
{ {
playlist_plugins_for_each_enabled(plugin) { for (const auto &plugin : GetEnabledPlaylistPlugins()) {
if (plugin->open_stream != nullptr && if (plugin.open_stream != nullptr &&
plugin->SupportsSuffix(suffix)) { plugin.SupportsSuffix(suffix)) {
/* rewind the stream, so each plugin gets a /* rewind the stream, so each plugin gets a
fresh start */ fresh start */
try { try {
@ -219,7 +224,7 @@ playlist_list_open_stream_suffix(InputStreamPtr &&is, std::string_view suffix)
} catch (...) { } catch (...) {
} }
auto playlist = plugin->open_stream(std::move(is)); auto playlist = plugin.open_stream(std::move(is));
if (playlist != nullptr) if (playlist != nullptr)
return playlist; return playlist;
} }
@ -257,9 +262,9 @@ playlist_list_open_stream(InputStreamPtr &&is, const char *uri)
const PlaylistPlugin * const PlaylistPlugin *
FindPlaylistPluginBySuffix(std::string_view suffix) noexcept FindPlaylistPluginBySuffix(std::string_view suffix) noexcept
{ {
playlist_plugins_for_each_enabled(plugin) { for (const auto &plugin : GetEnabledPlaylistPlugins()) {
if (plugin->SupportsSuffix(suffix)) if (plugin.SupportsSuffix(suffix))
return plugin; return &plugin;
} }
return nullptr; return nullptr;

View File

@ -1,11 +1,12 @@
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
// Copyright The Music Player Daemon Project // Copyright The Music Player Daemon Project
#ifndef MPD_PLAYLIST_REGISTRY_HXX #pragma once
#define MPD_PLAYLIST_REGISTRY_HXX
#include "input/Ptr.hxx" #include "input/Ptr.hxx"
#include "thread/Mutex.hxx" #include "thread/Mutex.hxx"
#include "util/DereferenceIterator.hxx"
#include "util/TerminatedArray.hxx"
#include <string_view> #include <string_view>
@ -15,11 +16,11 @@ class SongEnumerator;
extern const PlaylistPlugin *const playlist_plugins[]; extern const PlaylistPlugin *const playlist_plugins[];
#define playlist_plugins_for_each(plugin) \ static inline auto
for (const PlaylistPlugin *plugin, \ GetAllPlaylistPlugins() noexcept
*const*playlist_plugin_iterator = &playlist_plugins[0]; \ {
(plugin = *playlist_plugin_iterator) != nullptr; \ return DereferenceContainerAdapter{TerminatedArray<const PlaylistPlugin *const, nullptr>{playlist_plugins}};
++playlist_plugin_iterator) }
/** /**
* Initializes all playlist plugins. * Initializes all playlist plugins.
@ -85,5 +86,3 @@ playlist_suffix_supported(std::string_view suffix) noexcept
{ {
return FindPlaylistPluginBySuffix(suffix) != nullptr; return FindPlaylistPluginBySuffix(suffix) != nullptr;
} }
#endif