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.
This commit is contained in:
Max Kellermann 2024-07-11 15:50:37 +02:00
parent a27fb71c4c
commit c2470ebd9c
7 changed files with 71 additions and 62 deletions

View File

@ -68,9 +68,12 @@ ScanFileTagsNoGeneric(Path path_fs, TagHandler &handler)
const auto suffix_utf8 = Path::FromFS(suffix).ToUTF8(); const auto suffix_utf8 = Path::FromFS(suffix).ToUTF8();
TagFileScan tfs(path_fs, suffix_utf8.c_str(), handler); TagFileScan tfs(path_fs, suffix_utf8.c_str(), handler);
return decoder_plugins_try([&](const DecoderPlugin &plugin){ for (const auto &plugin : GetEnabledDecoderPlugins()) {
return tfs.Scan(plugin); if (tfs.Scan(plugin))
}); return true;
}
return false;
} }
bool bool

View File

@ -41,16 +41,20 @@ tag_stream_scan(InputStream &is, TagHandler &handler)
if (full_mime != nullptr) if (full_mime != nullptr)
mime_base = GetMimeTypeBase(full_mime); mime_base = GetMimeTypeBase(full_mime);
return decoder_plugins_try([suffix, mime_base, &is, for (const auto &plugin : GetEnabledDecoderPlugins()) {
&handler](const DecoderPlugin &plugin){ if (!CheckDecoderPlugin(plugin, suffix, mime_base))
try { continue;
is.LockRewind();
} catch (...) {
}
return CheckDecoderPlugin(plugin, suffix, mime_base) && try {
plugin.ScanStream(is, handler); is.LockRewind();
}); } catch (...) {
}
if (plugin.ScanStream(is, handler))
return true;
}
return false;
} }
bool bool

View File

@ -152,9 +152,10 @@ GetChromaprintCommand::DecodeStream(InputStream &is)
{ {
const auto suffix = uri_get_suffix(uri); const auto suffix = uri_get_suffix(uri);
decoder_plugins_try([this, &is, suffix](const DecoderPlugin &plugin){ for (const auto &plugin : GetEnabledDecoderPlugins()) {
return DecodeStream(is, suffix, plugin); if (DecodeStream(is, suffix, plugin))
}); break;
}
} }
inline bool inline bool
@ -175,9 +176,12 @@ GetChromaprintCommand::DecodeContainer(std::string_view suffix,
inline bool inline bool
GetChromaprintCommand::DecodeContainer(std::string_view suffix) GetChromaprintCommand::DecodeContainer(std::string_view suffix)
{ {
return decoder_plugins_try([this, suffix](const DecoderPlugin &plugin){ for (const auto &plugin : GetEnabledDecoderPlugins()) {
return DecodeContainer(suffix, plugin); if (DecodeContainer(suffix, plugin))
}); return true;
}
return false;
} }
inline bool inline bool
@ -230,10 +234,10 @@ GetChromaprintCommand::DecodeFile()
assert(input_stream); assert(input_stream);
auto &is = *input_stream; for (const auto &plugin : GetEnabledDecoderPlugins()) {
decoder_plugins_try([this, suffix, &is](const DecoderPlugin &plugin){ if (DecodeFile(suffix, *input_stream, plugin))
return DecodeFile(suffix, is, plugin); break;
}); }
} }
void void

View File

@ -35,6 +35,7 @@
#include "Log.hxx" #include "Log.hxx"
#include "PluginUnavailable.hxx" #include "PluginUnavailable.hxx"
#include <algorithm> // for std::any_of()
#include <iterator> #include <iterator>
#include <string.h> #include <string.h>
@ -164,7 +165,10 @@ decoder_plugin_deinit_all() noexcept
bool bool
decoder_plugins_supports_suffix(std::string_view suffix) noexcept decoder_plugins_supports_suffix(std::string_view suffix) noexcept
{ {
return decoder_plugins_try([suffix](const DecoderPlugin &plugin){ for (const auto &plugin : GetEnabledDecoderPlugins()) {
return plugin.SupportsSuffix(suffix); if (plugin.SupportsSuffix(suffix))
}); return true;
}
return false;
} }

View File

@ -64,17 +64,6 @@ decoder_plugins_find(F f) noexcept
return nullptr; return nullptr;
} }
template<typename F>
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 * Is there at least once #DecoderPlugin that supports the specified
* file name suffix? * file name suffix?

View File

@ -203,10 +203,12 @@ decoder_run_stream_locked(DecoderBridge &bridge, InputStream &is,
{ {
const auto suffix = uri_get_suffix(uri); const auto suffix = uri_get_suffix(uri);
const auto f = [&,suffix](const auto &plugin) for (const auto &plugin : GetEnabledDecoderPlugins()) {
{ return decoder_run_stream_plugin(bridge, is, lock, suffix, plugin, tried_r); }; 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 static bool
TryUriDecode(DecoderBridge &bridge, const char *uri) TryUriDecode(DecoderBridge &bridge, const char *uri)
{ {
return decoder_plugins_try([&bridge, uri](const DecoderPlugin &plugin){ for (const auto &plugin : GetEnabledDecoderPlugins()) {
if (!plugin.SupportsUri(uri)) if (!plugin.SupportsUri(uri))
return false; continue;
std::unique_lock lock{bridge.dc.mutex}; std::unique_lock lock{bridge.dc.mutex};
bridge.Reset(); 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, TryContainerDecoder(DecoderBridge &bridge, Path path_fs,
std::string_view suffix) std::string_view suffix)
{ {
return decoder_plugins_try([&bridge, path_fs, for (const auto &plugin : GetEnabledDecoderPlugins()) {
suffix](const DecoderPlugin &plugin){ if (TryContainerDecoder(bridge, path_fs, suffix, plugin))
return TryContainerDecoder(bridge, return true;
path_fs, }
suffix,
plugin); return false;
});
} }
/** /**
@ -408,15 +413,12 @@ decoder_run_file(DecoderBridge &bridge, const char *uri_utf8, Path path_fs)
MaybeLoadReplayGain(bridge, *input_stream); MaybeLoadReplayGain(bridge, *input_stream);
auto &is = *input_stream; for (const auto &plugin : GetEnabledDecoderPlugins()) {
return decoder_plugins_try([&bridge, path_fs, suffix, if (TryDecoderFile(bridge, path_fs, suffix, *input_stream, plugin))
&is](const DecoderPlugin &plugin){ return true;
return TryDecoderFile(bridge, }
path_fs,
suffix, return false;
is,
plugin);
});
} }
/** /**

View File

@ -65,7 +65,10 @@ uri_supported_scheme(const char *uri) noexcept
if (plugin->SupportsUri(uri)) if (plugin->SupportsUri(uri))
return true; return true;
return decoder_plugins_try([uri](const auto &plugin){ for (const auto &plugin : GetEnabledDecoderPlugins()) {
return plugin.SupportsUri(uri); if (plugin.SupportsUri(uri))
}); return true;
}
return false;
} }