[Github-comments] [geany/geany] Stash Settings: Add double key type (PR #3004)

xiota notifications at xxxxx
Wed Nov 17 06:41:59 UTC 2021


@kugel- 

I don't understand the benefit of using union over casting the pointers.  Based on what I'm reading online, they're both considered "bad".  It also looks like switching to using unions may require changing existing code, which I've been trying to avoid.

I'm probably doing this wrong...  I tried adding the following union to `stash.h`:
```C
typedef union
{
	gboolean b;
	gint n;
	gdouble d;
	gchar *s;
	gchar **v;
	const gchar *cs;
	const gchar **cv;
} StashPrefType;
```

The StashPref struct becomes:
```C
struct StashPref
{
	/* ... */
	StashPrefType *setting;
	StashPrefType default_value;
	/* ... */
};
```

Some functions need to be redefined:
```C
add_pref(StashGroup *group, GType type, StashPrefType *setting,
		const gchar *key_name, StashPrefType default_value);

static StashPref *
add_widget_pref(StashGroup *group, GType setting_type, StashPrefType *setting,
		const gchar *key_name, StashPrefType default_value,
		GType widget_type, StashWidgetID widget_id);

void stash_group_add_widget_property(StashGroup *group, StashPrefType *setting,
		const gchar *key_name, StashPrefType default_value, StashWidgetID widget_id,
		const gchar *property_name, GType type);
```

To use StashPrefType requires something like:
```C
int *setting = &se->setting->n;
double *setting = &se->setting->d;
```

Then calls to the above functions need to have casts to `StashPrefType`, like:
```C
add_pref(group, G_TYPE_BOOLEAN, (StashPrefType *) setting, key_name, (StashPrefType)default_value);
```

And any external uses of `stash_group_add_widget_property()` (`sidebar.c` and maybe plugins) also need to cast StashPrefType.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/geany/geany/pull/3004#issuecomment-971255719
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.geany.org/pipermail/github-comments/attachments/20211116/f01133a2/attachment.htm>


More information about the Github-comments mailing list