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 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 * @value: a valid #GValue whose type is derived from %G_TYPE_ENUM
572 * @v_enum: enum value to be set
574 * Set the contents of a %G_TYPE_ENUM #GValue to @v_enum.
577 g_value_set_enum (GValue
*value
,
580 g_return_if_fail (G_VALUE_HOLDS_ENUM (value
));
582 value
->data
[0].v_long
= v_enum
;
587 * @value: a valid #GValue whose type is derived from %G_TYPE_ENUM
589 * Get the contents of a %G_TYPE_ENUM #GValue.
591 * Returns: enum contents of @value
594 g_value_get_enum (const GValue
*value
)
596 g_return_val_if_fail (G_VALUE_HOLDS_ENUM (value
), 0);
598 return value
->data
[0].v_long
;
603 * @value: a valid #GValue whose type is derived from %G_TYPE_FLAGS
604 * @v_flags: flags value to be set
606 * Set the contents of a %G_TYPE_FLAGS #GValue to @v_flags.
609 g_value_set_flags (GValue
*value
,
612 g_return_if_fail (G_VALUE_HOLDS_FLAGS (value
));
614 value
->data
[0].v_ulong
= v_flags
;
619 * @value: a valid #GValue whose type is derived from %G_TYPE_FLAGS
621 * Get the contents of a %G_TYPE_FLAGS #GValue.
623 * Returns: flags contents of @value
626 g_value_get_flags (const GValue
*value
)
628 g_return_val_if_fail (G_VALUE_HOLDS_FLAGS (value
), 0);
630 return value
->data
[0].v_ulong
;