diff options
author | Max Kellermann <max@duempel.org> | 2013-10-21 22:02:19 +0200 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2013-10-21 22:02:19 +0200 |
commit | 74904b9cf2fe163c0ae1d53fb73b19826b256812 (patch) | |
tree | 5c7ee7cc1358b897d5734c14bba2f8c704e1c861 | |
parent | 82059645f18e4a8aa734e0a376d10bb52fc1cc7d (diff) | |
download | mpd-74904b9cf2fe163c0ae1d53fb73b19826b256812.tar.gz mpd-74904b9cf2fe163c0ae1d53fb73b19826b256812.tar.xz mpd-74904b9cf2fe163c0ae1d53fb73b19826b256812.zip |
DecoderList: reimplement _for_each() with function object
-rw-r--r-- | src/CommandLine.cxx | 16 | ||||
-rw-r--r-- | src/DecoderList.cxx | 11 | ||||
-rw-r--r-- | src/DecoderList.hxx | 38 | ||||
-rw-r--r-- | src/DecoderPrint.cxx | 22 |
4 files changed, 54 insertions, 33 deletions
diff --git a/src/CommandLine.cxx b/src/CommandLine.cxx index 84fb2f5f3..b0dd283a3 100644 --- a/src/CommandLine.cxx +++ b/src/CommandLine.cxx @@ -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:"); diff --git a/src/DecoderList.cxx b/src/DecoderList.cxx index 8fa821f58..2f957d970 100644 --- a/src/DecoderList.cxx +++ b/src/DecoderList.cxx @@ -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(); + }); } diff --git a/src/DecoderList.hxx b/src/DecoderList.hxx index c199caa4f..51aeb1d71 100644 --- a/src/DecoderList.hxx +++ b/src/DecoderList.hxx @@ -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 diff --git a/src/DecoderPrint.cxx b/src/DecoderPrint.cxx index 9845e45ed..2372272c2 100644 --- a/src/DecoderPrint.cxx +++ b/src/DecoderPrint.cxx @@ -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); } |