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.
30 #include "gvaluecollector.h"
31 #include "gobjectalias.h"
35 * SECTION:enumerations_flags
37 * @Short_description: Enumeration and flags types
39 * @See_also:#GParamSpecEnum, #GParamSpecFlags, g_param_spec_enum(),
40 * g_param_spec_flags(),
42 * <link linkend="glib-mkenums">glib-mkenums</link>
43 * @Title: Enumeration and Flag Types
45 * The GLib type system provides fundamental types for enumeration and
46 * flags types. (Flags types are like enumerations, but allow their
47 * values to be combined by bitwise or). A registered enumeration or
48 * flags type associates a name and a nickname with each allowed
49 * value, and the methods g_enum_get_value_by_name(),
50 * g_enum_get_value_by_nick(), g_flags_get_value_by_name() and
51 * g_flags_get_value_by_nick() can look up values by their name or
52 * nickname. When an enumeration or flags type is registered with the
53 * GLib type system, it can be used as value type for object
54 * properties, using g_param_spec_enum() or g_param_spec_flags().
56 * GObject ships with a utility called <link
57 * linkend="glib-mkenums">glib-mkenums</link> that can construct
58 * suitable type registration functions from C enumeration
63 /* --- prototypes --- */
64 static void g_enum_class_init (GEnumClass *class,
66 static void g_flags_class_init (GFlagsClass *class,
68 static void value_flags_enum_init (GValue *value);
69 static void value_flags_enum_copy_value (const GValue *src_value,
71 static gchar* value_flags_enum_collect_value (GValue *value,
72 guint n_collect_values,
73 GTypeCValue *collect_values,
75 static gchar* value_flags_enum_lcopy_value (const GValue *value,
76 guint n_collect_values,
77 GTypeCValue *collect_values,
80 /* --- functions --- */
82 g_enum_types_init (void)
84 static gboolean initialized = FALSE;
85 static const GTypeValueTable flags_enum_value_table = {
86 value_flags_enum_init, /* value_init */
87 NULL, /* value_free */
88 value_flags_enum_copy_value, /* value_copy */
89 NULL, /* value_peek_pointer */
90 "i", /* collect_format */
91 value_flags_enum_collect_value, /* collect_value */
92 "p", /* lcopy_format */
93 value_flags_enum_lcopy_value, /* lcopy_value */
95 static GTypeInfo info = {
98 NULL, /* base_destroy */
99 NULL, /* class_init */
100 NULL, /* class_destroy */
101 NULL, /* class_data */
102 0, /* instance_size */
104 NULL, /* instance_init */
105 &flags_enum_value_table, /* value_table */
107 static const GTypeFundamentalInfo finfo = {
108 G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_DERIVABLE,
112 g_return_if_fail (initialized == FALSE);
117 info.class_size = sizeof (GEnumClass);
118 type = g_type_register_fundamental (G_TYPE_ENUM, g_intern_static_string ("GEnum"), &info, &finfo,
119 G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT);
120 g_assert (type == G_TYPE_ENUM);
124 info.class_size = sizeof (GFlagsClass);
125 type = g_type_register_fundamental (G_TYPE_FLAGS, g_intern_static_string ("GFlags"), &info, &finfo,
126 G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT);
127 g_assert (type == G_TYPE_FLAGS);
131 value_flags_enum_init (GValue *value)
133 value->data[0].v_long = 0;
137 value_flags_enum_copy_value (const GValue *src_value,
140 dest_value->data[0].v_long = src_value->data[0].v_long;
144 value_flags_enum_collect_value (GValue *value,
145 guint n_collect_values,
146 GTypeCValue *collect_values,
149 value->data[0].v_long = collect_values[0].v_int;
155 value_flags_enum_lcopy_value (const GValue *value,
156 guint n_collect_values,
157 GTypeCValue *collect_values,
160 gint *int_p = collect_values[0].v_pointer;
163 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
165 *int_p = value->data[0].v_long;
171 * g_enum_register_static:
172 * @name: A nul-terminated string used as the name of the new type.
173 * @const_static_values: An array of #GEnumValue structs for the possible
174 * enumeration values. The array is terminated by a struct with all
175 * members being 0. GObject keeps a reference to the data, so it cannot
176 * be stack-allocated.
178 * Registers a new static enumeration type with the name @name.
180 * It is normally more convenient to let <link
181 * linkend="glib-mkenums">glib-mkenums</link> generate a
182 * my_enum_get_type() function from a usual C enumeration definition
183 * than to write one yourself using g_enum_register_static().
185 * Returns: The new type identifier.
188 g_enum_register_static (const gchar *name,
189 const GEnumValue *const_static_values)
191 GTypeInfo enum_type_info = {
192 sizeof (GEnumClass), /* class_size */
193 NULL, /* base_init */
194 NULL, /* base_finalize */
195 (GClassInitFunc) g_enum_class_init,
196 NULL, /* class_finalize */
197 NULL, /* class_data */
198 0, /* instance_size */
200 NULL, /* instance_init */
201 NULL, /* value_table */
205 g_return_val_if_fail (name != NULL, 0);
206 g_return_val_if_fail (const_static_values != NULL, 0);
208 enum_type_info.class_data = const_static_values;
210 type = g_type_register_static (G_TYPE_ENUM, name, &enum_type_info, 0);
216 * g_flags_register_static:
217 * @name: A nul-terminated string used as the name of the new type.
218 * @const_static_values: An array of #GFlagsValue structs for the possible
219 * flags values. The array is terminated by a struct with all members being 0.
220 * GObject keeps a reference to the data, so it cannot be stack-allocated.
222 * Registers a new static flags type with the name @name.
224 * It is normally more convenient to let <link
225 * linkend="glib-mkenums">glib-mkenums</link> generate a
226 * my_flags_get_type() function from a usual C enumeration definition
227 * than to write one yourself using g_flags_register_static().
229 * Returns: The new type identifier.
232 g_flags_register_static (const gchar *name,
233 const GFlagsValue *const_static_values)
235 GTypeInfo flags_type_info = {
236 sizeof (GFlagsClass), /* class_size */
237 NULL, /* base_init */
238 NULL, /* base_finalize */
239 (GClassInitFunc) g_flags_class_init,
240 NULL, /* class_finalize */
241 NULL, /* class_data */
242 0, /* instance_size */
244 NULL, /* instance_init */
245 NULL, /* value_table */
249 g_return_val_if_fail (name != NULL, 0);
250 g_return_val_if_fail (const_static_values != NULL, 0);
252 flags_type_info.class_data = const_static_values;
254 type = g_type_register_static (G_TYPE_FLAGS, name, &flags_type_info, 0);
260 * g_enum_complete_type_info:
261 * @g_enum_type: the type identifier of the type being completed
262 * @info: the #GTypeInfo struct to be filled in
263 * @const_values: An array of #GEnumValue structs for the possible
264 * enumeration values. The array is terminated by a struct with all
267 * This function is meant to be called from the complete_type_info()
268 * function of a #GTypePlugin implementation, as in the following
273 * my_enum_complete_type_info (GTypePlugin *plugin,
276 * GTypeValueTable *value_table)
278 * static const GEnumValue values[] = {
279 * { MY_ENUM_FOO, "MY_ENUM_FOO", "foo" },
280 * { MY_ENUM_BAR, "MY_ENUM_BAR", "bar" },
284 * g_enum_complete_type_info (type, info, values);
289 g_enum_complete_type_info (GType g_enum_type,
291 const GEnumValue *const_values)
293 g_return_if_fail (G_TYPE_IS_ENUM (g_enum_type));
294 g_return_if_fail (info != NULL);
295 g_return_if_fail (const_values != NULL);
297 info->class_size = sizeof (GEnumClass);
298 info->base_init = NULL;
299 info->base_finalize = NULL;
300 info->class_init = (GClassInitFunc) g_enum_class_init;
301 info->class_finalize = NULL;
302 info->class_data = const_values;
306 * g_flags_complete_type_info:
307 * @g_flags_type: the type identifier of the type being completed
308 * @info: the #GTypeInfo struct to be filled in
309 * @const_values: An array of #GFlagsValue structs for the possible
310 * enumeration values. The array is terminated by a struct with all
313 * This function is meant to be called from the complete_type_info()
314 * function of a #GTypePlugin implementation, see the example for
315 * g_enum_complete_type_info() above.
318 g_flags_complete_type_info (GType g_flags_type,
320 const GFlagsValue *const_values)
322 g_return_if_fail (G_TYPE_IS_FLAGS (g_flags_type));
323 g_return_if_fail (info != NULL);
324 g_return_if_fail (const_values != NULL);
326 info->class_size = sizeof (GFlagsClass);
327 info->base_init = NULL;
328 info->base_finalize = NULL;
329 info->class_init = (GClassInitFunc) g_flags_class_init;
330 info->class_finalize = NULL;
331 info->class_data = const_values;
335 g_enum_class_init (GEnumClass *class,
338 g_return_if_fail (G_IS_ENUM_CLASS (class));
343 class->values = class_data;
349 class->minimum = class->values->value;
350 class->maximum = class->values->value;
351 for (values = class->values; values->value_name; values++)
353 class->minimum = MIN (class->minimum, values->value);
354 class->maximum = MAX (class->maximum, values->value);
361 g_flags_class_init (GFlagsClass *class,
364 g_return_if_fail (G_IS_FLAGS_CLASS (class));
368 class->values = class_data;
374 for (values = class->values; values->value_name; values++)
376 class->mask |= values->value;
383 * g_enum_get_value_by_name:
384 * @enum_class: a #GEnumClass
385 * @name: the name to look up
387 * Looks up a #GEnumValue by name.
389 * Returns: the #GEnumValue with name @name, or %NULL if the
390 * enumeration doesn't have a member with that name
393 g_enum_get_value_by_name (GEnumClass *enum_class,
396 g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), NULL);
397 g_return_val_if_fail (name != NULL, NULL);
399 if (enum_class->n_values)
401 GEnumValue *enum_value;
403 for (enum_value = enum_class->values; enum_value->value_name; enum_value++)
404 if (strcmp (name, enum_value->value_name) == 0)
412 * g_flags_get_value_by_name:
413 * @flags_class: a #GFlagsClass
414 * @name: the name to look up
416 * Looks up a #GFlagsValue by name.
418 * Returns: the #GFlagsValue with name @name, or %NULL if there is no
419 * flag with that name
422 g_flags_get_value_by_name (GFlagsClass *flags_class,
425 g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL);
426 g_return_val_if_fail (name != NULL, NULL);
428 if (flags_class->n_values)
430 GFlagsValue *flags_value;
432 for (flags_value = flags_class->values; flags_value->value_name; flags_value++)
433 if (strcmp (name, flags_value->value_name) == 0)
441 * g_enum_get_value_by_nick:
442 * @enum_class: a #GEnumClass
443 * @nick: the nickname to look up
445 * Looks up a #GEnumValue by nickname.
447 * Returns: the #GEnumValue with nickname @nick, or %NULL if the
448 * enumeration doesn't have a member with that nickname
451 g_enum_get_value_by_nick (GEnumClass *enum_class,
454 g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), NULL);
455 g_return_val_if_fail (nick != NULL, NULL);
457 if (enum_class->n_values)
459 GEnumValue *enum_value;
461 for (enum_value = enum_class->values; enum_value->value_name; enum_value++)
462 if (enum_value->value_nick && strcmp (nick, enum_value->value_nick) == 0)
470 * g_flags_get_value_by_nick:
471 * @flags_class: a #GFlagsClass
472 * @nick: the nickname to look up
474 * Looks up a #GFlagsValue by nickname.
476 * Returns: the #GFlagsValue with nickname @nick, or %NULL if there is
477 * no flag with that nickname
480 g_flags_get_value_by_nick (GFlagsClass *flags_class,
483 g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL);
484 g_return_val_if_fail (nick != NULL, NULL);
486 if (flags_class->n_values)
488 GFlagsValue *flags_value;
490 for (flags_value = flags_class->values; flags_value->value_nick; flags_value++)
491 if (flags_value->value_nick && strcmp (nick, flags_value->value_nick) == 0)
500 * @enum_class: a #GEnumClass
501 * @value: the value to look up
503 * Returns the #GEnumValue for a value.
505 * Returns: the #GEnumValue for @value, or %NULL if @value is not a
506 * member of the enumeration
509 g_enum_get_value (GEnumClass *enum_class,
512 g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), NULL);
514 if (enum_class->n_values)
516 GEnumValue *enum_value;
518 for (enum_value = enum_class->values; enum_value->value_name; enum_value++)
519 if (enum_value->value == value)
527 * g_flags_get_first_value:
528 * @flags_class: a #GFlagsClass
531 * Returns the first #GFlagsValue which is set in @value.
533 * Returns: the first #GFlagsValue which is set in @value, or %NULL if
537 g_flags_get_first_value (GFlagsClass *flags_class,
540 g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL);
542 if (flags_class->n_values)
544 GFlagsValue *flags_value;
548 for (flags_value = flags_class->values; flags_value->value_name; flags_value++)
549 if (flags_value->value == 0)
554 for (flags_value = flags_class->values; flags_value->value_name; flags_value++)
555 if (flags_value->value != 0 && (flags_value->value & value) == flags_value->value)
565 * @value: a valid #GValue whose type is derived from %G_TYPE_ENUM
566 * @v_enum: enum value to be set
568 * Set the contents of a %G_TYPE_ENUM #GValue to @v_enum.
571 g_value_set_enum (GValue *value,
574 g_return_if_fail (G_VALUE_HOLDS_ENUM (value));
576 value->data[0].v_long = v_enum;
581 * @value: a valid #GValue whose type is derived from %G_TYPE_ENUM
583 * Get the contents of a %G_TYPE_ENUM #GValue.
585 * Returns: enum contents of @value
588 g_value_get_enum (const GValue *value)
590 g_return_val_if_fail (G_VALUE_HOLDS_ENUM (value), 0);
592 return value->data[0].v_long;
597 * @value: a valid #GValue whose type is derived from %G_TYPE_FLAGS
598 * @v_flags: flags value to be set
600 * Set the contents of a %G_TYPE_FLAGS #GValue to @v_flags.
603 g_value_set_flags (GValue *value,
606 g_return_if_fail (G_VALUE_HOLDS_FLAGS (value));
608 value->data[0].v_ulong = v_flags;
613 * @value: a valid #GValue whose type is derived from %G_TYPE_FLAGS
615 * Get the contents of a %G_TYPE_FLAGS #GValue.
617 * Returns: flags contents of @value
620 g_value_get_flags (const GValue *value)
622 g_return_val_if_fail (G_VALUE_HOLDS_FLAGS (value), 0);
624 return value->data[0].v_ulong;
627 #define __G_ENUMS_C__
628 #include "gobjectaliasdef.c"