diff --git a/src/CommandLine.cxx b/src/CommandLine.cxx index 54827b2a8..981572d40 100644 --- a/src/CommandLine.cxx +++ b/src/CommandLine.cxx @@ -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); diff --git a/src/archive/ArchiveList.cxx b/src/archive/ArchiveList.cxx index 38359d3ef..5b08c2090 100644 --- a/src/archive/ArchiveList.cxx +++ b/src/archive/ArchiveList.cxx @@ -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(); + } } diff --git a/src/archive/ArchiveList.hxx b/src/archive/ArchiveList.hxx index 257557b12..d5cb3627b 100644 --- a/src/archive/ArchiveList.hxx +++ b/src/archive/ArchiveList.hxx @@ -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 @@ -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{archive_plugins}}; +} /* interface for using plugins */ @@ -43,5 +45,3 @@ public: archive_plugin_deinit_all(); } }; - -#endif