added newly added gobject/ headers.
[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
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)
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        nth_value,
123                            GType       *collect_type,
124                            GTypeCValue *collect_value)
125 {
126   BoxedNode key, *node;
127
128   key.type = value->g_type;
129   node = g_bsearch_array_lookup (&boxed_bsa, &key);
130   value->data[0].v_pointer = node->copy (collect_value->v_pointer);
131
132   *collect_type = 0;
133   return NULL;
134 }
135
136 static gchar*
137 boxed_proxy_lcopy_value (const GValue *value,
138                          guint         nth_value,
139                          GType        *collect_type,
140                          GTypeCValue  *collect_value)
141 {
142   BoxedNode key, *node;
143   gpointer *boxed_p = collect_value->v_pointer;
144
145   if (!boxed_p)
146     return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
147
148   key.type = value->g_type;
149   node = g_bsearch_array_lookup (&boxed_bsa, &key);
150   *boxed_p = node->copy (value->data[0].v_pointer);
151
152   *collect_type = 0;
153   return NULL;
154 }
155
156 GType
157 g_boxed_type_register_static (const gchar   *name,
158                               GBoxedCopyFunc boxed_copy,
159                               GBoxedFreeFunc boxed_free)
160 {
161   static const GTypeValueTable vtable = {
162     boxed_proxy_value_init,
163     boxed_proxy_value_free,
164     boxed_proxy_value_copy,
165     boxed_proxy_value_peek_pointer,
166     G_VALUE_COLLECT_POINTER,
167     boxed_proxy_collect_value,
168     G_VALUE_COLLECT_POINTER,
169     boxed_proxy_lcopy_value,
170   };
171   static const GTypeInfo type_info = {
172     0,                  /* class_size */
173     NULL,               /* base_init */
174     NULL,               /* base_finalize */
175     NULL,               /* class_init */
176     NULL,               /* class_finalize */
177     NULL,               /* class_data */
178     0,                  /* instance_size */
179     0,                  /* n_preallocs */
180     NULL,               /* instance_init */
181     &vtable,            /* value_table */
182   };
183   GType type;
184
185   g_return_val_if_fail (name != NULL, 0);
186   g_return_val_if_fail (boxed_copy != NULL, 0);
187   g_return_val_if_fail (boxed_free != NULL, 0);
188   g_return_val_if_fail (g_type_from_name (name) == 0, 0);
189
190   type = g_type_register_static (G_TYPE_BOXED, name, &type_info, 0);
191
192   /* install proxy functions upon successfull registration */
193   if (type)
194     {
195       BoxedNode key;
196
197       key.type = type;
198       key.copy = boxed_copy;
199       key.free = boxed_free;
200       g_bsearch_array_insert (&boxed_bsa, &key, TRUE);
201     }
202
203   return type;
204 }
205
206 GBoxed*
207 g_boxed_copy (GType    boxed_type,
208               gpointer src_boxed)
209 {
210   GTypeValueTable *value_table;
211
212   g_return_val_if_fail (G_TYPE_IS_BOXED (boxed_type), NULL);
213   g_return_val_if_fail (G_TYPE_IS_ABSTRACT (boxed_type) == FALSE, NULL);
214   g_return_val_if_fail (src_boxed != NULL, NULL);
215
216   value_table = g_type_value_table_peek (boxed_type);
217   if (!value_table)
218     g_return_val_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type), NULL);
219
220   /* check if our proxying implementation is used, we can short-cut here */
221   if (value_table->value_copy == boxed_proxy_value_copy)
222     {
223       BoxedNode key, *node;
224
225       key.type = boxed_type;
226       node = g_bsearch_array_lookup (&boxed_bsa, &key);
227       src_boxed = node->copy (src_boxed);
228     }
229   else
230     {
231       GValue src_value, dest_value;
232       
233       /* we heavil rely on the gvalue.c implementation here */
234
235       memset (&src_value.data, 0, sizeof (src_value.data));
236       memset (&dest_value.data, 0, sizeof (dest_value.data));
237       dest_value.g_type = boxed_type;
238       src_value.g_type = boxed_type;
239       src_value.data[0].v_pointer = src_boxed;
240       value_table->value_copy (&src_value, &dest_value);
241       if (dest_value.data[1].v_ulong ||
242           dest_value.data[2].v_ulong ||
243           dest_value.data[3].v_ulong)
244         g_warning ("the copy_value() implementation of type `%s' seems to make use of reserved GValue fields",
245                    g_type_name (boxed_type));
246
247       src_boxed = dest_value.data[0].v_pointer;
248     }
249
250   return src_boxed;
251 }
252
253 void
254 g_boxed_free (GType    boxed_type,
255               gpointer boxed)
256 {
257   GTypeValueTable *value_table;
258
259   g_return_if_fail (G_TYPE_IS_BOXED (boxed_type));
260   g_return_if_fail (G_TYPE_IS_ABSTRACT (boxed_type) == FALSE);
261   g_return_if_fail (boxed != NULL);
262
263   value_table = g_type_value_table_peek (boxed_type);
264   if (!value_table)
265     g_return_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type));
266
267   /* check if our proxying implementation is used, we can short-cut here */
268   if (value_table->value_free == boxed_proxy_value_free)
269     {
270       BoxedNode key, *node;
271
272       key.type = boxed_type;
273       node = g_bsearch_array_lookup (&boxed_bsa, &key);
274       node->free (boxed);
275     }
276   else
277     {
278       GValue value;
279
280       /* we heavil rely on the gvalue.c implementation here */
281       memset (&value.data, 0, sizeof (value.data));
282       value.g_type = boxed_type;
283       value.data[0].v_pointer = boxed;
284       value_table->value_free (&value);
285     }
286 }
287
288 void
289 g_value_set_boxed (GValue  *value,
290                    gpointer boxed)
291 {
292   g_return_if_fail (G_IS_VALUE_BOXED (value));
293   g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
294
295   if (value->data[0].v_pointer)
296     g_boxed_free (G_VALUE_TYPE (value), value->data[0].v_pointer);
297   value->data[0].v_pointer = boxed ? g_boxed_copy (G_VALUE_TYPE (value), boxed) : NULL;
298 }
299
300 gpointer
301 g_value_get_boxed (GValue *value)
302 {
303   g_return_val_if_fail (G_IS_VALUE_BOXED (value), NULL);
304   g_return_val_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)), NULL);
305
306   return value->data[0].v_pointer;
307 }
308
309 gpointer
310 g_value_dup_boxed (GValue *value)
311 {
312   g_return_val_if_fail (G_IS_VALUE_BOXED (value), NULL);
313   g_return_val_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)), NULL);
314
315   return value->data[0].v_pointer ? g_boxed_copy (G_VALUE_TYPE (value), value->data[0].v_pointer) : NULL;
316 }