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:Enums and 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>
25 * The GLib type system provides fundamental types for enumeration and flags types. (Flags types
26 * are like enumerations, but allow their values to be combined by bitwise or). A registered
27 * enumeration or flags type associates a name and a nickname with each allowed value, and
28 * the methods g_enum_get_value_by_name(), g_enum_get_value_by_nick(), g_flags_get_value_by_name()
29 * and g_flags_get_value_by_nick() can look up values by their name or nickname.
30 * When an enumeration or flags type is registered with the GLib type system, it can
31 * be used as value type for object properties, using g_param_spec_enum() or
32 * g_param_spec_flags().
34 * GObject ships with a utility called <link linkend="glib-mkenums">glib-mkenums</link> that can
35 * construct suitable type registration functions from C enumeration definitions.
47 #include "gvaluecollector.h"
49 #include "gobjectalias.h"
52 /* --- prototypes --- */
53 static void g_enum_class_init (GEnumClass *class,
55 static void g_flags_class_init (GFlagsClass *class,
57 static void value_flags_enum_init (GValue *value);
58 static void value_flags_enum_copy_value (const GValue *src_value,
60 static gchar* value_flags_enum_collect_value (GValue *value,
61 guint n_collect_values,
62 GTypeCValue *collect_values,
64 static gchar* value_flags_enum_lcopy_value (const GValue *value,
65 guint n_collect_values,
66 GTypeCValue *collect_values,
69 /* --- functions --- */
71 g_enum_types_init (void)
73 static gboolean initialized = FALSE;
74 static const GTypeValueTable flags_enum_value_table = {
75 value_flags_enum_init, /* value_init */
76 NULL, /* value_free */
77 value_flags_enum_copy_value, /* value_copy */
78 NULL, /* value_peek_pointer */
79 "i", /* collect_format */
80 value_flags_enum_collect_value, /* collect_value */
81 "p", /* lcopy_format */
82 value_flags_enum_lcopy_value, /* lcopy_value */
84 static GTypeInfo info = {
87 NULL, /* base_destroy */
88 NULL, /* class_init */
89 NULL, /* class_destroy */
90 NULL, /* class_data */
91 0, /* instance_size */
93 NULL, /* instance_init */
94 &flags_enum_value_table, /* value_table */
96 static const GTypeFundamentalInfo finfo = {
97 G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_DERIVABLE,
101 g_return_if_fail (initialized == FALSE);
106 info.class_size = sizeof (GEnumClass);
107 type = g_type_register_fundamental (G_TYPE_ENUM, g_intern_static_string ("GEnum"), &info, &finfo,
108 G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT);
109 g_assert (type == G_TYPE_ENUM);
113 info.class_size = sizeof (GFlagsClass);
114 type = g_type_register_fundamental (G_TYPE_FLAGS, g_intern_static_string ("GFlags"), &info, &finfo,
115 G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT);
116 g_assert (type == G_TYPE_FLAGS);
120 value_flags_enum_init (GValue *value)
122 value->data[0].v_long = 0;
126 value_flags_enum_copy_value (const GValue *src_value,
129 dest_value->data[0].v_long = src_value->data[0].v_long;
133 value_flags_enum_collect_value (GValue *value,
134 guint n_collect_values,
135 GTypeCValue *collect_values,
138 value->data[0].v_long = collect_values[0].v_int;
144 value_flags_enum_lcopy_value (const GValue *value,
145 guint n_collect_values,
146 GTypeCValue *collect_values,
149 gint *int_p = collect_values[0].v_pointer;
152 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
154 *int_p = value->data[0].v_long;
160 * g_enum_register_static:
161 * @name: A nul-terminated string used as the name of the new type.
162 * @const_static_values: An array of #GEnumValue structs for the possible
163 * enumeration values. The array is terminated by a struct with all
164 * members being 0. GObject keeps a reference to the data, so it cannot
165 * be stack-allocated.
167 * Registers a new static enumeration type with the name @name.
169 * It is normally more convenient to let <link linkend="glib-mkenums">glib-mkenums</link>
170 * generate a my_enum_get_type() function from a usual C enumeration definition
171 * than to write one yourself using g_enum_register_static().
173 * Returns: The new type identifier.
176 g_enum_register_static (const gchar *name,
177 const GEnumValue *const_static_values)
179 GTypeInfo enum_type_info = {
180 sizeof (GEnumClass), /* class_size */
181 NULL, /* base_init */
182 NULL, /* base_finalize */
183 (GClassInitFunc) g_enum_class_init,
184 NULL, /* class_finalize */
185 NULL, /* class_data */
186 0, /* instance_size */
188 NULL, /* instance_init */
189 NULL, /* value_table */
193 g_return_val_if_fail (name != NULL, 0);
194 g_return_val_if_fail (const_static_values != NULL, 0);
196 enum_type_info.class_data = const_static_values;
198 type = g_type_register_static (G_TYPE_ENUM, name, &enum_type_info, 0);
204 * g_flags_register_static:
205 * @name: A nul-terminated string used as the name of the new type.
206 * @const_static_values: An array of #GFlagsValue structs for the possible
207 * flags values. The array is terminated by a struct with all members being 0.
208 * GObject keeps a reference to the data, so it cannot be stack-allocated.
210 * Registers a new static flags type with the name @name.
212 * It is normally more convenient to let <link linkend="glib-mkenums">glib-mkenums</link>
213 * generate a my_flags_get_type() function from a usual C enumeration definition
214 * than to write one yourself using g_flags_register_static().
216 * Returns: The new type identifier.
219 g_flags_register_static (const gchar *name,
220 const GFlagsValue *const_static_values)
222 GTypeInfo flags_type_info = {
223 sizeof (GFlagsClass), /* class_size */
224 NULL, /* base_init */
225 NULL, /* base_finalize */
226 (GClassInitFunc) g_flags_class_init,
227 NULL, /* class_finalize */
228 NULL, /* class_data */
229 0, /* instance_size */
231 NULL, /* instance_init */
232 NULL, /* value_table */
236 g_return_val_if_fail (name != NULL, 0);
237 g_return_val_if_fail (const_static_values != NULL, 0);
239 flags_type_info.class_data = const_static_values;
241 type = g_type_register_static (G_TYPE_FLAGS, name, &flags_type_info, 0);
247 * g_enum_complete_type_info:
248 * @g_enum_type: the type identifier of the type being completed
249 * @info: the #GTypeInfo struct to be filled in
250 * @const_values: An array of #GEnumValue structs for the possible
251 * enumeration values. The array is terminated by a struct with all
254 * This function is meant to be called from the complete_type_info() function
255 * of a #GTypePlugin implementation, as in the following example:
259 * my_enum_complete_type_info (GTypePlugin *plugin,
262 * GTypeValueTable *value_table)
264 * static const GEnumValue values[] = {
265 * { MY_ENUM_FOO, "MY_ENUM_FOO", "foo" },
266 * { MY_ENUM_BAR, "MY_ENUM_BAR", "bar" },
270 * g_enum_complete_type_info (type, info, values);
275 g_enum_complete_type_info (GType g_enum_type,
277 const GEnumValue *const_values)
279 g_return_if_fail (G_TYPE_IS_ENUM (g_enum_type));
280 g_return_if_fail (info != NULL);
281 g_return_if_fail (const_values != NULL);
283 info->class_size = sizeof (GEnumClass);
284 info->base_init = NULL;
285 info->base_finalize = NULL;
286 info->class_init = (GClassInitFunc) g_enum_class_init;
287 info->class_finalize = NULL;
288 info->class_data = const_values;
292 * g_flags_complete_type_info:
293 * @g_flags_type: the type identifier of the type being completed
294 * @info: the #GTypeInfo struct to be filled in
295 * @const_values: An array of #GFlagsValue structs for the possible
296 * enumeration values. The array is terminated by a struct with all
299 * This function is meant to be called from the complete_type_info() function
300 * of a #GTypePlugin implementation, see the example for
301 * g_enum_complete_type_info() above.
304 g_flags_complete_type_info (GType g_flags_type,
306 const GFlagsValue *const_values)
308 g_return_if_fail (G_TYPE_IS_FLAGS (g_flags_type));
309 g_return_if_fail (info != NULL);
310 g_return_if_fail (const_values != NULL);
312 info->class_size = sizeof (GFlagsClass);
313 info->base_init = NULL;
314 info->base_finalize = NULL;
315 info->class_init = (GClassInitFunc) g_flags_class_init;
316 info->class_finalize = NULL;
317 info->class_data = const_values;
321 g_enum_class_init (GEnumClass *class,
324 g_return_if_fail (G_IS_ENUM_CLASS (class));
329 class->values = class_data;
335 class->minimum = class->values->value;
336 class->maximum = class->values->value;
337 for (values = class->values; values->value_name; values++)
339 class->minimum = MIN (class->minimum, values->value);
340 class->maximum = MAX (class->maximum, values->value);
347 g_flags_class_init (GFlagsClass *class,
350 g_return_if_fail (G_IS_FLAGS_CLASS (class));
354 class->values = class_data;
360 for (values = class->values; values->value_name; values++)
362 class->mask |= values->value;
369 * g_enum_get_value_by_name:
370 * @enum_class: a #GEnumClass
371 * @name: the name to look up
373 * Looks up a #GEnumValue by name.
375 * Returns: the #GEnumValue with name @name, or %NULL if the enumeration doesn'
376 * t have a member with that name
379 g_enum_get_value_by_name (GEnumClass *enum_class,
382 g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), NULL);
383 g_return_val_if_fail (name != NULL, NULL);
385 if (enum_class->n_values)
387 GEnumValue *enum_value;
389 for (enum_value = enum_class->values; enum_value->value_name; enum_value++)
390 if (strcmp (name, enum_value->value_name) == 0)
398 * g_flags_get_value_by_name:
399 * @flags_class: a #GFlagsClass
400 * @name: the name to look up
402 * Looks up a #GFlagsValue by name.
404 * Returns: the #GFlagsValue with name @name, or %NULL if there is no flag with
408 g_flags_get_value_by_name (GFlagsClass *flags_class,
411 g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL);
412 g_return_val_if_fail (name != NULL, NULL);
414 if (flags_class->n_values)
416 GFlagsValue *flags_value;
418 for (flags_value = flags_class->values; flags_value->value_name; flags_value++)
419 if (strcmp (name, flags_value->value_name) == 0)
427 * g_enum_get_value_by_nick:
428 * @enum_class: a #GEnumClass
429 * @nick: the nickname to look up
431 * Looks up a #GEnumValue by nickname.
433 * Returns: the #GEnumValue with nickname @nick, or %NULL if the enumeration doesn'
434 * t have a member with that nickname
437 g_enum_get_value_by_nick (GEnumClass *enum_class,
440 g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), NULL);
441 g_return_val_if_fail (nick != NULL, NULL);
443 if (enum_class->n_values)
445 GEnumValue *enum_value;
447 for (enum_value = enum_class->values; enum_value->value_name; enum_value++)
448 if (enum_value->value_nick && strcmp (nick, enum_value->value_nick) == 0)
456 * g_flags_get_value_by_nick:
457 * @flags_class: a #GFlagsClass
458 * @nick: the nickname to look up
460 * Looks up a #GFlagsValue by nickname.
462 * Returns: the #GFlagsValue with nickname @nick, or %NULL if there is no flag
466 g_flags_get_value_by_nick (GFlagsClass *flags_class,
469 g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL);
470 g_return_val_if_fail (nick != NULL, NULL);
472 if (flags_class->n_values)
474 GFlagsValue *flags_value;
476 for (flags_value = flags_class->values; flags_value->value_nick; flags_value++)
477 if (flags_value->value_nick && strcmp (nick, flags_value->value_nick) == 0)
486 * @enum_class: a #GEnumClass
487 * @value: the value to look up
489 * Returns the #GEnumValue for a value.
491 * Returns: the #GEnumValue for @value, or %NULL if @value is not
492 * a member of the enumeration
495 g_enum_get_value (GEnumClass *enum_class,
498 g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), NULL);
500 if (enum_class->n_values)
502 GEnumValue *enum_value;
504 for (enum_value = enum_class->values; enum_value->value_name; enum_value++)
505 if (enum_value->value == value)
513 * g_flags_get_first_value:
514 * @flags_class: a #GFlagsClass
517 * Returns the first #GFlagsValue which is set in @value.
519 * Returns: the first #GFlagsValue which is set in @value, or %NULL if none is set
522 g_flags_get_first_value (GFlagsClass *flags_class,
525 g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL);
527 if (flags_class->n_values)
529 GFlagsValue *flags_value;
533 for (flags_value = flags_class->values; flags_value->value_name; flags_value++)
534 if (flags_value->value == 0)
539 for (flags_value = flags_class->values; flags_value->value_name; flags_value++)
540 if (flags_value->value != 0 && (flags_value->value & value) == flags_value->value)
549 g_value_set_enum (GValue *value,
552 g_return_if_fail (G_VALUE_HOLDS_ENUM (value));
554 value->data[0].v_long = v_enum;
558 g_value_get_enum (const GValue *value)
560 g_return_val_if_fail (G_VALUE_HOLDS_ENUM (value), 0);
562 return value->data[0].v_long;
566 g_value_set_flags (GValue *value,
569 g_return_if_fail (G_VALUE_HOLDS_FLAGS (value));
571 value->data[0].v_ulong = v_flags;
575 g_value_get_flags (const GValue *value)
577 g_return_val_if_fail (G_VALUE_HOLDS_FLAGS (value), 0);
579 return value->data[0].v_ulong;
582 #define __G_ENUMS_C__
583 #include "gobjectaliasdef.c"