[util] Templatize add_group logic

pull/3121/head
Behdad Esfahbod 3 years ago
parent fe90fbeba3
commit 3362b48f5a
  1. 25
      util/options.cc
  2. 22
      util/options.hh

@ -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<option_group_t *>(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<gpointer>(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)
{

@ -82,11 +82,31 @@ struct option_parser_t
void add_main_options ();
template <typename Type>
static gboolean
post_parse (GOptionContext *context G_GNUC_UNUSED,
GOptionGroup *group G_GNUC_UNUSED,
gpointer data,
GError **error)
{
auto *thiz = static_cast<Type *>(data);
thiz->post_parse (error);
return !*error;
}
template <typename Type>
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<gpointer>(closure), nullptr);
g_option_group_add_entries (group, entries);
g_option_group_set_parse_hooks (group, nullptr, post_parse<Type>);
g_option_context_add_group (context, group);
}
void free_later (char *p) {
g_ptr_array_add (to_free, p);

Loading…
Cancel
Save