1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2000-2001 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 "gvaluearray.h"
25 #include "gvaluecollector.h"
29 /* --- typedefs & structures --- */
38 /* --- prototypes --- */
39 static gint boxed_nodes_cmp (gconstpointer p1,
43 /* --- variables --- */
44 static GBSearchArray boxed_bsa = G_STATIC_BSEARCH_ARRAY_INIT (sizeof (BoxedNode), boxed_nodes_cmp, 0);
47 /* --- functions --- */
49 boxed_nodes_cmp (gconstpointer p1,
52 const BoxedNode *node1 = p1, *node2 = p2;
54 return G_BSEARCH_ARRAY_CMP (node1->type, node2->type);
57 static inline void /* keep this function in sync with gvalue.c */
58 value_meminit (GValue *value,
61 value->g_type = value_type;
62 memset (value->data, 0, sizeof (value->data));
66 value_copy (gpointer boxed)
68 const GValue *src_value = boxed;
69 GValue *dest_value = g_new0 (GValue, 1);
71 if (G_VALUE_TYPE (src_value))
73 g_value_init (dest_value, G_VALUE_TYPE (src_value));
74 g_value_copy (src_value, dest_value);
80 value_free (gpointer boxed)
82 GValue *value = boxed;
84 if (G_VALUE_TYPE (value))
85 g_value_unset (value);
90 gstring_copy (gpointer boxed)
92 const GString *src_gstring = boxed;
94 return g_string_new_len (src_gstring->str, src_gstring->len);
98 gstring_free (gpointer boxed)
100 GString *gstring = boxed;
102 g_string_free (gstring, TRUE);
106 g_boxed_type_init (void) /* sync with gtype.c */
108 static const GTypeInfo info = {
110 NULL, /* base_init */
111 NULL, /* base_destroy */
112 NULL, /* class_init */
113 NULL, /* class_destroy */
114 NULL, /* class_data */
115 0, /* instance_size */
117 NULL, /* instance_init */
118 NULL, /* value_table */
120 const GTypeFundamentalInfo finfo = { G_TYPE_FLAG_DERIVABLE, };
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);
129 /* boxed: G_TYPE_CLOSURE
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);
136 /* boxed: G_TYPE_VALUE
138 type = g_boxed_type_register_static ("GValue",
141 g_assert (type == G_TYPE_VALUE);
143 /* boxed: G_TYPE_VALUE_ARRAY
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);
150 /* boxed: G_TYPE_GSTRING
151 * yes, the naming is a bit odd, but GString is obviously not G_TYPE_STRING
153 type = g_boxed_type_register_static ("GString",
156 g_assert (type == G_TYPE_GSTRING);
160 boxed_proxy_value_init (GValue *value)
162 BoxedNode key, *node;
164 key.type = G_VALUE_TYPE (value);
165 node = g_bsearch_array_lookup (&boxed_bsa, &key);
166 value->data[0].v_pointer = NULL;
170 boxed_proxy_value_free (GValue *value)
172 if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
174 BoxedNode key, *node;
176 key.type = G_VALUE_TYPE (value);
177 node = g_bsearch_array_lookup (&boxed_bsa, &key);
178 node->free (value->data[0].v_pointer);
183 boxed_proxy_value_copy (const GValue *src_value,
186 if (src_value->data[0].v_pointer)
188 BoxedNode key, *node;
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);
195 dest_value->data[0].v_pointer = src_value->data[0].v_pointer;
199 boxed_proxy_value_peek_pointer (const GValue *value)
201 return value->data[0].v_pointer;
205 boxed_proxy_collect_value (GValue *value,
206 guint n_collect_values,
207 GTypeCValue *collect_values,
210 BoxedNode key, *node;
212 key.type = G_VALUE_TYPE (value);
213 node = g_bsearch_array_lookup (&boxed_bsa, &key);
215 if (!collect_values[0].v_pointer)
216 value->data[0].v_pointer = NULL;
219 if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
221 value->data[0].v_pointer = collect_values[0].v_pointer;
222 value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
225 value->data[0].v_pointer = node->copy (collect_values[0].v_pointer);
232 boxed_proxy_lcopy_value (const GValue *value,
233 guint n_collect_values,
234 GTypeCValue *collect_values,
237 gpointer *boxed_p = collect_values[0].v_pointer;
240 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
242 if (!value->data[0].v_pointer)
244 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
245 *boxed_p = value->data[0].v_pointer;
248 BoxedNode key, *node;
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);
259 g_boxed_type_register_static (const gchar *name,
260 GBoxedCopyFunc boxed_copy,
261 GBoxedFreeFunc boxed_free)
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,
269 boxed_proxy_collect_value,
271 boxed_proxy_lcopy_value,
273 static const GTypeInfo type_info = {
275 NULL, /* base_init */
276 NULL, /* base_finalize */
277 NULL, /* class_init */
278 NULL, /* class_finalize */
279 NULL, /* class_data */
280 0, /* instance_size */
282 NULL, /* instance_init */
283 &vtable, /* value_table */
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);
292 type = g_type_register_static (G_TYPE_BOXED, name, &type_info, 0);
294 /* install proxy functions upon successfull registration */
300 key.copy = boxed_copy;
301 key.free = boxed_free;
302 g_bsearch_array_insert (&boxed_bsa, &key, TRUE);
309 g_boxed_copy (GType boxed_type,
310 gconstpointer src_boxed)
312 GTypeValueTable *value_table;
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);
319 value_table = g_type_value_table_peek (boxed_type);
321 g_return_val_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type), NULL);
323 /* check if our proxying implementation is used, we can short-cut here */
324 if (value_table->value_copy == boxed_proxy_value_copy)
326 BoxedNode key, *node;
328 key.type = boxed_type;
329 node = g_bsearch_array_lookup (&boxed_bsa, &key);
330 dest_boxed = node->copy ((gpointer) src_boxed);
334 GValue src_value, dest_value;
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,
341 * but then, we can expect that since we layed out the
343 * data[1].v_uint&G_VALUE_NOCOPY_CONTENTS shouldn't be set
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;
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);
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));
362 dest_boxed = dest_value.data[0].v_pointer;
369 g_boxed_free (GType boxed_type,
372 GTypeValueTable *value_table;
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);
378 value_table = g_type_value_table_peek (boxed_type);
380 g_return_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type));
382 /* check if our proxying implementation is used, we can short-cut here */
383 if (value_table->value_free == boxed_proxy_value_free)
385 BoxedNode key, *node;
387 key.type = boxed_type;
388 node = g_bsearch_array_lookup (&boxed_bsa, &key);
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);
403 g_value_get_boxed (const GValue *value)
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);
408 return value->data[0].v_pointer;
412 g_value_dup_boxed (const GValue *value)
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);
417 return value->data[0].v_pointer ? g_boxed_copy (G_VALUE_TYPE (value), value->data[0].v_pointer) : NULL;
421 value_set_boxed_internal (GValue *value,
422 gconstpointer const_boxed,
426 BoxedNode key, *node;
427 gpointer boxed = (gpointer) const_boxed;
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
436 g_value_reset (value);
440 key.type = G_VALUE_TYPE (value);
441 node = g_bsearch_array_lookup (&boxed_bsa, &key);
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;
453 /* we don't handle this type, free contents and let g_boxed_copy()
454 * figure what's required
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;
464 g_value_set_boxed (GValue *value,
467 g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
468 g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
470 value_set_boxed_internal (value, boxed, TRUE, TRUE);
474 g_value_set_static_boxed (GValue *value,
477 g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
478 g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
480 value_set_boxed_internal (value, boxed, FALSE, FALSE);
484 g_value_set_boxed_take_ownership (GValue *value,
487 g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
488 g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
490 value_set_boxed_internal (value, boxed, FALSE, TRUE);