DecoderList: reimplement _for_each() with function object

This commit is contained in:
Max Kellermann 2013-10-21 22:02:19 +02:00
parent 82059645f1
commit 74904b9cf2
4 changed files with 54 additions and 33 deletions

View File

@ -75,16 +75,16 @@ static void version(void)
"\n" "\n"
"Decoders plugins:"); "Decoders plugins:");
decoder_plugins_for_each(plugin) { decoder_plugins_for_each([](const DecoderPlugin &plugin){
printf(" [%s]", plugin->name); printf(" [%s]", plugin.name);
const char *const*suffixes = plugin->suffixes; const char *const*suffixes = plugin.suffixes;
if (suffixes != nullptr) if (suffixes != nullptr)
for (; *suffixes != nullptr; ++suffixes) for (; *suffixes != nullptr; ++suffixes)
printf(" %s", *suffixes); printf(" %s", *suffixes);
puts(""); puts("");
} });
puts("\n" puts("\n"
"Output plugins:"); "Output plugins:");

View File

@ -180,9 +180,9 @@ decoder_plugin_from_mime_type(const char *mimeType, unsigned int next)
const struct DecoderPlugin * const struct DecoderPlugin *
decoder_plugin_from_name(const char *name) decoder_plugin_from_name(const char *name)
{ {
decoder_plugins_for_each_enabled(plugin) decoder_plugins_find([=](const DecoderPlugin &plugin){
if (strcmp(plugin->name, name) == 0) return strcmp(plugin.name, name) == 0;
return plugin; });
return nullptr; return nullptr;
} }
@ -233,6 +233,7 @@ void decoder_plugin_init_all(void)
void decoder_plugin_deinit_all(void) void decoder_plugin_deinit_all(void)
{ {
decoder_plugins_for_each_enabled(plugin) decoder_plugins_for_each_enabled([=](const DecoderPlugin &plugin){
plugin->Finish(); plugin.Finish();
});
} }

View File

@ -25,16 +25,6 @@ struct DecoderPlugin;
extern const struct DecoderPlugin *const decoder_plugins[]; extern const struct DecoderPlugin *const decoder_plugins[];
extern bool decoder_plugins_enabled[]; 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 */ /* interface for using plugins */
/** /**
@ -60,4 +50,32 @@ void decoder_plugin_init_all(void);
/* this is where we "unload" all the "plugins" */ /* this is where we "unload" all the "plugins" */
void decoder_plugin_deinit_all(void); 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 #endif

View File

@ -23,31 +23,33 @@
#include "DecoderPlugin.hxx" #include "DecoderPlugin.hxx"
#include "Client.hxx" #include "Client.hxx"
#include <functional>
#include <assert.h> #include <assert.h>
static void static void
decoder_plugin_print(Client &client, decoder_plugin_print(Client &client,
const struct DecoderPlugin *plugin) const DecoderPlugin &plugin)
{ {
const char *const*p; 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) if (plugin.suffixes != nullptr)
for (p = plugin->suffixes; *p != nullptr; ++p) for (p = plugin.suffixes; *p != nullptr; ++p)
client_printf(client, "suffix: %s\n", *p); client_printf(client, "suffix: %s\n", *p);
if (plugin->mime_types != nullptr) if (plugin.mime_types != nullptr)
for (p = plugin->mime_types; *p != nullptr; ++p) for (p = plugin.mime_types; *p != nullptr; ++p)
client_printf(client, "mime_type: %s\n", *p); client_printf(client, "mime_type: %s\n", *p);
} }
void void
decoder_list_print(Client &client) decoder_list_print(Client &client)
{ {
decoder_plugins_for_each_enabled(plugin) using namespace std::placeholders;
decoder_plugin_print(client, plugin); const auto f = std::bind(decoder_plugin_print, std::ref(client), _1);
decoder_plugins_for_each_enabled(f);
} }