Initial commit
[platform/upstream/glib2.0.git] / gobject / gboxed.c
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 2000-2001 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 #include "config.h"
21
22 #include <string.h>
23
24 #include "gboxed.h"
25 #include "gtype-private.h"
26 #include "gvalue.h"
27 #include "gvaluearray.h"
28 #include "gclosure.h"
29 #include "gvaluecollector.h"
30 #include "gobjectalias.h"
31
32
33 /**
34  * SECTION:gboxed
35  * @short_description: A mechanism to wrap opaque C structures registered
36  *     by the type system
37  * @see_also: #GParamSpecBoxed, g_param_spec_boxed()
38  * @title: Boxed Types
39  *
40  * GBoxed is a generic wrapper mechanism for arbitrary C structures. The only
41  * thing the type system needs to know about the structures is how to copy and
42  * free them, beyond that they are treated as opaque chunks of memory.
43  *
44  * Boxed types are useful for simple value-holder structures like rectangles or
45  * points. They can also be used for wrapping structures defined in non-GObject
46  * based libraries.
47  */
48
49 static inline void              /* keep this function in sync with gvalue.c */
50 value_meminit (GValue *value,
51                GType   value_type)
52 {
53   value->g_type = value_type;
54   memset (value->data, 0, sizeof (value->data));
55 }
56
57 static gpointer
58 value_copy (gpointer boxed)
59 {
60   const GValue *src_value = boxed;
61   GValue *dest_value = g_new0 (GValue, 1);
62
63   if (G_VALUE_TYPE (src_value))
64     {
65       g_value_init (dest_value, G_VALUE_TYPE (src_value));
66       g_value_copy (src_value, dest_value);
67     }
68   return dest_value;
69 }
70
71 static void
72 value_free (gpointer boxed)
73 {
74   GValue *value = boxed;
75
76   if (G_VALUE_TYPE (value))
77     g_value_unset (value);
78   g_free (value);
79 }
80
81 void
82 g_boxed_type_init (void)
83 {
84   static const GTypeInfo info = {
85     0,                          /* class_size */
86     NULL,                       /* base_init */
87     NULL,                       /* base_destroy */
88     NULL,                       /* class_init */
89     NULL,                       /* class_destroy */
90     NULL,                       /* class_data */
91     0,                          /* instance_size */
92     0,                          /* n_preallocs */
93     NULL,                       /* instance_init */
94     NULL,                       /* value_table */
95   };
96   const GTypeFundamentalInfo finfo = { G_TYPE_FLAG_DERIVABLE, };
97   GType type;
98
99   /* G_TYPE_BOXED
100    */
101   type = g_type_register_fundamental (G_TYPE_BOXED, g_intern_static_string ("GBoxed"), &info, &finfo,
102                                       G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT);
103   g_assert (type == G_TYPE_BOXED);
104 }
105
106 GType
107 g_closure_get_type (void)
108 {
109   static GType type_id = 0;
110
111   if (!type_id)
112     type_id = g_boxed_type_register_static (g_intern_static_string ("GClosure"),
113                                             (GBoxedCopyFunc) g_closure_ref,
114                                             (GBoxedFreeFunc) g_closure_unref);
115   return type_id;
116 }
117
118 GType
119 g_value_get_type (void)
120 {
121   static GType type_id = 0;
122
123   if (!type_id)
124     type_id = g_boxed_type_register_static (g_intern_static_string ("GValue"),
125                                             value_copy,
126                                             value_free);
127   return type_id;
128 }
129
130 GType
131 g_value_array_get_type (void)
132 {
133   static GType type_id = 0;
134
135   if (!type_id)
136     type_id = g_boxed_type_register_static (g_intern_static_string ("GValueArray"),
137                                             (GBoxedCopyFunc) g_value_array_copy,
138                                             (GBoxedFreeFunc) g_value_array_free);
139   return type_id;
140 }
141
142 static gpointer
143 gdate_copy (gpointer boxed)
144 {
145   const GDate *date = (const GDate*) boxed;
146
147   return g_date_new_julian (g_date_get_julian (date));
148 }
149
150 GType
151 g_date_get_type (void)
152 {
153   static GType type_id = 0;
154
155   if (!type_id)
156     type_id = g_boxed_type_register_static (g_intern_static_string ("GDate"),
157                                             (GBoxedCopyFunc) gdate_copy,
158                                             (GBoxedFreeFunc) g_date_free);
159   return type_id;
160 }
161
162 GType
163 g_strv_get_type (void)
164 {
165   static GType type_id = 0;
166
167   if (!type_id)
168     type_id = g_boxed_type_register_static (g_intern_static_string ("GStrv"),
169                                             (GBoxedCopyFunc) g_strdupv,
170                                             (GBoxedFreeFunc) g_strfreev);
171   return type_id;
172 }
173
174 static gpointer
175 gstring_copy (gpointer boxed)
176 {
177   const GString *src_gstring = boxed;
178
179   return g_string_new_len (src_gstring->str, src_gstring->len);
180 }
181
182 static void
183 gstring_free (gpointer boxed)
184 {
185   GString *gstring = boxed;
186
187   g_string_free (gstring, TRUE);
188 }
189
190 GType
191 g_gstring_get_type (void)
192 {
193   static GType type_id = 0;
194
195   if (!type_id)
196     type_id = g_boxed_type_register_static (g_intern_static_string ("GString"),
197                                             /* the naming is a bit odd, but GString is obviously not G_TYPE_STRING */
198                                             gstring_copy,
199                                             gstring_free);
200   return type_id;
201 }
202
203 static gpointer
204 hash_table_copy (gpointer boxed)
205 {
206   GHashTable *hash_table = boxed;
207   return g_hash_table_ref (hash_table);
208 }
209
210 static void
211 hash_table_free (gpointer boxed)
212 {
213   GHashTable *hash_table = boxed;
214   g_hash_table_unref (hash_table);
215 }
216
217 GType
218 g_hash_table_get_type (void)
219 {
220   static GType type_id = 0;
221   if (!type_id)
222     type_id = g_boxed_type_register_static (g_intern_static_string ("GHashTable"),
223                                             hash_table_copy, hash_table_free);
224   return type_id;
225 }
226
227 GType
228 g_regex_get_type (void)
229 {
230   static GType type_id = 0;
231
232 #ifdef ENABLE_REGEX
233   if (!type_id)
234     type_id = g_boxed_type_register_static (g_intern_static_string ("GRegex"),
235                                             (GBoxedCopyFunc) g_regex_ref,
236                                             (GBoxedFreeFunc) g_regex_unref);
237 #endif
238
239   return type_id;
240 }
241
242 GType
243 g_array_get_type (void)
244 {
245   static GType type_id = 0;
246   if (!type_id)
247     type_id = g_boxed_type_register_static (g_intern_static_string ("GArray"),
248                                             (GBoxedCopyFunc) g_array_ref,
249                                             (GBoxedFreeFunc) g_array_unref);
250   return type_id;
251 }
252
253 GType
254 g_ptr_array_get_type (void)
255 {
256   static GType type_id = 0;
257   if (!type_id)
258     type_id = g_boxed_type_register_static (g_intern_static_string ("GPtrArray"),
259                                             (GBoxedCopyFunc) g_ptr_array_ref,
260                                             (GBoxedFreeFunc) g_ptr_array_unref);
261   return type_id;
262 }
263
264 GType
265 g_byte_array_get_type (void)
266 {
267   static GType type_id = 0;
268   if (!type_id)
269     type_id = g_boxed_type_register_static (g_intern_static_string ("GByteArray"),
270                                             (GBoxedCopyFunc) g_byte_array_ref,
271                                             (GBoxedFreeFunc) g_byte_array_unref);
272
273   return type_id;
274 }
275
276 GType
277 g_variant_type_get_gtype (void)
278 {
279   static GType type_id = 0;
280
281   if (!type_id)
282     type_id = g_boxed_type_register_static (g_intern_static_string ("GVariantType"),
283                                             (GBoxedCopyFunc) g_variant_type_copy,
284                                             (GBoxedFreeFunc) g_variant_type_free);
285
286   return type_id;
287 }
288
289 GType
290 g_variant_get_gtype (void)
291 {
292   static GType type_id = 0;
293
294   if (!type_id)
295     type_id = g_boxed_type_register_static (g_intern_static_string ("GVariant"),
296                                             (GBoxedCopyFunc) g_variant_ref,
297                                             (GBoxedFreeFunc) g_variant_unref);
298
299   return type_id;
300 }
301
302 static void
303 boxed_proxy_value_init (GValue *value)
304 {
305   value->data[0].v_pointer = NULL;
306 }
307
308 static void
309 boxed_proxy_value_free (GValue *value)
310 {
311   if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
312     _g_type_boxed_free (G_VALUE_TYPE (value), value->data[0].v_pointer);
313 }
314
315 static void
316 boxed_proxy_value_copy (const GValue *src_value,
317                         GValue       *dest_value)
318 {
319   if (src_value->data[0].v_pointer)
320     dest_value->data[0].v_pointer = _g_type_boxed_copy (G_VALUE_TYPE (src_value), src_value->data[0].v_pointer);
321   else
322     dest_value->data[0].v_pointer = src_value->data[0].v_pointer;
323 }
324
325 static gpointer
326 boxed_proxy_value_peek_pointer (const GValue *value)
327 {
328   return value->data[0].v_pointer;
329 }
330
331 static gchar*
332 boxed_proxy_collect_value (GValue      *value,
333                            guint        n_collect_values,
334                            GTypeCValue *collect_values,
335                            guint        collect_flags)
336 {
337   if (!collect_values[0].v_pointer)
338     value->data[0].v_pointer = NULL;
339   else
340     {
341       if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
342         {
343           value->data[0].v_pointer = collect_values[0].v_pointer;
344           value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
345         }
346       else
347         value->data[0].v_pointer = _g_type_boxed_copy (G_VALUE_TYPE (value), collect_values[0].v_pointer);
348     }
349
350   return NULL;
351 }
352
353 static gchar*
354 boxed_proxy_lcopy_value (const GValue *value,
355                          guint         n_collect_values,
356                          GTypeCValue  *collect_values,
357                          guint         collect_flags)
358 {
359   gpointer *boxed_p = collect_values[0].v_pointer;
360
361   if (!boxed_p)
362     return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
363
364   if (!value->data[0].v_pointer)
365     *boxed_p = NULL;
366   else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
367     *boxed_p = value->data[0].v_pointer;
368   else
369     *boxed_p = _g_type_boxed_copy (G_VALUE_TYPE (value), value->data[0].v_pointer);
370
371   return NULL;
372 }
373
374 /**
375  * g_boxed_type_register_static:
376  * @name: Name of the new boxed type.
377  * @boxed_copy: Boxed structure copy function.
378  * @boxed_free: Boxed structure free function.
379  *
380  * This function creates a new %G_TYPE_BOXED derived type id for a new
381  * boxed type with name @name. Boxed type handling functions have to be
382  * provided to copy and free opaque boxed structures of this type.
383  *
384  * Returns: New %G_TYPE_BOXED derived type id for @name.
385  */
386 GType
387 g_boxed_type_register_static (const gchar   *name,
388                               GBoxedCopyFunc boxed_copy,
389                               GBoxedFreeFunc boxed_free)
390 {
391   static const GTypeValueTable vtable = {
392     boxed_proxy_value_init,
393     boxed_proxy_value_free,
394     boxed_proxy_value_copy,
395     boxed_proxy_value_peek_pointer,
396     "p",
397     boxed_proxy_collect_value,
398     "p",
399     boxed_proxy_lcopy_value,
400   };
401   GTypeInfo type_info = {
402     0,                  /* class_size */
403     NULL,               /* base_init */
404     NULL,               /* base_finalize */
405     NULL,               /* class_init */
406     NULL,               /* class_finalize */
407     NULL,               /* class_data */
408     0,                  /* instance_size */
409     0,                  /* n_preallocs */
410     NULL,               /* instance_init */
411     &vtable,            /* value_table */
412   };
413   GType type;
414
415   g_return_val_if_fail (name != NULL, 0);
416   g_return_val_if_fail (boxed_copy != NULL, 0);
417   g_return_val_if_fail (boxed_free != NULL, 0);
418   g_return_val_if_fail (g_type_from_name (name) == 0, 0);
419
420   type = g_type_register_static (G_TYPE_BOXED, name, &type_info, 0);
421
422   /* install proxy functions upon successfull registration */
423   if (type)
424     _g_type_boxed_init (type, boxed_copy, boxed_free);
425
426   return type;
427 }
428
429 /**
430  * g_boxed_copy:
431  * @boxed_type: The type of @src_boxed.
432  * @src_boxed: The boxed structure to be copied.
433  * 
434  * Provide a copy of a boxed structure @src_boxed which is of type @boxed_type.
435  * 
436  * Returns: The newly created copy of the boxed structure.
437  */
438 gpointer
439 g_boxed_copy (GType         boxed_type,
440               gconstpointer src_boxed)
441 {
442   GTypeValueTable *value_table;
443   gpointer dest_boxed;
444
445   g_return_val_if_fail (G_TYPE_IS_BOXED (boxed_type), NULL);
446   g_return_val_if_fail (G_TYPE_IS_ABSTRACT (boxed_type) == FALSE, NULL);
447   g_return_val_if_fail (src_boxed != NULL, NULL);
448
449   value_table = g_type_value_table_peek (boxed_type);
450   if (!value_table)
451     g_return_val_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type), NULL);
452
453   /* check if our proxying implementation is used, we can short-cut here */
454   if (value_table->value_copy == boxed_proxy_value_copy)
455     dest_boxed = _g_type_boxed_copy (boxed_type, (gpointer) src_boxed);
456   else
457     {
458       GValue src_value, dest_value;
459
460       /* we heavily rely on third-party boxed type value vtable
461        * implementations to follow normal boxed value storage
462        * (data[0].v_pointer is the boxed struct, and
463        * data[1].v_uint holds the G_VALUE_NOCOPY_CONTENTS flag,
464        * rest zero).
465        * but then, we can expect that since we layed out the
466        * g_boxed_*() API.
467        * data[1].v_uint&G_VALUE_NOCOPY_CONTENTS shouldn't be set
468        * after a copy.
469        */
470       /* equiv. to g_value_set_static_boxed() */
471       value_meminit (&src_value, boxed_type);
472       src_value.data[0].v_pointer = (gpointer) src_boxed;
473       src_value.data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
474
475       /* call third-party code copy function, fingers-crossed */
476       value_meminit (&dest_value, boxed_type);
477       value_table->value_copy (&src_value, &dest_value);
478
479       /* double check and grouse if things went wrong */
480       if (dest_value.data[1].v_ulong)
481         g_warning ("the copy_value() implementation of type `%s' seems to make use of reserved GValue fields",
482                    g_type_name (boxed_type));
483
484       dest_boxed = dest_value.data[0].v_pointer;
485     }
486
487   return dest_boxed;
488 }
489
490 /**
491  * g_boxed_free:
492  * @boxed_type: The type of @boxed.
493  * @boxed: The boxed structure to be freed.
494  *
495  * Free the boxed structure @boxed which is of type @boxed_type.
496  */
497 void
498 g_boxed_free (GType    boxed_type,
499               gpointer boxed)
500 {
501   GTypeValueTable *value_table;
502
503   g_return_if_fail (G_TYPE_IS_BOXED (boxed_type));
504   g_return_if_fail (G_TYPE_IS_ABSTRACT (boxed_type) == FALSE);
505   g_return_if_fail (boxed != NULL);
506
507   value_table = g_type_value_table_peek (boxed_type);
508   if (!value_table)
509     g_return_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type));
510
511   /* check if our proxying implementation is used, we can short-cut here */
512   if (value_table->value_free == boxed_proxy_value_free)
513     _g_type_boxed_free (boxed_type, boxed);
514   else
515     {
516       GValue value;
517
518       /* see g_boxed_copy() on why we think we can do this */
519       value_meminit (&value, boxed_type);
520       value.data[0].v_pointer = boxed;
521       value_table->value_free (&value);
522     }
523 }
524
525 /**
526  * g_value_get_boxed:
527  * @value: a valid #GValue of %G_TYPE_BOXED derived type
528  *
529  * Get the contents of a %G_TYPE_BOXED derived #GValue.
530  *
531  * Returns: boxed contents of @value
532  */
533 gpointer
534 g_value_get_boxed (const GValue *value)
535 {
536   g_return_val_if_fail (G_VALUE_HOLDS_BOXED (value), NULL);
537   g_return_val_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)), NULL);
538
539   return value->data[0].v_pointer;
540 }
541
542 /**
543  * g_value_dup_boxed:
544  * @value: a valid #GValue of %G_TYPE_BOXED derived type
545  *
546  * Get the contents of a %G_TYPE_BOXED derived #GValue.  Upon getting,
547  * the boxed value is duplicated and needs to be later freed with
548  * g_boxed_free(), e.g. like: g_boxed_free (G_VALUE_TYPE (@value),
549  * return_value);
550  *
551  * Returns: boxed contents of @value
552  */
553 gpointer
554 g_value_dup_boxed (const GValue *value)
555 {
556   g_return_val_if_fail (G_VALUE_HOLDS_BOXED (value), NULL);
557   g_return_val_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)), NULL);
558
559   return value->data[0].v_pointer ? g_boxed_copy (G_VALUE_TYPE (value), value->data[0].v_pointer) : NULL;
560 }
561
562 static inline void
563 value_set_boxed_internal (GValue       *value,
564                           gconstpointer boxed,
565                           gboolean      need_copy,
566                           gboolean      need_free)
567 {
568   if (!boxed)
569     {
570       /* just resetting to NULL might not be desired, need to
571        * have value reinitialized also (for values defaulting
572        * to other default value states than a NULL data pointer),
573        * g_value_reset() will handle this
574        */
575       g_value_reset (value);
576       return;
577     }
578
579   if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
580     g_boxed_free (G_VALUE_TYPE (value), value->data[0].v_pointer);
581   value->data[1].v_uint = need_free ? 0 : G_VALUE_NOCOPY_CONTENTS;
582   value->data[0].v_pointer = need_copy ? g_boxed_copy (G_VALUE_TYPE (value), boxed) : (gpointer) boxed;
583 }
584
585 /**
586  * g_value_set_boxed:
587  * @value: a valid #GValue of %G_TYPE_BOXED derived type
588  * @v_boxed: boxed value to be set
589  *
590  * Set the contents of a %G_TYPE_BOXED derived #GValue to @v_boxed.
591  */
592 void
593 g_value_set_boxed (GValue       *value,
594                    gconstpointer boxed)
595 {
596   g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
597   g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
598
599   value_set_boxed_internal (value, boxed, TRUE, TRUE);
600 }
601
602 /**
603  * g_value_set_static_boxed:
604  * @value: a valid #GValue of %G_TYPE_BOXED derived type
605  * @v_boxed: static boxed value to be set
606  *
607  * Set the contents of a %G_TYPE_BOXED derived #GValue to @v_boxed.
608  * The boxed value is assumed to be static, and is thus not duplicated
609  * when setting the #GValue.
610  */
611 void
612 g_value_set_static_boxed (GValue       *value,
613                           gconstpointer boxed)
614 {
615   g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
616   g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
617
618   value_set_boxed_internal (value, boxed, FALSE, FALSE);
619 }
620
621 /**
622  * g_value_set_boxed_take_ownership:
623  * @value: a valid #GValue of %G_TYPE_BOXED derived type
624  * @v_boxed: duplicated unowned boxed value to be set
625  *
626  * This is an internal function introduced mainly for C marshallers.
627  *
628  * Deprecated: 2.4: Use g_value_take_boxed() instead.
629  */
630 void
631 g_value_set_boxed_take_ownership (GValue       *value,
632                                   gconstpointer boxed)
633 {
634   g_value_take_boxed (value, boxed);
635 }
636
637 /**
638  * g_value_take_boxed:
639  * @value: a valid #GValue of %G_TYPE_BOXED derived type
640  * @v_boxed: duplicated unowned boxed value to be set
641  *
642  * Sets the contents of a %G_TYPE_BOXED derived #GValue to @v_boxed
643  * and takes over the ownership of the callers reference to @v_boxed;
644  * the caller doesn't have to unref it any more.
645  *
646  * Since: 2.4
647  */
648 void
649 g_value_take_boxed (GValue       *value,
650                     gconstpointer boxed)
651 {
652   g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
653   g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
654
655   value_set_boxed_internal (value, boxed, FALSE, TRUE);
656 }
657
658 #define __G_BOXED_C__
659 #include "gobjectaliasdef.c"