Remove g_type_init() calls
[platform/upstream/glib.git] / gobject / gvalue.c
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 1997-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  * FIXME: MT-safety
22  */
23
24 #include "config.h"
25
26 #include <string.h>
27
28 #include "gvalue.h"
29 #include "gvaluecollector.h"
30 #include "gbsearcharray.h"
31 #include "gtype-private.h"
32
33
34 /**
35  * SECTION:generic_values
36  * @short_description: A polymorphic type that can hold values of any
37  *     other type
38  * @see_also: The fundamental types which all support #GValue
39  *     operations and thus can be used as a type initializer for
40  *     g_value_init() are defined by a separate interface.  See the <link
41  *     linkend="gobject-Standard-Parameter-and-Value-Types">Standard
42  *     Values API</link> for details.
43  * @title: Generic values
44  *
45  * The #GValue structure is basically a variable container that consists
46  * of a type identifier and a specific value of that type.
47  * The type identifier within a #GValue structure always determines the
48  * type of the associated value.
49  * To create a undefined #GValue structure, simply create a zero-filled
50  * #GValue structure. To initialize the #GValue, use the g_value_init()
51  * function. A #GValue cannot be used until it is initialized.
52  * The basic type operations (such as freeing and copying) are determined
53  * by the #GTypeValueTable associated with the type ID stored in the #GValue.
54  * Other #GValue operations (such as converting values between types) are
55  * provided by this interface.
56  *
57  * The code in the example program below demonstrates #GValue's
58  * features.
59  *
60  * |[
61  * #include &lt;glib-object.h&gt;
62  *
63  * static void
64  * int2string (const GValue *src_value,
65  *             GValue       *dest_value)
66  * {
67  *   if (g_value_get_int (src_value) == 42)
68  *     g_value_set_static_string (dest_value, "An important number");
69  *   else
70  *     g_value_set_static_string (dest_value, "What's that?");
71  * }
72  *
73  * int
74  * main (int   argc,
75  *       char *argv[])
76  * {
77  *   /&ast; GValues must be initialized &ast;/
78  *   GValue a = G_VALUE_INIT;
79  *   GValue b = G_VALUE_INIT;
80  *   const gchar *message;
81  *
82  *   /&ast; The GValue starts empty &ast;/
83  *   g_assert (!G_VALUE_HOLDS_STRING (&amp;a));
84  *
85  *   /&ast; Put a string in it &ast;/
86  *   g_value_init (&amp;a, G_TYPE_STRING);
87  *   g_assert (G_VALUE_HOLDS_STRING (&amp;a));
88  *   g_value_set_static_string (&amp;a, "Hello, world!");
89  *   g_printf ("%s\n", g_value_get_string (&amp;a));
90  *
91  *   /&ast; Reset it to its pristine state &ast;/
92  *   g_value_unset (&amp;a);
93  *
94  *   /&ast; It can then be reused for another type &ast;/
95  *   g_value_init (&amp;a, G_TYPE_INT);
96  *   g_value_set_int (&amp;a, 42);
97  *
98  *   /&ast; Attempt to transform it into a GValue of type STRING &ast;/
99  *   g_value_init (&amp;b, G_TYPE_STRING);
100  *
101  *   /&ast; An INT is transformable to a STRING &ast;/
102  *   g_assert (g_value_type_transformable (G_TYPE_INT, G_TYPE_STRING));
103  *
104  *   g_value_transform (&amp;a, &amp;b);
105  *   g_printf ("%s\n", g_value_get_string (&amp;b));
106  *
107  *   /&ast; Attempt to transform it again using a custom transform function &ast;/
108  *   g_value_register_transform_func (G_TYPE_INT, G_TYPE_STRING, int2string);
109  *   g_value_transform (&amp;a, &amp;b);
110  *   g_printf ("%s\n", g_value_get_string (&amp;b));
111  *   return 0;
112  * }
113  * ]|
114  */
115
116
117 /* --- typedefs & structures --- */
118 typedef struct {
119   GType src_type;
120   GType dest_type;
121   GValueTransform func;
122 } TransformEntry;
123
124
125 /* --- prototypes --- */
126 static gint     transform_entries_cmp   (gconstpointer bsearch_node1,
127                                          gconstpointer bsearch_node2);
128
129
130 /* --- variables --- */
131 static GBSearchArray *transform_array = NULL;
132 static GBSearchConfig transform_bconfig = {
133   sizeof (TransformEntry),
134   transform_entries_cmp,
135   G_BSEARCH_ARRAY_ALIGN_POWER2,
136 };
137
138
139 /* --- functions --- */
140 void
141 _g_value_c_init (void)
142 {
143   transform_array = g_bsearch_array_create (&transform_bconfig);
144 }
145
146 static inline void              /* keep this function in sync with gvaluecollector.h and gboxed.c */
147 value_meminit (GValue *value,
148                GType   value_type)
149 {
150   value->g_type = value_type;
151   memset (value->data, 0, sizeof (value->data));
152 }
153
154 /**
155  * g_value_init:
156  * @value: A zero-filled (uninitialized) #GValue structure.
157  * @g_type: Type the #GValue should hold values of.
158  *
159  * Initializes @value with the default value of @type.
160  *
161  * Returns: (transfer none): the #GValue structure that has been passed in
162  */
163 GValue*
164 g_value_init (GValue *value,
165               GType   g_type)
166 {
167   /* g_return_val_if_fail (G_TYPE_IS_VALUE (g_type), NULL);     be more elaborate below */
168   g_return_val_if_fail (value != NULL, NULL);
169   /* g_return_val_if_fail (G_VALUE_TYPE (value) == 0, NULL);    be more elaborate below */
170
171   if (G_TYPE_IS_VALUE (g_type) && G_VALUE_TYPE (value) == 0)
172     {
173       GTypeValueTable *value_table = g_type_value_table_peek (g_type);
174
175       /* setup and init */
176       value_meminit (value, g_type);
177       value_table->value_init (value);
178     }
179   else if (G_VALUE_TYPE (value))
180     g_warning ("%s: cannot initialize GValue with type `%s', the value has already been initialized as `%s'",
181                G_STRLOC,
182                g_type_name (g_type),
183                g_type_name (G_VALUE_TYPE (value)));
184   else /* !G_TYPE_IS_VALUE (g_type) */
185     g_warning ("%s: cannot initialize GValue with type `%s', %s",
186                G_STRLOC,
187                g_type_name (g_type),
188                g_type_value_table_peek (g_type) ?
189                "this type is abstract with regards to GValue use, use a more specific (derived) type" :
190                "this type has no GTypeValueTable implementation");
191   return value;
192 }
193
194 /**
195  * g_value_copy:
196  * @src_value: An initialized #GValue structure.
197  * @dest_value: An initialized #GValue structure of the same type as @src_value.
198  *
199  * Copies the value of @src_value into @dest_value.
200  */
201 void
202 g_value_copy (const GValue *src_value,
203               GValue       *dest_value)
204 {
205   g_return_if_fail (G_IS_VALUE (src_value));
206   g_return_if_fail (G_IS_VALUE (dest_value));
207   g_return_if_fail (g_value_type_compatible (G_VALUE_TYPE (src_value), G_VALUE_TYPE (dest_value)));
208   
209   if (src_value != dest_value)
210     {
211       GType dest_type = G_VALUE_TYPE (dest_value);
212       GTypeValueTable *value_table = g_type_value_table_peek (dest_type);
213
214       /* make sure dest_value's value is free()d */
215       if (value_table->value_free)
216         value_table->value_free (dest_value);
217
218       /* setup and copy */
219       value_meminit (dest_value, dest_type);
220       value_table->value_copy (src_value, dest_value);
221     }
222 }
223
224 /**
225  * g_value_reset:
226  * @value: An initialized #GValue structure.
227  *
228  * Clears the current value in @value and resets it to the default value
229  * (as if the value had just been initialized).
230  *
231  * Returns: the #GValue structure that has been passed in
232  */
233 GValue*
234 g_value_reset (GValue *value)
235 {
236   GTypeValueTable *value_table;
237   GType g_type;
238   
239   g_return_val_if_fail (G_IS_VALUE (value), NULL);
240   
241   g_type = G_VALUE_TYPE (value);
242   value_table = g_type_value_table_peek (g_type);
243
244   /* make sure value's value is free()d */
245   if (value_table->value_free)
246     value_table->value_free (value);
247
248   /* setup and init */
249   value_meminit (value, g_type);
250   value_table->value_init (value);
251
252   return value;
253 }
254
255 /**
256  * g_value_unset:
257  * @value: An initialized #GValue structure.
258  *
259  * Clears the current value in @value and "unsets" the type,
260  * this releases all resources associated with this GValue.
261  * An unset value is the same as an uninitialized (zero-filled)
262  * #GValue structure.
263  */
264 void
265 g_value_unset (GValue *value)
266 {
267   GTypeValueTable *value_table;
268   
269   g_return_if_fail (G_IS_VALUE (value));
270
271   value_table = g_type_value_table_peek (G_VALUE_TYPE (value));
272
273   if (value_table->value_free)
274     value_table->value_free (value);
275   memset (value, 0, sizeof (*value));
276 }
277
278 /**
279  * g_value_fits_pointer:
280  * @value: An initialized #GValue structure.
281  *
282  * Determines if @value will fit inside the size of a pointer value.
283  * This is an internal function introduced mainly for C marshallers.
284  *
285  * Returns: %TRUE if @value will fit inside a pointer value.
286  */
287 gboolean
288 g_value_fits_pointer (const GValue *value)
289 {
290   GTypeValueTable *value_table;
291
292   g_return_val_if_fail (G_IS_VALUE (value), FALSE);
293
294   value_table = g_type_value_table_peek (G_VALUE_TYPE (value));
295
296   return value_table->value_peek_pointer != NULL;
297 }
298
299 /**
300  * g_value_peek_pointer:
301  * @value: An initialized #GValue structure.
302  *
303  * Returns: (transfer none): the value contents as pointer. This
304  * function asserts that g_value_fits_pointer() returned %TRUE for the
305  * passed in value.  This is an internal function introduced mainly
306  * for C marshallers.
307  */
308 gpointer
309 g_value_peek_pointer (const GValue *value)
310 {
311   GTypeValueTable *value_table;
312
313   g_return_val_if_fail (G_IS_VALUE (value), NULL);
314
315   value_table = g_type_value_table_peek (G_VALUE_TYPE (value));
316   if (!value_table->value_peek_pointer)
317     {
318       g_return_val_if_fail (g_value_fits_pointer (value) == TRUE, NULL);
319       return NULL;
320     }
321
322   return value_table->value_peek_pointer (value);
323 }
324
325 /**
326  * g_value_set_instance:
327  * @value: An initialized #GValue structure.
328  * @instance: (allow-none): the instance
329  *
330  * Sets @value from an instantiatable type via the
331  * value_table's collect_value() function.
332  */
333 void
334 g_value_set_instance (GValue  *value,
335                       gpointer instance)
336 {
337   GType g_type;
338   GTypeValueTable *value_table;
339   GTypeCValue cvalue;
340   gchar *error_msg;
341   
342   g_return_if_fail (G_IS_VALUE (value));
343   if (instance)
344     {
345       g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
346       g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (instance), G_VALUE_TYPE (value)));
347     }
348   
349   g_type = G_VALUE_TYPE (value);
350   value_table = g_type_value_table_peek (g_type);
351   
352   g_return_if_fail (strcmp (value_table->collect_format, "p") == 0);
353   
354   memset (&cvalue, 0, sizeof (cvalue));
355   cvalue.v_pointer = instance;
356   
357   /* make sure value's value is free()d */
358   if (value_table->value_free)
359     value_table->value_free (value);
360
361   /* setup and collect */
362   value_meminit (value, g_type);
363   error_msg = value_table->collect_value (value, 1, &cvalue, 0);
364   if (error_msg)
365     {
366       g_warning ("%s: %s", G_STRLOC, error_msg);
367       g_free (error_msg);
368       
369       /* we purposely leak the value here, it might not be
370        * in a sane state if an error condition occoured
371        */
372       value_meminit (value, g_type);
373       value_table->value_init (value);
374     }
375 }
376
377 static GValueTransform
378 transform_func_lookup (GType src_type,
379                        GType dest_type)
380 {
381   TransformEntry entry;
382
383   entry.src_type = src_type;
384   do
385     {
386       entry.dest_type = dest_type;
387       do
388         {
389           TransformEntry *e;
390           
391           e = g_bsearch_array_lookup (transform_array, &transform_bconfig, &entry);
392           if (e)
393             {
394               /* need to check that there hasn't been a change in value handling */
395               if (g_type_value_table_peek (entry.dest_type) == g_type_value_table_peek (dest_type) &&
396                   g_type_value_table_peek (entry.src_type) == g_type_value_table_peek (src_type))
397                 return e->func;
398             }
399           entry.dest_type = g_type_parent (entry.dest_type);
400         }
401       while (entry.dest_type);
402       
403       entry.src_type = g_type_parent (entry.src_type);
404     }
405   while (entry.src_type);
406
407   return NULL;
408 }
409
410 static gint
411 transform_entries_cmp (gconstpointer bsearch_node1,
412                        gconstpointer bsearch_node2)
413 {
414   const TransformEntry *e1 = bsearch_node1;
415   const TransformEntry *e2 = bsearch_node2;
416   gint cmp = G_BSEARCH_ARRAY_CMP (e1->src_type, e2->src_type);
417
418   if (cmp)
419     return cmp;
420   else
421     return G_BSEARCH_ARRAY_CMP (e1->dest_type, e2->dest_type);
422 }
423
424 /**
425  * g_value_register_transform_func: (skip)
426  * @src_type: Source type.
427  * @dest_type: Target type.
428  * @transform_func: a function which transforms values of type @src_type
429  *  into value of type @dest_type
430  *
431  * Registers a value transformation function for use in g_value_transform().
432  * A previously registered transformation function for @src_type and @dest_type
433  * will be replaced.
434  */
435 void
436 g_value_register_transform_func (GType           src_type,
437                                  GType           dest_type,
438                                  GValueTransform transform_func)
439 {
440   TransformEntry entry;
441
442   /* these checks won't pass for dynamic types.
443    * g_return_if_fail (G_TYPE_HAS_VALUE_TABLE (src_type));
444    * g_return_if_fail (G_TYPE_HAS_VALUE_TABLE (dest_type));
445    */
446   g_return_if_fail (transform_func != NULL);
447
448   entry.src_type = src_type;
449   entry.dest_type = dest_type;
450
451 #if 0 /* let transform function replacement be a valid operation */
452   if (g_bsearch_array_lookup (transform_array, &transform_bconfig, &entry))
453     g_warning ("reregistering value transformation function (%p) for `%s' to `%s'",
454                transform_func,
455                g_type_name (src_type),
456                g_type_name (dest_type));
457 #endif
458
459   entry.func = transform_func;
460   transform_array = g_bsearch_array_replace (transform_array, &transform_bconfig, &entry);
461 }
462
463 /**
464  * g_value_type_transformable:
465  * @src_type: Source type.
466  * @dest_type: Target type.
467  *
468  * Check whether g_value_transform() is able to transform values
469  * of type @src_type into values of type @dest_type.
470  *
471  * Returns: %TRUE if the transformation is possible, %FALSE otherwise.
472  */
473 gboolean
474 g_value_type_transformable (GType src_type,
475                             GType dest_type)
476 {
477   g_return_val_if_fail (G_TYPE_IS_VALUE (src_type), FALSE);
478   g_return_val_if_fail (G_TYPE_IS_VALUE (dest_type), FALSE);
479
480   return (g_value_type_compatible (src_type, dest_type) ||
481           transform_func_lookup (src_type, dest_type) != NULL);
482 }
483
484 /**
485  * g_value_type_compatible:
486  * @src_type: source type to be copied.
487  * @dest_type: destination type for copying.
488  *
489  * Returns whether a #GValue of type @src_type can be copied into
490  * a #GValue of type @dest_type.
491  *
492  * Returns: %TRUE if g_value_copy() is possible with @src_type and @dest_type.
493  */
494 gboolean
495 g_value_type_compatible (GType src_type,
496                          GType dest_type)
497 {
498   g_return_val_if_fail (G_TYPE_IS_VALUE (src_type), FALSE);
499   g_return_val_if_fail (G_TYPE_IS_VALUE (dest_type), FALSE);
500
501   return (g_type_is_a (src_type, dest_type) &&
502           g_type_value_table_peek (dest_type) == g_type_value_table_peek (src_type));
503 }
504
505 /**
506  * g_value_transform:
507  * @src_value: Source value.
508  * @dest_value: Target value.
509  *
510  * Tries to cast the contents of @src_value into a type appropriate
511  * to store in @dest_value, e.g. to transform a %G_TYPE_INT value
512  * into a %G_TYPE_FLOAT value. Performing transformations between
513  * value types might incur precision lossage. Especially
514  * transformations into strings might reveal seemingly arbitrary
515  * results and shouldn't be relied upon for production code (such
516  * as rcfile value or object property serialization).
517  *
518  * Returns: Whether a transformation rule was found and could be applied.
519  *  Upon failing transformations, @dest_value is left untouched.
520  */
521 gboolean
522 g_value_transform (const GValue *src_value,
523                    GValue       *dest_value)
524 {
525   GType dest_type;
526
527   g_return_val_if_fail (G_IS_VALUE (src_value), FALSE);
528   g_return_val_if_fail (G_IS_VALUE (dest_value), FALSE);
529
530   dest_type = G_VALUE_TYPE (dest_value);
531   if (g_value_type_compatible (G_VALUE_TYPE (src_value), dest_type))
532     {
533       g_value_copy (src_value, dest_value);
534       
535       return TRUE;
536     }
537   else
538     {
539       GValueTransform transform = transform_func_lookup (G_VALUE_TYPE (src_value), dest_type);
540
541       if (transform)
542         {
543           g_value_unset (dest_value);
544           
545           /* setup and transform */
546           value_meminit (dest_value, dest_type);
547           transform (src_value, dest_value);
548           
549           return TRUE;
550         }
551     }
552   return FALSE;
553 }