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 --- */
36 gboolean is_refcounted;
40 /* --- prototypes --- */
41 static gint boxed_nodes_cmp (gconstpointer p1,
45 /* --- variables --- */
46 static GBSearchArray boxed_bsa = G_STATIC_BSEARCH_ARRAY_INIT (sizeof (BoxedNode), boxed_nodes_cmp, 0);
49 /* --- functions --- */
51 boxed_nodes_cmp (gconstpointer p1,
54 const BoxedNode *node1 = p1, *node2 = p2;
56 return G_BSEARCH_ARRAY_CMP (node1->type, node2->type);
59 static inline void /* keep this function in sync with gvalue.c */
60 value_meminit (GValue *value,
63 value->g_type = value_type;
64 memset (value->data, 0, sizeof (value->data));
68 value_copy (gpointer boxed)
70 const GValue *src_value = boxed;
71 GValue *dest_value = g_new0 (GValue, 1);
73 if (G_VALUE_TYPE (src_value))
75 g_value_init (dest_value, G_VALUE_TYPE (src_value));
76 g_value_copy (src_value, dest_value);
82 value_free (gpointer boxed)
84 GValue *value = boxed;
86 if (G_VALUE_TYPE (value))
87 g_value_unset (value);
92 value_array_init (void)
94 return g_value_array_new (0);
100 return g_string_new ("");
104 gstring_copy (gpointer boxed)
106 const GString *src_gstring = boxed;
108 return g_string_new_len (src_gstring->str, src_gstring->len);
112 gstring_free (gpointer boxed)
114 GString *gstring = boxed;
116 g_string_free (gstring, TRUE);
120 g_boxed_type_init (void) /* sync with gtype.c */
122 static const GTypeInfo info = {
124 NULL, /* base_init */
125 NULL, /* base_destroy */
126 NULL, /* class_init */
127 NULL, /* class_destroy */
128 NULL, /* class_data */
129 0, /* instance_size */
131 NULL, /* instance_init */
132 NULL, /* value_table */
134 const GTypeFundamentalInfo finfo = { G_TYPE_FLAG_DERIVABLE, };
139 type = g_type_register_fundamental (G_TYPE_BOXED, "GBoxed", &info, &finfo,
140 G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT);
141 g_assert (type == G_TYPE_BOXED);
143 /* boxed: G_TYPE_CLOSURE
145 type = g_boxed_type_register_static ("GClosure",
146 (GBoxedInitFunc) NULL,
147 (GBoxedCopyFunc) g_closure_ref,
148 (GBoxedFreeFunc) g_closure_unref,
150 g_assert (type == G_TYPE_CLOSURE);
152 /* boxed: G_TYPE_VALUE
154 type = g_boxed_type_register_static ("GValue",
155 (GBoxedInitFunc) NULL,
159 g_assert (type == G_TYPE_VALUE);
161 /* boxed: G_TYPE_VALUE_ARRAY
163 type = g_boxed_type_register_static ("GValueArray",
164 value_array_init, /* don't allow NULL values */
165 (GBoxedCopyFunc) g_value_array_copy,
166 (GBoxedFreeFunc) g_value_array_free,
168 g_assert (type == G_TYPE_VALUE_ARRAY);
170 /* boxed: G_TYPE_GSTRING
171 * yes, the naming is a bit odd, but GString is obviously not G_TYPE_STRING
173 type = g_boxed_type_register_static ("GString",
174 gstring_init, /* don't allow NULL values */
178 g_assert (type == G_TYPE_GSTRING);
182 boxed_proxy_value_init (GValue *value)
184 BoxedNode key, *node;
186 key.type = G_VALUE_TYPE (value);
187 node = g_bsearch_array_lookup (&boxed_bsa, &key);
188 value->data[0].v_pointer = node->init ? node->init () : NULL;
192 boxed_proxy_value_free (GValue *value)
194 if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
196 BoxedNode key, *node;
198 key.type = G_VALUE_TYPE (value);
199 node = g_bsearch_array_lookup (&boxed_bsa, &key);
200 node->free (value->data[0].v_pointer);
205 boxed_proxy_value_copy (const GValue *src_value,
208 if (src_value->data[0].v_pointer)
210 BoxedNode key, *node;
212 key.type = G_VALUE_TYPE (src_value);
213 node = g_bsearch_array_lookup (&boxed_bsa, &key);
214 dest_value->data[0].v_pointer = node->copy (src_value->data[0].v_pointer);
217 dest_value->data[0].v_pointer = src_value->data[0].v_pointer;
221 boxed_proxy_value_peek_pointer (const GValue *value)
223 return value->data[0].v_pointer;
227 boxed_proxy_collect_value (GValue *value,
228 guint n_collect_values,
229 GTypeCValue *collect_values,
232 BoxedNode key, *node;
234 key.type = G_VALUE_TYPE (value);
235 node = g_bsearch_array_lookup (&boxed_bsa, &key);
237 /* for NULL values, we have to call GBoxedInitFunc */
238 if (!collect_values[0].v_pointer)
239 value->data[0].v_pointer = node->init ? node->init () : NULL;
242 /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
243 if (!node->is_refcounted && (collect_flags & G_VALUE_NOCOPY_CONTENTS))
245 value->data[0].v_pointer = collect_values[0].v_pointer;
246 value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
249 value->data[0].v_pointer = node->copy (collect_values[0].v_pointer);
256 boxed_proxy_lcopy_value (const GValue *value,
257 guint n_collect_values,
258 GTypeCValue *collect_values,
261 gpointer *boxed_p = collect_values[0].v_pointer;
264 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
266 if (!value->data[0].v_pointer)
268 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
269 *boxed_p = value->data[0].v_pointer;
272 BoxedNode key, *node;
274 key.type = G_VALUE_TYPE (value);
275 node = g_bsearch_array_lookup (&boxed_bsa, &key);
276 *boxed_p = node->copy (value->data[0].v_pointer);
283 g_boxed_type_register_static (const gchar *name,
284 GBoxedInitFunc boxed_init,
285 GBoxedCopyFunc boxed_copy,
286 GBoxedFreeFunc boxed_free,
287 gboolean is_refcounted)
289 static const GTypeValueTable vtable = {
290 boxed_proxy_value_init,
291 boxed_proxy_value_free,
292 boxed_proxy_value_copy,
293 boxed_proxy_value_peek_pointer,
295 boxed_proxy_collect_value,
297 boxed_proxy_lcopy_value,
299 static const GTypeInfo type_info = {
301 NULL, /* base_init */
302 NULL, /* base_finalize */
303 NULL, /* class_init */
304 NULL, /* class_finalize */
305 NULL, /* class_data */
306 0, /* instance_size */
308 NULL, /* instance_init */
309 &vtable, /* value_table */
313 g_return_val_if_fail (name != NULL, 0);
314 g_return_val_if_fail (boxed_copy != NULL, 0);
315 g_return_val_if_fail (boxed_free != NULL, 0);
316 g_return_val_if_fail (g_type_from_name (name) == 0, 0);
318 type = g_type_register_static (G_TYPE_BOXED, name, &type_info, 0);
320 /* install proxy functions upon successfull registration */
326 key.init = boxed_init;
327 key.copy = boxed_copy;
328 key.free = boxed_free;
329 key.is_refcounted = is_refcounted != FALSE;
330 g_bsearch_array_insert (&boxed_bsa, &key, TRUE);
337 g_boxed_copy (GType boxed_type,
338 gconstpointer src_boxed)
340 GTypeValueTable *value_table;
343 g_return_val_if_fail (G_TYPE_IS_BOXED (boxed_type), NULL);
344 g_return_val_if_fail (G_TYPE_IS_ABSTRACT (boxed_type) == FALSE, NULL);
345 g_return_val_if_fail (src_boxed != NULL, NULL);
347 value_table = g_type_value_table_peek (boxed_type);
349 g_return_val_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type), NULL);
351 /* check if our proxying implementation is used, we can short-cut here */
352 if (value_table->value_copy == boxed_proxy_value_copy)
354 BoxedNode key, *node;
356 key.type = boxed_type;
357 node = g_bsearch_array_lookup (&boxed_bsa, &key);
358 dest_boxed = node->copy ((gpointer) src_boxed);
362 GValue src_value, dest_value;
364 /* we heavil rely on third-party boxed type value vtable
365 * implementations to follow normal boxed value storage
366 * (data[0].v_pointer is the boxed struct, and
367 * data[1].v_uint holds the G_VALUE_NOCOPY_CONTENTS flag,
369 * but then, we can expect that since we layed out the
371 * data[1].v_uint&G_VALUE_NOCOPY_CONTENTS shouldn't be set
374 /* equiv. to g_value_set_static_boxed() */
375 value_meminit (&src_value, boxed_type);
376 src_value.data[0].v_pointer = (gpointer) src_boxed;
377 src_value.data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
379 /* call third-party code copy fucntion, fingers-crossed */
380 value_meminit (&dest_value, boxed_type);
381 value_table->value_copy (&src_value, &dest_value);
383 /* double check and grouse if things went wrong */
384 if (dest_value.data[1].v_ulong ||
385 dest_value.data[2].v_ulong ||
386 dest_value.data[3].v_ulong)
387 g_warning ("the copy_value() implementation of type `%s' seems to make use of reserved GValue fields",
388 g_type_name (boxed_type));
390 dest_boxed = dest_value.data[0].v_pointer;
397 g_boxed_free (GType boxed_type,
400 GTypeValueTable *value_table;
402 g_return_if_fail (G_TYPE_IS_BOXED (boxed_type));
403 g_return_if_fail (G_TYPE_IS_ABSTRACT (boxed_type) == FALSE);
404 g_return_if_fail (boxed != NULL);
406 value_table = g_type_value_table_peek (boxed_type);
408 g_return_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type));
410 /* check if our proxying implementation is used, we can short-cut here */
411 if (value_table->value_free == boxed_proxy_value_free)
413 BoxedNode key, *node;
415 key.type = boxed_type;
416 node = g_bsearch_array_lookup (&boxed_bsa, &key);
423 /* see g_boxed_copy() on why we think we can do this */
424 value_meminit (&value, boxed_type);
425 value.data[0].v_pointer = boxed;
426 value_table->value_free (&value);
431 g_value_get_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;
440 g_value_dup_boxed (const GValue *value)
442 g_return_val_if_fail (G_VALUE_HOLDS_BOXED (value), NULL);
443 g_return_val_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)), NULL);
445 return value->data[0].v_pointer ? g_boxed_copy (G_VALUE_TYPE (value), value->data[0].v_pointer) : NULL;
449 value_set_boxed_internal (GValue *value,
450 gconstpointer const_boxed,
454 BoxedNode key, *node;
455 gpointer boxed = (gpointer) const_boxed;
459 /* just resetting to NULL might not be desired, need to
460 * have value reinitialized also (for values defaulting
461 * to other default value states than a NULL data pointer),
462 * g_value_reset() will handle this
464 g_value_reset (value);
468 key.type = G_VALUE_TYPE (value);
469 node = g_bsearch_array_lookup (&boxed_bsa, &key);
473 /* we proxy this type, free contents and copy right away */
474 if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
475 node->free (value->data[0].v_pointer);
476 value->data[1].v_uint = need_free ? 0 : G_VALUE_NOCOPY_CONTENTS;
477 value->data[0].v_pointer = need_copy ? node->copy (boxed) : boxed;
481 /* we don't handle this type, free contents and let g_boxed_copy()
482 * figure what's required
484 if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
485 g_boxed_free (G_VALUE_TYPE (value), value->data[0].v_pointer);
486 value->data[1].v_uint = need_free ? 0 : G_VALUE_NOCOPY_CONTENTS;
487 value->data[0].v_pointer = need_copy ? g_boxed_copy (G_VALUE_TYPE (value), boxed) : boxed;
492 g_value_set_boxed (GValue *value,
495 g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
496 g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
498 value_set_boxed_internal (value, boxed, TRUE, TRUE);
502 g_value_set_static_boxed (GValue *value,
505 g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
506 g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
508 value_set_boxed_internal (value, boxed, FALSE, FALSE);
512 g_value_set_boxed_take_ownership (GValue *value,
515 g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
516 g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
518 value_set_boxed_internal (value, boxed, FALSE, TRUE);