1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
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.
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.
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/>.
27 #include "gtype-private.h"
29 #include "gvaluecollector.h"
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()
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().
50 * GObject ships with a utility called [glib-mkenums][glib-mkenums],
51 * that can construct suitable type registration functions from C enumeration
54 * Example of how to get a string representation of an enum value:
55 * |[<!-- language="C" -->
56 * GEnumClass *enum_class;
57 * GEnumValue *enum_value;
59 * enum_class = g_type_class_ref (MAMAN_TYPE_MY_ENUM);
60 * enum_value = g_enum_get_value (enum_class, MAMAN_MY_ENUM_FOO);
62 * g_print ("Name: %s\n", enum_value->value_name);
64 * g_type_class_unref (enum_class);
69 /* --- prototypes --- */
70 static void g_enum_class_init (GEnumClass
*class,
72 static void g_flags_class_init (GFlagsClass
*class,
74 static void value_flags_enum_init (GValue
*value
);
75 static void value_flags_enum_copy_value (const GValue
*src_value
,
77 static gchar
* value_flags_enum_collect_value (GValue
*value
,
78 guint n_collect_values
,
79 GTypeCValue
*collect_values
,
81 static gchar
* value_flags_enum_lcopy_value (const GValue
*value
,
82 guint n_collect_values
,
83 GTypeCValue
*collect_values
,
86 /* --- functions --- */
88 _g_enum_types_init (void)
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 */
103 NULL
, /* base_init */
104 NULL
, /* base_destroy */
105 NULL
, /* class_init */
106 NULL
, /* class_destroy */
107 NULL
, /* class_data */
108 0, /* instance_size */
110 NULL
, /* instance_init */
111 &flags_enum_value_table
, /* value_table */
113 static const GTypeFundamentalInfo finfo
= {
114 G_TYPE_FLAG_CLASSED
| G_TYPE_FLAG_DERIVABLE
,
118 g_return_if_fail (initialized
== FALSE
);
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
);
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
);
137 value_flags_enum_init (GValue
*value
)
139 value
->data
[0].v_long
= 0;
143 value_flags_enum_copy_value (const GValue
*src_value
,
146 dest_value
->data
[0].v_long
= src_value
->data
[0].v_long
;
150 value_flags_enum_collect_value (GValue
*value
,
151 guint n_collect_values
,
152 GTypeCValue
*collect_values
,
155 value
->data
[0].v_long
= collect_values
[0].v_int
;
161 value_flags_enum_lcopy_value (const GValue
*value
,
162 guint n_collect_values
,
163 GTypeCValue
*collect_values
,
166 gint
*int_p
= collect_values
[0].v_pointer
;
169 return g_strdup_printf ("value location for '%s' passed as NULL", G_VALUE_TYPE_NAME (value
));
171 *int_p
= value
->data
[0].v_long
;
177 * g_enum_register_static:
178 * @name: A nul-terminated string used as the name of the new type.
179 * @const_static_values: An array of #GEnumValue structs for the possible
180 * enumeration values. The array is terminated by a struct with all
181 * members being 0. GObject keeps a reference to the data, so it cannot
182 * be stack-allocated.
184 * Registers a new static enumeration type with the name @name.
186 * It is normally more convenient to let [glib-mkenums][glib-mkenums],
187 * generate a my_enum_get_type() function from a usual C enumeration
188 * definition than to write one yourself using g_enum_register_static().
190 * Returns: The new type identifier.
193 g_enum_register_static (const gchar
*name
,
194 const GEnumValue
*const_static_values
)
196 GTypeInfo enum_type_info
= {
197 sizeof (GEnumClass
), /* class_size */
198 NULL
, /* base_init */
199 NULL
, /* base_finalize */
200 (GClassInitFunc
) g_enum_class_init
,
201 NULL
, /* class_finalize */
202 NULL
, /* class_data */
203 0, /* instance_size */
205 NULL
, /* instance_init */
206 NULL
, /* value_table */
210 g_return_val_if_fail (name
!= NULL
, 0);
211 g_return_val_if_fail (const_static_values
!= NULL
, 0);
213 enum_type_info
.class_data
= const_static_values
;
215 type
= g_type_register_static (G_TYPE_ENUM
, name
, &enum_type_info
, 0);
221 * g_flags_register_static:
222 * @name: A nul-terminated string used as the name of the new type.
223 * @const_static_values: An array of #GFlagsValue structs for the possible
224 * flags values. The array is terminated by a struct with all members being 0.
225 * GObject keeps a reference to the data, so it cannot be stack-allocated.
227 * Registers a new static flags type with the name @name.
229 * It is normally more convenient to let [glib-mkenums][glib-mkenums]
230 * generate a my_flags_get_type() function from a usual C enumeration
231 * definition than to write one yourself using g_flags_register_static().
233 * Returns: The new type identifier.
236 g_flags_register_static (const gchar
*name
,
237 const GFlagsValue
*const_static_values
)
239 GTypeInfo flags_type_info
= {
240 sizeof (GFlagsClass
), /* class_size */
241 NULL
, /* base_init */
242 NULL
, /* base_finalize */
243 (GClassInitFunc
) g_flags_class_init
,
244 NULL
, /* class_finalize */
245 NULL
, /* class_data */
246 0, /* instance_size */
248 NULL
, /* instance_init */
249 NULL
, /* value_table */
253 g_return_val_if_fail (name
!= NULL
, 0);
254 g_return_val_if_fail (const_static_values
!= NULL
, 0);
256 flags_type_info
.class_data
= const_static_values
;
258 type
= g_type_register_static (G_TYPE_FLAGS
, name
, &flags_type_info
, 0);
264 * g_enum_complete_type_info:
265 * @g_enum_type: the type identifier of the type being completed
266 * @info: (out callee-allocates): the #GTypeInfo struct to be filled in
267 * @const_values: An array of #GEnumValue structs for the possible
268 * enumeration values. The array is terminated by a struct with all
271 * This function is meant to be called from the `complete_type_info`
272 * function of a #GTypePlugin implementation, as in the following
275 * |[<!-- language="C" -->
277 * my_enum_complete_type_info (GTypePlugin *plugin,
280 * GTypeValueTable *value_table)
282 * static const GEnumValue values[] = {
283 * { MY_ENUM_FOO, "MY_ENUM_FOO", "foo" },
284 * { MY_ENUM_BAR, "MY_ENUM_BAR", "bar" },
288 * g_enum_complete_type_info (type, info, values);
293 g_enum_complete_type_info (GType g_enum_type
,
295 const GEnumValue
*const_values
)
297 g_return_if_fail (G_TYPE_IS_ENUM (g_enum_type
));
298 g_return_if_fail (info
!= NULL
);
299 g_return_if_fail (const_values
!= NULL
);
301 info
->class_size
= sizeof (GEnumClass
);
302 info
->base_init
= NULL
;
303 info
->base_finalize
= NULL
;
304 info
->class_init
= (GClassInitFunc
) g_enum_class_init
;
305 info
->class_finalize
= NULL
;
306 info
->class_data
= const_values
;
310 * g_flags_complete_type_info:
311 * @g_flags_type: the type identifier of the type being completed
312 * @info: (out callee-allocates): the #GTypeInfo struct to be filled in
313 * @const_values: An array of #GFlagsValue structs for the possible
314 * enumeration values. The array is terminated by a struct with all
317 * This function is meant to be called from the complete_type_info()
318 * function of a #GTypePlugin implementation, see the example for
319 * g_enum_complete_type_info() above.
322 g_flags_complete_type_info (GType g_flags_type
,
324 const GFlagsValue
*const_values
)
326 g_return_if_fail (G_TYPE_IS_FLAGS (g_flags_type
));
327 g_return_if_fail (info
!= NULL
);
328 g_return_if_fail (const_values
!= NULL
);
330 info
->class_size
= sizeof (GFlagsClass
);
331 info
->base_init
= NULL
;
332 info
->base_finalize
= NULL
;
333 info
->class_init
= (GClassInitFunc
) g_flags_class_init
;
334 info
->class_finalize
= NULL
;
335 info
->class_data
= const_values
;
339 g_enum_class_init (GEnumClass
*class,
342 g_return_if_fail (G_IS_ENUM_CLASS (class));
347 class->values
= class_data
;
353 class->minimum
= class->values
->value
;
354 class->maximum
= class->values
->value
;
355 for (values
= class->values
; values
->value_name
; values
++)
357 class->minimum
= MIN (class->minimum
, values
->value
);
358 class->maximum
= MAX (class->maximum
, values
->value
);
365 g_flags_class_init (GFlagsClass
*class,
368 g_return_if_fail (G_IS_FLAGS_CLASS (class));
372 class->values
= class_data
;
378 for (values
= class->values
; values
->value_name
; values
++)
380 class->mask
|= values
->value
;
387 * g_enum_get_value_by_name:
388 * @enum_class: a #GEnumClass
389 * @name: the name to look up
391 * Looks up a #GEnumValue by name.
393 * Returns: (transfer none): the #GEnumValue with name @name,
394 * or %NULL if the enumeration doesn't have a member
398 g_enum_get_value_by_name (GEnumClass
*enum_class
,
401 g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class
), NULL
);
402 g_return_val_if_fail (name
!= NULL
, NULL
);
404 if (enum_class
->n_values
)
406 GEnumValue
*enum_value
;
408 for (enum_value
= enum_class
->values
; enum_value
->value_name
; enum_value
++)
409 if (strcmp (name
, enum_value
->value_name
) == 0)
417 * g_flags_get_value_by_name:
418 * @flags_class: a #GFlagsClass
419 * @name: the name to look up
421 * Looks up a #GFlagsValue by name.
423 * Returns: (transfer none): the #GFlagsValue with name @name,
424 * or %NULL if there is no flag with that name
427 g_flags_get_value_by_name (GFlagsClass
*flags_class
,
430 g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class
), NULL
);
431 g_return_val_if_fail (name
!= NULL
, NULL
);
433 if (flags_class
->n_values
)
435 GFlagsValue
*flags_value
;
437 for (flags_value
= flags_class
->values
; flags_value
->value_name
; flags_value
++)
438 if (strcmp (name
, flags_value
->value_name
) == 0)
446 * g_enum_get_value_by_nick:
447 * @enum_class: a #GEnumClass
448 * @nick: the nickname to look up
450 * Looks up a #GEnumValue by nickname.
452 * Returns: (transfer none): the #GEnumValue with nickname @nick,
453 * or %NULL if the enumeration doesn't have a member
457 g_enum_get_value_by_nick (GEnumClass
*enum_class
,
460 g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class
), NULL
);
461 g_return_val_if_fail (nick
!= NULL
, NULL
);
463 if (enum_class
->n_values
)
465 GEnumValue
*enum_value
;
467 for (enum_value
= enum_class
->values
; enum_value
->value_name
; enum_value
++)
468 if (enum_value
->value_nick
&& strcmp (nick
, enum_value
->value_nick
) == 0)
476 * g_flags_get_value_by_nick:
477 * @flags_class: a #GFlagsClass
478 * @nick: the nickname to look up
480 * Looks up a #GFlagsValue by nickname.
482 * Returns: (transfer none): the #GFlagsValue with nickname @nick,
483 * or %NULL if there is no flag with that nickname
486 g_flags_get_value_by_nick (GFlagsClass
*flags_class
,
489 g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class
), NULL
);
490 g_return_val_if_fail (nick
!= NULL
, NULL
);
492 if (flags_class
->n_values
)
494 GFlagsValue
*flags_value
;
496 for (flags_value
= flags_class
->values
; flags_value
->value_nick
; flags_value
++)
497 if (flags_value
->value_nick
&& strcmp (nick
, flags_value
->value_nick
) == 0)
506 * @enum_class: a #GEnumClass
507 * @value: the value to look up
509 * Returns the #GEnumValue for a value.
511 * Returns: (transfer none): the #GEnumValue for @value, or %NULL
512 * if @value is not a member of the enumeration
515 g_enum_get_value (GEnumClass
*enum_class
,
518 g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class
), NULL
);
520 if (enum_class
->n_values
)
522 GEnumValue
*enum_value
;
524 for (enum_value
= enum_class
->values
; enum_value
->value_name
; enum_value
++)
525 if (enum_value
->value
== value
)
533 * g_flags_get_first_value:
534 * @flags_class: a #GFlagsClass
537 * Returns the first #GFlagsValue which is set in @value.
539 * Returns: (transfer none): the first #GFlagsValue which is set in
540 * @value, or %NULL if none is set
543 g_flags_get_first_value (GFlagsClass
*flags_class
,
546 g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class
), NULL
);
548 if (flags_class
->n_values
)
550 GFlagsValue
*flags_value
;
554 for (flags_value
= flags_class
->values
; flags_value
->value_name
; flags_value
++)
555 if (flags_value
->value
== 0)
560 for (flags_value
= flags_class
->values
; flags_value
->value_name
; flags_value
++)
561 if (flags_value
->value
!= 0 && (flags_value
->value
& value
) == flags_value
->value
)
571 * @g_enum_type: the type identifier of a #GEnumClass type
574 * Pretty-prints @value in the form of the enum’s name.
576 * This is intended to be used for debugging purposes. The format of the output
577 * may change in the future.
579 * Returns: (transfer full): a newly-allocated text string
584 g_enum_to_string (GType g_enum_type
,
588 GEnumClass
*enum_class
;
589 GEnumValue
*enum_value
;
591 g_return_val_if_fail (G_TYPE_IS_ENUM (g_enum_type
), NULL
);
593 enum_class
= g_type_class_ref (g_enum_type
);
596 if (enum_class
== NULL
)
597 return g_strdup_printf ("%d", value
);
599 enum_value
= g_enum_get_value (enum_class
, value
);
601 if (enum_value
== NULL
)
602 result
= g_strdup_printf ("%d", value
);
604 result
= g_strdup (enum_value
->value_name
);
606 g_type_class_unref (enum_class
);
611 * g_flags_get_value_string:
612 * @flags_class: a #GFlagsClass
615 * Pretty-prints @value in the form of the flag names separated by ` | ` and
616 * sorted. Any extra bits will be shown at the end as a hexadecimal number.
618 * This is intended to be used for debugging purposes. The format of the output
619 * may change in the future.
621 * Returns: (transfer full): a newly-allocated text string
626 g_flags_get_value_string (GFlagsClass
*flags_class
,
630 GFlagsValue
*flags_value
;
632 g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class
), NULL
);
634 str
= g_string_new (NULL
);
636 while ((str
->len
== 0 || value
!= 0) &&
637 (flags_value
= g_flags_get_first_value (flags_class
, value
)) != NULL
)
640 g_string_append (str
, " | ");
642 g_string_append (str
, flags_value
->value_name
);
644 value
&= ~flags_value
->value
;
647 /* Show the extra bits */
648 if (value
!= 0 || str
->len
== 0)
651 g_string_append (str
, " | ");
653 g_string_append_printf (str
, "0x%x", value
);
656 return g_string_free (str
, FALSE
);
661 * @flags_type: the type identifier of a #GFlagsClass type
664 * Pretty-prints @value in the form of the flag names separated by ` | ` and
665 * sorted. Any extra bits will be shown at the end as a hexadecimal number.
667 * This is intended to be used for debugging purposes. The format of the output
668 * may change in the future.
670 * Returns: (transfer full): a newly-allocated text string
675 g_flags_to_string (GType flags_type
,
679 GFlagsClass
*flags_class
;
681 g_return_val_if_fail (G_TYPE_IS_FLAGS (flags_type
), NULL
);
683 flags_class
= g_type_class_ref (flags_type
);
686 if (flags_class
== NULL
)
689 result
= g_flags_get_value_string (flags_class
, value
);
691 g_type_class_unref (flags_class
);
698 * @value: a valid #GValue whose type is derived from %G_TYPE_ENUM
699 * @v_enum: enum value to be set
701 * Set the contents of a %G_TYPE_ENUM #GValue to @v_enum.
704 g_value_set_enum (GValue
*value
,
707 g_return_if_fail (G_VALUE_HOLDS_ENUM (value
));
709 value
->data
[0].v_long
= v_enum
;
714 * @value: a valid #GValue whose type is derived from %G_TYPE_ENUM
716 * Get the contents of a %G_TYPE_ENUM #GValue.
718 * Returns: enum contents of @value
721 g_value_get_enum (const GValue
*value
)
723 g_return_val_if_fail (G_VALUE_HOLDS_ENUM (value
), 0);
725 return value
->data
[0].v_long
;
730 * @value: a valid #GValue whose type is derived from %G_TYPE_FLAGS
731 * @v_flags: flags value to be set
733 * Set the contents of a %G_TYPE_FLAGS #GValue to @v_flags.
736 g_value_set_flags (GValue
*value
,
739 g_return_if_fail (G_VALUE_HOLDS_FLAGS (value
));
741 value
->data
[0].v_ulong
= v_flags
;
746 * @value: a valid #GValue whose type is derived from %G_TYPE_FLAGS
748 * Get the contents of a %G_TYPE_FLAGS #GValue.
750 * Returns: flags contents of @value
753 g_value_get_flags (const GValue
*value
)
755 g_return_val_if_fail (G_VALUE_HOLDS_FLAGS (value
), 0);
757 return value
->data
[0].v_ulong
;