File: gperl-i11n-enums.c

package info (click to toggle)
libglib-object-introspection-perl 0.048-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 544 kB
  • sloc: ansic: 3,468; perl: 2,785; makefile: 9; sh: 3
file content (54 lines) | stat: -rw-r--r-- 1,806 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* -*- mode: c; indent-tabs-mode: t; c-basic-offset: 8; -*- */

#define FILL_VALUES(values, value_type)                                 \
	{ gint i; \
	for (i = 0; i < n_values; i++) { \
		GIValueInfo *value_info = g_enum_info_get_value (info, i); \
		(values)[i].value = (value_type) g_value_info_get_value (value_info); \
		/* FIXME: Can we assume that the strings will stick around long enough? */ \
		(values)[i].value_nick = g_base_info_get_name (value_info); \
		(values)[i].value_name = g_base_info_get_attribute (value_info, "c:identifier"); \
		if (!(values)[i].value_name) \
			(values)[i].value_name = (values)[i].value_nick; \
		g_base_info_unref (value_info); \
	} }

static GType
register_unregistered_enum (GIEnumInfo *info)
{
	GType gtype = G_TYPE_NONE;
	gchar *full_name;
	GIInfoType info_type;
	void *values;
	gint n_values;

	/* Abort if there already is a GType under this name. */
	full_name = synthesize_prefixed_gtype_name (info);
	if (g_type_from_name (full_name)) {
		g_free (full_name);
		return gtype;
	}

	info_type = g_base_info_get_type (info);

	/* We have to leak 'values' as g_enum_register_static and
	 * g_flags_register_static assume that what we pass in will be valid
	 * throughout the lifetime of the program. */
	n_values = g_enum_info_get_n_values (info);
	if (info_type == GI_INFO_TYPE_ENUM) {
		values = g_new0 (GEnumValue, n_values+1); /* zero-terminated */
		FILL_VALUES ((GEnumValue *) values, gint);
	} else {
		values = g_new0 (GFlagsValue, n_values+1); /* zero-terminated */
		FILL_VALUES ((GFlagsValue *) values, guint);
	}

	if (info_type == GI_INFO_TYPE_ENUM) {
		gtype = g_enum_register_static (full_name, (GEnumValue *) values);
	} else {
		gtype = g_flags_register_static (full_name, (GFlagsValue *) values);
	}

	g_free (full_name);
	return gtype;
}