From cf5970a6e17f8c99abedd2853de83ae8de8a529f Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 12 Jul 2024 15:52:44 +0200 Subject: [PATCH] output/Registry: replace output_plugins_for_each() with a container class --- src/CommandLine.cxx | 5 +++-- src/output/Init.cxx | 10 +++++----- src/output/Registry.cxx | 7 ++++--- src/output/Registry.hxx | 19 ++++++++++--------- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/CommandLine.cxx b/src/CommandLine.cxx index 23ed7816a..b83251ddd 100644 --- a/src/CommandLine.cxx +++ b/src/CommandLine.cxx @@ -173,8 +173,9 @@ static void version() #endif "\n\n" "Output plugins:\n"); - audio_output_plugins_for_each(plugin) - fmt::print(" {}", plugin->name); + for (const auto &plugin : GetAllAudioOutputPlugins()) { + fmt::print(" {}", plugin.name); + } fmt::print("\n" #ifdef ENABLE_ENCODER diff --git a/src/output/Init.cxx b/src/output/Init.cxx index 4277671bd..1edcd6ebf 100644 --- a/src/output/Init.cxx +++ b/src/output/Init.cxx @@ -51,15 +51,15 @@ audio_output_detect() { LogInfo(output_domain, "Attempt to detect audio output device"); - audio_output_plugins_for_each(plugin) { - if (plugin->test_default_device == nullptr) + for (const auto &plugin : GetAllAudioOutputPlugins()) { + if (plugin.test_default_device == nullptr) continue; FmtInfo(output_domain, "Attempting to detect a {:?} audio device", - plugin->name); - if (ao_plugin_test_default_device(plugin)) - return plugin; + plugin.name); + if (ao_plugin_test_default_device(&plugin)) + return &plugin; } throw std::runtime_error("Unable to detect an audio device"); diff --git a/src/output/Registry.cxx b/src/output/Registry.cxx index 993e02fbc..7ba9e8c06 100644 --- a/src/output/Registry.cxx +++ b/src/output/Registry.cxx @@ -92,9 +92,10 @@ constinit const AudioOutputPlugin *const audio_output_plugins[] = { const AudioOutputPlugin * GetAudioOutputPluginByName(const char *name) noexcept { - audio_output_plugins_for_each(plugin) - if (StringIsEqual(plugin->name, name)) - return plugin; + for (const auto &plugin : GetAllAudioOutputPlugins()) { + if (StringIsEqual(plugin.name, name)) + return &plugin; + } return nullptr; } diff --git a/src/output/Registry.hxx b/src/output/Registry.hxx index 8fd077bb3..a31a6ae26 100644 --- a/src/output/Registry.hxx +++ b/src/output/Registry.hxx @@ -1,20 +1,21 @@ // SPDX-License-Identifier: GPL-2.0-or-later // Copyright The Music Player Daemon Project -#ifndef MPD_OUTPUT_LIST_HXX -#define MPD_OUTPUT_LIST_HXX +#pragma once + +#include "util/DereferenceIterator.hxx" +#include "util/TerminatedArray.hxx" struct AudioOutputPlugin; extern const AudioOutputPlugin *const audio_output_plugins[]; +static inline auto +GetAllAudioOutputPlugins() noexcept +{ + return DereferenceContainerAdapter{TerminatedArray{audio_output_plugins}}; +} + [[gnu::pure]] const AudioOutputPlugin * GetAudioOutputPluginByName(const char *name) noexcept; - -#define audio_output_plugins_for_each(plugin) \ - for (const AudioOutputPlugin *plugin, \ - *const*output_plugin_iterator = &audio_output_plugins[0]; \ - (plugin = *output_plugin_iterator) != nullptr; ++output_plugin_iterator) - -#endif