Branch: refs/heads/master Author: Thomas Martitz thomas.martitz@mailbox.org Committer: GitHub noreply@github.com Date: Wed, 17 Nov 2021 08:45:26 UTC Commit: cf77c8c0f1f0cc6ad7bf3132ea1d59639a7a83e7 https://github.com/geany/geany/commit/cf77c8c0f1f0cc6ad7bf3132ea1d59639a7a83...
Log Message: ----------- Use union to store StashPref default values (#3005)
This gives better type-safety than casting everything to gpointer.
Modified Paths: -------------- src/stash.c
Modified: src/stash.c 66 lines changed, 39 insertions(+), 27 deletions(-) =================================================================== @@ -98,13 +98,23 @@ static GType get_combo_box_entry_type(void) return type; }
+/* storage for StashPref default values */ +union Value +{ + gboolean bool_val; + gint int_val; + gchar *str_val; + gchar **strv_val; + gpointer *ptr_val; + GtkWidget *widget_val; +};
struct StashPref { GType setting_type; /* e.g. G_TYPE_INT */ gpointer setting; /* Address of a variable */ const gchar *key_name; - gpointer default_value; /* Default value, e.g. (gpointer)1 */ + union Value default_value; /* Default value, as per setting_type above, e.g. .int_val */ GType widget_type; /* e.g. GTK_TYPE_TOGGLE_BUTTON */ StashWidgetID widget_id; /* (GtkWidget*) or (gchar*) */ union @@ -158,7 +168,7 @@ static void handle_boolean_setting(StashGroup *group, StashPref *se, { case SETTING_READ: *setting = utils_get_setting_boolean(config, group->name, se->key_name, - GPOINTER_TO_INT(se->default_value)); + se->default_value.bool_val); break; case SETTING_WRITE: g_key_file_set_boolean(config, group->name, se->key_name, *setting); @@ -176,7 +186,7 @@ static void handle_integer_setting(StashGroup *group, StashPref *se, { case SETTING_READ: *setting = utils_get_setting_integer(config, group->name, se->key_name, - GPOINTER_TO_INT(se->default_value)); + se->default_value.int_val); break; case SETTING_WRITE: g_key_file_set_integer(config, group->name, se->key_name, *setting); @@ -195,7 +205,7 @@ static void handle_string_setting(StashGroup *group, StashPref *se, case SETTING_READ: g_free(*setting); *setting = utils_get_setting_string(config, group->name, se->key_name, - se->default_value); + se->default_value.str_val); break; case SETTING_WRITE: g_key_file_set_string(config, group->name, se->key_name, @@ -217,7 +227,7 @@ static void handle_strv_setting(StashGroup *group, StashPref *se, *setting = g_key_file_get_string_list(config, group->name, se->key_name, NULL, NULL); if (*setting == NULL) - *setting = g_strdupv(se->default_value); + *setting = g_strdupv(se->default_value.strv_val); break;
case SETTING_WRITE: @@ -445,18 +455,17 @@ void stash_group_set_use_defaults(StashGroup *group, gboolean use_defaults)
static StashPref * add_pref(StashGroup *group, GType type, gpointer setting, - const gchar *key_name, gpointer default_value) + const gchar *key_name, union Value default_value) { - StashPref init = {type, setting, key_name, default_value, G_TYPE_NONE, NULL, {NULL}}; StashPref *entry = g_slice_new(StashPref);
- *entry = init; + *entry = (StashPref) {type, setting, key_name, default_value, G_TYPE_NONE, NULL, {NULL}};
/* init any pointer settings to NULL so they can be freed later */ - if (type == G_TYPE_STRING || - type == G_TYPE_STRV) + if (type == G_TYPE_STRING || type == G_TYPE_STRV) { if (group->use_defaults) *(gpointer**)setting = NULL; + }
g_ptr_array_add(group->entries, entry); return entry; @@ -472,7 +481,7 @@ GEANY_API_SYMBOL void stash_group_add_boolean(StashGroup *group, gboolean *setting, const gchar *key_name, gboolean default_value) { - add_pref(group, G_TYPE_BOOLEAN, setting, key_name, GINT_TO_POINTER(default_value)); + add_pref(group, G_TYPE_BOOLEAN, setting, key_name, (union Value) {.bool_val = default_value}); }
@@ -485,7 +494,7 @@ GEANY_API_SYMBOL void stash_group_add_integer(StashGroup *group, gint *setting, const gchar *key_name, gint default_value) { - add_pref(group, G_TYPE_INT, setting, key_name, GINT_TO_POINTER(default_value)); + add_pref(group, G_TYPE_INT, setting, key_name, (union Value) {.int_val = default_value}); }
@@ -499,7 +508,7 @@ GEANY_API_SYMBOL void stash_group_add_string(StashGroup *group, gchar **setting, const gchar *key_name, const gchar *default_value) { - add_pref(group, G_TYPE_STRING, setting, key_name, (gpointer)default_value); + add_pref(group, G_TYPE_STRING, setting, key_name, (union Value) {.str_val = (gchar *) default_value}); }
@@ -513,7 +522,7 @@ GEANY_API_SYMBOL void stash_group_add_string_vector(StashGroup *group, gchar ***setting, const gchar *key_name, const gchar **default_value) { - add_pref(group, G_TYPE_STRV, setting, key_name, (gpointer)default_value); + add_pref(group, G_TYPE_STRV, setting, key_name, (union Value) {.strv_val = (gchar **) default_value}); }
@@ -799,7 +808,7 @@ void stash_group_update(StashGroup *group, GtkWidget *owner)
static StashPref * add_widget_pref(StashGroup *group, GType setting_type, gpointer setting, - const gchar *key_name, gpointer default_value, + const gchar *key_name, union Value default_value, GType widget_type, StashWidgetID widget_id) { StashPref *entry = @@ -822,7 +831,7 @@ GEANY_API_SYMBOL void stash_group_add_toggle_button(StashGroup *group, gboolean *setting, const gchar *key_name, gboolean default_value, StashWidgetID widget_id) { - add_widget_pref(group, G_TYPE_BOOLEAN, setting, key_name, GINT_TO_POINTER(default_value), + add_widget_pref(group, G_TYPE_BOOLEAN, setting, key_name, (union Value) {.bool_val = default_value}, GTK_TYPE_TOGGLE_BUTTON, widget_id); }
@@ -847,7 +856,7 @@ void stash_group_add_radio_buttons(StashGroup *group, gint *setting, StashWidgetID widget_id, gint enum_id, ...) { StashPref *entry = - add_widget_pref(group, G_TYPE_INT, setting, key_name, GINT_TO_POINTER(default_value), + add_widget_pref(group, G_TYPE_INT, setting, key_name, (union Value) {.int_val = default_value}, GTK_TYPE_RADIO_BUTTON, NULL); va_list args; gsize count = 1; @@ -896,8 +905,8 @@ GEANY_API_SYMBOL void stash_group_add_spin_button_integer(StashGroup *group, gint *setting, const gchar *key_name, gint default_value, StashWidgetID widget_id) { - add_widget_pref(group, G_TYPE_INT, setting, key_name, GINT_TO_POINTER(default_value), - GTK_TYPE_SPIN_BUTTON, widget_id); + add_widget_pref(group, G_TYPE_INT, setting, key_name, + (union Value) {.int_val = default_value}, GTK_TYPE_SPIN_BUTTON, widget_id); }
@@ -912,8 +921,8 @@ GEANY_API_SYMBOL void stash_group_add_combo_box(StashGroup *group, gint *setting, const gchar *key_name, gint default_value, StashWidgetID widget_id) { - add_widget_pref(group, G_TYPE_INT, setting, key_name, GINT_TO_POINTER(default_value), - GTK_TYPE_COMBO_BOX, widget_id); + add_widget_pref(group, G_TYPE_INT, setting, key_name, + (union Value) {.int_val = default_value}, GTK_TYPE_COMBO_BOX, widget_id); }
@@ -929,8 +938,8 @@ GEANY_API_SYMBOL void stash_group_add_combo_box_entry(StashGroup *group, gchar **setting, const gchar *key_name, const gchar *default_value, StashWidgetID widget_id) { - add_widget_pref(group, G_TYPE_STRING, setting, key_name, (gpointer)default_value, - TYPE_COMBO_BOX_ENTRY, widget_id); + add_widget_pref(group, G_TYPE_STRING, setting, key_name, + (union Value) {.str_val = (gchar *) default_value}, TYPE_COMBO_BOX_ENTRY, widget_id); }
@@ -944,8 +953,8 @@ GEANY_API_SYMBOL void stash_group_add_entry(StashGroup *group, gchar **setting, const gchar *key_name, const gchar *default_value, StashWidgetID widget_id) { - add_widget_pref(group, G_TYPE_STRING, setting, key_name, (gpointer)default_value, - GTK_TYPE_ENTRY, widget_id); + add_widget_pref(group, G_TYPE_STRING, setting, key_name, + (union Value) {.str_val = (gchar *) default_value}, GTK_TYPE_ENTRY, widget_id); }
@@ -978,11 +987,14 @@ void stash_group_add_widget_property(StashGroup *group, gpointer setting, const gchar *key_name, gpointer default_value, StashWidgetID widget_id, const gchar *property_name, GType type) { + StashPref *entry; + if (!type) type = object_get_property_type(G_OBJECT(widget_id), property_name);
- add_widget_pref(group, type, setting, key_name, default_value, - G_TYPE_PARAM, widget_id)->extra.property_name = property_name; + entry = add_widget_pref(group, type, setting, key_name, + (union Value) {.ptr_val = default_value}, G_TYPE_PARAM, widget_id); + entry->extra.property_name = property_name; }
-------------- This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).