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);
131 g_closure_get_type (void)
133 static GType type_id = 0;
136 type_id = g_boxed_type_register_static ("GClosure",
137 (GBoxedCopyFunc) g_closure_ref,
138 (GBoxedFreeFunc) g_closure_unref);
143 g_value_get_type (void)
145 static GType type_id = 0;
148 type_id = g_boxed_type_register_static ("GValue",
155 g_value_array_get_type (void)
157 static GType type_id = 0;
160 type_id = g_boxed_type_register_static ("GValueArray",
161 (GBoxedCopyFunc) g_value_array_copy,
162 (GBoxedFreeFunc) g_value_array_free);
167 g_gstring_get_type (void)
169 static GType type_id = 0;
172 type_id = g_boxed_type_register_static ("GString", /* the naming is a bit odd, but GString is obviously not G_TYPE_STRING */
179 boxed_proxy_value_init (GValue *value)
181 BoxedNode key, *node;
183 key.type = G_VALUE_TYPE (value);
184 node = g_bsearch_array_lookup (&boxed_bsa, &key);
185 value->data[0].v_pointer = NULL;
189 boxed_proxy_value_free (GValue *value)
191 if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
193 BoxedNode key, *node;
195 key.type = G_VALUE_TYPE (value);
196 node = g_bsearch_array_lookup (&boxed_bsa, &key);
197 node->free (value->data[0].v_pointer);
202 boxed_proxy_value_copy (const GValue *src_value,
205 if (src_value->data[0].v_pointer)
207 BoxedNode key, *node;
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);
214 dest_value->data[0].v_pointer = src_value->data[0].v_pointer;
218 boxed_proxy_value_peek_pointer (const GValue *value)
220 return value->data[0].v_pointer;
224 boxed_proxy_collect_value (GValue *value,
225 guint n_collect_values,
226 GTypeCValue *collect_values,
229 BoxedNode key, *node;
231 key.type = G_VALUE_TYPE (value);
232 node = g_bsearch_array_lookup (&boxed_bsa, &key);
234 if (!collect_values[0].v_pointer)
235 value->data[0].v_pointer = NULL;
238 if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
240 value->data[0].v_pointer = collect_values[0].v_pointer;
241 value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
244 value->data[0].v_pointer = node->copy (collect_values[0].v_pointer);
251 boxed_proxy_lcopy_value (const GValue *value,
252 guint n_collect_values,
253 GTypeCValue *collect_values,
256 gpointer *boxed_p = collect_values[0].v_pointer;
259 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
261 if (!value->data[0].v_pointer)
263 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
264 *boxed_p = value->data[0].v_pointer;
267 BoxedNode key, *node;
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);
278 g_boxed_type_register_static (const gchar *name,
279 GBoxedCopyFunc boxed_copy,
280 GBoxedFreeFunc boxed_free)
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,
288 boxed_proxy_collect_value,
290 boxed_proxy_lcopy_value,
292 static const GTypeInfo type_info = {
294 NULL, /* base_init */
295 NULL, /* base_finalize */
296 NULL, /* class_init */
297 NULL, /* class_finalize */
298 NULL, /* class_data */
299 0, /* instance_size */
301 NULL, /* instance_init */
302 &vtable, /* value_table */
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);
311 type = g_type_register_static (G_TYPE_BOXED, name, &type_info, 0);
313 /* install proxy functions upon successfull registration */
319 key.copy = boxed_copy;
320 key.free = boxed_free;
321 g_bsearch_array_insert (&boxed_bsa, &key, TRUE);
328 g_boxed_copy (GType boxed_type,
329 gconstpointer src_boxed)
331 GTypeValueTable *value_table;
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);
338 value_table = g_type_value_table_peek (boxed_type);
340 g_return_val_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type), NULL);
342 /* check if our proxying implementation is used, we can short-cut here */
343 if (value_table->value_copy == boxed_proxy_value_copy)
345 BoxedNode key, *node;
347 key.type = boxed_type;
348 node = g_bsearch_array_lookup (&boxed_bsa, &key);
349 dest_boxed = node->copy ((gpointer) src_boxed);
353 GValue src_value, dest_value;
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,
360 * but then, we can expect that since we layed out the
362 * data[1].v_uint&G_VALUE_NOCOPY_CONTENTS shouldn't be set
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;
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);
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));
381 dest_boxed = dest_value.data[0].v_pointer;
388 g_boxed_free (GType boxed_type,
391 GTypeValueTable *value_table;
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);
397 value_table = g_type_value_table_peek (boxed_type);
399 g_return_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type));
401 /* check if our proxying implementation is used, we can short-cut here */
402 if (value_table->value_free == boxed_proxy_value_free)
404 BoxedNode key, *node;
406 key.type = boxed_type;
407 node = g_bsearch_array_lookup (&boxed_bsa, &key);
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);
422 g_value_get_boxed (const GValue *value)
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);
427 return value->data[0].v_pointer;
431 g_value_dup_boxed (const GValue *value)
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);
436 return value->data[0].v_pointer ? g_boxed_copy (G_VALUE_TYPE (value), value->data[0].v_pointer) : NULL;
440 value_set_boxed_internal (GValue *value,
441 gconstpointer const_boxed,
445 BoxedNode key, *node;
446 gpointer boxed = (gpointer) const_boxed;
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
455 g_value_reset (value);
459 key.type = G_VALUE_TYPE (value);
460 node = g_bsearch_array_lookup (&boxed_bsa, &key);
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;
472 /* we don't handle this type, free contents and let g_boxed_copy()
473 * figure what's required
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;
483 g_value_set_boxed (GValue *value,
486 g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
487 g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
489 value_set_boxed_internal (value, boxed, TRUE, TRUE);
493 g_value_set_static_boxed (GValue *value,
496 g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
497 g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
499 value_set_boxed_internal (value, boxed, FALSE, FALSE);
503 g_value_set_boxed_take_ownership (GValue *value,
506 g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
507 g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
509 value_set_boxed_internal (value, boxed, FALSE, TRUE);