From 3362b48f5a5eee9fe6378787ffce3304b341e474 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 5 Aug 2021 11:30:39 -0600 Subject: [PATCH] [util] Templatize add_group logic --- util/options.cc | 25 ------------------------- util/options.hh | 22 +++++++++++++++++++++- 2 files changed, 21 insertions(+), 26 deletions(-) diff --git a/util/options.cc b/util/options.cc index 19d8a1c8d..14f04b8f1 100644 --- a/util/options.cc +++ b/util/options.cc @@ -108,31 +108,6 @@ option_parser_t::add_main_options () g_option_context_add_main_entries (context, entries, nullptr); } -static gboolean -post_parse (GOptionContext *context G_GNUC_UNUSED, - GOptionGroup *group G_GNUC_UNUSED, - gpointer data, - GError **error) -{ - option_group_t *option_group = static_cast(data); - option_group->post_parse (error); - return !*error; -} - -void -option_parser_t::add_group (GOptionEntry *entries, - const gchar *name, - const gchar *description, - const gchar *help_description, - option_group_t *option_group) -{ - GOptionGroup *group = g_option_group_new (name, description, help_description, - static_cast(option_group), nullptr); - g_option_group_add_entries (group, entries); - g_option_group_set_parse_hooks (group, nullptr, post_parse); - g_option_context_add_group (context, group); -} - void option_parser_t::parse (int *argc, char ***argv) { diff --git a/util/options.hh b/util/options.hh index 1062eef20..d738d2473 100644 --- a/util/options.hh +++ b/util/options.hh @@ -82,11 +82,31 @@ struct option_parser_t void add_main_options (); + template + static gboolean + post_parse (GOptionContext *context G_GNUC_UNUSED, + GOptionGroup *group G_GNUC_UNUSED, + gpointer data, + GError **error) + { + auto *thiz = static_cast(data); + thiz->post_parse (error); + return !*error; + } + + template void add_group (GOptionEntry *entries, const gchar *name, const gchar *description, const gchar *help_description, - option_group_t *option_group); + Type *closure) + { + GOptionGroup *group = g_option_group_new (name, description, help_description, + static_cast(closure), nullptr); + g_option_group_add_entries (group, entries); + g_option_group_set_parse_hooks (group, nullptr, post_parse); + g_option_context_add_group (context, group); + } void free_later (char *p) { g_ptr_array_add (to_free, p);