DecoderList: reimplement _for_each() with function object
This commit is contained in:
parent
82059645f1
commit
74904b9cf2
@ -75,16 +75,16 @@ static void version(void)
|
||||
"\n"
|
||||
"Decoders plugins:");
|
||||
|
||||
decoder_plugins_for_each(plugin) {
|
||||
printf(" [%s]", plugin->name);
|
||||
decoder_plugins_for_each([](const DecoderPlugin &plugin){
|
||||
printf(" [%s]", plugin.name);
|
||||
|
||||
const char *const*suffixes = plugin->suffixes;
|
||||
if (suffixes != nullptr)
|
||||
for (; *suffixes != nullptr; ++suffixes)
|
||||
printf(" %s", *suffixes);
|
||||
const char *const*suffixes = plugin.suffixes;
|
||||
if (suffixes != nullptr)
|
||||
for (; *suffixes != nullptr; ++suffixes)
|
||||
printf(" %s", *suffixes);
|
||||
|
||||
puts("");
|
||||
}
|
||||
puts("");
|
||||
});
|
||||
|
||||
puts("\n"
|
||||
"Output plugins:");
|
||||
|
@ -180,9 +180,9 @@ decoder_plugin_from_mime_type(const char *mimeType, unsigned int next)
|
||||
const struct DecoderPlugin *
|
||||
decoder_plugin_from_name(const char *name)
|
||||
{
|
||||
decoder_plugins_for_each_enabled(plugin)
|
||||
if (strcmp(plugin->name, name) == 0)
|
||||
return plugin;
|
||||
decoder_plugins_find([=](const DecoderPlugin &plugin){
|
||||
return strcmp(plugin.name, name) == 0;
|
||||
});
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
@ -233,6 +233,7 @@ void decoder_plugin_init_all(void)
|
||||
|
||||
void decoder_plugin_deinit_all(void)
|
||||
{
|
||||
decoder_plugins_for_each_enabled(plugin)
|
||||
plugin->Finish();
|
||||
decoder_plugins_for_each_enabled([=](const DecoderPlugin &plugin){
|
||||
plugin.Finish();
|
||||
});
|
||||
}
|
||||
|
@ -25,16 +25,6 @@ struct DecoderPlugin;
|
||||
extern const struct DecoderPlugin *const decoder_plugins[];
|
||||
extern bool decoder_plugins_enabled[];
|
||||
|
||||
#define decoder_plugins_for_each(plugin) \
|
||||
for (const struct DecoderPlugin *plugin, \
|
||||
*const*decoder_plugin_iterator = &decoder_plugins[0]; \
|
||||
(plugin = *decoder_plugin_iterator) != nullptr; \
|
||||
++decoder_plugin_iterator)
|
||||
|
||||
#define decoder_plugins_for_each_enabled(plugin) \
|
||||
decoder_plugins_for_each(plugin) \
|
||||
if (decoder_plugins_enabled[decoder_plugin_iterator - decoder_plugins])
|
||||
|
||||
/* interface for using plugins */
|
||||
|
||||
/**
|
||||
@ -60,4 +50,32 @@ void decoder_plugin_init_all(void);
|
||||
/* this is where we "unload" all the "plugins" */
|
||||
void decoder_plugin_deinit_all(void);
|
||||
|
||||
template<typename F>
|
||||
static inline const DecoderPlugin *
|
||||
decoder_plugins_find(F f)
|
||||
{
|
||||
for (unsigned i = 0; decoder_plugins[i] != nullptr; ++i)
|
||||
if (decoder_plugins_enabled[i] && f(*decoder_plugins[i]))
|
||||
return decoder_plugins[i];
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
static inline void
|
||||
decoder_plugins_for_each(F f)
|
||||
{
|
||||
for (auto i = decoder_plugins; *i != nullptr; ++i)
|
||||
f(**i);
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
static inline void
|
||||
decoder_plugins_for_each_enabled(F f)
|
||||
{
|
||||
for (unsigned i = 0; decoder_plugins[i] != nullptr; ++i)
|
||||
if (decoder_plugins_enabled[i])
|
||||
f(*decoder_plugins[i]);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -23,31 +23,33 @@
|
||||
#include "DecoderPlugin.hxx"
|
||||
#include "Client.hxx"
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
static void
|
||||
decoder_plugin_print(Client &client,
|
||||
const struct DecoderPlugin *plugin)
|
||||
const DecoderPlugin &plugin)
|
||||
{
|
||||
const char *const*p;
|
||||
|
||||
assert(plugin != nullptr);
|
||||
assert(plugin->name != nullptr);
|
||||
assert(plugin.name != nullptr);
|
||||
|
||||
client_printf(client, "plugin: %s\n", plugin->name);
|
||||
client_printf(client, "plugin: %s\n", plugin.name);
|
||||
|
||||
if (plugin->suffixes != nullptr)
|
||||
for (p = plugin->suffixes; *p != nullptr; ++p)
|
||||
if (plugin.suffixes != nullptr)
|
||||
for (p = plugin.suffixes; *p != nullptr; ++p)
|
||||
client_printf(client, "suffix: %s\n", *p);
|
||||
|
||||
if (plugin->mime_types != nullptr)
|
||||
for (p = plugin->mime_types; *p != nullptr; ++p)
|
||||
if (plugin.mime_types != nullptr)
|
||||
for (p = plugin.mime_types; *p != nullptr; ++p)
|
||||
client_printf(client, "mime_type: %s\n", *p);
|
||||
}
|
||||
|
||||
void
|
||||
decoder_list_print(Client &client)
|
||||
{
|
||||
decoder_plugins_for_each_enabled(plugin)
|
||||
decoder_plugin_print(client, plugin);
|
||||
using namespace std::placeholders;
|
||||
const auto f = std::bind(decoder_plugin_print, std::ref(client), _1);
|
||||
decoder_plugins_for_each_enabled(f);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user