From c2470ebd9c5bf15a0a4a71ae0202dbeeda7c66fa Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 11 Jul 2024 15:50:37 +0200 Subject: [PATCH] decoder/List: eliminate decoder_plugins_try() Migrate callers to GetEnabledDecoderPlugins(). By not using lambdas, we can switch to enums as return value for better diagnostics. --- src/TagFile.cxx | 9 ++++-- src/TagStream.cxx | 22 +++++++------ src/command/FingerprintCommands.cxx | 24 +++++++++------ src/decoder/DecoderList.cxx | 10 ++++-- src/decoder/DecoderList.hxx | 11 ------- src/decoder/Thread.cxx | 48 +++++++++++++++-------------- src/ls.cxx | 9 ++++-- 7 files changed, 71 insertions(+), 62 deletions(-) diff --git a/src/TagFile.cxx b/src/TagFile.cxx index 90c971516..8f6ede340 100644 --- a/src/TagFile.cxx +++ b/src/TagFile.cxx @@ -68,9 +68,12 @@ ScanFileTagsNoGeneric(Path path_fs, TagHandler &handler) const auto suffix_utf8 = Path::FromFS(suffix).ToUTF8(); TagFileScan tfs(path_fs, suffix_utf8.c_str(), handler); - return decoder_plugins_try([&](const DecoderPlugin &plugin){ - return tfs.Scan(plugin); - }); + for (const auto &plugin : GetEnabledDecoderPlugins()) { + if (tfs.Scan(plugin)) + return true; + } + + return false; } bool diff --git a/src/TagStream.cxx b/src/TagStream.cxx index 9f797f215..e8f26b382 100644 --- a/src/TagStream.cxx +++ b/src/TagStream.cxx @@ -41,16 +41,20 @@ tag_stream_scan(InputStream &is, TagHandler &handler) if (full_mime != nullptr) mime_base = GetMimeTypeBase(full_mime); - return decoder_plugins_try([suffix, mime_base, &is, - &handler](const DecoderPlugin &plugin){ - try { - is.LockRewind(); - } catch (...) { - } + for (const auto &plugin : GetEnabledDecoderPlugins()) { + if (!CheckDecoderPlugin(plugin, suffix, mime_base)) + continue; - return CheckDecoderPlugin(plugin, suffix, mime_base) && - plugin.ScanStream(is, handler); - }); + try { + is.LockRewind(); + } catch (...) { + } + + if (plugin.ScanStream(is, handler)) + return true; + } + + return false; } bool diff --git a/src/command/FingerprintCommands.cxx b/src/command/FingerprintCommands.cxx index c3bc6d7f4..e029d795f 100644 --- a/src/command/FingerprintCommands.cxx +++ b/src/command/FingerprintCommands.cxx @@ -152,9 +152,10 @@ GetChromaprintCommand::DecodeStream(InputStream &is) { const auto suffix = uri_get_suffix(uri); - decoder_plugins_try([this, &is, suffix](const DecoderPlugin &plugin){ - return DecodeStream(is, suffix, plugin); - }); + for (const auto &plugin : GetEnabledDecoderPlugins()) { + if (DecodeStream(is, suffix, plugin)) + break; + } } inline bool @@ -175,9 +176,12 @@ GetChromaprintCommand::DecodeContainer(std::string_view suffix, inline bool GetChromaprintCommand::DecodeContainer(std::string_view suffix) { - return decoder_plugins_try([this, suffix](const DecoderPlugin &plugin){ - return DecodeContainer(suffix, plugin); - }); + for (const auto &plugin : GetEnabledDecoderPlugins()) { + if (DecodeContainer(suffix, plugin)) + return true; + } + + return false; } inline bool @@ -230,10 +234,10 @@ GetChromaprintCommand::DecodeFile() assert(input_stream); - auto &is = *input_stream; - decoder_plugins_try([this, suffix, &is](const DecoderPlugin &plugin){ - return DecodeFile(suffix, is, plugin); - }); + for (const auto &plugin : GetEnabledDecoderPlugins()) { + if (DecodeFile(suffix, *input_stream, plugin)) + break; + } } void diff --git a/src/decoder/DecoderList.cxx b/src/decoder/DecoderList.cxx index e6deef145..f5b83acb9 100644 --- a/src/decoder/DecoderList.cxx +++ b/src/decoder/DecoderList.cxx @@ -35,6 +35,7 @@ #include "Log.hxx" #include "PluginUnavailable.hxx" +#include // for std::any_of() #include #include @@ -164,7 +165,10 @@ decoder_plugin_deinit_all() noexcept bool decoder_plugins_supports_suffix(std::string_view suffix) noexcept { - return decoder_plugins_try([suffix](const DecoderPlugin &plugin){ - return plugin.SupportsSuffix(suffix); - }); + for (const auto &plugin : GetEnabledDecoderPlugins()) { + if (plugin.SupportsSuffix(suffix)) + return true; + } + + return false; } diff --git a/src/decoder/DecoderList.hxx b/src/decoder/DecoderList.hxx index 2e0a39f3b..120f8a4c1 100644 --- a/src/decoder/DecoderList.hxx +++ b/src/decoder/DecoderList.hxx @@ -64,17 +64,6 @@ decoder_plugins_find(F f) noexcept return nullptr; } -template -static inline bool -decoder_plugins_try(F f) -{ - for (const auto &plugin : GetEnabledDecoderPlugins()) - if (f(plugin)) - return true; - - return false; -} - /** * Is there at least once #DecoderPlugin that supports the specified * file name suffix? diff --git a/src/decoder/Thread.cxx b/src/decoder/Thread.cxx index 5f11e7346..0713540a6 100644 --- a/src/decoder/Thread.cxx +++ b/src/decoder/Thread.cxx @@ -203,10 +203,12 @@ decoder_run_stream_locked(DecoderBridge &bridge, InputStream &is, { const auto suffix = uri_get_suffix(uri); - const auto f = [&,suffix](const auto &plugin) - { return decoder_run_stream_plugin(bridge, is, lock, suffix, plugin, tried_r); }; + for (const auto &plugin : GetEnabledDecoderPlugins()) { + if (decoder_run_stream_plugin(bridge, is, lock, suffix, plugin, tried_r)) + return true; + } - return decoder_plugins_try(f); + return false; } /** @@ -268,14 +270,18 @@ MaybeLoadReplayGain(DecoderBridge &bridge, InputStream &is) static bool TryUriDecode(DecoderBridge &bridge, const char *uri) { - return decoder_plugins_try([&bridge, uri](const DecoderPlugin &plugin){ + for (const auto &plugin : GetEnabledDecoderPlugins()) { if (!plugin.SupportsUri(uri)) - return false; + continue; std::unique_lock lock{bridge.dc.mutex}; bridge.Reset(); - return DecoderUriDecode(plugin, bridge, uri); - }); + + if (DecoderUriDecode(plugin, bridge, uri)) + return true; + } + + return false; } /** @@ -367,13 +373,12 @@ static bool TryContainerDecoder(DecoderBridge &bridge, Path path_fs, std::string_view suffix) { - return decoder_plugins_try([&bridge, path_fs, - suffix](const DecoderPlugin &plugin){ - return TryContainerDecoder(bridge, - path_fs, - suffix, - plugin); - }); + for (const auto &plugin : GetEnabledDecoderPlugins()) { + if (TryContainerDecoder(bridge, path_fs, suffix, plugin)) + return true; + } + + return false; } /** @@ -408,15 +413,12 @@ decoder_run_file(DecoderBridge &bridge, const char *uri_utf8, Path path_fs) MaybeLoadReplayGain(bridge, *input_stream); - auto &is = *input_stream; - return decoder_plugins_try([&bridge, path_fs, suffix, - &is](const DecoderPlugin &plugin){ - return TryDecoderFile(bridge, - path_fs, - suffix, - is, - plugin); - }); + for (const auto &plugin : GetEnabledDecoderPlugins()) { + if (TryDecoderFile(bridge, path_fs, suffix, *input_stream, plugin)) + return true; + } + + return false; } /** diff --git a/src/ls.cxx b/src/ls.cxx index 6f2961695..33e3268f6 100644 --- a/src/ls.cxx +++ b/src/ls.cxx @@ -65,7 +65,10 @@ uri_supported_scheme(const char *uri) noexcept if (plugin->SupportsUri(uri)) return true; - return decoder_plugins_try([uri](const auto &plugin){ - return plugin.SupportsUri(uri); - }); + for (const auto &plugin : GetEnabledDecoderPlugins()) { + if (plugin.SupportsUri(uri)) + return true; + } + + return false; }