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