archive/ArchiveList: replace archive_plugins_for_each() with a container class

This commit is contained in:
Max Kellermann 2024-07-12 09:53:36 +02:00
parent 90dfa437e0
commit 040c4a8560
3 changed files with 33 additions and 27 deletions

View File

@ -188,10 +188,10 @@ static void version()
#ifdef ENABLE_ARCHIVE
"\n"
"Archive plugins:\n");
archive_plugins_for_each(plugin) {
fmt::print(" [{}]", plugin->name);
for (const auto &plugin : GetAllArchivePlugins()) {
fmt::print(" [{}]", plugin.name);
const char *const*suffixes = plugin->suffixes;
const char *const*suffixes = plugin.suffixes;
if (suffixes != nullptr)
for (; *suffixes != nullptr; ++suffixes)
fmt::print(" {}", *suffixes);

View File

@ -6,6 +6,7 @@
#include "archive/Features.h"
#include "config/Data.hxx"
#include "config/Block.hxx"
#include "util/FilteredContainer.hxx"
#include "util/StringUtil.hxx"
#include "plugins/Bzip2ArchivePlugin.hxx"
#include "plugins/Iso9660ArchivePlugin.hxx"
@ -35,17 +36,21 @@ static constexpr std::size_t n_archive_plugins = std::size(archive_plugins) - 1;
forbidden in C++ */
static bool archive_plugins_enabled[std::max(n_archive_plugins, std::size_t(1))];
#define archive_plugins_for_each_enabled(plugin) \
archive_plugins_for_each(plugin) \
if (archive_plugins_enabled[archive_plugin_iterator - archive_plugins])
static inline auto
GetEnabledArchivePlugins() noexcept
{
const auto all = GetAllArchivePlugins();
return FilteredContainer{all.begin(), all.end(), archive_plugins_enabled};
}
const ArchivePlugin *
archive_plugin_from_suffix(std::string_view suffix) noexcept
{
archive_plugins_for_each_enabled(plugin)
if (plugin->suffixes != nullptr &&
StringArrayContainsCase(plugin->suffixes, suffix))
return plugin;
for (const auto &plugin : GetEnabledArchivePlugins()) {
if (plugin.suffixes != nullptr &&
StringArrayContainsCase(plugin.suffixes, suffix))
return &plugin;
}
return nullptr;
}
@ -53,9 +58,10 @@ archive_plugin_from_suffix(std::string_view suffix) noexcept
const ArchivePlugin *
archive_plugin_from_name(const char *name) noexcept
{
archive_plugins_for_each_enabled(plugin)
if (strcmp(plugin->name, name) == 0)
return plugin;
for (const auto &plugin : GetEnabledArchivePlugins()) {
if (strcmp(plugin.name, name) == 0)
return &plugin;
}
return nullptr;
}
@ -67,7 +73,6 @@ archive_plugin_init_all(const ConfigData &config)
for (unsigned i = 0; archive_plugins[i] != nullptr; ++i) {
const auto &plugin = *archive_plugins[i];
const auto *param =
config.FindBlock(ConfigBlockOption::ARCHIVE_PLUGIN,
"name", plugin.name);
@ -83,9 +88,10 @@ archive_plugin_init_all(const ConfigData &config)
void
archive_plugin_deinit_all() noexcept
{
unsigned i = 0;
archive_plugins_for_each_enabled(plugin)
if (archive_plugins_enabled[i++] && plugin->finish != nullptr)
plugin->finish();
for (unsigned i = 0; archive_plugins[i] != nullptr; ++i) {
const auto &plugin = *archive_plugins[i];
if (archive_plugins_enabled[i] && plugin.finish != nullptr)
plugin.finish();
}
}

View File

@ -1,8 +1,10 @@
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright The Music Player Daemon Project
#ifndef MPD_ARCHIVE_LIST_HXX
#define MPD_ARCHIVE_LIST_HXX
#pragma once
#include "util/DereferenceIterator.hxx"
#include "util/TerminatedArray.hxx"
#include <string_view>
@ -11,11 +13,11 @@ struct ArchivePlugin;
extern const ArchivePlugin *const archive_plugins[];
#define archive_plugins_for_each(plugin) \
for (const ArchivePlugin *plugin, \
*const*archive_plugin_iterator = &archive_plugins[0]; \
(plugin = *archive_plugin_iterator) != nullptr; \
++archive_plugin_iterator)
static inline auto
GetAllArchivePlugins() noexcept
{
return DereferenceContainerAdapter{TerminatedArray<const ArchivePlugin *const, nullptr>{archive_plugins}};
}
/* interface for using plugins */
@ -43,5 +45,3 @@ public:
archive_plugin_deinit_all();
}
};
#endif