02adca97c035fb9b397293a840cf0999758938ee
[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
130 GType
131 g_closure_get_type (void)
132 {
133   static GType type_id = 0;
134
135   if (!type_id)
136     type_id = g_boxed_type_register_static ("GClosure",
137                                             (GBoxedCopyFunc) g_closure_ref,
138                                             (GBoxedFreeFunc) g_closure_unref);
139   return type_id;
140 }
141
142 GType
143 g_value_get_type (void)
144 {
145   static GType type_id = 0;
146
147   if (!type_id)
148     type_id = g_boxed_type_register_static ("GValue",
149                                             value_copy,
150                                             value_free);
151   return type_id;
152 }
153
154 GType
155 g_value_array_get_type (void)
156 {
157   static GType type_id = 0;
158
159   if (!type_id)
160     type_id = g_boxed_type_register_static ("GValueArray",
161                                             (GBoxedCopyFunc) g_value_array_copy,
162                                             (GBoxedFreeFunc) g_value_array_free);
163   return type_id;
164 }
165
166 GType
167 g_gstring_get_type (void)
168 {
169   static GType type_id = 0;
170
171   if (!type_id)
172     type_id = g_boxed_type_register_static ("GString",  /* the naming is a bit odd, but GString is obviously not G_TYPE_STRING */
173                                             gstring_copy,
174                                             gstring_free);
175   return type_id;
176 }
177
178 static void
179 boxed_proxy_value_init (GValue *value)
180 {
181   BoxedNode key, *node;
182
183   key.type = G_VALUE_TYPE (value);
184   node = g_bsearch_array_lookup (&boxed_bsa, &key);
185   value->data[0].v_pointer = NULL;
186 }
187
188 static void
189 boxed_proxy_value_free (GValue *value)
190 {
191   if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
192     {
193       BoxedNode key, *node;
194
195       key.type = G_VALUE_TYPE (value);
196       node = g_bsearch_array_lookup (&boxed_bsa, &key);
197       node->free (value->data[0].v_pointer);
198     }
199 }
200
201 static void
202 boxed_proxy_value_copy (const GValue *src_value,
203                         GValue       *dest_value)
204 {
205   if (src_value->data[0].v_pointer)
206     {
207       BoxedNode key, *node;
208
209       key.type = G_VALUE_TYPE (src_value);
210       node = g_bsearch_array_lookup (&boxed_bsa, &key);
211       dest_value->data[0].v_pointer = node->copy (src_value->data[0].v_pointer);
212     }
213   else
214     dest_value->data[0].v_pointer = src_value->data[0].v_pointer;
215 }
216
217 static gpointer
218 boxed_proxy_value_peek_pointer (const GValue *value)
219 {
220   return value->data[0].v_pointer;
221 }
222
223 static gchar*
224 boxed_proxy_collect_value (GValue      *value,
225                            guint        n_collect_values,
226                            GTypeCValue *collect_values,
227                            guint        collect_flags)
228 {
229   BoxedNode key, *node;
230
231   key.type = G_VALUE_TYPE (value);
232   node = g_bsearch_array_lookup (&boxed_bsa, &key);
233
234   if (!collect_values[0].v_pointer)
235     value->data[0].v_pointer = NULL;
236   else
237     {
238       if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
239         {
240           value->data[0].v_pointer = collect_values[0].v_pointer;
241           value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
242         }
243       else
244         value->data[0].v_pointer = node->copy (collect_values[0].v_pointer);
245     }
246
247   return NULL;
248 }
249
250 static gchar*
251 boxed_proxy_lcopy_value (const GValue *value,
252                          guint         n_collect_values,
253                          GTypeCValue  *collect_values,
254                          guint         collect_flags)
255 {
256   gpointer *boxed_p = collect_values[0].v_pointer;
257
258   if (!boxed_p)
259     return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
260
261   if (!value->data[0].v_pointer)
262     *boxed_p = NULL;
263   else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
264     *boxed_p = value->data[0].v_pointer;
265   else
266     {
267       BoxedNode key, *node;
268
269       key.type = G_VALUE_TYPE (value);
270       node = g_bsearch_array_lookup (&boxed_bsa, &key);
271       *boxed_p = node->copy (value->data[0].v_pointer);
272     }
273
274   return NULL;
275 }
276
277 GType
278 g_boxed_type_register_static (const gchar   *name,
279                               GBoxedCopyFunc boxed_copy,
280                               GBoxedFreeFunc boxed_free)
281 {
282   static const GTypeValueTable vtable = {
283     boxed_proxy_value_init,
284     boxed_proxy_value_free,
285     boxed_proxy_value_copy,
286     boxed_proxy_value_peek_pointer,
287     "p",
288     boxed_proxy_collect_value,
289     "p",
290     boxed_proxy_lcopy_value,
291   };
292   static const GTypeInfo type_info = {
293     0,                  /* class_size */
294     NULL,               /* base_init */
295     NULL,               /* base_finalize */
296     NULL,               /* class_init */
297     NULL,               /* class_finalize */
298     NULL,               /* class_data */
299     0,                  /* instance_size */
300     0,                  /* n_preallocs */
301     NULL,               /* instance_init */
302     &vtable,            /* value_table */
303   };
304   GType type;
305
306   g_return_val_if_fail (name != NULL, 0);
307   g_return_val_if_fail (boxed_copy != NULL, 0);
308   g_return_val_if_fail (boxed_free != NULL, 0);
309   g_return_val_if_fail (g_type_from_name (name) == 0, 0);
310
311   type = g_type_register_static (G_TYPE_BOXED, name, &type_info, 0);
312
313   /* install proxy functions upon successfull registration */
314   if (type)
315     {
316       BoxedNode key;
317
318       key.type = type;
319       key.copy = boxed_copy;
320       key.free = boxed_free;
321       g_bsearch_array_insert (&boxed_bsa, &key, TRUE);
322     }
323
324   return type;
325 }
326
327 gpointer
328 g_boxed_copy (GType         boxed_type,
329               gconstpointer src_boxed)
330 {
331   GTypeValueTable *value_table;
332   gpointer dest_boxed;
333
334   g_return_val_if_fail (G_TYPE_IS_BOXED (boxed_type), NULL);
335   g_return_val_if_fail (G_TYPE_IS_ABSTRACT (boxed_type) == FALSE, NULL);
336   g_return_val_if_fail (src_boxed != NULL, NULL);
337
338   value_table = g_type_value_table_peek (boxed_type);
339   if (!value_table)
340     g_return_val_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type), NULL);
341
342   /* check if our proxying implementation is used, we can short-cut here */
343   if (value_table->value_copy == boxed_proxy_value_copy)
344     {
345       BoxedNode key, *node;
346
347       key.type = boxed_type;
348       node = g_bsearch_array_lookup (&boxed_bsa, &key);
349       dest_boxed = node->copy ((gpointer) src_boxed);
350     }
351   else
352     {
353       GValue src_value, dest_value;
354       
355       /* we heavil rely on third-party boxed type value vtable
356        * implementations to follow normal boxed value storage
357        * (data[0].v_pointer is the boxed struct, and
358        * data[1].v_uint holds the G_VALUE_NOCOPY_CONTENTS flag,
359        * rest zero).
360        * but then, we can expect that since we layed out the
361        * g_boxed_*() API.
362        * data[1].v_uint&G_VALUE_NOCOPY_CONTENTS shouldn't be set
363        * after a copy.
364        */
365       /* equiv. to g_value_set_static_boxed() */
366       value_meminit (&src_value, boxed_type);
367       src_value.data[0].v_pointer = (gpointer) src_boxed;
368       src_value.data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
369
370       /* call third-party code copy fucntion, fingers-crossed */
371       value_meminit (&dest_value, boxed_type);
372       value_table->value_copy (&src_value, &dest_value);
373
374       /* double check and grouse if things went wrong */
375       if (dest_value.data[1].v_ulong ||
376           dest_value.data[2].v_ulong ||
377           dest_value.data[3].v_ulong)
378         g_warning ("the copy_value() implementation of type `%s' seems to make use of reserved GValue fields",
379                    g_type_name (boxed_type));
380
381       dest_boxed = dest_value.data[0].v_pointer;
382     }
383
384   return dest_boxed;
385 }
386
387 void
388 g_boxed_free (GType    boxed_type,
389               gpointer boxed)
390 {
391   GTypeValueTable *value_table;
392
393   g_return_if_fail (G_TYPE_IS_BOXED (boxed_type));
394   g_return_if_fail (G_TYPE_IS_ABSTRACT (boxed_type) == FALSE);
395   g_return_if_fail (boxed != NULL);
396
397   value_table = g_type_value_table_peek (boxed_type);
398   if (!value_table)
399     g_return_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type));
400
401   /* check if our proxying implementation is used, we can short-cut here */
402   if (value_table->value_free == boxed_proxy_value_free)
403     {
404       BoxedNode key, *node;
405
406       key.type = boxed_type;
407       node = g_bsearch_array_lookup (&boxed_bsa, &key);
408       node->free (boxed);
409     }
410   else
411     {
412       GValue value;
413
414       /* see g_boxed_copy() on why we think we can do this */
415       value_meminit (&value, boxed_type);
416       value.data[0].v_pointer = boxed;
417       value_table->value_free (&value);
418     }
419 }
420
421 gpointer
422 g_value_get_boxed (const GValue *value)
423 {
424   g_return_val_if_fail (G_VALUE_HOLDS_BOXED (value), NULL);
425   g_return_val_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)), NULL);
426
427   return value->data[0].v_pointer;
428 }
429
430 gpointer
431 g_value_dup_boxed (const GValue *value)
432 {
433   g_return_val_if_fail (G_VALUE_HOLDS_BOXED (value), NULL);
434   g_return_val_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)), NULL);
435
436   return value->data[0].v_pointer ? g_boxed_copy (G_VALUE_TYPE (value), value->data[0].v_pointer) : NULL;
437 }
438
439 static inline void
440 value_set_boxed_internal (GValue       *value,
441                           gconstpointer const_boxed,
442                           gboolean      need_copy,
443                           gboolean      need_free)
444 {
445   BoxedNode key, *node;
446   gpointer boxed = (gpointer) const_boxed;
447
448   if (!boxed)
449     {
450       /* just resetting to NULL might not be desired, need to
451        * have value reinitialized also (for values defaulting
452        * to other default value states than a NULL data pointer),
453        * g_value_reset() will handle this
454        */
455       g_value_reset (value);
456       return;
457     }
458
459   key.type = G_VALUE_TYPE (value);
460   node = g_bsearch_array_lookup (&boxed_bsa, &key);
461
462   if (node)
463     {
464       /* we proxy this type, free contents and copy right away */
465       if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
466         node->free (value->data[0].v_pointer);
467       value->data[1].v_uint = need_free ? 0 : G_VALUE_NOCOPY_CONTENTS;
468       value->data[0].v_pointer = need_copy ? node->copy (boxed) : boxed;
469     }
470   else
471     {
472       /* we don't handle this type, free contents and let g_boxed_copy()
473        * figure what's required
474        */
475       if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
476         g_boxed_free (G_VALUE_TYPE (value), value->data[0].v_pointer);
477       value->data[1].v_uint = need_free ? 0 : G_VALUE_NOCOPY_CONTENTS;
478       value->data[0].v_pointer = need_copy ? g_boxed_copy (G_VALUE_TYPE (value), boxed) : boxed;
479     }
480 }
481
482 void
483 g_value_set_boxed (GValue       *value,
484                    gconstpointer boxed)
485 {
486   g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
487   g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
488
489   value_set_boxed_internal (value, boxed, TRUE, TRUE);
490 }
491
492 void
493 g_value_set_static_boxed (GValue       *value,
494                           gconstpointer boxed)
495 {
496   g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
497   g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
498
499   value_set_boxed_internal (value, boxed, FALSE, FALSE);
500 }
501
502 void
503 g_value_set_boxed_take_ownership (GValue       *value,
504                                   gconstpointer boxed)
505 {
506   g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
507   g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
508
509   value_set_boxed_internal (value, boxed, FALSE, TRUE);
510 }