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