Update German translation
[glib.git] / gobject / genums.c
bloba4f22c98a8f306b86736bf7641e4859dc5ec4ed2
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, 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_value_set_enum:
571 * @value: a valid #GValue whose type is derived from %G_TYPE_ENUM
572 * @v_enum: enum value to be set
574 * Set the contents of a %G_TYPE_ENUM #GValue to @v_enum.
576 void
577 g_value_set_enum (GValue *value,
578 gint v_enum)
580 g_return_if_fail (G_VALUE_HOLDS_ENUM (value));
582 value->data[0].v_long = v_enum;
586 * g_value_get_enum:
587 * @value: a valid #GValue whose type is derived from %G_TYPE_ENUM
589 * Get the contents of a %G_TYPE_ENUM #GValue.
591 * Returns: enum contents of @value
593 gint
594 g_value_get_enum (const GValue *value)
596 g_return_val_if_fail (G_VALUE_HOLDS_ENUM (value), 0);
598 return value->data[0].v_long;
602 * g_value_set_flags:
603 * @value: a valid #GValue whose type is derived from %G_TYPE_FLAGS
604 * @v_flags: flags value to be set
606 * Set the contents of a %G_TYPE_FLAGS #GValue to @v_flags.
608 void
609 g_value_set_flags (GValue *value,
610 guint v_flags)
612 g_return_if_fail (G_VALUE_HOLDS_FLAGS (value));
614 value->data[0].v_ulong = v_flags;
618 * g_value_get_flags:
619 * @value: a valid #GValue whose type is derived from %G_TYPE_FLAGS
621 * Get the contents of a %G_TYPE_FLAGS #GValue.
623 * Returns: flags contents of @value
625 guint
626 g_value_get_flags (const GValue *value)
628 g_return_val_if_fail (G_VALUE_HOLDS_FLAGS (value), 0);
630 return value->data[0].v_ulong;