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 = NULL;
45 static const GBSearchConfig boxed_bconfig = {
52 /* --- functions --- */
54 boxed_nodes_cmp (gconstpointer p1,
57 const BoxedNode *node1 = p1, *node2 = p2;
59 return G_BSEARCH_ARRAY_CMP (node1->type, node2->type);
62 static inline void /* keep this function in sync with gvalue.c */
63 value_meminit (GValue *value,
66 value->g_type = value_type;
67 memset (value->data, 0, sizeof (value->data));
71 value_copy (gpointer boxed)
73 const GValue *src_value = boxed;
74 GValue *dest_value = g_new0 (GValue, 1);
76 if (G_VALUE_TYPE (src_value))
78 g_value_init (dest_value, G_VALUE_TYPE (src_value));
79 g_value_copy (src_value, dest_value);
85 value_free (gpointer boxed)
87 GValue *value = boxed;
89 if (G_VALUE_TYPE (value))
90 g_value_unset (value);
95 gstring_copy (gpointer boxed)
97 const GString *src_gstring = boxed;
99 return g_string_new_len (src_gstring->str, src_gstring->len);
103 gstring_free (gpointer boxed)
105 GString *gstring = boxed;
107 g_string_free (gstring, TRUE);
111 g_boxed_type_init (void) /* sync with gtype.c */
113 static const GTypeInfo info = {
115 NULL, /* base_init */
116 NULL, /* base_destroy */
117 NULL, /* class_init */
118 NULL, /* class_destroy */
119 NULL, /* class_data */
120 0, /* instance_size */
122 NULL, /* instance_init */
123 NULL, /* value_table */
125 const GTypeFundamentalInfo finfo = { G_TYPE_FLAG_DERIVABLE, };
128 boxed_bsa = g_bsearch_array_create (&boxed_bconfig);
132 type = g_type_register_fundamental (G_TYPE_BOXED, "GBoxed", &info, &finfo,
133 G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT);
134 g_assert (type == G_TYPE_BOXED);
138 g_closure_get_type (void)
140 static GType type_id = 0;
143 type_id = g_boxed_type_register_static ("GClosure",
144 (GBoxedCopyFunc) g_closure_ref,
145 (GBoxedFreeFunc) g_closure_unref);
150 g_value_get_type (void)
152 static GType type_id = 0;
155 type_id = g_boxed_type_register_static ("GValue",
162 g_value_array_get_type (void)
164 static GType type_id = 0;
167 type_id = g_boxed_type_register_static ("GValueArray",
168 (GBoxedCopyFunc) g_value_array_copy,
169 (GBoxedFreeFunc) g_value_array_free);
174 g_strv_get_type (void)
176 static GType type_id = 0;
179 type_id = g_boxed_type_register_static ("GStrv",
180 (GBoxedCopyFunc) g_strdupv,
181 (GBoxedFreeFunc) g_strfreev);
186 g_gstring_get_type (void)
188 static GType type_id = 0;
191 type_id = g_boxed_type_register_static ("GString", /* the naming is a bit odd, but GString is obviously not G_TYPE_STRING */
198 boxed_proxy_value_init (GValue *value)
200 value->data[0].v_pointer = NULL;
204 boxed_proxy_value_free (GValue *value)
206 if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
208 BoxedNode key, *node;
210 key.type = G_VALUE_TYPE (value);
211 node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
212 node->free (value->data[0].v_pointer);
217 boxed_proxy_value_copy (const GValue *src_value,
220 if (src_value->data[0].v_pointer)
222 BoxedNode key, *node;
224 key.type = G_VALUE_TYPE (src_value);
225 node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
226 dest_value->data[0].v_pointer = node->copy (src_value->data[0].v_pointer);
229 dest_value->data[0].v_pointer = src_value->data[0].v_pointer;
233 boxed_proxy_value_peek_pointer (const GValue *value)
235 return value->data[0].v_pointer;
239 boxed_proxy_collect_value (GValue *value,
240 guint n_collect_values,
241 GTypeCValue *collect_values,
244 BoxedNode key, *node;
246 key.type = G_VALUE_TYPE (value);
247 node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
249 if (!collect_values[0].v_pointer)
250 value->data[0].v_pointer = NULL;
253 if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
255 value->data[0].v_pointer = collect_values[0].v_pointer;
256 value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
259 value->data[0].v_pointer = node->copy (collect_values[0].v_pointer);
266 boxed_proxy_lcopy_value (const GValue *value,
267 guint n_collect_values,
268 GTypeCValue *collect_values,
271 gpointer *boxed_p = collect_values[0].v_pointer;
274 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
276 if (!value->data[0].v_pointer)
278 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
279 *boxed_p = value->data[0].v_pointer;
282 BoxedNode key, *node;
284 key.type = G_VALUE_TYPE (value);
285 node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
286 *boxed_p = node->copy (value->data[0].v_pointer);
293 g_boxed_type_register_static (const gchar *name,
294 GBoxedCopyFunc boxed_copy,
295 GBoxedFreeFunc boxed_free)
297 static const GTypeValueTable vtable = {
298 boxed_proxy_value_init,
299 boxed_proxy_value_free,
300 boxed_proxy_value_copy,
301 boxed_proxy_value_peek_pointer,
303 boxed_proxy_collect_value,
305 boxed_proxy_lcopy_value,
307 static const GTypeInfo type_info = {
309 NULL, /* base_init */
310 NULL, /* base_finalize */
311 NULL, /* class_init */
312 NULL, /* class_finalize */
313 NULL, /* class_data */
314 0, /* instance_size */
316 NULL, /* instance_init */
317 &vtable, /* value_table */
321 g_return_val_if_fail (name != NULL, 0);
322 g_return_val_if_fail (boxed_copy != NULL, 0);
323 g_return_val_if_fail (boxed_free != NULL, 0);
324 g_return_val_if_fail (g_type_from_name (name) == 0, 0);
326 type = g_type_register_static (G_TYPE_BOXED, name, &type_info, 0);
328 /* install proxy functions upon successfull registration */
334 key.copy = boxed_copy;
335 key.free = boxed_free;
336 boxed_bsa = g_bsearch_array_insert (boxed_bsa, &boxed_bconfig, &key);
343 g_boxed_copy (GType boxed_type,
344 gconstpointer src_boxed)
346 GTypeValueTable *value_table;
349 g_return_val_if_fail (G_TYPE_IS_BOXED (boxed_type), NULL);
350 g_return_val_if_fail (G_TYPE_IS_ABSTRACT (boxed_type) == FALSE, NULL);
351 g_return_val_if_fail (src_boxed != NULL, NULL);
353 value_table = g_type_value_table_peek (boxed_type);
355 g_return_val_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type), NULL);
357 /* check if our proxying implementation is used, we can short-cut here */
358 if (value_table->value_copy == boxed_proxy_value_copy)
360 BoxedNode key, *node;
362 key.type = boxed_type;
363 node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
364 dest_boxed = node->copy ((gpointer) src_boxed);
368 GValue src_value, dest_value;
370 /* we heavily rely on third-party boxed type value vtable
371 * implementations to follow normal boxed value storage
372 * (data[0].v_pointer is the boxed struct, and
373 * data[1].v_uint holds the G_VALUE_NOCOPY_CONTENTS flag,
375 * but then, we can expect that since we layed out the
377 * data[1].v_uint&G_VALUE_NOCOPY_CONTENTS shouldn't be set
380 /* equiv. to g_value_set_static_boxed() */
381 value_meminit (&src_value, boxed_type);
382 src_value.data[0].v_pointer = (gpointer) src_boxed;
383 src_value.data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
385 /* call third-party code copy function, fingers-crossed */
386 value_meminit (&dest_value, boxed_type);
387 value_table->value_copy (&src_value, &dest_value);
389 /* double check and grouse if things went wrong */
390 if (dest_value.data[1].v_ulong)
391 g_warning ("the copy_value() implementation of type `%s' seems to make use of reserved GValue fields",
392 g_type_name (boxed_type));
394 dest_boxed = dest_value.data[0].v_pointer;
401 g_boxed_free (GType boxed_type,
404 GTypeValueTable *value_table;
406 g_return_if_fail (G_TYPE_IS_BOXED (boxed_type));
407 g_return_if_fail (G_TYPE_IS_ABSTRACT (boxed_type) == FALSE);
408 g_return_if_fail (boxed != NULL);
410 value_table = g_type_value_table_peek (boxed_type);
412 g_return_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type));
414 /* check if our proxying implementation is used, we can short-cut here */
415 if (value_table->value_free == boxed_proxy_value_free)
417 BoxedNode key, *node;
419 key.type = boxed_type;
420 node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
427 /* see g_boxed_copy() on why we think we can do this */
428 value_meminit (&value, boxed_type);
429 value.data[0].v_pointer = boxed;
430 value_table->value_free (&value);
435 g_value_get_boxed (const GValue *value)
437 g_return_val_if_fail (G_VALUE_HOLDS_BOXED (value), NULL);
438 g_return_val_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)), NULL);
440 return value->data[0].v_pointer;
444 g_value_dup_boxed (const GValue *value)
446 g_return_val_if_fail (G_VALUE_HOLDS_BOXED (value), NULL);
447 g_return_val_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)), NULL);
449 return value->data[0].v_pointer ? g_boxed_copy (G_VALUE_TYPE (value), value->data[0].v_pointer) : NULL;
453 value_set_boxed_internal (GValue *value,
454 gconstpointer const_boxed,
458 BoxedNode key, *node;
459 gpointer boxed = (gpointer) const_boxed;
463 /* just resetting to NULL might not be desired, need to
464 * have value reinitialized also (for values defaulting
465 * to other default value states than a NULL data pointer),
466 * g_value_reset() will handle this
468 g_value_reset (value);
472 key.type = G_VALUE_TYPE (value);
473 node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
477 /* we proxy this type, free contents and copy right away */
478 if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
479 node->free (value->data[0].v_pointer);
480 value->data[1].v_uint = need_free ? 0 : G_VALUE_NOCOPY_CONTENTS;
481 value->data[0].v_pointer = need_copy ? node->copy (boxed) : boxed;
485 /* we don't handle this type, free contents and let g_boxed_copy()
486 * figure what's required
488 if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
489 g_boxed_free (G_VALUE_TYPE (value), value->data[0].v_pointer);
490 value->data[1].v_uint = need_free ? 0 : G_VALUE_NOCOPY_CONTENTS;
491 value->data[0].v_pointer = need_copy ? g_boxed_copy (G_VALUE_TYPE (value), boxed) : boxed;
496 g_value_set_boxed (GValue *value,
499 g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
500 g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
502 value_set_boxed_internal (value, boxed, TRUE, TRUE);
506 g_value_set_static_boxed (GValue *value,
509 g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
510 g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
512 value_set_boxed_internal (value, boxed, FALSE, FALSE);
516 g_value_set_boxed_take_ownership (GValue *value,
519 g_value_take_boxed (value, boxed);
523 g_value_take_boxed (GValue *value,
526 g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
527 g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
529 value_set_boxed_internal (value, boxed, FALSE, TRUE);