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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
20 * SECTION:enumerations_flags
21 * @Short_description: Enumeration and flags types
22 * @See_also:#GParamSpecEnum, #GParamSpecFlags, g_param_spec_enum(), g_param_spec_flags(),
23 * <link linkend="glib-mkenums">glib-mkenums</link>
24 * @Title: Enumeration and Flag Types
26 * The GLib type system provides fundamental types for enumeration and flags types. (Flags types
27 * are like enumerations, but allow their values to be combined by bitwise or). A registered
28 * enumeration or flags type associates a name and a nickname with each allowed value, and
29 * the methods g_enum_get_value_by_name(), g_enum_get_value_by_nick(), g_flags_get_value_by_name()
30 * and g_flags_get_value_by_nick() can look up values by their name or nickname.
31 * When an enumeration or flags type is registered with the GLib type system, it can
32 * be used as value type for object properties, using g_param_spec_enum() or
33 * g_param_spec_flags().
35 * GObject ships with a utility called <link linkend="glib-mkenums">glib-mkenums</link> that can
36 * construct suitable type registration functions from C enumeration definitions.
48 #include "gvaluecollector.h"
50 #include "gobjectalias.h"
53 /* --- prototypes --- */
54 static void g_enum_class_init (GEnumClass *class,
56 static void g_flags_class_init (GFlagsClass *class,
58 static void value_flags_enum_init (GValue *value);
59 static void value_flags_enum_copy_value (const GValue *src_value,
61 static gchar* value_flags_enum_collect_value (GValue *value,
62 guint n_collect_values,
63 GTypeCValue *collect_values,
65 static gchar* value_flags_enum_lcopy_value (const GValue *value,
66 guint n_collect_values,
67 GTypeCValue *collect_values,
70 /* --- functions --- */
72 g_enum_types_init (void)
74 static gboolean initialized = FALSE;
75 static const GTypeValueTable flags_enum_value_table = {
76 value_flags_enum_init, /* value_init */
77 NULL, /* value_free */
78 value_flags_enum_copy_value, /* value_copy */
79 NULL, /* value_peek_pointer */
80 "i", /* collect_format */
81 value_flags_enum_collect_value, /* collect_value */
82 "p", /* lcopy_format */
83 value_flags_enum_lcopy_value, /* lcopy_value */
85 static GTypeInfo info = {
88 NULL, /* base_destroy */
89 NULL, /* class_init */
90 NULL, /* class_destroy */
91 NULL, /* class_data */
92 0, /* instance_size */
94 NULL, /* instance_init */
95 &flags_enum_value_table, /* value_table */
97 static const GTypeFundamentalInfo finfo = {
98 G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_DERIVABLE,
102 g_return_if_fail (initialized == FALSE);
107 info.class_size = sizeof (GEnumClass);
108 type = g_type_register_fundamental (G_TYPE_ENUM, g_intern_static_string ("GEnum"), &info, &finfo,
109 G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT);
110 g_assert (type == G_TYPE_ENUM);
114 info.class_size = sizeof (GFlagsClass);
115 type = g_type_register_fundamental (G_TYPE_FLAGS, g_intern_static_string ("GFlags"), &info, &finfo,
116 G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT);
117 g_assert (type == G_TYPE_FLAGS);
121 value_flags_enum_init (GValue *value)
123 value->data[0].v_long = 0;
127 value_flags_enum_copy_value (const GValue *src_value,
130 dest_value->data[0].v_long = src_value->data[0].v_long;
134 value_flags_enum_collect_value (GValue *value,
135 guint n_collect_values,
136 GTypeCValue *collect_values,
139 value->data[0].v_long = collect_values[0].v_int;
145 value_flags_enum_lcopy_value (const GValue *value,
146 guint n_collect_values,
147 GTypeCValue *collect_values,
150 gint *int_p = collect_values[0].v_pointer;
153 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
155 *int_p = value->data[0].v_long;
161 * g_enum_register_static:
162 * @name: A nul-terminated string used as the name of the new type.
163 * @const_static_values: An array of #GEnumValue structs for the possible
164 * enumeration values. The array is terminated by a struct with all
165 * members being 0. GObject keeps a reference to the data, so it cannot
166 * be stack-allocated.
168 * Registers a new static enumeration type with the name @name.
170 * It is normally more convenient to let <link linkend="glib-mkenums">glib-mkenums</link>
171 * generate a my_enum_get_type() function from a usual C enumeration definition
172 * than to write one yourself using g_enum_register_static().
174 * Returns: The new type identifier.
177 g_enum_register_static (const gchar *name,
178 const GEnumValue *const_static_values)
180 GTypeInfo enum_type_info = {
181 sizeof (GEnumClass), /* class_size */
182 NULL, /* base_init */
183 NULL, /* base_finalize */
184 (GClassInitFunc) g_enum_class_init,
185 NULL, /* class_finalize */
186 NULL, /* class_data */
187 0, /* instance_size */
189 NULL, /* instance_init */
190 NULL, /* value_table */
194 g_return_val_if_fail (name != NULL, 0);
195 g_return_val_if_fail (const_static_values != NULL, 0);
197 enum_type_info.class_data = const_static_values;
199 type = g_type_register_static (G_TYPE_ENUM, name, &enum_type_info, 0);
205 * g_flags_register_static:
206 * @name: A nul-terminated string used as the name of the new type.
207 * @const_static_values: An array of #GFlagsValue structs for the possible
208 * flags values. The array is terminated by a struct with all members being 0.
209 * GObject keeps a reference to the data, so it cannot be stack-allocated.
211 * Registers a new static flags type with the name @name.
213 * It is normally more convenient to let <link linkend="glib-mkenums">glib-mkenums</link>
214 * generate a my_flags_get_type() function from a usual C enumeration definition
215 * than to write one yourself using g_flags_register_static().
217 * Returns: The new type identifier.
220 g_flags_register_static (const gchar *name,
221 const GFlagsValue *const_static_values)
223 GTypeInfo flags_type_info = {
224 sizeof (GFlagsClass), /* class_size */
225 NULL, /* base_init */
226 NULL, /* base_finalize */
227 (GClassInitFunc) g_flags_class_init,
228 NULL, /* class_finalize */
229 NULL, /* class_data */
230 0, /* instance_size */
232 NULL, /* instance_init */
233 NULL, /* value_table */
237 g_return_val_if_fail (name != NULL, 0);
238 g_return_val_if_fail (const_static_values != NULL, 0);
240 flags_type_info.class_data = const_static_values;
242 type = g_type_register_static (G_TYPE_FLAGS, name, &flags_type_info, 0);
248 * g_enum_complete_type_info:
249 * @g_enum_type: the type identifier of the type being completed
250 * @info: the #GTypeInfo struct to be filled in
251 * @const_values: An array of #GEnumValue structs for the possible
252 * enumeration values. The array is terminated by a struct with all
255 * This function is meant to be called from the complete_type_info() function
256 * of a #GTypePlugin implementation, as in the following example:
260 * my_enum_complete_type_info (GTypePlugin *plugin,
263 * GTypeValueTable *value_table)
265 * static const GEnumValue values[] = {
266 * { MY_ENUM_FOO, "MY_ENUM_FOO", "foo" },
267 * { MY_ENUM_BAR, "MY_ENUM_BAR", "bar" },
271 * g_enum_complete_type_info (type, info, values);
276 g_enum_complete_type_info (GType g_enum_type,
278 const GEnumValue *const_values)
280 g_return_if_fail (G_TYPE_IS_ENUM (g_enum_type));
281 g_return_if_fail (info != NULL);
282 g_return_if_fail (const_values != NULL);
284 info->class_size = sizeof (GEnumClass);
285 info->base_init = NULL;
286 info->base_finalize = NULL;
287 info->class_init = (GClassInitFunc) g_enum_class_init;
288 info->class_finalize = NULL;
289 info->class_data = const_values;
293 * g_flags_complete_type_info:
294 * @g_flags_type: the type identifier of the type being completed
295 * @info: the #GTypeInfo struct to be filled in
296 * @const_values: An array of #GFlagsValue structs for the possible
297 * enumeration values. The array is terminated by a struct with all
300 * This function is meant to be called from the complete_type_info() function
301 * of a #GTypePlugin implementation, see the example for
302 * g_enum_complete_type_info() above.
305 g_flags_complete_type_info (GType g_flags_type,
307 const GFlagsValue *const_values)
309 g_return_if_fail (G_TYPE_IS_FLAGS (g_flags_type));
310 g_return_if_fail (info != NULL);
311 g_return_if_fail (const_values != NULL);
313 info->class_size = sizeof (GFlagsClass);
314 info->base_init = NULL;
315 info->base_finalize = NULL;
316 info->class_init = (GClassInitFunc) g_flags_class_init;
317 info->class_finalize = NULL;
318 info->class_data = const_values;
322 g_enum_class_init (GEnumClass *class,
325 g_return_if_fail (G_IS_ENUM_CLASS (class));
330 class->values = class_data;
336 class->minimum = class->values->value;
337 class->maximum = class->values->value;
338 for (values = class->values; values->value_name; values++)
340 class->minimum = MIN (class->minimum, values->value);
341 class->maximum = MAX (class->maximum, values->value);
348 g_flags_class_init (GFlagsClass *class,
351 g_return_if_fail (G_IS_FLAGS_CLASS (class));
355 class->values = class_data;
361 for (values = class->values; values->value_name; values++)
363 class->mask |= values->value;
370 * g_enum_get_value_by_name:
371 * @enum_class: a #GEnumClass
372 * @name: the name to look up
374 * Looks up a #GEnumValue by name.
376 * Returns: the #GEnumValue with name @name, or %NULL if the enumeration doesn'
377 * t have a member with that name
380 g_enum_get_value_by_name (GEnumClass *enum_class,
383 g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), NULL);
384 g_return_val_if_fail (name != NULL, NULL);
386 if (enum_class->n_values)
388 GEnumValue *enum_value;
390 for (enum_value = enum_class->values; enum_value->value_name; enum_value++)
391 if (strcmp (name, enum_value->value_name) == 0)
399 * g_flags_get_value_by_name:
400 * @flags_class: a #GFlagsClass
401 * @name: the name to look up
403 * Looks up a #GFlagsValue by name.
405 * Returns: the #GFlagsValue with name @name, or %NULL if there is no flag with
409 g_flags_get_value_by_name (GFlagsClass *flags_class,
412 g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL);
413 g_return_val_if_fail (name != NULL, NULL);
415 if (flags_class->n_values)
417 GFlagsValue *flags_value;
419 for (flags_value = flags_class->values; flags_value->value_name; flags_value++)
420 if (strcmp (name, flags_value->value_name) == 0)
428 * g_enum_get_value_by_nick:
429 * @enum_class: a #GEnumClass
430 * @nick: the nickname to look up
432 * Looks up a #GEnumValue by nickname.
434 * Returns: the #GEnumValue with nickname @nick, or %NULL if the enumeration doesn'
435 * t have a member with that nickname
438 g_enum_get_value_by_nick (GEnumClass *enum_class,
441 g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), NULL);
442 g_return_val_if_fail (nick != NULL, NULL);
444 if (enum_class->n_values)
446 GEnumValue *enum_value;
448 for (enum_value = enum_class->values; enum_value->value_name; enum_value++)
449 if (enum_value->value_nick && strcmp (nick, enum_value->value_nick) == 0)
457 * g_flags_get_value_by_nick:
458 * @flags_class: a #GFlagsClass
459 * @nick: the nickname to look up
461 * Looks up a #GFlagsValue by nickname.
463 * Returns: the #GFlagsValue with nickname @nick, or %NULL if there is no flag
467 g_flags_get_value_by_nick (GFlagsClass *flags_class,
470 g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL);
471 g_return_val_if_fail (nick != NULL, NULL);
473 if (flags_class->n_values)
475 GFlagsValue *flags_value;
477 for (flags_value = flags_class->values; flags_value->value_nick; flags_value++)
478 if (flags_value->value_nick && strcmp (nick, flags_value->value_nick) == 0)
487 * @enum_class: a #GEnumClass
488 * @value: the value to look up
490 * Returns the #GEnumValue for a value.
492 * Returns: the #GEnumValue for @value, or %NULL if @value is not
493 * a member of the enumeration
496 g_enum_get_value (GEnumClass *enum_class,
499 g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), NULL);
501 if (enum_class->n_values)
503 GEnumValue *enum_value;
505 for (enum_value = enum_class->values; enum_value->value_name; enum_value++)
506 if (enum_value->value == value)
514 * g_flags_get_first_value:
515 * @flags_class: a #GFlagsClass
518 * Returns the first #GFlagsValue which is set in @value.
520 * Returns: the first #GFlagsValue which is set in @value, or %NULL if none is set
523 g_flags_get_first_value (GFlagsClass *flags_class,
526 g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL);
528 if (flags_class->n_values)
530 GFlagsValue *flags_value;
534 for (flags_value = flags_class->values; flags_value->value_name; flags_value++)
535 if (flags_value->value == 0)
540 for (flags_value = flags_class->values; flags_value->value_name; flags_value++)
541 if (flags_value->value != 0 && (flags_value->value & value) == flags_value->value)
550 g_value_set_enum (GValue *value,
553 g_return_if_fail (G_VALUE_HOLDS_ENUM (value));
555 value->data[0].v_long = v_enum;
559 g_value_get_enum (const GValue *value)
561 g_return_val_if_fail (G_VALUE_HOLDS_ENUM (value), 0);
563 return value->data[0].v_long;
567 g_value_set_flags (GValue *value,
570 g_return_if_fail (G_VALUE_HOLDS_FLAGS (value));
572 value->data[0].v_ulong = v_flags;
576 g_value_get_flags (const GValue *value)
578 g_return_val_if_fail (G_VALUE_HOLDS_FLAGS (value), 0);
580 return value->data[0].v_ulong;
583 #define __G_ENUMS_C__
584 #include "gobjectaliasdef.c"