/src/glib/gobject/genums.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* GObject - GLib Type, Object, Parameter and Signal Library |
2 | | * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc. |
3 | | * |
4 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
5 | | * |
6 | | * This library is free software; you can redistribute it and/or |
7 | | * modify it under the terms of the GNU Lesser General Public |
8 | | * License as published by the Free Software Foundation; either |
9 | | * version 2.1 of the License, or (at your option) any later version. |
10 | | * |
11 | | * This library is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | | * Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General |
17 | | * Public License along with this library; if not, see <http://www.gnu.org/licenses/>. |
18 | | */ |
19 | | |
20 | | /* |
21 | | * MT safe |
22 | | */ |
23 | | |
24 | | #include "config.h" |
25 | | |
26 | | #include <string.h> |
27 | | |
28 | | #include "genums.h" |
29 | | #include "gtype-private.h" |
30 | | #include "gvalue.h" |
31 | | #include "gvaluecollector.h" |
32 | | |
33 | | |
34 | | /** |
35 | | * SECTION:enumerations_flags |
36 | | * @short_description: Enumeration and flags types |
37 | | * @title: Enumeration and Flag Types |
38 | | * @see_also:#GParamSpecEnum, #GParamSpecFlags, g_param_spec_enum(), |
39 | | * g_param_spec_flags() |
40 | | * |
41 | | * The GLib type system provides fundamental types for enumeration and |
42 | | * flags types. (Flags types are like enumerations, but allow their |
43 | | * values to be combined by bitwise or). A registered enumeration or |
44 | | * flags type associates a name and a nickname with each allowed |
45 | | * value, and the methods g_enum_get_value_by_name(), |
46 | | * g_enum_get_value_by_nick(), g_flags_get_value_by_name() and |
47 | | * g_flags_get_value_by_nick() can look up values by their name or |
48 | | * nickname. When an enumeration or flags type is registered with the |
49 | | * GLib type system, it can be used as value type for object |
50 | | * properties, using g_param_spec_enum() or g_param_spec_flags(). |
51 | | * |
52 | | * GObject ships with a utility called [glib-mkenums][glib-mkenums], |
53 | | * that can construct suitable type registration functions from C enumeration |
54 | | * definitions. |
55 | | * |
56 | | * Example of how to get a string representation of an enum value: |
57 | | * |[<!-- language="C" --> |
58 | | * GEnumClass *enum_class; |
59 | | * GEnumValue *enum_value; |
60 | | * |
61 | | * enum_class = g_type_class_ref (MAMAN_TYPE_MY_ENUM); |
62 | | * enum_value = g_enum_get_value (enum_class, MAMAN_MY_ENUM_FOO); |
63 | | * |
64 | | * g_print ("Name: %s\n", enum_value->value_name); |
65 | | * |
66 | | * g_type_class_unref (enum_class); |
67 | | * ]| |
68 | | */ |
69 | | |
70 | | |
71 | | /* --- prototypes --- */ |
72 | | static void g_enum_class_init (GEnumClass *class, |
73 | | gpointer class_data); |
74 | | static void g_flags_class_init (GFlagsClass *class, |
75 | | gpointer class_data); |
76 | | static void value_flags_enum_init (GValue *value); |
77 | | static void value_flags_enum_copy_value (const GValue *src_value, |
78 | | GValue *dest_value); |
79 | | static gchar* value_flags_enum_collect_value (GValue *value, |
80 | | guint n_collect_values, |
81 | | GTypeCValue *collect_values, |
82 | | guint collect_flags); |
83 | | static gchar* value_flags_enum_lcopy_value (const GValue *value, |
84 | | guint n_collect_values, |
85 | | GTypeCValue *collect_values, |
86 | | guint collect_flags); |
87 | | |
88 | | /* --- functions --- */ |
89 | | void |
90 | | _g_enum_types_init (void) |
91 | 4 | { |
92 | 4 | static gboolean initialized = FALSE; |
93 | 4 | static const GTypeValueTable flags_enum_value_table = { |
94 | 4 | value_flags_enum_init, /* value_init */ |
95 | 4 | NULL, /* value_free */ |
96 | 4 | value_flags_enum_copy_value, /* value_copy */ |
97 | 4 | NULL, /* value_peek_pointer */ |
98 | 4 | "i", /* collect_format */ |
99 | 4 | value_flags_enum_collect_value, /* collect_value */ |
100 | 4 | "p", /* lcopy_format */ |
101 | 4 | value_flags_enum_lcopy_value, /* lcopy_value */ |
102 | 4 | }; |
103 | 4 | GTypeInfo info = { |
104 | 4 | 0, /* class_size */ |
105 | 4 | NULL, /* base_init */ |
106 | 4 | NULL, /* base_destroy */ |
107 | 4 | NULL, /* class_init */ |
108 | 4 | NULL, /* class_destroy */ |
109 | 4 | NULL, /* class_data */ |
110 | 4 | 0, /* instance_size */ |
111 | 4 | 0, /* n_preallocs */ |
112 | 4 | NULL, /* instance_init */ |
113 | 4 | &flags_enum_value_table, /* value_table */ |
114 | 4 | }; |
115 | 4 | static const GTypeFundamentalInfo finfo = { |
116 | 4 | G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_DERIVABLE, |
117 | 4 | }; |
118 | 4 | GType type G_GNUC_UNUSED /* when compiling with G_DISABLE_ASSERT */; |
119 | | |
120 | 4 | g_return_if_fail (initialized == FALSE); |
121 | 4 | initialized = TRUE; |
122 | | |
123 | | /* G_TYPE_ENUM |
124 | | */ |
125 | 4 | info.class_size = sizeof (GEnumClass); |
126 | 4 | type = g_type_register_fundamental (G_TYPE_ENUM, g_intern_static_string ("GEnum"), &info, &finfo, |
127 | 4 | G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT); |
128 | 4 | g_assert (type == G_TYPE_ENUM); |
129 | | |
130 | | /* G_TYPE_FLAGS |
131 | | */ |
132 | 4 | info.class_size = sizeof (GFlagsClass); |
133 | 4 | type = g_type_register_fundamental (G_TYPE_FLAGS, g_intern_static_string ("GFlags"), &info, &finfo, |
134 | 4 | G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT); |
135 | 4 | g_assert (type == G_TYPE_FLAGS); |
136 | 4 | } |
137 | | |
138 | | static void |
139 | | value_flags_enum_init (GValue *value) |
140 | 0 | { |
141 | 0 | value->data[0].v_long = 0; |
142 | 0 | } |
143 | | |
144 | | static void |
145 | | value_flags_enum_copy_value (const GValue *src_value, |
146 | | GValue *dest_value) |
147 | 0 | { |
148 | 0 | dest_value->data[0].v_long = src_value->data[0].v_long; |
149 | 0 | } |
150 | | |
151 | | static gchar* |
152 | | value_flags_enum_collect_value (GValue *value, |
153 | | guint n_collect_values, |
154 | | GTypeCValue *collect_values, |
155 | | guint collect_flags) |
156 | 0 | { |
157 | 0 | if (G_VALUE_HOLDS_ENUM (value)) |
158 | 0 | value->data[0].v_long = collect_values[0].v_int; |
159 | 0 | else |
160 | 0 | value->data[0].v_ulong = (guint) collect_values[0].v_int; |
161 | |
|
162 | 0 | return NULL; |
163 | 0 | } |
164 | | |
165 | | static gchar* |
166 | | value_flags_enum_lcopy_value (const GValue *value, |
167 | | guint n_collect_values, |
168 | | GTypeCValue *collect_values, |
169 | | guint collect_flags) |
170 | 0 | { |
171 | 0 | gint *int_p = collect_values[0].v_pointer; |
172 | |
|
173 | 0 | g_return_val_if_fail (int_p != NULL, g_strdup_printf ("value location for '%s' passed as NULL", G_VALUE_TYPE_NAME (value))); |
174 | | |
175 | 0 | *int_p = value->data[0].v_long; |
176 | | |
177 | 0 | return NULL; |
178 | 0 | } |
179 | | |
180 | | /** |
181 | | * g_enum_register_static: |
182 | | * @name: A nul-terminated string used as the name of the new type. |
183 | | * @const_static_values: An array of #GEnumValue structs for the possible |
184 | | * enumeration values. The array is terminated by a struct with all |
185 | | * members being 0. GObject keeps a reference to the data, so it cannot |
186 | | * be stack-allocated. |
187 | | * |
188 | | * Registers a new static enumeration type with the name @name. |
189 | | * |
190 | | * It is normally more convenient to let [glib-mkenums][glib-mkenums], |
191 | | * generate a my_enum_get_type() function from a usual C enumeration |
192 | | * definition than to write one yourself using g_enum_register_static(). |
193 | | * |
194 | | * Returns: The new type identifier. |
195 | | */ |
196 | | GType |
197 | | g_enum_register_static (const gchar *name, |
198 | | const GEnumValue *const_static_values) |
199 | 0 | { |
200 | 0 | GTypeInfo enum_type_info = { |
201 | 0 | sizeof (GEnumClass), /* class_size */ |
202 | 0 | NULL, /* base_init */ |
203 | 0 | NULL, /* base_finalize */ |
204 | 0 | (GClassInitFunc) g_enum_class_init, |
205 | 0 | NULL, /* class_finalize */ |
206 | 0 | NULL, /* class_data */ |
207 | 0 | 0, /* instance_size */ |
208 | 0 | 0, /* n_preallocs */ |
209 | 0 | NULL, /* instance_init */ |
210 | 0 | NULL, /* value_table */ |
211 | 0 | }; |
212 | 0 | GType type; |
213 | | |
214 | 0 | g_return_val_if_fail (name != NULL, 0); |
215 | 0 | g_return_val_if_fail (const_static_values != NULL, 0); |
216 | | |
217 | 0 | enum_type_info.class_data = const_static_values; |
218 | | |
219 | 0 | type = g_type_register_static (G_TYPE_ENUM, name, &enum_type_info, 0); |
220 | | |
221 | 0 | return type; |
222 | 0 | } |
223 | | |
224 | | /** |
225 | | * g_flags_register_static: |
226 | | * @name: A nul-terminated string used as the name of the new type. |
227 | | * @const_static_values: An array of #GFlagsValue structs for the possible |
228 | | * flags values. The array is terminated by a struct with all members being 0. |
229 | | * GObject keeps a reference to the data, so it cannot be stack-allocated. |
230 | | * |
231 | | * Registers a new static flags type with the name @name. |
232 | | * |
233 | | * It is normally more convenient to let [glib-mkenums][glib-mkenums] |
234 | | * generate a my_flags_get_type() function from a usual C enumeration |
235 | | * definition than to write one yourself using g_flags_register_static(). |
236 | | * |
237 | | * Returns: The new type identifier. |
238 | | */ |
239 | | GType |
240 | | g_flags_register_static (const gchar *name, |
241 | | const GFlagsValue *const_static_values) |
242 | 0 | { |
243 | 0 | GTypeInfo flags_type_info = { |
244 | 0 | sizeof (GFlagsClass), /* class_size */ |
245 | 0 | NULL, /* base_init */ |
246 | 0 | NULL, /* base_finalize */ |
247 | 0 | (GClassInitFunc) g_flags_class_init, |
248 | 0 | NULL, /* class_finalize */ |
249 | 0 | NULL, /* class_data */ |
250 | 0 | 0, /* instance_size */ |
251 | 0 | 0, /* n_preallocs */ |
252 | 0 | NULL, /* instance_init */ |
253 | 0 | NULL, /* value_table */ |
254 | 0 | }; |
255 | 0 | GType type; |
256 | | |
257 | 0 | g_return_val_if_fail (name != NULL, 0); |
258 | 0 | g_return_val_if_fail (const_static_values != NULL, 0); |
259 | | |
260 | 0 | flags_type_info.class_data = const_static_values; |
261 | | |
262 | 0 | type = g_type_register_static (G_TYPE_FLAGS, name, &flags_type_info, 0); |
263 | | |
264 | 0 | return type; |
265 | 0 | } |
266 | | |
267 | | /** |
268 | | * g_enum_complete_type_info: |
269 | | * @g_enum_type: the type identifier of the type being completed |
270 | | * @info: (out callee-allocates): the #GTypeInfo struct to be filled in |
271 | | * @const_values: An array of #GEnumValue structs for the possible |
272 | | * enumeration values. The array is terminated by a struct with all |
273 | | * members being 0. |
274 | | * |
275 | | * This function is meant to be called from the `complete_type_info` |
276 | | * function of a #GTypePlugin implementation, as in the following |
277 | | * example: |
278 | | * |
279 | | * |[<!-- language="C" --> |
280 | | * static void |
281 | | * my_enum_complete_type_info (GTypePlugin *plugin, |
282 | | * GType g_type, |
283 | | * GTypeInfo *info, |
284 | | * GTypeValueTable *value_table) |
285 | | * { |
286 | | * static const GEnumValue values[] = { |
287 | | * { MY_ENUM_FOO, "MY_ENUM_FOO", "foo" }, |
288 | | * { MY_ENUM_BAR, "MY_ENUM_BAR", "bar" }, |
289 | | * { 0, NULL, NULL } |
290 | | * }; |
291 | | * |
292 | | * g_enum_complete_type_info (type, info, values); |
293 | | * } |
294 | | * ]| |
295 | | */ |
296 | | void |
297 | | g_enum_complete_type_info (GType g_enum_type, |
298 | | GTypeInfo *info, |
299 | | const GEnumValue *const_values) |
300 | 0 | { |
301 | 0 | g_return_if_fail (G_TYPE_IS_ENUM (g_enum_type)); |
302 | 0 | g_return_if_fail (info != NULL); |
303 | 0 | g_return_if_fail (const_values != NULL); |
304 | | |
305 | 0 | info->class_size = sizeof (GEnumClass); |
306 | 0 | info->base_init = NULL; |
307 | 0 | info->base_finalize = NULL; |
308 | 0 | info->class_init = (GClassInitFunc) g_enum_class_init; |
309 | 0 | info->class_finalize = NULL; |
310 | 0 | info->class_data = const_values; |
311 | 0 | } |
312 | | |
313 | | /** |
314 | | * g_flags_complete_type_info: |
315 | | * @g_flags_type: the type identifier of the type being completed |
316 | | * @info: (out callee-allocates): the #GTypeInfo struct to be filled in |
317 | | * @const_values: An array of #GFlagsValue structs for the possible |
318 | | * enumeration values. The array is terminated by a struct with all |
319 | | * members being 0. |
320 | | * |
321 | | * This function is meant to be called from the complete_type_info() |
322 | | * function of a #GTypePlugin implementation, see the example for |
323 | | * g_enum_complete_type_info() above. |
324 | | */ |
325 | | void |
326 | | g_flags_complete_type_info (GType g_flags_type, |
327 | | GTypeInfo *info, |
328 | | const GFlagsValue *const_values) |
329 | 0 | { |
330 | 0 | g_return_if_fail (G_TYPE_IS_FLAGS (g_flags_type)); |
331 | 0 | g_return_if_fail (info != NULL); |
332 | 0 | g_return_if_fail (const_values != NULL); |
333 | | |
334 | 0 | info->class_size = sizeof (GFlagsClass); |
335 | 0 | info->base_init = NULL; |
336 | 0 | info->base_finalize = NULL; |
337 | 0 | info->class_init = (GClassInitFunc) g_flags_class_init; |
338 | 0 | info->class_finalize = NULL; |
339 | 0 | info->class_data = const_values; |
340 | 0 | } |
341 | | |
342 | | static void |
343 | | g_enum_class_init (GEnumClass *class, |
344 | | gpointer class_data) |
345 | 0 | { |
346 | 0 | g_return_if_fail (G_IS_ENUM_CLASS (class)); |
347 | | |
348 | 0 | class->minimum = 0; |
349 | 0 | class->maximum = 0; |
350 | 0 | class->n_values = 0; |
351 | 0 | class->values = class_data; |
352 | | |
353 | 0 | if (class->values) |
354 | 0 | { |
355 | 0 | GEnumValue *values; |
356 | | |
357 | 0 | class->minimum = class->values->value; |
358 | 0 | class->maximum = class->values->value; |
359 | 0 | for (values = class->values; values->value_name; values++) |
360 | 0 | { |
361 | 0 | class->minimum = MIN (class->minimum, values->value); |
362 | 0 | class->maximum = MAX (class->maximum, values->value); |
363 | 0 | class->n_values++; |
364 | 0 | } |
365 | 0 | } |
366 | 0 | } |
367 | | |
368 | | static void |
369 | | g_flags_class_init (GFlagsClass *class, |
370 | | gpointer class_data) |
371 | 0 | { |
372 | 0 | g_return_if_fail (G_IS_FLAGS_CLASS (class)); |
373 | | |
374 | 0 | class->mask = 0; |
375 | 0 | class->n_values = 0; |
376 | 0 | class->values = class_data; |
377 | | |
378 | 0 | if (class->values) |
379 | 0 | { |
380 | 0 | GFlagsValue *values; |
381 | | |
382 | 0 | for (values = class->values; values->value_name; values++) |
383 | 0 | { |
384 | 0 | class->mask |= values->value; |
385 | 0 | class->n_values++; |
386 | 0 | } |
387 | 0 | } |
388 | 0 | } |
389 | | |
390 | | /** |
391 | | * g_enum_get_value_by_name: |
392 | | * @enum_class: a #GEnumClass |
393 | | * @name: the name to look up |
394 | | * |
395 | | * Looks up a #GEnumValue by name. |
396 | | * |
397 | | * Returns: (transfer none) (nullable): the #GEnumValue with name @name, |
398 | | * or %NULL if the enumeration doesn't have a member |
399 | | * with that name |
400 | | */ |
401 | | GEnumValue* |
402 | | g_enum_get_value_by_name (GEnumClass *enum_class, |
403 | | const gchar *name) |
404 | 0 | { |
405 | 0 | g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), NULL); |
406 | 0 | g_return_val_if_fail (name != NULL, NULL); |
407 | | |
408 | 0 | if (enum_class->n_values) |
409 | 0 | { |
410 | 0 | GEnumValue *enum_value; |
411 | | |
412 | 0 | for (enum_value = enum_class->values; enum_value->value_name; enum_value++) |
413 | 0 | if (strcmp (name, enum_value->value_name) == 0) |
414 | 0 | return enum_value; |
415 | 0 | } |
416 | | |
417 | 0 | return NULL; |
418 | 0 | } |
419 | | |
420 | | /** |
421 | | * g_flags_get_value_by_name: |
422 | | * @flags_class: a #GFlagsClass |
423 | | * @name: the name to look up |
424 | | * |
425 | | * Looks up a #GFlagsValue by name. |
426 | | * |
427 | | * Returns: (transfer none) (nullable): the #GFlagsValue with name @name, |
428 | | * or %NULL if there is no flag with that name |
429 | | */ |
430 | | GFlagsValue* |
431 | | g_flags_get_value_by_name (GFlagsClass *flags_class, |
432 | | const gchar *name) |
433 | 0 | { |
434 | 0 | g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL); |
435 | 0 | g_return_val_if_fail (name != NULL, NULL); |
436 | | |
437 | 0 | if (flags_class->n_values) |
438 | 0 | { |
439 | 0 | GFlagsValue *flags_value; |
440 | | |
441 | 0 | for (flags_value = flags_class->values; flags_value->value_name; flags_value++) |
442 | 0 | if (strcmp (name, flags_value->value_name) == 0) |
443 | 0 | return flags_value; |
444 | 0 | } |
445 | | |
446 | 0 | return NULL; |
447 | 0 | } |
448 | | |
449 | | /** |
450 | | * g_enum_get_value_by_nick: |
451 | | * @enum_class: a #GEnumClass |
452 | | * @nick: the nickname to look up |
453 | | * |
454 | | * Looks up a #GEnumValue by nickname. |
455 | | * |
456 | | * Returns: (transfer none) (nullable): the #GEnumValue with nickname @nick, |
457 | | * or %NULL if the enumeration doesn't have a member |
458 | | * with that nickname |
459 | | */ |
460 | | GEnumValue* |
461 | | g_enum_get_value_by_nick (GEnumClass *enum_class, |
462 | | const gchar *nick) |
463 | 0 | { |
464 | 0 | g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), NULL); |
465 | 0 | g_return_val_if_fail (nick != NULL, NULL); |
466 | | |
467 | 0 | if (enum_class->n_values) |
468 | 0 | { |
469 | 0 | GEnumValue *enum_value; |
470 | | |
471 | 0 | for (enum_value = enum_class->values; enum_value->value_name; enum_value++) |
472 | 0 | if (enum_value->value_nick && strcmp (nick, enum_value->value_nick) == 0) |
473 | 0 | return enum_value; |
474 | 0 | } |
475 | | |
476 | 0 | return NULL; |
477 | 0 | } |
478 | | |
479 | | /** |
480 | | * g_flags_get_value_by_nick: |
481 | | * @flags_class: a #GFlagsClass |
482 | | * @nick: the nickname to look up |
483 | | * |
484 | | * Looks up a #GFlagsValue by nickname. |
485 | | * |
486 | | * Returns: (transfer none) (nullable): the #GFlagsValue with nickname @nick, |
487 | | * or %NULL if there is no flag with that nickname |
488 | | */ |
489 | | GFlagsValue* |
490 | | g_flags_get_value_by_nick (GFlagsClass *flags_class, |
491 | | const gchar *nick) |
492 | 0 | { |
493 | 0 | g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL); |
494 | 0 | g_return_val_if_fail (nick != NULL, NULL); |
495 | | |
496 | 0 | if (flags_class->n_values) |
497 | 0 | { |
498 | 0 | GFlagsValue *flags_value; |
499 | | |
500 | 0 | for (flags_value = flags_class->values; flags_value->value_nick; flags_value++) |
501 | 0 | if (flags_value->value_nick && strcmp (nick, flags_value->value_nick) == 0) |
502 | 0 | return flags_value; |
503 | 0 | } |
504 | | |
505 | 0 | return NULL; |
506 | 0 | } |
507 | | |
508 | | /** |
509 | | * g_enum_get_value: |
510 | | * @enum_class: a #GEnumClass |
511 | | * @value: the value to look up |
512 | | * |
513 | | * Returns the #GEnumValue for a value. |
514 | | * |
515 | | * Returns: (transfer none) (nullable): the #GEnumValue for @value, or %NULL |
516 | | * if @value is not a member of the enumeration |
517 | | */ |
518 | | GEnumValue* |
519 | | g_enum_get_value (GEnumClass *enum_class, |
520 | | gint value) |
521 | 0 | { |
522 | 0 | g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), NULL); |
523 | | |
524 | 0 | if (enum_class->n_values) |
525 | 0 | { |
526 | 0 | GEnumValue *enum_value; |
527 | | |
528 | 0 | for (enum_value = enum_class->values; enum_value->value_name; enum_value++) |
529 | 0 | if (enum_value->value == value) |
530 | 0 | return enum_value; |
531 | 0 | } |
532 | | |
533 | 0 | return NULL; |
534 | 0 | } |
535 | | |
536 | | /** |
537 | | * g_flags_get_first_value: |
538 | | * @flags_class: a #GFlagsClass |
539 | | * @value: the value |
540 | | * |
541 | | * Returns the first #GFlagsValue which is set in @value. |
542 | | * |
543 | | * Returns: (transfer none) (nullable): the first #GFlagsValue which is set in |
544 | | * @value, or %NULL if none is set |
545 | | */ |
546 | | GFlagsValue* |
547 | | g_flags_get_first_value (GFlagsClass *flags_class, |
548 | | guint value) |
549 | 0 | { |
550 | 0 | g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL); |
551 | | |
552 | 0 | if (flags_class->n_values) |
553 | 0 | { |
554 | 0 | GFlagsValue *flags_value; |
555 | |
|
556 | 0 | if (value == 0) |
557 | 0 | { |
558 | 0 | for (flags_value = flags_class->values; flags_value->value_name; flags_value++) |
559 | 0 | if (flags_value->value == 0) |
560 | 0 | return flags_value; |
561 | 0 | } |
562 | 0 | else |
563 | 0 | { |
564 | 0 | for (flags_value = flags_class->values; flags_value->value_name; flags_value++) |
565 | 0 | if (flags_value->value != 0 && (flags_value->value & value) == flags_value->value) |
566 | 0 | return flags_value; |
567 | 0 | } |
568 | 0 | } |
569 | | |
570 | 0 | return NULL; |
571 | 0 | } |
572 | | |
573 | | /** |
574 | | * g_enum_to_string: |
575 | | * @g_enum_type: the type identifier of a #GEnumClass type |
576 | | * @value: the value |
577 | | * |
578 | | * Pretty-prints @value in the form of the enum’s name. |
579 | | * |
580 | | * This is intended to be used for debugging purposes. The format of the output |
581 | | * may change in the future. |
582 | | * |
583 | | * Returns: (transfer full): a newly-allocated text string |
584 | | * |
585 | | * Since: 2.54 |
586 | | */ |
587 | | gchar * |
588 | | g_enum_to_string (GType g_enum_type, |
589 | | gint value) |
590 | 0 | { |
591 | 0 | gchar *result; |
592 | 0 | GEnumClass *enum_class; |
593 | 0 | GEnumValue *enum_value; |
594 | |
|
595 | 0 | g_return_val_if_fail (G_TYPE_IS_ENUM (g_enum_type), NULL); |
596 | | |
597 | 0 | enum_class = g_type_class_ref (g_enum_type); |
598 | | |
599 | | /* Already warned */ |
600 | 0 | if (enum_class == NULL) |
601 | 0 | return g_strdup_printf ("%d", value); |
602 | | |
603 | 0 | enum_value = g_enum_get_value (enum_class, value); |
604 | |
|
605 | 0 | if (enum_value == NULL) |
606 | 0 | result = g_strdup_printf ("%d", value); |
607 | 0 | else |
608 | 0 | result = g_strdup (enum_value->value_name); |
609 | |
|
610 | 0 | g_type_class_unref (enum_class); |
611 | 0 | return result; |
612 | 0 | } |
613 | | |
614 | | /* |
615 | | * g_flags_get_value_string: |
616 | | * @flags_class: a #GFlagsClass |
617 | | * @value: the value |
618 | | * |
619 | | * Pretty-prints @value in the form of the flag names separated by ` | ` and |
620 | | * sorted. Any extra bits will be shown at the end as a hexadecimal number. |
621 | | * |
622 | | * This is intended to be used for debugging purposes. The format of the output |
623 | | * may change in the future. |
624 | | * |
625 | | * Returns: (transfer full): a newly-allocated text string |
626 | | * |
627 | | * Since: 2.54 |
628 | | */ |
629 | | static gchar * |
630 | | g_flags_get_value_string (GFlagsClass *flags_class, |
631 | | guint value) |
632 | 0 | { |
633 | 0 | GString *str; |
634 | 0 | GFlagsValue *flags_value; |
635 | |
|
636 | 0 | g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL); |
637 | | |
638 | 0 | str = g_string_new (NULL); |
639 | |
|
640 | 0 | while ((str->len == 0 || value != 0) && |
641 | 0 | (flags_value = g_flags_get_first_value (flags_class, value)) != NULL) |
642 | 0 | { |
643 | 0 | if (str->len > 0) |
644 | 0 | g_string_append (str, " | "); |
645 | |
|
646 | 0 | g_string_append (str, flags_value->value_name); |
647 | |
|
648 | 0 | value &= ~flags_value->value; |
649 | 0 | } |
650 | | |
651 | | /* Show the extra bits */ |
652 | 0 | if (value != 0 || str->len == 0) |
653 | 0 | { |
654 | 0 | if (str->len > 0) |
655 | 0 | g_string_append (str, " | "); |
656 | |
|
657 | 0 | g_string_append_printf (str, "0x%x", value); |
658 | 0 | } |
659 | |
|
660 | 0 | return g_string_free (str, FALSE); |
661 | 0 | } |
662 | | |
663 | | /** |
664 | | * g_flags_to_string: |
665 | | * @flags_type: the type identifier of a #GFlagsClass type |
666 | | * @value: the value |
667 | | * |
668 | | * Pretty-prints @value in the form of the flag names separated by ` | ` and |
669 | | * sorted. Any extra bits will be shown at the end as a hexadecimal number. |
670 | | * |
671 | | * This is intended to be used for debugging purposes. The format of the output |
672 | | * may change in the future. |
673 | | * |
674 | | * Returns: (transfer full): a newly-allocated text string |
675 | | * |
676 | | * Since: 2.54 |
677 | | */ |
678 | | gchar * |
679 | | g_flags_to_string (GType flags_type, |
680 | | guint value) |
681 | 0 | { |
682 | 0 | gchar *result; |
683 | 0 | GFlagsClass *flags_class; |
684 | |
|
685 | 0 | g_return_val_if_fail (G_TYPE_IS_FLAGS (flags_type), NULL); |
686 | | |
687 | 0 | flags_class = g_type_class_ref (flags_type); |
688 | | |
689 | | /* Already warned */ |
690 | 0 | if (flags_class == NULL) |
691 | 0 | return NULL; |
692 | | |
693 | 0 | result = g_flags_get_value_string (flags_class, value); |
694 | |
|
695 | 0 | g_type_class_unref (flags_class); |
696 | 0 | return result; |
697 | 0 | } |
698 | | |
699 | | |
700 | | /** |
701 | | * g_value_set_enum: |
702 | | * @value: a valid #GValue whose type is derived from %G_TYPE_ENUM |
703 | | * @v_enum: enum value to be set |
704 | | * |
705 | | * Set the contents of a %G_TYPE_ENUM #GValue to @v_enum. |
706 | | */ |
707 | | void |
708 | | g_value_set_enum (GValue *value, |
709 | | gint v_enum) |
710 | 0 | { |
711 | 0 | g_return_if_fail (G_VALUE_HOLDS_ENUM (value)); |
712 | | |
713 | 0 | value->data[0].v_long = v_enum; |
714 | 0 | } |
715 | | |
716 | | /** |
717 | | * g_value_get_enum: |
718 | | * @value: a valid #GValue whose type is derived from %G_TYPE_ENUM |
719 | | * |
720 | | * Get the contents of a %G_TYPE_ENUM #GValue. |
721 | | * |
722 | | * Returns: enum contents of @value |
723 | | */ |
724 | | gint |
725 | | g_value_get_enum (const GValue *value) |
726 | 0 | { |
727 | 0 | g_return_val_if_fail (G_VALUE_HOLDS_ENUM (value), 0); |
728 | | |
729 | 0 | return value->data[0].v_long; |
730 | 0 | } |
731 | | |
732 | | /** |
733 | | * g_value_set_flags: |
734 | | * @value: a valid #GValue whose type is derived from %G_TYPE_FLAGS |
735 | | * @v_flags: flags value to be set |
736 | | * |
737 | | * Set the contents of a %G_TYPE_FLAGS #GValue to @v_flags. |
738 | | */ |
739 | | void |
740 | | g_value_set_flags (GValue *value, |
741 | | guint v_flags) |
742 | 0 | { |
743 | 0 | g_return_if_fail (G_VALUE_HOLDS_FLAGS (value)); |
744 | | |
745 | 0 | value->data[0].v_ulong = v_flags; |
746 | 0 | } |
747 | | |
748 | | /** |
749 | | * g_value_get_flags: |
750 | | * @value: a valid #GValue whose type is derived from %G_TYPE_FLAGS |
751 | | * |
752 | | * Get the contents of a %G_TYPE_FLAGS #GValue. |
753 | | * |
754 | | * Returns: flags contents of @value |
755 | | */ |
756 | | guint |
757 | | g_value_get_flags (const GValue *value) |
758 | 0 | { |
759 | 0 | g_return_val_if_fail (G_VALUE_HOLDS_FLAGS (value), 0); |
760 | | |
761 | 0 | return value->data[0].v_ulong; |
762 | 0 | } |