Removed is_refcounted and GBoxedInitFunc from
[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 #include        "gboxed.h"
20
21 #include        "gbsearcharray.h"
22 #include        "gvalue.h"
23 #include        "gvaluearray.h"
24 #include        "gclosure.h"
25 #include        "gvaluecollector.h"
26
27 #include <string.h>
28
29 /* --- typedefs & structures --- */
30 typedef struct
31 {
32   GType          type;
33   GBoxedCopyFunc copy;
34   GBoxedFreeFunc free;
35 } BoxedNode;
36
37
38 /* --- prototypes --- */
39 static gint     boxed_nodes_cmp         (gconstpointer  p1,
40                                          gconstpointer  p2);
41
42
43 /* --- variables --- */
44 static GBSearchArray boxed_bsa = G_STATIC_BSEARCH_ARRAY_INIT (sizeof (BoxedNode), boxed_nodes_cmp, 0);
45
46
47 /* --- functions --- */
48 static gint
49 boxed_nodes_cmp (gconstpointer p1,
50                  gconstpointer p2)
51 {
52   const BoxedNode *node1 = p1, *node2 = p2;
53
54   return G_BSEARCH_ARRAY_CMP (node1->type, node2->type);
55 }
56
57 static inline void              /* keep this function in sync with gvalue.c */
58 value_meminit (GValue *value,
59                GType   value_type)
60 {
61   value->g_type = value_type;
62   memset (value->data, 0, sizeof (value->data));
63 }
64
65 static gpointer
66 value_copy (gpointer boxed)
67 {
68   const GValue *src_value = boxed;
69   GValue *dest_value = g_new0 (GValue, 1);
70
71   if (G_VALUE_TYPE (src_value))
72     {
73       g_value_init (dest_value, G_VALUE_TYPE (src_value));
74       g_value_copy (src_value, dest_value);
75     }
76   return dest_value;
77 }
78
79 static void
80 value_free (gpointer boxed)
81 {
82   GValue *value = boxed;
83
84   if (G_VALUE_TYPE (value))
85     g_value_unset (value);
86   g_free (value);
87 }
88
89 static gpointer
90 gstring_copy (gpointer boxed)
91 {
92   const GString *src_gstring = boxed;
93
94   return g_string_new_len (src_gstring->str, src_gstring->len);
95 }
96
97 static void
98 gstring_free (gpointer boxed)
99 {
100   GString *gstring = boxed;
101
102   g_string_free (gstring, TRUE);
103 }
104
105 void
106 g_boxed_type_init (void)  /* sync with gtype.c */
107 {
108   static const GTypeInfo info = {
109     0,                          /* class_size */
110     NULL,                       /* base_init */
111     NULL,                       /* base_destroy */
112     NULL,                       /* class_init */
113     NULL,                       /* class_destroy */
114     NULL,                       /* class_data */
115     0,                          /* instance_size */
116     0,                          /* n_preallocs */
117     NULL,                       /* instance_init */
118     NULL,                       /* value_table */
119   };
120   const GTypeFundamentalInfo finfo = { G_TYPE_FLAG_DERIVABLE, };
121   GType type;
122
123   /* G_TYPE_BOXED
124    */
125   type = g_type_register_fundamental (G_TYPE_BOXED, "GBoxed", &info, &finfo,
126                                       G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT);
127   g_assert (type == G_TYPE_BOXED);
128
129   /* boxed: G_TYPE_CLOSURE
130    */
131   type = g_boxed_type_register_static ("GClosure",
132                                        (GBoxedCopyFunc) g_closure_ref,
133                                        (GBoxedFreeFunc) g_closure_unref);
134   g_assert (type == G_TYPE_CLOSURE);
135
136   /* boxed: G_TYPE_VALUE
137    */
138   type = g_boxed_type_register_static ("GValue",
139                                        value_copy,
140                                        value_free);
141   g_assert (type == G_TYPE_VALUE);
142
143   /* boxed: G_TYPE_VALUE_ARRAY
144    */
145   type = g_boxed_type_register_static ("GValueArray",
146                                        (GBoxedCopyFunc) g_value_array_copy,
147                                        (GBoxedFreeFunc) g_value_array_free);
148   g_assert (type == G_TYPE_VALUE_ARRAY);
149
150   /* boxed: G_TYPE_GSTRING
151    * yes, the naming is a bit odd, but GString is obviously not G_TYPE_STRING
152    */
153   type = g_boxed_type_register_static ("GString",
154                                        gstring_copy,
155                                        gstring_free);
156   g_assert (type == G_TYPE_GSTRING);
157 }
158
159 static void
160 boxed_proxy_value_init (GValue *value)
161 {
162   BoxedNode key, *node;
163
164   key.type = G_VALUE_TYPE (value);
165   node = g_bsearch_array_lookup (&boxed_bsa, &key);
166   value->data[0].v_pointer = NULL;
167 }
168
169 static void
170 boxed_proxy_value_free (GValue *value)
171 {
172   if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
173     {
174       BoxedNode key, *node;
175
176       key.type = G_VALUE_TYPE (value);
177       node = g_bsearch_array_lookup (&boxed_bsa, &key);
178       node->free (value->data[0].v_pointer);
179     }
180 }
181
182 static void
183 boxed_proxy_value_copy (const GValue *src_value,
184                         GValue       *dest_value)
185 {
186   if (src_value->data[0].v_pointer)
187     {
188       BoxedNode key, *node;
189
190       key.type = G_VALUE_TYPE (src_value);
191       node = g_bsearch_array_lookup (&boxed_bsa, &key);
192       dest_value->data[0].v_pointer = node->copy (src_value->data[0].v_pointer);
193     }
194   else
195     dest_value->data[0].v_pointer = src_value->data[0].v_pointer;
196 }
197
198 static gpointer
199 boxed_proxy_value_peek_pointer (const GValue *value)
200 {
201   return value->data[0].v_pointer;
202 }
203
204 static gchar*
205 boxed_proxy_collect_value (GValue      *value,
206                            guint        n_collect_values,
207                            GTypeCValue *collect_values,
208                            guint        collect_flags)
209 {
210   BoxedNode key, *node;
211
212   key.type = G_VALUE_TYPE (value);
213   node = g_bsearch_array_lookup (&boxed_bsa, &key);
214
215   if (!collect_values[0].v_pointer)
216     value->data[0].v_pointer = NULL;
217   else
218     {
219       if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
220         {
221           value->data[0].v_pointer = collect_values[0].v_pointer;
222           value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
223         }
224       else
225         value->data[0].v_pointer = node->copy (collect_values[0].v_pointer);
226     }
227
228   return NULL;
229 }
230
231 static gchar*
232 boxed_proxy_lcopy_value (const GValue *value,
233                          guint         n_collect_values,
234                          GTypeCValue  *collect_values,
235                          guint         collect_flags)
236 {
237   gpointer *boxed_p = collect_values[0].v_pointer;
238
239   if (!boxed_p)
240     return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
241
242   if (!value->data[0].v_pointer)
243     *boxed_p = NULL;
244   else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
245     *boxed_p = value->data[0].v_pointer;
246   else
247     {
248       BoxedNode key, *node;
249
250       key.type = G_VALUE_TYPE (value);
251       node = g_bsearch_array_lookup (&boxed_bsa, &key);
252       *boxed_p = node->copy (value->data[0].v_pointer);
253     }
254
255   return NULL;
256 }
257
258 GType
259 g_boxed_type_register_static (const gchar   *name,
260                               GBoxedCopyFunc boxed_copy,
261                               GBoxedFreeFunc boxed_free)
262 {
263   static const GTypeValueTable vtable = {
264     boxed_proxy_value_init,
265     boxed_proxy_value_free,
266     boxed_proxy_value_copy,
267     boxed_proxy_value_peek_pointer,
268     "p",
269     boxed_proxy_collect_value,
270     "p",
271     boxed_proxy_lcopy_value,
272   };
273   static const GTypeInfo type_info = {
274     0,                  /* class_size */
275     NULL,               /* base_init */
276     NULL,               /* base_finalize */
277     NULL,               /* class_init */
278     NULL,               /* class_finalize */
279     NULL,               /* class_data */
280     0,                  /* instance_size */
281     0,                  /* n_preallocs */
282     NULL,               /* instance_init */
283     &vtable,            /* value_table */
284   };
285   GType type;
286
287   g_return_val_if_fail (name != NULL, 0);
288   g_return_val_if_fail (boxed_copy != NULL, 0);
289   g_return_val_if_fail (boxed_free != NULL, 0);
290   g_return_val_if_fail (g_type_from_name (name) == 0, 0);
291
292   type = g_type_register_static (G_TYPE_BOXED, name, &type_info, 0);
293
294   /* install proxy functions upon successfull registration */
295   if (type)
296     {
297       BoxedNode key;
298
299       key.type = type;
300       key.copy = boxed_copy;
301       key.free = boxed_free;
302       g_bsearch_array_insert (&boxed_bsa, &key, TRUE);
303     }
304
305   return type;
306 }
307
308 gpointer
309 g_boxed_copy (GType         boxed_type,
310               gconstpointer src_boxed)
311 {
312   GTypeValueTable *value_table;
313   gpointer dest_boxed;
314
315   g_return_val_if_fail (G_TYPE_IS_BOXED (boxed_type), NULL);
316   g_return_val_if_fail (G_TYPE_IS_ABSTRACT (boxed_type) == FALSE, NULL);
317   g_return_val_if_fail (src_boxed != NULL, NULL);
318
319   value_table = g_type_value_table_peek (boxed_type);
320   if (!value_table)
321     g_return_val_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type), NULL);
322
323   /* check if our proxying implementation is used, we can short-cut here */
324   if (value_table->value_copy == boxed_proxy_value_copy)
325     {
326       BoxedNode key, *node;
327
328       key.type = boxed_type;
329       node = g_bsearch_array_lookup (&boxed_bsa, &key);
330       dest_boxed = node->copy ((gpointer) src_boxed);
331     }
332   else
333     {
334       GValue src_value, dest_value;
335       
336       /* we heavil rely on third-party boxed type value vtable
337        * implementations to follow normal boxed value storage
338        * (data[0].v_pointer is the boxed struct, and
339        * data[1].v_uint holds the G_VALUE_NOCOPY_CONTENTS flag,
340        * rest zero).
341        * but then, we can expect that since we layed out the
342        * g_boxed_*() API.
343        * data[1].v_uint&G_VALUE_NOCOPY_CONTENTS shouldn't be set
344        * after a copy.
345        */
346       /* equiv. to g_value_set_static_boxed() */
347       value_meminit (&src_value, boxed_type);
348       src_value.data[0].v_pointer = (gpointer) src_boxed;
349       src_value.data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
350
351       /* call third-party code copy fucntion, fingers-crossed */
352       value_meminit (&dest_value, boxed_type);
353       value_table->value_copy (&src_value, &dest_value);
354
355       /* double check and grouse if things went wrong */
356       if (dest_value.data[1].v_ulong ||
357           dest_value.data[2].v_ulong ||
358           dest_value.data[3].v_ulong)
359         g_warning ("the copy_value() implementation of type `%s' seems to make use of reserved GValue fields",
360                    g_type_name (boxed_type));
361
362       dest_boxed = dest_value.data[0].v_pointer;
363     }
364
365   return dest_boxed;
366 }
367
368 void
369 g_boxed_free (GType    boxed_type,
370               gpointer boxed)
371 {
372   GTypeValueTable *value_table;
373
374   g_return_if_fail (G_TYPE_IS_BOXED (boxed_type));
375   g_return_if_fail (G_TYPE_IS_ABSTRACT (boxed_type) == FALSE);
376   g_return_if_fail (boxed != NULL);
377
378   value_table = g_type_value_table_peek (boxed_type);
379   if (!value_table)
380     g_return_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type));
381
382   /* check if our proxying implementation is used, we can short-cut here */
383   if (value_table->value_free == boxed_proxy_value_free)
384     {
385       BoxedNode key, *node;
386
387       key.type = boxed_type;
388       node = g_bsearch_array_lookup (&boxed_bsa, &key);
389       node->free (boxed);
390     }
391   else
392     {
393       GValue value;
394
395       /* see g_boxed_copy() on why we think we can do this */
396       value_meminit (&value, boxed_type);
397       value.data[0].v_pointer = boxed;
398       value_table->value_free (&value);
399     }
400 }
401
402 gpointer
403 g_value_get_boxed (const GValue *value)
404 {
405   g_return_val_if_fail (G_VALUE_HOLDS_BOXED (value), NULL);
406   g_return_val_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)), NULL);
407
408   return value->data[0].v_pointer;
409 }
410
411 gpointer
412 g_value_dup_boxed (const GValue *value)
413 {
414   g_return_val_if_fail (G_VALUE_HOLDS_BOXED (value), NULL);
415   g_return_val_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)), NULL);
416
417   return value->data[0].v_pointer ? g_boxed_copy (G_VALUE_TYPE (value), value->data[0].v_pointer) : NULL;
418 }
419
420 static inline void
421 value_set_boxed_internal (GValue       *value,
422                           gconstpointer const_boxed,
423                           gboolean      need_copy,
424                           gboolean      need_free)
425 {
426   BoxedNode key, *node;
427   gpointer boxed = (gpointer) const_boxed;
428
429   if (!boxed)
430     {
431       /* just resetting to NULL might not be desired, need to
432        * have value reinitialized also (for values defaulting
433        * to other default value states than a NULL data pointer),
434        * g_value_reset() will handle this
435        */
436       g_value_reset (value);
437       return;
438     }
439
440   key.type = G_VALUE_TYPE (value);
441   node = g_bsearch_array_lookup (&boxed_bsa, &key);
442
443   if (node)
444     {
445       /* we proxy this type, free contents and copy right away */
446       if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
447         node->free (value->data[0].v_pointer);
448       value->data[1].v_uint = need_free ? 0 : G_VALUE_NOCOPY_CONTENTS;
449       value->data[0].v_pointer = need_copy ? node->copy (boxed) : boxed;
450     }
451   else
452     {
453       /* we don't handle this type, free contents and let g_boxed_copy()
454        * figure what's required
455        */
456       if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
457         g_boxed_free (G_VALUE_TYPE (value), value->data[0].v_pointer);
458       value->data[1].v_uint = need_free ? 0 : G_VALUE_NOCOPY_CONTENTS;
459       value->data[0].v_pointer = need_copy ? g_boxed_copy (G_VALUE_TYPE (value), boxed) : boxed;
460     }
461 }
462
463 void
464 g_value_set_boxed (GValue       *value,
465                    gconstpointer boxed)
466 {
467   g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
468   g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
469
470   value_set_boxed_internal (value, boxed, TRUE, TRUE);
471 }
472
473 void
474 g_value_set_static_boxed (GValue       *value,
475                           gconstpointer boxed)
476 {
477   g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
478   g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
479
480   value_set_boxed_internal (value, boxed, FALSE, FALSE);
481 }
482
483 void
484 g_value_set_boxed_take_ownership (GValue       *value,
485                                   gconstpointer boxed)
486 {
487   g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
488   g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
489
490   value_set_boxed_internal (value, boxed, FALSE, TRUE);
491 }