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