1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2000 Red Hat, Inc.
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.
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.
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.
21 #include "gbsearcharray.h"
23 #include "gvaluecollector.h"
27 /* --- typedefs & structures --- */
36 /* --- prototypes --- */
37 static gint boxed_nodes_cmp (gconstpointer p1,
41 /* --- variables --- */
42 static GBSearchArray boxed_bsa = { boxed_nodes_cmp, sizeof (BoxedNode), 0, 0, NULL };
45 /* --- functions --- */
47 boxed_nodes_cmp (gconstpointer p1,
50 const BoxedNode *node1 = p1, *node2 = p2;
52 return G_BSEARCH_ARRAY_CMP (node1->type, node2->type);
56 g_boxed_type_init (void) /* sync with gtype.c */
58 static const GTypeInfo info = {
61 NULL, /* base_destroy */
62 NULL, /* class_init */
63 NULL, /* class_destroy */
64 NULL, /* class_data */
65 0, /* instance_size */
67 NULL, /* instance_init */
68 NULL, /* value_table */
70 const GTypeFundamentalInfo finfo = { G_TYPE_FLAG_DERIVABLE, };
75 type = g_type_register_fundamental (G_TYPE_BOXED, "GBoxed", &info, &finfo, G_TYPE_FLAG_ABSTRACT);
76 g_assert (type == G_TYPE_BOXED);
80 boxed_proxy_value_init (GValue *value)
82 value->data[0].v_pointer = 0;
86 boxed_proxy_value_free (GValue *value)
88 if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
92 key.type = value->g_type;
93 node = g_bsearch_array_lookup (&boxed_bsa, &key);
94 node->free (value->data[0].v_pointer);
99 boxed_proxy_value_copy (const GValue *src_value,
102 if (src_value->data[0].v_pointer)
104 BoxedNode key, *node;
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);
111 dest_value->data[0].v_pointer = src_value->data[0].v_pointer;
115 boxed_proxy_value_peek_pointer (const GValue *value)
117 return value->data[0].v_pointer;
121 boxed_proxy_collect_value (GValue *value,
122 guint n_collect_values,
123 GTypeCValue *collect_values,
126 if (!collect_values[0].v_pointer)
127 value->data[0].v_pointer = NULL;
128 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
130 value->data[0].v_pointer = collect_values[0].v_pointer;
131 value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
135 BoxedNode key, *node;
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);
146 boxed_proxy_lcopy_value (const GValue *value,
147 guint n_collect_values,
148 GTypeCValue *collect_values,
151 gpointer *boxed_p = collect_values[0].v_pointer;
154 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
156 if (!value->data[0].v_pointer)
158 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
159 *boxed_p = value->data[0].v_pointer;
162 BoxedNode key, *node;
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);
173 g_boxed_type_register_static (const gchar *name,
174 GBoxedCopyFunc boxed_copy,
175 GBoxedFreeFunc boxed_free)
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,
183 boxed_proxy_collect_value,
185 boxed_proxy_lcopy_value,
187 static const GTypeInfo type_info = {
189 NULL, /* base_init */
190 NULL, /* base_finalize */
191 NULL, /* class_init */
192 NULL, /* class_finalize */
193 NULL, /* class_data */
194 0, /* instance_size */
196 NULL, /* instance_init */
197 &vtable, /* value_table */
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);
206 type = g_type_register_static (G_TYPE_BOXED, name, &type_info, 0);
208 /* install proxy functions upon successfull registration */
214 key.copy = boxed_copy;
215 key.free = boxed_free;
216 g_bsearch_array_insert (&boxed_bsa, &key, TRUE);
223 g_boxed_copy (GType boxed_type,
224 gconstpointer src_boxed)
226 GTypeValueTable *value_table;
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);
233 value_table = g_type_value_table_peek (boxed_type);
235 g_return_val_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type), NULL);
237 /* check if our proxying implementation is used, we can short-cut here */
238 if (value_table->value_copy == boxed_proxy_value_copy)
240 BoxedNode key, *node;
242 key.type = boxed_type;
243 node = g_bsearch_array_lookup (&boxed_bsa, &key);
244 dest_boxed = node->copy ((gpointer) src_boxed);
248 GValue src_value, dest_value;
250 /* we heavil rely on the gvalue.c implementation here */
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));
264 dest_boxed = dest_value.data[0].v_pointer;
271 g_boxed_free (GType boxed_type,
274 GTypeValueTable *value_table;
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);
280 value_table = g_type_value_table_peek (boxed_type);
282 g_return_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type));
284 /* check if our proxying implementation is used, we can short-cut here */
285 if (value_table->value_free == boxed_proxy_value_free)
287 BoxedNode key, *node;
289 key.type = boxed_type;
290 node = g_bsearch_array_lookup (&boxed_bsa, &key);
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);
306 g_value_set_boxed (GValue *value,
309 g_return_if_fail (G_IS_VALUE_BOXED (value));
310 g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
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;
319 g_value_set_static_boxed (GValue *value,
322 g_return_if_fail (G_IS_VALUE_BOXED (value));
323 g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
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;
332 g_value_get_boxed (const GValue *value)
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);
337 return value->data[0].v_pointer;
341 g_value_dup_boxed (GValue *value)
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);
346 return value->data[0].v_pointer ? g_boxed_copy (G_VALUE_TYPE (value), value->data[0].v_pointer) : NULL;