remove trailing whitespace from newly added gtk-doc comments and
[platform/upstream/glib.git] / gobject / genums.c
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
21  * MT safe
22  */
23
24 #include "config.h"
25
26 #include <string.h>
27
28 #include "genums.h"
29 #include "gvalue.h"
30 #include "gvaluecollector.h"
31 #include "gobjectalias.h"
32
33
34 /**
35  * SECTION:enumerations_flags
36  *
37  * @Short_description: Enumeration and flags types
38  *
39  * @See_also:#GParamSpecEnum, #GParamSpecFlags, g_param_spec_enum(),
40  * g_param_spec_flags(),
41  *
42  * <link linkend="glib-mkenums">glib-mkenums</link>
43  * @Title: Enumeration and Flag Types
44  *
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().
55  *
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
59  * definitions.
60  */
61
62
63 /* --- prototypes --- */
64 static void     g_enum_class_init               (GEnumClass     *class,
65                                                  gpointer        class_data);
66 static void     g_flags_class_init              (GFlagsClass    *class,
67                                                  gpointer        class_data);
68 static void     value_flags_enum_init           (GValue         *value);
69 static void     value_flags_enum_copy_value     (const GValue   *src_value,
70                                                  GValue         *dest_value);
71 static gchar*   value_flags_enum_collect_value  (GValue         *value,
72                                                  guint           n_collect_values,
73                                                  GTypeCValue    *collect_values,
74                                                  guint           collect_flags);
75 static gchar*   value_flags_enum_lcopy_value    (const GValue   *value,
76                                                  guint           n_collect_values,
77                                                  GTypeCValue    *collect_values,
78                                                  guint           collect_flags);
79
80 /* --- functions --- */
81 void
82 g_enum_types_init (void)
83 {
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 */
94   };
95   static GTypeInfo info = {
96     0,                          /* class_size */
97     NULL,                       /* base_init */
98     NULL,                       /* base_destroy */
99     NULL,                       /* class_init */
100     NULL,                       /* class_destroy */
101     NULL,                       /* class_data */
102     0,                          /* instance_size */
103     0,                          /* n_preallocs */
104     NULL,                       /* instance_init */
105     &flags_enum_value_table,    /* value_table */
106   };
107   static const GTypeFundamentalInfo finfo = {
108     G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_DERIVABLE,
109   };
110   GType type;
111   
112   g_return_if_fail (initialized == FALSE);
113   initialized = TRUE;
114   
115   /* G_TYPE_ENUM
116    */
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);
121   
122   /* G_TYPE_FLAGS
123    */
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);
128 }
129
130 static void
131 value_flags_enum_init (GValue *value)
132 {
133   value->data[0].v_long = 0;
134 }
135
136 static void
137 value_flags_enum_copy_value (const GValue *src_value,
138                              GValue       *dest_value)
139 {
140   dest_value->data[0].v_long = src_value->data[0].v_long;
141 }
142
143 static gchar*
144 value_flags_enum_collect_value (GValue      *value,
145                                 guint        n_collect_values,
146                                 GTypeCValue *collect_values,
147                                 guint        collect_flags)
148 {
149   value->data[0].v_long = collect_values[0].v_int;
150
151   return NULL;
152 }
153
154 static gchar*
155 value_flags_enum_lcopy_value (const GValue *value,
156                               guint         n_collect_values,
157                               GTypeCValue  *collect_values,
158                               guint         collect_flags)
159 {
160   gint *int_p = collect_values[0].v_pointer;
161   
162   if (!int_p)
163     return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
164   
165   *int_p = value->data[0].v_long;
166   
167   return NULL;
168 }
169
170 /**
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.
177  *
178  * Registers a new static enumeration type with the name @name.
179  *
180  * It is normally more convenient to let <link
181  * linkend="glib-mkenums">glib-mkenums</link> generate a
182  * my_enum_get_type() function from a usual C enumeration definition
183  * than to write one yourself using g_enum_register_static().
184  *
185  * Returns: The new type identifier.
186  */
187 GType
188 g_enum_register_static (const gchar      *name,
189                         const GEnumValue *const_static_values)
190 {
191   GTypeInfo enum_type_info = {
192     sizeof (GEnumClass), /* class_size */
193     NULL,                /* base_init */
194     NULL,                /* base_finalize */
195     (GClassInitFunc) g_enum_class_init,
196     NULL,                /* class_finalize */
197     NULL,                /* class_data */
198     0,                   /* instance_size */
199     0,                   /* n_preallocs */
200     NULL,                /* instance_init */
201     NULL,                /* value_table */
202   };
203   GType type;
204   
205   g_return_val_if_fail (name != NULL, 0);
206   g_return_val_if_fail (const_static_values != NULL, 0);
207   
208   enum_type_info.class_data = const_static_values;
209   
210   type = g_type_register_static (G_TYPE_ENUM, name, &enum_type_info, 0);
211   
212   return type;
213 }
214
215 /**
216  * g_flags_register_static:
217  * @name: A nul-terminated string used as the name of the new type.
218  * @const_static_values: An array of #GFlagsValue structs for the possible
219  *  flags values. The array is terminated by a struct with all members being 0.
220  *  GObject keeps a reference to the data, so it cannot be stack-allocated.
221  *
222  * Registers a new static flags type with the name @name.
223  *
224  * It is normally more convenient to let <link
225  * linkend="glib-mkenums">glib-mkenums</link> generate a
226  * my_flags_get_type() function from a usual C enumeration definition
227  * than to write one yourself using g_flags_register_static().
228  *
229  * Returns: The new type identifier.
230  */
231 GType
232 g_flags_register_static (const gchar       *name,
233                          const GFlagsValue *const_static_values)
234 {
235   GTypeInfo flags_type_info = {
236     sizeof (GFlagsClass), /* class_size */
237     NULL,                 /* base_init */
238     NULL,                 /* base_finalize */
239     (GClassInitFunc) g_flags_class_init,
240     NULL,                 /* class_finalize */
241     NULL,                 /* class_data */
242     0,                    /* instance_size */
243     0,                    /* n_preallocs */
244     NULL,                 /* instance_init */
245     NULL,                 /* value_table */
246   };
247   GType type;
248   
249   g_return_val_if_fail (name != NULL, 0);
250   g_return_val_if_fail (const_static_values != NULL, 0);
251   
252   flags_type_info.class_data = const_static_values;
253   
254   type = g_type_register_static (G_TYPE_FLAGS, name, &flags_type_info, 0);
255   
256   return type;
257 }
258
259 /**
260  * g_enum_complete_type_info:
261  * @g_enum_type: the type identifier of the type being completed
262  * @info: the #GTypeInfo struct to be filled in
263  * @const_values: An array of #GEnumValue structs for the possible
264  *  enumeration values. The array is terminated by a struct with all
265  *  members being 0.
266  *
267  * This function is meant to be called from the complete_type_info()
268  * function of a #GTypePlugin implementation, as in the following
269  * example:
270  *
271  * |[
272  * static void
273  * my_enum_complete_type_info (GTypePlugin     *plugin,
274  *                             GType            g_type,
275  *                             GTypeInfo       *info,
276  *                             GTypeValueTable *value_table)
277  * {
278  *   static const GEnumValue values[] = {
279  *     { MY_ENUM_FOO, "MY_ENUM_FOO", "foo" },
280  *     { MY_ENUM_BAR, "MY_ENUM_BAR", "bar" },
281  *     { 0, NULL, NULL }
282  *   };
283  *
284  *   g_enum_complete_type_info (type, info, values);
285  * }
286  * ]|
287  */
288 void
289 g_enum_complete_type_info (GType             g_enum_type,
290                            GTypeInfo        *info,
291                            const GEnumValue *const_values)
292 {
293   g_return_if_fail (G_TYPE_IS_ENUM (g_enum_type));
294   g_return_if_fail (info != NULL);
295   g_return_if_fail (const_values != NULL);
296   
297   info->class_size = sizeof (GEnumClass);
298   info->base_init = NULL;
299   info->base_finalize = NULL;
300   info->class_init = (GClassInitFunc) g_enum_class_init;
301   info->class_finalize = NULL;
302   info->class_data = const_values;
303 }
304
305 /**
306  * g_flags_complete_type_info:
307  * @g_flags_type: the type identifier of the type being completed
308  * @info: the #GTypeInfo struct to be filled in
309  * @const_values: An array of #GFlagsValue structs for the possible
310  *  enumeration values. The array is terminated by a struct with all
311  *  members being 0.
312  *
313  * This function is meant to be called from the complete_type_info()
314  * function of a #GTypePlugin implementation, see the example for
315  * g_enum_complete_type_info() above.
316  */
317 void
318 g_flags_complete_type_info (GType              g_flags_type,
319                             GTypeInfo         *info,
320                             const GFlagsValue *const_values)
321 {
322   g_return_if_fail (G_TYPE_IS_FLAGS (g_flags_type));
323   g_return_if_fail (info != NULL);
324   g_return_if_fail (const_values != NULL);
325   
326   info->class_size = sizeof (GFlagsClass);
327   info->base_init = NULL;
328   info->base_finalize = NULL;
329   info->class_init = (GClassInitFunc) g_flags_class_init;
330   info->class_finalize = NULL;
331   info->class_data = const_values;
332 }
333
334 static void
335 g_enum_class_init (GEnumClass *class,
336                    gpointer    class_data)
337 {
338   g_return_if_fail (G_IS_ENUM_CLASS (class));
339   
340   class->minimum = 0;
341   class->maximum = 0;
342   class->n_values = 0;
343   class->values = class_data;
344   
345   if (class->values)
346     {
347       GEnumValue *values;
348       
349       class->minimum = class->values->value;
350       class->maximum = class->values->value;
351       for (values = class->values; values->value_name; values++)
352         {
353           class->minimum = MIN (class->minimum, values->value);
354           class->maximum = MAX (class->maximum, values->value);
355           class->n_values++;
356         }
357     }
358 }
359
360 static void
361 g_flags_class_init (GFlagsClass *class,
362                     gpointer     class_data)
363 {
364   g_return_if_fail (G_IS_FLAGS_CLASS (class));
365   
366   class->mask = 0;
367   class->n_values = 0;
368   class->values = class_data;
369   
370   if (class->values)
371     {
372       GFlagsValue *values;
373       
374       for (values = class->values; values->value_name; values++)
375         {
376           class->mask |= values->value;
377           class->n_values++;
378         }
379     }
380 }
381
382 /**
383  * g_enum_get_value_by_name:
384  * @enum_class: a #GEnumClass
385  * @name: the name to look up
386  *
387  * Looks up a #GEnumValue by name.
388  *
389  * Returns: the #GEnumValue with name @name, or %NULL if the
390  *          enumeration doesn't have a member with that name
391  */
392 GEnumValue*
393 g_enum_get_value_by_name (GEnumClass  *enum_class,
394                           const gchar *name)
395 {
396   g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), NULL);
397   g_return_val_if_fail (name != NULL, NULL);
398   
399   if (enum_class->n_values)
400     {
401       GEnumValue *enum_value;
402       
403       for (enum_value = enum_class->values; enum_value->value_name; enum_value++)
404         if (strcmp (name, enum_value->value_name) == 0)
405           return enum_value;
406     }
407   
408   return NULL;
409 }
410
411 /**
412  * g_flags_get_value_by_name:
413  * @flags_class: a #GFlagsClass
414  * @name: the name to look up
415  *
416  * Looks up a #GFlagsValue by name.
417  *
418  * Returns: the #GFlagsValue with name @name, or %NULL if there is no
419  *          flag with that name
420  */
421 GFlagsValue*
422 g_flags_get_value_by_name (GFlagsClass *flags_class,
423                            const gchar *name)
424 {
425   g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL);
426   g_return_val_if_fail (name != NULL, NULL);
427   
428   if (flags_class->n_values)
429     {
430       GFlagsValue *flags_value;
431       
432       for (flags_value = flags_class->values; flags_value->value_name; flags_value++)
433         if (strcmp (name, flags_value->value_name) == 0)
434           return flags_value;
435     }
436   
437   return NULL;
438 }
439
440 /**
441  * g_enum_get_value_by_nick:
442  * @enum_class: a #GEnumClass
443  * @nick: the nickname to look up
444  *
445  * Looks up a #GEnumValue by nickname.
446  *
447  * Returns: the #GEnumValue with nickname @nick, or %NULL if the
448  *          enumeration doesn't have a member with that nickname
449  */
450 GEnumValue*
451 g_enum_get_value_by_nick (GEnumClass  *enum_class,
452                           const gchar *nick)
453 {
454   g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), NULL);
455   g_return_val_if_fail (nick != NULL, NULL);
456   
457   if (enum_class->n_values)
458     {
459       GEnumValue *enum_value;
460       
461       for (enum_value = enum_class->values; enum_value->value_name; enum_value++)
462         if (enum_value->value_nick && strcmp (nick, enum_value->value_nick) == 0)
463           return enum_value;
464     }
465   
466   return NULL;
467 }
468
469 /**
470  * g_flags_get_value_by_nick:
471  * @flags_class: a #GFlagsClass
472  * @nick: the nickname to look up
473  *
474  * Looks up a #GFlagsValue by nickname.
475  *
476  * Returns: the #GFlagsValue with nickname @nick, or %NULL if there is
477  *          no flag with that nickname
478  */
479 GFlagsValue*
480 g_flags_get_value_by_nick (GFlagsClass *flags_class,
481                            const gchar *nick)
482 {
483   g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL);
484   g_return_val_if_fail (nick != NULL, NULL);
485   
486   if (flags_class->n_values)
487     {
488       GFlagsValue *flags_value;
489       
490       for (flags_value = flags_class->values; flags_value->value_nick; flags_value++)
491         if (flags_value->value_nick && strcmp (nick, flags_value->value_nick) == 0)
492           return flags_value;
493     }
494   
495   return NULL;
496 }
497
498 /**
499  * g_enum_get_value:
500  * @enum_class: a #GEnumClass
501  * @value: the value to look up
502  *
503  * Returns the #GEnumValue for a value.
504  *
505  * Returns: the #GEnumValue for @value, or %NULL if @value is not a
506  *          member of the enumeration
507  */
508 GEnumValue*
509 g_enum_get_value (GEnumClass *enum_class,
510                   gint        value)
511 {
512   g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), NULL);
513   
514   if (enum_class->n_values)
515     {
516       GEnumValue *enum_value;
517       
518       for (enum_value = enum_class->values; enum_value->value_name; enum_value++)
519         if (enum_value->value == value)
520           return enum_value;
521     }
522   
523   return NULL;
524 }
525
526 /**
527  * g_flags_get_first_value:
528  * @flags_class: a #GFlagsClass
529  * @value: the value
530  *
531  * Returns the first #GFlagsValue which is set in @value.
532  *
533  * Returns: the first #GFlagsValue which is set in @value, or %NULL if
534  *          none is set
535  */
536 GFlagsValue*
537 g_flags_get_first_value (GFlagsClass *flags_class,
538                          guint        value)
539 {
540   g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL);
541   
542   if (flags_class->n_values)
543     {
544       GFlagsValue *flags_value;
545
546       if (value == 0)
547         {
548           for (flags_value = flags_class->values; flags_value->value_name; flags_value++)
549             if (flags_value->value == 0)
550               return flags_value;
551         }
552       else
553         {
554           for (flags_value = flags_class->values; flags_value->value_name; flags_value++)
555             if (flags_value->value != 0 && (flags_value->value & value) == flags_value->value)
556               return flags_value;
557         }      
558     }
559   
560   return NULL;
561 }
562
563 /**
564  * g_value_set_enum:
565  * @value: a valid #GValue whose type is derived from %G_TYPE_ENUM
566  * @v_enum: enum value to be set
567  *
568  * Set the contents of a %G_TYPE_ENUM #GValue to @v_enum.
569  */
570 void
571 g_value_set_enum (GValue *value,
572                   gint    v_enum)
573 {
574   g_return_if_fail (G_VALUE_HOLDS_ENUM (value));
575   
576   value->data[0].v_long = v_enum;
577 }
578
579 /**
580  * g_value_get_enum:
581  * @value: a valid #GValue whose type is derived from %G_TYPE_ENUM
582  *
583  * Get the contents of a %G_TYPE_ENUM #GValue.
584  *
585  * Returns: enum contents of @value
586  */
587 gint
588 g_value_get_enum (const GValue *value)
589 {
590   g_return_val_if_fail (G_VALUE_HOLDS_ENUM (value), 0);
591   
592   return value->data[0].v_long;
593 }
594
595 /**
596  * g_value_set_flags:
597  * @value: a valid #GValue whose type is derived from %G_TYPE_FLAGS
598  * @v_flags: flags value to be set
599  *
600  * Set the contents of a %G_TYPE_FLAGS #GValue to @v_flags.
601  */
602 void
603 g_value_set_flags (GValue *value,
604                    guint   v_flags)
605 {
606   g_return_if_fail (G_VALUE_HOLDS_FLAGS (value));
607   
608   value->data[0].v_ulong = v_flags;
609 }
610
611 /**
612  * g_value_get_flags:
613  * @value: a valid #GValue whose type is derived from %G_TYPE_FLAGS
614  *
615  * Get the contents of a %G_TYPE_FLAGS #GValue.
616  *
617  * Returns: flags contents of @value
618  */
619 guint
620 g_value_get_flags (const GValue *value)
621 {
622   g_return_val_if_fail (G_VALUE_HOLDS_FLAGS (value), 0);
623   
624   return value->data[0].v_ulong;
625 }
626
627 #define __G_ENUMS_C__
628 #include "gobjectaliasdef.c"