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 linkend="glib-mkenums">glib-mkenums</link>
181 * generate a my_enum_get_type() function from a usual C enumeration definition
182 * than to write one yourself using g_enum_register_static().
184 * Returns: The new type identifier.
187 g_enum_register_static (const gchar *name,
188 const GEnumValue *const_static_values)
190 GTypeInfo enum_type_info = {
191 sizeof (GEnumClass), /* class_size */
192 NULL, /* base_init */
193 NULL, /* base_finalize */
194 (GClassInitFunc) g_enum_class_init,
195 NULL, /* class_finalize */
196 NULL, /* class_data */
197 0, /* instance_size */
199 NULL, /* instance_init */
200 NULL, /* value_table */
204 g_return_val_if_fail (name != NULL, 0);
205 g_return_val_if_fail (const_static_values != NULL, 0);
207 enum_type_info.class_data = const_static_values;
209 type = g_type_register_static (G_TYPE_ENUM, name, &enum_type_info, 0);
215 * g_flags_register_static:
216 * @name: A nul-terminated string used as the name of the new type.
217 * @const_static_values: An array of #GFlagsValue structs for the possible
218 * flags values. The array is terminated by a struct with all members being 0.
219 * GObject keeps a reference to the data, so it cannot be stack-allocated.
221 * Registers a new static flags type with the name @name.
223 * It is normally more convenient to let <link linkend="glib-mkenums">glib-mkenums</link>
224 * generate a my_flags_get_type() function from a usual C enumeration definition
225 * than to write one yourself using g_flags_register_static().
227 * Returns: The new type identifier.
230 g_flags_register_static (const gchar *name,
231 const GFlagsValue *const_static_values)
233 GTypeInfo flags_type_info = {
234 sizeof (GFlagsClass), /* class_size */
235 NULL, /* base_init */
236 NULL, /* base_finalize */
237 (GClassInitFunc) g_flags_class_init,
238 NULL, /* class_finalize */
239 NULL, /* class_data */
240 0, /* instance_size */
242 NULL, /* instance_init */
243 NULL, /* value_table */
247 g_return_val_if_fail (name != NULL, 0);
248 g_return_val_if_fail (const_static_values != NULL, 0);
250 flags_type_info.class_data = const_static_values;
252 type = g_type_register_static (G_TYPE_FLAGS, name, &flags_type_info, 0);
258 * g_enum_complete_type_info:
259 * @g_enum_type: the type identifier of the type being completed
260 * @info: the #GTypeInfo struct to be filled in
261 * @const_values: An array of #GEnumValue structs for the possible
262 * enumeration values. The array is terminated by a struct with all
265 * This function is meant to be called from the complete_type_info() function
266 * of a #GTypePlugin implementation, as in the following example:
270 * my_enum_complete_type_info (GTypePlugin *plugin,
273 * GTypeValueTable *value_table)
275 * static const GEnumValue values[] = {
276 * { MY_ENUM_FOO, "MY_ENUM_FOO", "foo" },
277 * { MY_ENUM_BAR, "MY_ENUM_BAR", "bar" },
281 * g_enum_complete_type_info (type, info, values);
286 g_enum_complete_type_info (GType g_enum_type,
288 const GEnumValue *const_values)
290 g_return_if_fail (G_TYPE_IS_ENUM (g_enum_type));
291 g_return_if_fail (info != NULL);
292 g_return_if_fail (const_values != NULL);
294 info->class_size = sizeof (GEnumClass);
295 info->base_init = NULL;
296 info->base_finalize = NULL;
297 info->class_init = (GClassInitFunc) g_enum_class_init;
298 info->class_finalize = NULL;
299 info->class_data = const_values;
303 * g_flags_complete_type_info:
304 * @g_flags_type: the type identifier of the type being completed
305 * @info: the #GTypeInfo struct to be filled in
306 * @const_values: An array of #GFlagsValue structs for the possible
307 * enumeration values. The array is terminated by a struct with all
310 * This function is meant to be called from the complete_type_info() function
311 * of a #GTypePlugin implementation, see the example for
312 * g_enum_complete_type_info() above.
315 g_flags_complete_type_info (GType g_flags_type,
317 const GFlagsValue *const_values)
319 g_return_if_fail (G_TYPE_IS_FLAGS (g_flags_type));
320 g_return_if_fail (info != NULL);
321 g_return_if_fail (const_values != NULL);
323 info->class_size = sizeof (GFlagsClass);
324 info->base_init = NULL;
325 info->base_finalize = NULL;
326 info->class_init = (GClassInitFunc) g_flags_class_init;
327 info->class_finalize = NULL;
328 info->class_data = const_values;
332 g_enum_class_init (GEnumClass *class,
335 g_return_if_fail (G_IS_ENUM_CLASS (class));
340 class->values = class_data;
346 class->minimum = class->values->value;
347 class->maximum = class->values->value;
348 for (values = class->values; values->value_name; values++)
350 class->minimum = MIN (class->minimum, values->value);
351 class->maximum = MAX (class->maximum, values->value);
358 g_flags_class_init (GFlagsClass *class,
361 g_return_if_fail (G_IS_FLAGS_CLASS (class));
365 class->values = class_data;
371 for (values = class->values; values->value_name; values++)
373 class->mask |= values->value;
380 * g_enum_get_value_by_name:
381 * @enum_class: a #GEnumClass
382 * @name: the name to look up
384 * Looks up a #GEnumValue by name.
386 * Returns: the #GEnumValue with name @name, or %NULL if the enumeration doesn'
387 * t have a member with that name
390 g_enum_get_value_by_name (GEnumClass *enum_class,
393 g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), NULL);
394 g_return_val_if_fail (name != NULL, NULL);
396 if (enum_class->n_values)
398 GEnumValue *enum_value;
400 for (enum_value = enum_class->values; enum_value->value_name; enum_value++)
401 if (strcmp (name, enum_value->value_name) == 0)
409 * g_flags_get_value_by_name:
410 * @flags_class: a #GFlagsClass
411 * @name: the name to look up
413 * Looks up a #GFlagsValue by name.
415 * Returns: the #GFlagsValue with name @name, or %NULL if there is no flag with
419 g_flags_get_value_by_name (GFlagsClass *flags_class,
422 g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL);
423 g_return_val_if_fail (name != NULL, NULL);
425 if (flags_class->n_values)
427 GFlagsValue *flags_value;
429 for (flags_value = flags_class->values; flags_value->value_name; flags_value++)
430 if (strcmp (name, flags_value->value_name) == 0)
438 * g_enum_get_value_by_nick:
439 * @enum_class: a #GEnumClass
440 * @nick: the nickname to look up
442 * Looks up a #GEnumValue by nickname.
444 * Returns: the #GEnumValue with nickname @nick, or %NULL if the enumeration doesn'
445 * t have a member with that nickname
448 g_enum_get_value_by_nick (GEnumClass *enum_class,
451 g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), NULL);
452 g_return_val_if_fail (nick != NULL, NULL);
454 if (enum_class->n_values)
456 GEnumValue *enum_value;
458 for (enum_value = enum_class->values; enum_value->value_name; enum_value++)
459 if (enum_value->value_nick && strcmp (nick, enum_value->value_nick) == 0)
467 * g_flags_get_value_by_nick:
468 * @flags_class: a #GFlagsClass
469 * @nick: the nickname to look up
471 * Looks up a #GFlagsValue by nickname.
473 * Returns: the #GFlagsValue with nickname @nick, or %NULL if there is no flag
477 g_flags_get_value_by_nick (GFlagsClass *flags_class,
480 g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL);
481 g_return_val_if_fail (nick != NULL, NULL);
483 if (flags_class->n_values)
485 GFlagsValue *flags_value;
487 for (flags_value = flags_class->values; flags_value->value_nick; flags_value++)
488 if (flags_value->value_nick && strcmp (nick, flags_value->value_nick) == 0)
497 * @enum_class: a #GEnumClass
498 * @value: the value to look up
500 * Returns the #GEnumValue for a value.
502 * Returns: the #GEnumValue for @value, or %NULL if @value is not
503 * a member of the enumeration
506 g_enum_get_value (GEnumClass *enum_class,
509 g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), NULL);
511 if (enum_class->n_values)
513 GEnumValue *enum_value;
515 for (enum_value = enum_class->values; enum_value->value_name; enum_value++)
516 if (enum_value->value == value)
524 * g_flags_get_first_value:
525 * @flags_class: a #GFlagsClass
528 * Returns the first #GFlagsValue which is set in @value.
530 * Returns: the first #GFlagsValue which is set in @value, or %NULL if none is set
533 g_flags_get_first_value (GFlagsClass *flags_class,
536 g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL);
538 if (flags_class->n_values)
540 GFlagsValue *flags_value;
544 for (flags_value = flags_class->values; flags_value->value_name; flags_value++)
545 if (flags_value->value == 0)
550 for (flags_value = flags_class->values; flags_value->value_name; flags_value++)
551 if (flags_value->value != 0 && (flags_value->value & value) == flags_value->value)
561 * @value: a valid #GValue whose type is derived from %G_TYPE_ENUM
562 * @v_enum: enum value to be set
564 * Set the contents of a %G_TYPE_ENUM #GValue to @v_enum.
567 g_value_set_enum (GValue *value,
570 g_return_if_fail (G_VALUE_HOLDS_ENUM (value));
572 value->data[0].v_long = v_enum;
577 * @value: a valid #GValue whose type is derived from %G_TYPE_ENUM
579 * Get the contents of a %G_TYPE_ENUM #GValue.
581 * Returns: enum contents of @value
584 g_value_get_enum (const GValue *value)
586 g_return_val_if_fail (G_VALUE_HOLDS_ENUM (value), 0);
588 return value->data[0].v_long;
593 * @value: a valid #GValue whose type is derived from %G_TYPE_FLAGS
594 * @v_flags: flags value to be set
596 * Set the contents of a %G_TYPE_FLAGS #GValue to @v_flags.
599 g_value_set_flags (GValue *value,
602 g_return_if_fail (G_VALUE_HOLDS_FLAGS (value));
604 value->data[0].v_ulong = v_flags;
609 * @value: a valid #GValue whose type is derived from %G_TYPE_FLAGS
611 * Get the contents of a %G_TYPE_FLAGS #GValue.
613 * Returns: flags contents of @value
616 g_value_get_flags (const GValue *value)
618 g_return_val_if_fail (G_VALUE_HOLDS_FLAGS (value), 0);
620 return value->data[0].v_ulong;
623 #define __G_ENUMS_C__
624 #include "gobjectaliasdef.c"