[util] Templatize add_group logic

This commit is contained in:
Behdad Esfahbod 2021-08-05 11:30:39 -06:00
parent fe90fbeba3
commit 3362b48f5a
2 changed files with 21 additions and 26 deletions

View File

@ -108,31 +108,6 @@ option_parser_t::add_main_options ()
g_option_context_add_main_entries (context, entries, nullptr); 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 void
option_parser_t::parse (int *argc, char ***argv) option_parser_t::parse (int *argc, char ***argv)
{ {

View File

@ -82,11 +82,31 @@ struct option_parser_t
void add_main_options (); 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, void add_group (GOptionEntry *entries,
const gchar *name, const gchar *name,
const gchar *description, const gchar *description,
const gchar *help_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) { void free_later (char *p) {
g_ptr_array_add (to_free, p); g_ptr_array_add (to_free, p);