Merge branch 'wip/Jehan/fopen-modes' into 'master'
[glib.git] / gobject / genums.c
blob8212dd99d5563f3880a15b1b660845c063c411c6
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.1 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, see <http://www.gnu.org/licenses/>.
19 * MT safe
22 #include "config.h"
24 #include <string.h>
26 #include "genums.h"
27 #include "gtype-private.h"
28 #include "gvalue.h"
29 #include "gvaluecollector.h"
32 /**
33 * SECTION:enumerations_flags
34 * @short_description: Enumeration and flags types
35 * @title: Enumeration and Flag Types
36 * @see_also:#GParamSpecEnum, #GParamSpecFlags, g_param_spec_enum(),
37 * g_param_spec_flags()
39 * The GLib type system provides fundamental types for enumeration and
40 * flags types. (Flags types are like enumerations, but allow their
41 * values to be combined by bitwise or). A registered enumeration or
42 * flags type associates a name and a nickname with each allowed
43 * value, and the methods g_enum_get_value_by_name(),
44 * g_enum_get_value_by_nick(), g_flags_get_value_by_name() and
45 * g_flags_get_value_by_nick() can look up values by their name or
46 * nickname. When an enumeration or flags type is registered with the
47 * GLib type system, it can be used as value type for object
48 * properties, using g_param_spec_enum() or g_param_spec_flags().
50 * GObject ships with a utility called [glib-mkenums][glib-mkenums],
51 * that can construct suitable type registration functions from C enumeration
52 * definitions.
54 * Example of how to get a string representation of an enum value:
55 * |[<!-- language="C" -->
56 * GEnumClass *enum_class;
57 * GEnumValue *enum_value;
59 * enum_class = g_type_class_ref (MAMAN_TYPE_MY_ENUM);
60 * enum_value = g_enum_get_value (enum_class, MAMAN_MY_ENUM_FOO);
62 * g_print ("Name: %s\n", enum_value->value_name);
64 * g_type_class_unref (enum_class);
65 * ]|
69 /* --- prototypes --- */
70 static void g_enum_class_init (GEnumClass *class,
71 gpointer class_data);
72 static void g_flags_class_init (GFlagsClass *class,
73 gpointer class_data);
74 static void value_flags_enum_init (GValue *value);
75 static void value_flags_enum_copy_value (const GValue *src_value,
76 GValue *dest_value);
77 static gchar* value_flags_enum_collect_value (GValue *value,
78 guint n_collect_values,
79 GTypeCValue *collect_values,
80 guint collect_flags);
81 static gchar* value_flags_enum_lcopy_value (const GValue *value,
82 guint n_collect_values,
83 GTypeCValue *collect_values,
84 guint collect_flags);
86 /* --- functions --- */
87 void
88 _g_enum_types_init (void)
90 static gboolean initialized = FALSE;
91 static const GTypeValueTable flags_enum_value_table = {
92 value_flags_enum_init, /* value_init */
93 NULL, /* value_free */
94 value_flags_enum_copy_value, /* value_copy */
95 NULL, /* value_peek_pointer */
96 "i", /* collect_format */
97 value_flags_enum_collect_value, /* collect_value */
98 "p", /* lcopy_format */
99 value_flags_enum_lcopy_value, /* lcopy_value */
101 GTypeInfo info = {
102 0, /* class_size */
103 NULL, /* base_init */
104 NULL, /* base_destroy */
105 NULL, /* class_init */
106 NULL, /* class_destroy */
107 NULL, /* class_data */
108 0, /* instance_size */
109 0, /* n_preallocs */
110 NULL, /* instance_init */
111 &flags_enum_value_table, /* value_table */
113 static const GTypeFundamentalInfo finfo = {
114 G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_DERIVABLE,
116 GType type;
118 g_return_if_fail (initialized == FALSE);
119 initialized = TRUE;
121 /* G_TYPE_ENUM
123 info.class_size = sizeof (GEnumClass);
124 type = g_type_register_fundamental (G_TYPE_ENUM, g_intern_static_string ("GEnum"), &info, &finfo,
125 G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT);
126 g_assert (type == G_TYPE_ENUM);
128 /* G_TYPE_FLAGS
130 info.class_size = sizeof (GFlagsClass);
131 type = g_type_register_fundamental (G_TYPE_FLAGS, g_intern_static_string ("GFlags"), &info, &finfo,
132 G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT);
133 g_assert (type == G_TYPE_FLAGS);
136 static void
137 value_flags_enum_init (GValue *value)
139 value->data[0].v_long = 0;
142 static void
143 value_flags_enum_copy_value (const GValue *src_value,
144 GValue *dest_value)
146 dest_value->data[0].v_long = src_value->data[0].v_long;
149 static gchar*
150 value_flags_enum_collect_value (GValue *value,
151 guint n_collect_values,
152 GTypeCValue *collect_values,
153 guint collect_flags)
155 value->data[0].v_long = collect_values[0].v_int;
157 return NULL;
160 static gchar*
161 value_flags_enum_lcopy_value (const GValue *value,
162 guint n_collect_values,
163 GTypeCValue *collect_values,
164 guint collect_flags)
166 gint *int_p = collect_values[0].v_pointer;
168 if (!int_p)
169 return g_strdup_printf ("value location for '%s' passed as NULL", G_VALUE_TYPE_NAME (value));
171 *int_p = value->data[0].v_long;
173 return NULL;
177 * g_enum_register_static:
178 * @name: A nul-terminated string used as the name of the new type.
179 * @const_static_values: An array of #GEnumValue structs for the possible
180 * enumeration values. The array is terminated by a struct with all
181 * members being 0. GObject keeps a reference to the data, so it cannot
182 * be stack-allocated.
184 * Registers a new static enumeration type with the name @name.
186 * It is normally more convenient to let [glib-mkenums][glib-mkenums],
187 * generate a my_enum_get_type() function from a usual C enumeration
188 * definition than to write one yourself using g_enum_register_static().
190 * Returns: The new type identifier.
192 GType
193 g_enum_register_static (const gchar *name,
194 const GEnumValue *const_static_values)
196 GTypeInfo enum_type_info = {
197 sizeof (GEnumClass), /* class_size */
198 NULL, /* base_init */
199 NULL, /* base_finalize */
200 (GClassInitFunc) g_enum_class_init,
201 NULL, /* class_finalize */
202 NULL, /* class_data */
203 0, /* instance_size */
204 0, /* n_preallocs */
205 NULL, /* instance_init */
206 NULL, /* value_table */
208 GType type;
210 g_return_val_if_fail (name != NULL, 0);
211 g_return_val_if_fail (const_static_values != NULL, 0);
213 enum_type_info.class_data = const_static_values;
215 type = g_type_register_static (G_TYPE_ENUM, name, &enum_type_info, 0);
217 return type;
221 * g_flags_register_static:
222 * @name: A nul-terminated string used as the name of the new type.
223 * @const_static_values: An array of #GFlagsValue structs for the possible
224 * flags values. The array is terminated by a struct with all members being 0.
225 * GObject keeps a reference to the data, so it cannot be stack-allocated.
227 * Registers a new static flags type with the name @name.
229 * It is normally more convenient to let [glib-mkenums][glib-mkenums]
230 * generate a my_flags_get_type() function from a usual C enumeration
231 * definition than to write one yourself using g_flags_register_static().
233 * Returns: The new type identifier.
235 GType
236 g_flags_register_static (const gchar *name,
237 const GFlagsValue *const_static_values)
239 GTypeInfo flags_type_info = {
240 sizeof (GFlagsClass), /* class_size */
241 NULL, /* base_init */
242 NULL, /* base_finalize */
243 (GClassInitFunc) g_flags_class_init,
244 NULL, /* class_finalize */
245 NULL, /* class_data */
246 0, /* instance_size */
247 0, /* n_preallocs */
248 NULL, /* instance_init */
249 NULL, /* value_table */
251 GType type;
253 g_return_val_if_fail (name != NULL, 0);
254 g_return_val_if_fail (const_static_values != NULL, 0);
256 flags_type_info.class_data = const_static_values;
258 type = g_type_register_static (G_TYPE_FLAGS, name, &flags_type_info, 0);
260 return type;
264 * g_enum_complete_type_info:
265 * @g_enum_type: the type identifier of the type being completed
266 * @info: (out callee-allocates): the #GTypeInfo struct to be filled in
267 * @const_values: An array of #GEnumValue structs for the possible
268 * enumeration values. The array is terminated by a struct with all
269 * members being 0.
271 * This function is meant to be called from the `complete_type_info`
272 * function of a #GTypePlugin implementation, as in the following
273 * example:
275 * |[<!-- language="C" -->
276 * static void
277 * my_enum_complete_type_info (GTypePlugin *plugin,
278 * GType g_type,
279 * GTypeInfo *info,
280 * GTypeValueTable *value_table)
282 * static const GEnumValue values[] = {
283 * { MY_ENUM_FOO, "MY_ENUM_FOO", "foo" },
284 * { MY_ENUM_BAR, "MY_ENUM_BAR", "bar" },
285 * { 0, NULL, NULL }
286 * };
288 * g_enum_complete_type_info (type, info, values);
290 * ]|
292 void
293 g_enum_complete_type_info (GType g_enum_type,
294 GTypeInfo *info,
295 const GEnumValue *const_values)
297 g_return_if_fail (G_TYPE_IS_ENUM (g_enum_type));
298 g_return_if_fail (info != NULL);
299 g_return_if_fail (const_values != NULL);
301 info->class_size = sizeof (GEnumClass);
302 info->base_init = NULL;
303 info->base_finalize = NULL;
304 info->class_init = (GClassInitFunc) g_enum_class_init;
305 info->class_finalize = NULL;
306 info->class_data = const_values;
310 * g_flags_complete_type_info:
311 * @g_flags_type: the type identifier of the type being completed
312 * @info: (out callee-allocates): the #GTypeInfo struct to be filled in
313 * @const_values: An array of #GFlagsValue structs for the possible
314 * enumeration values. The array is terminated by a struct with all
315 * members being 0.
317 * This function is meant to be called from the complete_type_info()
318 * function of a #GTypePlugin implementation, see the example for
319 * g_enum_complete_type_info() above.
321 void
322 g_flags_complete_type_info (GType g_flags_type,
323 GTypeInfo *info,
324 const GFlagsValue *const_values)
326 g_return_if_fail (G_TYPE_IS_FLAGS (g_flags_type));
327 g_return_if_fail (info != NULL);
328 g_return_if_fail (const_values != NULL);
330 info->class_size = sizeof (GFlagsClass);
331 info->base_init = NULL;
332 info->base_finalize = NULL;
333 info->class_init = (GClassInitFunc) g_flags_class_init;
334 info->class_finalize = NULL;
335 info->class_data = const_values;
338 static void
339 g_enum_class_init (GEnumClass *class,
340 gpointer class_data)
342 g_return_if_fail (G_IS_ENUM_CLASS (class));
344 class->minimum = 0;
345 class->maximum = 0;
346 class->n_values = 0;
347 class->values = class_data;
349 if (class->values)
351 GEnumValue *values;
353 class->minimum = class->values->value;
354 class->maximum = class->values->value;
355 for (values = class->values; values->value_name; values++)
357 class->minimum = MIN (class->minimum, values->value);
358 class->maximum = MAX (class->maximum, values->value);
359 class->n_values++;
364 static void
365 g_flags_class_init (GFlagsClass *class,
366 gpointer class_data)
368 g_return_if_fail (G_IS_FLAGS_CLASS (class));
370 class->mask = 0;
371 class->n_values = 0;
372 class->values = class_data;
374 if (class->values)
376 GFlagsValue *values;
378 for (values = class->values; values->value_name; values++)
380 class->mask |= values->value;
381 class->n_values++;
387 * g_enum_get_value_by_name:
388 * @enum_class: a #GEnumClass
389 * @name: the name to look up
391 * Looks up a #GEnumValue by name.
393 * Returns: (transfer none): the #GEnumValue with name @name,
394 * or %NULL if the enumeration doesn't have a member
395 * with that name
397 GEnumValue*
398 g_enum_get_value_by_name (GEnumClass *enum_class,
399 const gchar *name)
401 g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), NULL);
402 g_return_val_if_fail (name != NULL, NULL);
404 if (enum_class->n_values)
406 GEnumValue *enum_value;
408 for (enum_value = enum_class->values; enum_value->value_name; enum_value++)
409 if (strcmp (name, enum_value->value_name) == 0)
410 return enum_value;
413 return NULL;
417 * g_flags_get_value_by_name:
418 * @flags_class: a #GFlagsClass
419 * @name: the name to look up
421 * Looks up a #GFlagsValue by name.
423 * Returns: (transfer none): the #GFlagsValue with name @name,
424 * or %NULL if there is no flag with that name
426 GFlagsValue*
427 g_flags_get_value_by_name (GFlagsClass *flags_class,
428 const gchar *name)
430 g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL);
431 g_return_val_if_fail (name != NULL, NULL);
433 if (flags_class->n_values)
435 GFlagsValue *flags_value;
437 for (flags_value = flags_class->values; flags_value->value_name; flags_value++)
438 if (strcmp (name, flags_value->value_name) == 0)
439 return flags_value;
442 return NULL;
446 * g_enum_get_value_by_nick:
447 * @enum_class: a #GEnumClass
448 * @nick: the nickname to look up
450 * Looks up a #GEnumValue by nickname.
452 * Returns: (transfer none): the #GEnumValue with nickname @nick,
453 * or %NULL if the enumeration doesn't have a member
454 * with that nickname
456 GEnumValue*
457 g_enum_get_value_by_nick (GEnumClass *enum_class,
458 const gchar *nick)
460 g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), NULL);
461 g_return_val_if_fail (nick != NULL, NULL);
463 if (enum_class->n_values)
465 GEnumValue *enum_value;
467 for (enum_value = enum_class->values; enum_value->value_name; enum_value++)
468 if (enum_value->value_nick && strcmp (nick, enum_value->value_nick) == 0)
469 return enum_value;
472 return NULL;
476 * g_flags_get_value_by_nick:
477 * @flags_class: a #GFlagsClass
478 * @nick: the nickname to look up
480 * Looks up a #GFlagsValue by nickname.
482 * Returns: (transfer none): the #GFlagsValue with nickname @nick,
483 * or %NULL if there is no flag with that nickname
485 GFlagsValue*
486 g_flags_get_value_by_nick (GFlagsClass *flags_class,
487 const gchar *nick)
489 g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL);
490 g_return_val_if_fail (nick != NULL, NULL);
492 if (flags_class->n_values)
494 GFlagsValue *flags_value;
496 for (flags_value = flags_class->values; flags_value->value_nick; flags_value++)
497 if (flags_value->value_nick && strcmp (nick, flags_value->value_nick) == 0)
498 return flags_value;
501 return NULL;
505 * g_enum_get_value:
506 * @enum_class: a #GEnumClass
507 * @value: the value to look up
509 * Returns the #GEnumValue for a value.
511 * Returns: (transfer none): the #GEnumValue for @value, or %NULL
512 * if @value is not a member of the enumeration
514 GEnumValue*
515 g_enum_get_value (GEnumClass *enum_class,
516 gint value)
518 g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), NULL);
520 if (enum_class->n_values)
522 GEnumValue *enum_value;
524 for (enum_value = enum_class->values; enum_value->value_name; enum_value++)
525 if (enum_value->value == value)
526 return enum_value;
529 return NULL;
533 * g_flags_get_first_value:
534 * @flags_class: a #GFlagsClass
535 * @value: the value
537 * Returns the first #GFlagsValue which is set in @value.
539 * Returns: (transfer none): the first #GFlagsValue which is set in
540 * @value, or %NULL if none is set
542 GFlagsValue*
543 g_flags_get_first_value (GFlagsClass *flags_class,
544 guint value)
546 g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL);
548 if (flags_class->n_values)
550 GFlagsValue *flags_value;
552 if (value == 0)
554 for (flags_value = flags_class->values; flags_value->value_name; flags_value++)
555 if (flags_value->value == 0)
556 return flags_value;
558 else
560 for (flags_value = flags_class->values; flags_value->value_name; flags_value++)
561 if (flags_value->value != 0 && (flags_value->value & value) == flags_value->value)
562 return flags_value;
566 return NULL;
570 * g_enum_to_string:
571 * @g_enum_type: the type identifier of a #GEnumClass type
572 * @value: the value
574 * Pretty-prints @value in the form of the enum’s name.
576 * This is intended to be used for debugging purposes. The format of the output
577 * may change in the future.
579 * Returns: (transfer full): a newly-allocated text string
581 * Since: 2.54
583 gchar *
584 g_enum_to_string (GType g_enum_type,
585 gint value)
587 gchar *result;
588 GEnumClass *enum_class;
589 GEnumValue *enum_value;
591 g_return_val_if_fail (G_TYPE_IS_ENUM (g_enum_type), NULL);
593 enum_class = g_type_class_ref (g_enum_type);
595 /* Already warned */
596 if (enum_class == NULL)
597 return g_strdup_printf ("%d", value);
599 enum_value = g_enum_get_value (enum_class, value);
601 if (enum_value == NULL)
602 result = g_strdup_printf ("%d", value);
603 else
604 result = g_strdup (enum_value->value_name);
606 g_type_class_unref (enum_class);
607 return result;
611 * g_flags_get_value_string:
612 * @flags_class: a #GFlagsClass
613 * @value: the value
615 * Pretty-prints @value in the form of the flag names separated by ` | ` and
616 * sorted. Any extra bits will be shown at the end as a hexadecimal number.
618 * This is intended to be used for debugging purposes. The format of the output
619 * may change in the future.
621 * Returns: (transfer full): a newly-allocated text string
623 * Since: 2.54
625 static gchar *
626 g_flags_get_value_string (GFlagsClass *flags_class,
627 guint value)
629 GString *str;
630 GFlagsValue *flags_value;
632 g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL);
634 str = g_string_new (NULL);
636 while ((str->len == 0 || value != 0) &&
637 (flags_value = g_flags_get_first_value (flags_class, value)) != NULL)
639 if (str->len > 0)
640 g_string_append (str, " | ");
642 g_string_append (str, flags_value->value_name);
644 value &= ~flags_value->value;
647 /* Show the extra bits */
648 if (value != 0 || str->len == 0)
650 if (str->len > 0)
651 g_string_append (str, " | ");
653 g_string_append_printf (str, "0x%x", value);
656 return g_string_free (str, FALSE);
660 * g_flags_to_string:
661 * @flags_type: the type identifier of a #GFlagsClass type
662 * @value: the value
664 * Pretty-prints @value in the form of the flag names separated by ` | ` and
665 * sorted. Any extra bits will be shown at the end as a hexadecimal number.
667 * This is intended to be used for debugging purposes. The format of the output
668 * may change in the future.
670 * Returns: (transfer full): a newly-allocated text string
672 * Since: 2.54
674 gchar *
675 g_flags_to_string (GType flags_type,
676 guint value)
678 gchar *result;
679 GFlagsClass *flags_class;
681 g_return_val_if_fail (G_TYPE_IS_FLAGS (flags_type), NULL);
683 flags_class = g_type_class_ref (flags_type);
685 /* Already warned */
686 if (flags_class == NULL)
687 return NULL;
689 result = g_flags_get_value_string (flags_class, value);
691 g_type_class_unref (flags_class);
692 return result;
697 * g_value_set_enum:
698 * @value: a valid #GValue whose type is derived from %G_TYPE_ENUM
699 * @v_enum: enum value to be set
701 * Set the contents of a %G_TYPE_ENUM #GValue to @v_enum.
703 void
704 g_value_set_enum (GValue *value,
705 gint v_enum)
707 g_return_if_fail (G_VALUE_HOLDS_ENUM (value));
709 value->data[0].v_long = v_enum;
713 * g_value_get_enum:
714 * @value: a valid #GValue whose type is derived from %G_TYPE_ENUM
716 * Get the contents of a %G_TYPE_ENUM #GValue.
718 * Returns: enum contents of @value
720 gint
721 g_value_get_enum (const GValue *value)
723 g_return_val_if_fail (G_VALUE_HOLDS_ENUM (value), 0);
725 return value->data[0].v_long;
729 * g_value_set_flags:
730 * @value: a valid #GValue whose type is derived from %G_TYPE_FLAGS
731 * @v_flags: flags value to be set
733 * Set the contents of a %G_TYPE_FLAGS #GValue to @v_flags.
735 void
736 g_value_set_flags (GValue *value,
737 guint v_flags)
739 g_return_if_fail (G_VALUE_HOLDS_FLAGS (value));
741 value->data[0].v_ulong = v_flags;
745 * g_value_get_flags:
746 * @value: a valid #GValue whose type is derived from %G_TYPE_FLAGS
748 * Get the contents of a %G_TYPE_FLAGS #GValue.
750 * Returns: flags contents of @value
752 guint
753 g_value_get_flags (const GValue *value)
755 g_return_val_if_fail (G_VALUE_HOLDS_FLAGS (value), 0);
757 return value->data[0].v_ulong;