changed collect_format, collect_value() and lcopy_format, lcopy_value() in
[platform/upstream/glib.git] / gobject / gboxed.c
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 2000 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        "gvaluecollector.h"
24
25 #include <string.h>
26
27 /* --- typedefs & structures --- */
28 typedef struct
29 {
30   GType          type;
31   GBoxedCopyFunc copy;
32   GBoxedFreeFunc free;
33 } BoxedNode;
34
35
36 /* --- prototypes --- */
37 static gint     boxed_nodes_cmp         (gconstpointer  p1,
38                                          gconstpointer  p2);
39
40
41 /* --- variables --- */
42 static GBSearchArray boxed_bsa = { boxed_nodes_cmp, sizeof (BoxedNode), 0, 0, NULL };
43
44
45 /* --- functions --- */
46 static gint
47 boxed_nodes_cmp (gconstpointer p1,
48                  gconstpointer p2)
49 {
50   const BoxedNode *node1 = p1, *node2 = p2;
51
52   return G_BSEARCH_ARRAY_CMP (node1->type, node2->type);
53 }
54
55 void
56 g_boxed_type_init (void)  /* sync with gtype.c */
57 {
58   static const GTypeInfo info = {
59     0,                          /* class_size */
60     NULL,                       /* base_init */
61     NULL,                       /* base_destroy */
62     NULL,                       /* class_init */
63     NULL,                       /* class_destroy */
64     NULL,                       /* class_data */
65     0,                          /* instance_size */
66     0,                          /* n_preallocs */
67     NULL,                       /* instance_init */
68     NULL,                       /* value_table */
69   };
70   const GTypeFundamentalInfo finfo = { G_TYPE_FLAG_DERIVABLE, };
71   GType type;
72
73   /* G_TYPE_BOXED
74    */
75   type = g_type_register_fundamental (G_TYPE_BOXED, "GBoxed", &info, &finfo, G_TYPE_FLAG_ABSTRACT);
76   g_assert (type == G_TYPE_BOXED);
77 }
78
79 static void
80 boxed_proxy_value_init (GValue *value)
81 {
82   value->data[0].v_pointer = 0;
83 }
84
85 static void
86 boxed_proxy_value_free (GValue *value)
87 {
88   if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
89     {
90       BoxedNode key, *node;
91
92       key.type = value->g_type;
93       node = g_bsearch_array_lookup (&boxed_bsa, &key);
94       node->free (value->data[0].v_pointer);
95     }
96 }
97
98 static void
99 boxed_proxy_value_copy (const GValue *src_value,
100                         GValue       *dest_value)
101 {
102   if (src_value->data[0].v_pointer)
103     {
104       BoxedNode key, *node;
105
106       key.type = src_value->g_type;
107       node = g_bsearch_array_lookup (&boxed_bsa, &key);
108       dest_value->data[0].v_pointer = node->copy (src_value->data[0].v_pointer);
109     }
110   else
111     dest_value->data[0].v_pointer = src_value->data[0].v_pointer;
112 }
113
114 static gpointer
115 boxed_proxy_value_peek_pointer (const GValue *value)
116 {
117   return value->data[0].v_pointer;
118 }
119
120 static gchar*
121 boxed_proxy_collect_value (GValue      *value,
122                            guint        n_collect_values,
123                            GTypeCValue *collect_values,
124                            guint        collect_flags)
125 {
126   if (!collect_values[0].v_pointer)
127     value->data[0].v_pointer = NULL;
128   else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
129     {
130       value->data[0].v_pointer = collect_values[0].v_pointer;
131       value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
132     }
133   else
134     {
135       BoxedNode key, *node;
136       
137       key.type = value->g_type;
138       node = g_bsearch_array_lookup (&boxed_bsa, &key);
139       value->data[0].v_pointer = node->copy (collect_values[0].v_pointer);
140     }
141
142   return NULL;
143 }
144
145 static gchar*
146 boxed_proxy_lcopy_value (const GValue *value,
147                          guint         n_collect_values,
148                          GTypeCValue  *collect_values,
149                          guint         collect_flags)
150 {
151   gpointer *boxed_p = collect_values[0].v_pointer;
152
153   if (!boxed_p)
154     return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
155
156   if (!value->data[0].v_pointer)
157     *boxed_p = NULL;
158   else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
159     *boxed_p = value->data[0].v_pointer;
160   else
161     {
162       BoxedNode key, *node;
163
164       key.type = value->g_type;
165       node = g_bsearch_array_lookup (&boxed_bsa, &key);
166       *boxed_p = node->copy (value->data[0].v_pointer);
167     }
168
169   return NULL;
170 }
171
172 GType
173 g_boxed_type_register_static (const gchar   *name,
174                               GBoxedCopyFunc boxed_copy,
175                               GBoxedFreeFunc boxed_free)
176 {
177   static const GTypeValueTable vtable = {
178     boxed_proxy_value_init,
179     boxed_proxy_value_free,
180     boxed_proxy_value_copy,
181     boxed_proxy_value_peek_pointer,
182     "p",
183     boxed_proxy_collect_value,
184     "p",
185     boxed_proxy_lcopy_value,
186   };
187   static const GTypeInfo type_info = {
188     0,                  /* class_size */
189     NULL,               /* base_init */
190     NULL,               /* base_finalize */
191     NULL,               /* class_init */
192     NULL,               /* class_finalize */
193     NULL,               /* class_data */
194     0,                  /* instance_size */
195     0,                  /* n_preallocs */
196     NULL,               /* instance_init */
197     &vtable,            /* value_table */
198   };
199   GType type;
200
201   g_return_val_if_fail (name != NULL, 0);
202   g_return_val_if_fail (boxed_copy != NULL, 0);
203   g_return_val_if_fail (boxed_free != NULL, 0);
204   g_return_val_if_fail (g_type_from_name (name) == 0, 0);
205
206   type = g_type_register_static (G_TYPE_BOXED, name, &type_info, 0);
207
208   /* install proxy functions upon successfull registration */
209   if (type)
210     {
211       BoxedNode key;
212
213       key.type = type;
214       key.copy = boxed_copy;
215       key.free = boxed_free;
216       g_bsearch_array_insert (&boxed_bsa, &key, TRUE);
217     }
218
219   return type;
220 }
221
222 GBoxed*
223 g_boxed_copy (GType         boxed_type,
224               gconstpointer src_boxed)
225 {
226   GTypeValueTable *value_table;
227   gpointer dest_boxed;
228
229   g_return_val_if_fail (G_TYPE_IS_BOXED (boxed_type), NULL);
230   g_return_val_if_fail (G_TYPE_IS_ABSTRACT (boxed_type) == FALSE, NULL);
231   g_return_val_if_fail (src_boxed != NULL, NULL);
232
233   value_table = g_type_value_table_peek (boxed_type);
234   if (!value_table)
235     g_return_val_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type), NULL);
236
237   /* check if our proxying implementation is used, we can short-cut here */
238   if (value_table->value_copy == boxed_proxy_value_copy)
239     {
240       BoxedNode key, *node;
241
242       key.type = boxed_type;
243       node = g_bsearch_array_lookup (&boxed_bsa, &key);
244       dest_boxed = node->copy ((gpointer) src_boxed);
245     }
246   else
247     {
248       GValue src_value, dest_value;
249       
250       /* we heavil rely on the gvalue.c implementation here */
251
252       memset (&src_value.data, 0, sizeof (src_value.data));
253       memset (&dest_value.data, 0, sizeof (dest_value.data));
254       dest_value.g_type = boxed_type;
255       src_value.g_type = boxed_type;
256       src_value.data[0].v_pointer = (gpointer) src_boxed;
257       value_table->value_copy (&src_value, &dest_value);
258       if (dest_value.data[1].v_ulong ||
259           dest_value.data[2].v_ulong ||
260           dest_value.data[3].v_ulong)
261         g_warning ("the copy_value() implementation of type `%s' seems to make use of reserved GValue fields",
262                    g_type_name (boxed_type));
263
264       dest_boxed = dest_value.data[0].v_pointer;
265     }
266
267   return dest_boxed;
268 }
269
270 void
271 g_boxed_free (GType    boxed_type,
272               gpointer boxed)
273 {
274   GTypeValueTable *value_table;
275
276   g_return_if_fail (G_TYPE_IS_BOXED (boxed_type));
277   g_return_if_fail (G_TYPE_IS_ABSTRACT (boxed_type) == FALSE);
278   g_return_if_fail (boxed != NULL);
279
280   value_table = g_type_value_table_peek (boxed_type);
281   if (!value_table)
282     g_return_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type));
283
284   /* check if our proxying implementation is used, we can short-cut here */
285   if (value_table->value_free == boxed_proxy_value_free)
286     {
287       BoxedNode key, *node;
288
289       key.type = boxed_type;
290       node = g_bsearch_array_lookup (&boxed_bsa, &key);
291       node->free (boxed);
292     }
293   else
294     {
295       GValue value;
296
297       /* we heavil rely on the gvalue.c implementation here */
298       memset (&value.data, 0, sizeof (value.data));
299       value.g_type = boxed_type;
300       value.data[0].v_pointer = boxed;
301       value_table->value_free (&value);
302     }
303 }
304
305 void
306 g_value_set_boxed (GValue       *value,
307                    gconstpointer boxed)
308 {
309   g_return_if_fail (G_IS_VALUE_BOXED (value));
310   g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
311
312   if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
313     g_boxed_free (G_VALUE_TYPE (value), value->data[0].v_pointer);
314   value->data[0].v_pointer = boxed ? g_boxed_copy (G_VALUE_TYPE (value), boxed) : NULL;
315   value->data[1].v_uint = 0;
316 }
317
318 void
319 g_value_set_static_boxed (GValue       *value,
320                           gconstpointer boxed)
321 {
322   g_return_if_fail (G_IS_VALUE_BOXED (value));
323   g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
324
325   if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
326     g_boxed_free (G_VALUE_TYPE (value), value->data[0].v_pointer);
327   value->data[0].v_pointer = (gpointer) boxed;
328   value->data[1].v_uint = boxed ? G_VALUE_NOCOPY_CONTENTS : 0;
329 }
330
331 gpointer
332 g_value_get_boxed (const GValue *value)
333 {
334   g_return_val_if_fail (G_IS_VALUE_BOXED (value), NULL);
335   g_return_val_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)), NULL);
336
337   return value->data[0].v_pointer;
338 }
339
340 gpointer
341 g_value_dup_boxed (GValue *value)
342 {
343   g_return_val_if_fail (G_IS_VALUE_BOXED (value), NULL);
344   g_return_val_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)), NULL);
345
346   return value->data[0].v_pointer ? g_boxed_copy (G_VALUE_TYPE (value), value->data[0].v_pointer) : NULL;
347 }