Skip to content

Commit

Permalink
archive/ArchiveList: replace archive_plugins_for_each() with a contai…
Browse files Browse the repository at this point in the history
…ner class
  • Loading branch information
MaxKellermann committed Jul 12, 2024
1 parent 90dfa43 commit 040c4a8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 27 deletions.
6 changes: 3 additions & 3 deletions src/CommandLine.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
36 changes: 21 additions & 15 deletions src/archive/ArchiveList.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -35,27 +36,32 @@ 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;
}

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;
}
Expand All @@ -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);
Expand All @@ -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();
}
}

18 changes: 9 additions & 9 deletions src/archive/ArchiveList.hxx
Original file line number Diff line number Diff line change
@@ -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>

Expand All @@ -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 */

Expand Down Expand Up @@ -43,5 +45,3 @@ public:
archive_plugin_deinit_all();
}
};

#endif

0 comments on commit 040c4a8

Please sign in to comment.