Skip to content

Commit

Permalink
playlist/Registry: replace playlist_plugins_for_each() with a contain…
Browse files Browse the repository at this point in the history
…er class
  • Loading branch information
MaxKellermann committed Jul 12, 2024
1 parent c64b483 commit 29747a2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 27 deletions.
5 changes: 3 additions & 2 deletions src/CommandLine.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,9 @@ static void version()

fmt::print("\n\n"
"Playlist plugins:\n");
playlist_plugins_for_each(plugin)
fmt::print(" {}", plugin->name);
for (const auto &plugin : GetAllPlaylistPlugins()) {
fmt::print(" {}", plugin.name);
}

fmt::print("\n\n"
"Protocols:\n");
Expand Down
37 changes: 21 additions & 16 deletions src/playlist/PlaylistRegistry.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "plugins/EmbeddedCuePlaylistPlugin.hxx"
#include "decoder/Features.h"
#include "input/InputStream.hxx"
#include "util/FilteredContainer.hxx"
#include "util/MimeType.hxx"
#include "util/UriExtract.hxx"
#include "config/Data.hxx"
Expand Down Expand Up @@ -57,9 +58,12 @@ static bool playlist_plugins_enabled[n_playlist_plugins];
/** which plugins have the "as_folder" option enabled? */
static bool playlist_plugins_as_folder[n_playlist_plugins];

#define playlist_plugins_for_each_enabled(plugin) \
playlist_plugins_for_each(plugin) \
if (playlist_plugins_enabled[playlist_plugin_iterator - playlist_plugins])
static inline auto
GetEnabledPlaylistPlugins() noexcept
{
const auto all = GetAllPlaylistPlugins();
return FilteredContainer{all.begin(), all.end(), playlist_plugins_enabled};
}

void
playlist_list_global_init(const ConfigData &config)
Expand Down Expand Up @@ -92,8 +96,9 @@ playlist_list_global_init(const ConfigData &config)
void
playlist_list_global_finish() noexcept
{
playlist_plugins_for_each_enabled(plugin)
playlist_plugin_finish(plugin);
for (const auto &plugin : GetEnabledPlaylistPlugins()) {
playlist_plugin_finish(&plugin);
}
}

bool
Expand Down Expand Up @@ -179,17 +184,17 @@ playlist_list_open_uri(const char *uri, Mutex &mutex)
static std::unique_ptr<SongEnumerator>
playlist_list_open_stream_mime2(InputStreamPtr &&is, std::string_view mime)
{
playlist_plugins_for_each_enabled(plugin) {
if (plugin->open_stream != nullptr &&
plugin->SupportsMimeType(mime)) {
for (const auto &plugin : GetEnabledPlaylistPlugins()) {
if (plugin.open_stream != nullptr &&
plugin.SupportsMimeType(mime)) {
/* rewind the stream, so each plugin gets a
fresh start */
try {
is->LockRewind();
} catch (...) {
}

auto playlist = plugin->open_stream(std::move(is));
auto playlist = plugin.open_stream(std::move(is));
if (playlist != nullptr)
return playlist;
}
Expand All @@ -209,17 +214,17 @@ playlist_list_open_stream_mime(InputStreamPtr &&is, std::string_view mime)
std::unique_ptr<SongEnumerator>
playlist_list_open_stream_suffix(InputStreamPtr &&is, std::string_view suffix)
{
playlist_plugins_for_each_enabled(plugin) {
if (plugin->open_stream != nullptr &&
plugin->SupportsSuffix(suffix)) {
for (const auto &plugin : GetEnabledPlaylistPlugins()) {
if (plugin.open_stream != nullptr &&
plugin.SupportsSuffix(suffix)) {
/* rewind the stream, so each plugin gets a
fresh start */
try {
is->LockRewind();
} catch (...) {
}

auto playlist = plugin->open_stream(std::move(is));
auto playlist = plugin.open_stream(std::move(is));
if (playlist != nullptr)
return playlist;
}
Expand Down Expand Up @@ -257,9 +262,9 @@ playlist_list_open_stream(InputStreamPtr &&is, const char *uri)
const PlaylistPlugin *
FindPlaylistPluginBySuffix(std::string_view suffix) noexcept
{
playlist_plugins_for_each_enabled(plugin) {
if (plugin->SupportsSuffix(suffix))
return plugin;
for (const auto &plugin : GetEnabledPlaylistPlugins()) {
if (plugin.SupportsSuffix(suffix))
return &plugin;
}

return nullptr;
Expand Down
17 changes: 8 additions & 9 deletions src/playlist/PlaylistRegistry.hxx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright The Music Player Daemon Project

#ifndef MPD_PLAYLIST_REGISTRY_HXX
#define MPD_PLAYLIST_REGISTRY_HXX
#pragma once

#include "input/Ptr.hxx"
#include "thread/Mutex.hxx"
#include "util/DereferenceIterator.hxx"
#include "util/TerminatedArray.hxx"

#include <string_view>

Expand All @@ -15,11 +16,11 @@ class SongEnumerator;

extern const PlaylistPlugin *const playlist_plugins[];

#define playlist_plugins_for_each(plugin) \
for (const PlaylistPlugin *plugin, \
*const*playlist_plugin_iterator = &playlist_plugins[0]; \
(plugin = *playlist_plugin_iterator) != nullptr; \
++playlist_plugin_iterator)
static inline auto
GetAllPlaylistPlugins() noexcept
{
return DereferenceContainerAdapter{TerminatedArray<const PlaylistPlugin *const, nullptr>{playlist_plugins}};
}

/**
* Initializes all playlist plugins.
Expand Down Expand Up @@ -85,5 +86,3 @@ playlist_suffix_supported(std::string_view suffix) noexcept
{
return FindPlaylistPluginBySuffix(suffix) != nullptr;
}

#endif

0 comments on commit 29747a2

Please sign in to comment.