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