From c64b4838dc1ef314d7a17f1ee779e3b4d1906e5a Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 12 Jul 2024 14:58:09 +0200 Subject: [PATCH] encoder/List: replace encoder_plugins_for_each() with a container class --- src/CommandLine.cxx | 5 +++-- src/encoder/EncoderList.cxx | 7 ++++--- src/encoder/EncoderList.hxx | 18 +++++++++--------- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/CommandLine.cxx b/src/CommandLine.cxx index 981572d40..393269fcb 100644 --- a/src/CommandLine.cxx +++ b/src/CommandLine.cxx @@ -180,8 +180,9 @@ static void version() #ifdef ENABLE_ENCODER "\n" "Encoder plugins:\n"); - encoder_plugins_for_each(plugin) - fmt::print(" {}", plugin->name); + for (const auto &plugin : GetAllEncoderPlugins()) { + fmt::print(" {}", plugin.name); + } fmt::print("\n" #endif diff --git a/src/encoder/EncoderList.cxx b/src/encoder/EncoderList.cxx index f4db535d0..39083f448 100644 --- a/src/encoder/EncoderList.cxx +++ b/src/encoder/EncoderList.cxx @@ -46,9 +46,10 @@ constinit const EncoderPlugin *const encoder_plugins[] = { const EncoderPlugin * encoder_plugin_get(const char *name) { - encoder_plugins_for_each(plugin) - if (strcmp(plugin->name, name) == 0) - return plugin; + for (const auto &plugin : GetAllEncoderPlugins()) { + if (strcmp(plugin.name, name) == 0) + return &plugin; + } return nullptr; } diff --git a/src/encoder/EncoderList.hxx b/src/encoder/EncoderList.hxx index e7b381419..e5c2fe77a 100644 --- a/src/encoder/EncoderList.hxx +++ b/src/encoder/EncoderList.hxx @@ -1,18 +1,20 @@ // SPDX-License-Identifier: GPL-2.0-or-later // Copyright The Music Player Daemon Project -#ifndef MPD_ENCODER_LIST_HXX -#define MPD_ENCODER_LIST_HXX +#pragma once + +#include "util/DereferenceIterator.hxx" +#include "util/TerminatedArray.hxx" struct EncoderPlugin; extern const EncoderPlugin *const encoder_plugins[]; -#define encoder_plugins_for_each(plugin) \ - for (const EncoderPlugin *plugin, \ - *const*encoder_plugin_iterator = &encoder_plugins[0]; \ - (plugin = *encoder_plugin_iterator) != nullptr; \ - ++encoder_plugin_iterator) +static inline auto +GetAllEncoderPlugins() noexcept +{ + return DereferenceContainerAdapter{TerminatedArray{encoder_plugins}}; +} /** * Looks up an encoder plugin by its name. @@ -23,5 +25,3 @@ extern const EncoderPlugin *const encoder_plugins[]; */ const EncoderPlugin * encoder_plugin_get(const char *name); - -#endif