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 #ifdef ENABLE_ARCHIVE
"\n" "\n"
"Archive plugins:\n"); "Archive plugins:\n");
archive_plugins_for_each(plugin) { for (const auto &plugin : GetAllArchivePlugins()) {
fmt::print(" [{}]", plugin->name); fmt::print(" [{}]", plugin.name);
const char *const*suffixes = plugin->suffixes; const char *const*suffixes = plugin.suffixes;
if (suffixes != nullptr) if (suffixes != nullptr)
for (; *suffixes != nullptr; ++suffixes) for (; *suffixes != nullptr; ++suffixes)
fmt::print(" {}", *suffixes); fmt::print(" {}", *suffixes);

View File

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

View File

@ -1,8 +1,10 @@
// 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_ARCHIVE_LIST_HXX #pragma once
#define MPD_ARCHIVE_LIST_HXX
#include "util/DereferenceIterator.hxx"
#include "util/TerminatedArray.hxx"
#include <string_view> #include <string_view>
@ -11,11 +13,11 @@ struct ArchivePlugin;
extern const ArchivePlugin *const archive_plugins[]; extern const ArchivePlugin *const archive_plugins[];
#define archive_plugins_for_each(plugin) \ static inline auto
for (const ArchivePlugin *plugin, \ GetAllArchivePlugins() noexcept
*const*archive_plugin_iterator = &archive_plugins[0]; \ {
(plugin = *archive_plugin_iterator) != nullptr; \ return DereferenceContainerAdapter{TerminatedArray<const ArchivePlugin *const, nullptr>{archive_plugins}};
++archive_plugin_iterator) }
/* interface for using plugins */ /* interface for using plugins */
@ -43,5 +45,3 @@ public:
archive_plugin_deinit_all(); archive_plugin_deinit_all();
} }
}; };
#endif