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"
34 * SECTION:enumerations_flags
35 * @short_description: Enumeration and flags types
36 * @title: Enumeration and Flag Types
37 * @see_also:#GParamSpecEnum, #GParamSpecFlags, g_param_spec_enum(),
38 * g_param_spec_flags()
40 * The GLib type system provides fundamental types for enumeration and
41 * flags types. (Flags types are like enumerations, but allow their
42 * values to be combined by bitwise or). A registered enumeration or
43 * flags type associates a name and a nickname with each allowed
44 * value, and the methods g_enum_get_value_by_name(),
45 * g_enum_get_value_by_nick(), g_flags_get_value_by_name() and
46 * g_flags_get_value_by_nick() can look up values by their name or
47 * nickname. When an enumeration or flags type is registered with the
48 * GLib type system, it can be used as value type for object
49 * properties, using g_param_spec_enum() or g_param_spec_flags().
51 * GObject ships with a utility called <link
52 * linkend="glib-mkenums">glib-mkenums</link> that can construct
53 * suitable type registration functions from C enumeration
58 /* --- prototypes --- */
59 static void g_enum_class_init (GEnumClass *class,
61 static void g_flags_class_init (GFlagsClass *class,
63 static void value_flags_enum_init (GValue *value);
64 static void value_flags_enum_copy_value (const GValue *src_value,
66 static gchar* value_flags_enum_collect_value (GValue *value,
67 guint n_collect_values,
68 GTypeCValue *collect_values,
70 static gchar* value_flags_enum_lcopy_value (const GValue *value,
71 guint n_collect_values,
72 GTypeCValue *collect_values,
75 /* --- functions --- */
77 g_enum_types_init (void)
79 static gboolean initialized = FALSE;
80 static const GTypeValueTable flags_enum_value_table = {
81 value_flags_enum_init, /* value_init */
82 NULL, /* value_free */
83 value_flags_enum_copy_value, /* value_copy */
84 NULL, /* value_peek_pointer */
85 "i", /* collect_format */
86 value_flags_enum_collect_value, /* collect_value */
87 "p", /* lcopy_format */
88 value_flags_enum_lcopy_value, /* lcopy_value */
90 static GTypeInfo info = {
93 NULL, /* base_destroy */
94 NULL, /* class_init */
95 NULL, /* class_destroy */
96 NULL, /* class_data */
97 0, /* instance_size */
99 NULL, /* instance_init */
100 &flags_enum_value_table, /* value_table */
102 static const GTypeFundamentalInfo finfo = {
103 G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_DERIVABLE,
107 g_return_if_fail (initialized == FALSE);
112 info.class_size = sizeof (GEnumClass);
113 type = g_type_register_fundamental (G_TYPE_ENUM, g_intern_static_string ("GEnum"), &info, &finfo,
114 G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT);
115 g_assert (type == G_TYPE_ENUM);
119 info.class_size = sizeof (GFlagsClass);
120 type = g_type_register_fundamental (G_TYPE_FLAGS, g_intern_static_string ("GFlags"), &info, &finfo,
121 G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT);
122 g_assert (type == G_TYPE_FLAGS);
126 value_flags_enum_init (GValue *value)
128 value->data[0].v_long = 0;
132 value_flags_enum_copy_value (const GValue *src_value,
135 dest_value->data[0].v_long = src_value->data[0].v_long;
139 value_flags_enum_collect_value (GValue *value,
140 guint n_collect_values,
141 GTypeCValue *collect_values,
144 value->data[0].v_long = collect_values[0].v_int;
150 value_flags_enum_lcopy_value (const GValue *value,
151 guint n_collect_values,
152 GTypeCValue *collect_values,
155 gint *int_p = collect_values[0].v_pointer;
158 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
160 *int_p = value->data[0].v_long;
166 * g_enum_register_static:
167 * @name: A nul-terminated string used as the name of the new type.
168 * @const_static_values: An array of #GEnumValue structs for the possible
169 * enumeration values. The array is terminated by a struct with all
170 * members being 0. GObject keeps a reference to the data, so it cannot
171 * be stack-allocated.
173 * Registers a new static enumeration type with the name @name.
175 * It is normally more convenient to let <link
176 * linkend="glib-mkenums">glib-mkenums</link> generate a
177 * my_enum_get_type() function from a usual C enumeration definition
178 * than to write one yourself using g_enum_register_static().
180 * Returns: The new type identifier.
183 g_enum_register_static (const gchar *name,
184 const GEnumValue *const_static_values)
186 GTypeInfo enum_type_info = {
187 sizeof (GEnumClass), /* class_size */
188 NULL, /* base_init */
189 NULL, /* base_finalize */
190 (GClassInitFunc) g_enum_class_init,
191 NULL, /* class_finalize */
192 NULL, /* class_data */
193 0, /* instance_size */
195 NULL, /* instance_init */
196 NULL, /* value_table */
200 g_return_val_if_fail (name != NULL, 0);
201 g_return_val_if_fail (const_static_values != NULL, 0);
203 enum_type_info.class_data = const_static_values;
205 type = g_type_register_static (G_TYPE_ENUM, name, &enum_type_info, 0);
211 * g_flags_register_static:
212 * @name: A nul-terminated string used as the name of the new type.
213 * @const_static_values: An array of #GFlagsValue structs for the possible
214 * flags values. The array is terminated by a struct with all members being 0.
215 * GObject keeps a reference to the data, so it cannot be stack-allocated.
217 * Registers a new static flags type with the name @name.
219 * It is normally more convenient to let <link
220 * linkend="glib-mkenums">glib-mkenums</link> generate a
221 * my_flags_get_type() function from a usual C enumeration definition
222 * than to write one yourself using g_flags_register_static().
224 * Returns: The new type identifier.
227 g_flags_register_static (const gchar *name,
228 const GFlagsValue *const_static_values)
230 GTypeInfo flags_type_info = {
231 sizeof (GFlagsClass), /* class_size */
232 NULL, /* base_init */
233 NULL, /* base_finalize */
234 (GClassInitFunc) g_flags_class_init,
235 NULL, /* class_finalize */
236 NULL, /* class_data */
237 0, /* instance_size */
239 NULL, /* instance_init */
240 NULL, /* value_table */
244 g_return_val_if_fail (name != NULL, 0);
245 g_return_val_if_fail (const_static_values != NULL, 0);
247 flags_type_info.class_data = const_static_values;
249 type = g_type_register_static (G_TYPE_FLAGS, name, &flags_type_info, 0);
255 * g_enum_complete_type_info:
256 * @g_enum_type: the type identifier of the type being completed
257 * @info: the #GTypeInfo struct to be filled in
258 * @const_values: An array of #GEnumValue structs for the possible
259 * enumeration values. The array is terminated by a struct with all
262 * This function is meant to be called from the complete_type_info()
263 * function of a #GTypePlugin implementation, as in the following
268 * my_enum_complete_type_info (GTypePlugin *plugin,
271 * GTypeValueTable *value_table)
273 * static const GEnumValue values[] = {
274 * { MY_ENUM_FOO, "MY_ENUM_FOO", "foo" },
275 * { MY_ENUM_BAR, "MY_ENUM_BAR", "bar" },
279 * g_enum_complete_type_info (type, info, values);
284 g_enum_complete_type_info (GType g_enum_type,
286 const GEnumValue *const_values)
288 g_return_if_fail (G_TYPE_IS_ENUM (g_enum_type));
289 g_return_if_fail (info != NULL);
290 g_return_if_fail (const_values != NULL);
292 info->class_size = sizeof (GEnumClass);
293 info->base_init = NULL;
294 info->base_finalize = NULL;
295 info->class_init = (GClassInitFunc) g_enum_class_init;
296 info->class_finalize = NULL;
297 info->class_data = const_values;
301 * g_flags_complete_type_info:
302 * @g_flags_type: the type identifier of the type being completed
303 * @info: the #GTypeInfo struct to be filled in
304 * @const_values: An array of #GFlagsValue structs for the possible
305 * enumeration values. The array is terminated by a struct with all
308 * This function is meant to be called from the complete_type_info()
309 * function of a #GTypePlugin implementation, see the example for
310 * g_enum_complete_type_info() above.
313 g_flags_complete_type_info (GType g_flags_type,
315 const GFlagsValue *const_values)
317 g_return_if_fail (G_TYPE_IS_FLAGS (g_flags_type));
318 g_return_if_fail (info != NULL);
319 g_return_if_fail (const_values != NULL);
321 info->class_size = sizeof (GFlagsClass);
322 info->base_init = NULL;
323 info->base_finalize = NULL;
324 info->class_init = (GClassInitFunc) g_flags_class_init;
325 info->class_finalize = NULL;
326 info->class_data = const_values;
330 g_enum_class_init (GEnumClass *class,
333 g_return_if_fail (G_IS_ENUM_CLASS (class));
338 class->values = class_data;
344 class->minimum = class->values->value;
345 class->maximum = class->values->value;
346 for (values = class->values; values->value_name; values++)
348 class->minimum = MIN (class->minimum, values->value);
349 class->maximum = MAX (class->maximum, values->value);
356 g_flags_class_init (GFlagsClass *class,
359 g_return_if_fail (G_IS_FLAGS_CLASS (class));
363 class->values = class_data;
369 for (values = class->values; values->value_name; values++)
371 class->mask |= values->value;
378 * g_enum_get_value_by_name:
379 * @enum_class: a #GEnumClass
380 * @name: the name to look up
382 * Looks up a #GEnumValue by name.
384 * Returns: the #GEnumValue with name @name, or %NULL if the
385 * enumeration doesn't have a member with that name
388 g_enum_get_value_by_name (GEnumClass *enum_class,
391 g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), NULL);
392 g_return_val_if_fail (name != NULL, NULL);
394 if (enum_class->n_values)
396 GEnumValue *enum_value;
398 for (enum_value = enum_class->values; enum_value->value_name; enum_value++)
399 if (strcmp (name, enum_value->value_name) == 0)
407 * g_flags_get_value_by_name:
408 * @flags_class: a #GFlagsClass
409 * @name: the name to look up
411 * Looks up a #GFlagsValue by name.
413 * Returns: the #GFlagsValue with name @name, or %NULL if there is no
414 * flag with that name
417 g_flags_get_value_by_name (GFlagsClass *flags_class,
420 g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL);
421 g_return_val_if_fail (name != NULL, NULL);
423 if (flags_class->n_values)
425 GFlagsValue *flags_value;
427 for (flags_value = flags_class->values; flags_value->value_name; flags_value++)
428 if (strcmp (name, flags_value->value_name) == 0)
436 * g_enum_get_value_by_nick:
437 * @enum_class: a #GEnumClass
438 * @nick: the nickname to look up
440 * Looks up a #GEnumValue by nickname.
442 * Returns: the #GEnumValue with nickname @nick, or %NULL if the
443 * enumeration doesn't have a member with that nickname
446 g_enum_get_value_by_nick (GEnumClass *enum_class,
449 g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), NULL);
450 g_return_val_if_fail (nick != NULL, NULL);
452 if (enum_class->n_values)
454 GEnumValue *enum_value;
456 for (enum_value = enum_class->values; enum_value->value_name; enum_value++)
457 if (enum_value->value_nick && strcmp (nick, enum_value->value_nick) == 0)
465 * g_flags_get_value_by_nick:
466 * @flags_class: a #GFlagsClass
467 * @nick: the nickname to look up
469 * Looks up a #GFlagsValue by nickname.
471 * Returns: the #GFlagsValue with nickname @nick, or %NULL if there is
472 * no flag with that nickname
475 g_flags_get_value_by_nick (GFlagsClass *flags_class,
478 g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL);
479 g_return_val_if_fail (nick != NULL, NULL);
481 if (flags_class->n_values)
483 GFlagsValue *flags_value;
485 for (flags_value = flags_class->values; flags_value->value_nick; flags_value++)
486 if (flags_value->value_nick && strcmp (nick, flags_value->value_nick) == 0)
495 * @enum_class: a #GEnumClass
496 * @value: the value to look up
498 * Returns the #GEnumValue for a value.
500 * Returns: the #GEnumValue for @value, or %NULL if @value is not a
501 * member of the enumeration
504 g_enum_get_value (GEnumClass *enum_class,
507 g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), NULL);
509 if (enum_class->n_values)
511 GEnumValue *enum_value;
513 for (enum_value = enum_class->values; enum_value->value_name; enum_value++)
514 if (enum_value->value == value)
522 * g_flags_get_first_value:
523 * @flags_class: a #GFlagsClass
526 * Returns the first #GFlagsValue which is set in @value.
528 * Returns: the first #GFlagsValue which is set in @value, or %NULL if
532 g_flags_get_first_value (GFlagsClass *flags_class,
535 g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL);
537 if (flags_class->n_values)
539 GFlagsValue *flags_value;
543 for (flags_value = flags_class->values; flags_value->value_name; flags_value++)
544 if (flags_value->value == 0)
549 for (flags_value = flags_class->values; flags_value->value_name; flags_value++)
550 if (flags_value->value != 0 && (flags_value->value & value) == flags_value->value)
560 * @value: a valid #GValue whose type is derived from %G_TYPE_ENUM
561 * @v_enum: enum value to be set
563 * Set the contents of a %G_TYPE_ENUM #GValue to @v_enum.
566 g_value_set_enum (GValue *value,
569 g_return_if_fail (G_VALUE_HOLDS_ENUM (value));
571 value->data[0].v_long = v_enum;
576 * @value: a valid #GValue whose type is derived from %G_TYPE_ENUM
578 * Get the contents of a %G_TYPE_ENUM #GValue.
580 * Returns: enum contents of @value
583 g_value_get_enum (const GValue *value)
585 g_return_val_if_fail (G_VALUE_HOLDS_ENUM (value), 0);
587 return value->data[0].v_long;
592 * @value: a valid #GValue whose type is derived from %G_TYPE_FLAGS
593 * @v_flags: flags value to be set
595 * Set the contents of a %G_TYPE_FLAGS #GValue to @v_flags.
598 g_value_set_flags (GValue *value,
601 g_return_if_fail (G_VALUE_HOLDS_FLAGS (value));
603 value->data[0].v_ulong = v_flags;
608 * @value: a valid #GValue whose type is derived from %G_TYPE_FLAGS
610 * Get the contents of a %G_TYPE_FLAGS #GValue.
612 * Returns: flags contents of @value
615 g_value_get_flags (const GValue *value)
617 g_return_val_if_fail (G_VALUE_HOLDS_FLAGS (value), 0);
619 return value->data[0].v_ulong;