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.
25 #include "gbsearcharray.h"
27 #include "gvaluearray.h"
29 #include "gvaluecollector.h"
30 #include "gobjectalias.h"
35 * @short_description: A mechanism to wrap opaque C structures registered
37 * @see_also: #GParamSpecBoxed, g_param_spec_boxed()
40 * GBoxed is a generic wrapper mechanism for arbitrary C structures. The only
41 * thing the type system needs to know about the structures is how to copy and
42 * free them, beyond that they are treated as opaque chunks of memory.
44 * Boxed types are useful for simple value-holder structures like rectangles or
45 * points. They can also be used for wrapping structures defined in non-GObject
49 /* --- typedefs & structures --- */
58 /* --- prototypes --- */
59 static gint boxed_nodes_cmp (gconstpointer p1,
63 /* --- variables --- */
64 static GBSearchArray *boxed_bsa = NULL;
65 static const GBSearchConfig boxed_bconfig = {
72 /* --- functions --- */
74 boxed_nodes_cmp (gconstpointer p1,
77 const BoxedNode *node1 = p1, *node2 = p2;
79 return G_BSEARCH_ARRAY_CMP (node1->type, node2->type);
82 static inline void /* keep this function in sync with gvalue.c */
83 value_meminit (GValue *value,
86 value->g_type = value_type;
87 memset (value->data, 0, sizeof (value->data));
91 value_copy (gpointer boxed)
93 const GValue *src_value = boxed;
94 GValue *dest_value = g_new0 (GValue, 1);
96 if (G_VALUE_TYPE (src_value))
98 g_value_init (dest_value, G_VALUE_TYPE (src_value));
99 g_value_copy (src_value, dest_value);
105 value_free (gpointer boxed)
107 GValue *value = boxed;
109 if (G_VALUE_TYPE (value))
110 g_value_unset (value);
115 g_boxed_type_init (void)
117 static const GTypeInfo info = {
119 NULL, /* base_init */
120 NULL, /* base_destroy */
121 NULL, /* class_init */
122 NULL, /* class_destroy */
123 NULL, /* class_data */
124 0, /* instance_size */
126 NULL, /* instance_init */
127 NULL, /* value_table */
129 const GTypeFundamentalInfo finfo = { G_TYPE_FLAG_DERIVABLE, };
132 boxed_bsa = g_bsearch_array_create (&boxed_bconfig);
136 type = g_type_register_fundamental (G_TYPE_BOXED, g_intern_static_string ("GBoxed"), &info, &finfo,
137 G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT);
138 g_assert (type == G_TYPE_BOXED);
142 g_closure_get_type (void)
144 static GType type_id = 0;
147 type_id = g_boxed_type_register_static (g_intern_static_string ("GClosure"),
148 (GBoxedCopyFunc) g_closure_ref,
149 (GBoxedFreeFunc) g_closure_unref);
154 g_value_get_type (void)
156 static GType type_id = 0;
159 type_id = g_boxed_type_register_static (g_intern_static_string ("GValue"),
166 g_value_array_get_type (void)
168 static GType type_id = 0;
171 type_id = g_boxed_type_register_static (g_intern_static_string ("GValueArray"),
172 (GBoxedCopyFunc) g_value_array_copy,
173 (GBoxedFreeFunc) g_value_array_free);
178 gdate_copy (gpointer boxed)
180 const GDate *date = (const GDate*) boxed;
182 return g_date_new_julian (g_date_get_julian (date));
186 g_date_get_type (void)
188 static GType type_id = 0;
191 type_id = g_boxed_type_register_static (g_intern_static_string ("GDate"),
192 (GBoxedCopyFunc) gdate_copy,
193 (GBoxedFreeFunc) g_date_free);
198 g_strv_get_type (void)
200 static GType type_id = 0;
203 type_id = g_boxed_type_register_static (g_intern_static_string ("GStrv"),
204 (GBoxedCopyFunc) g_strdupv,
205 (GBoxedFreeFunc) g_strfreev);
210 gstring_copy (gpointer boxed)
212 const GString *src_gstring = boxed;
214 return g_string_new_len (src_gstring->str, src_gstring->len);
218 gstring_free (gpointer boxed)
220 GString *gstring = boxed;
222 g_string_free (gstring, TRUE);
226 g_gstring_get_type (void)
228 static GType type_id = 0;
231 type_id = g_boxed_type_register_static (g_intern_static_string ("GString"),
232 /* the naming is a bit odd, but GString is obviously not G_TYPE_STRING */
239 hash_table_copy (gpointer boxed)
241 GHashTable *hash_table = boxed;
242 return g_hash_table_ref (hash_table);
246 hash_table_free (gpointer boxed)
248 GHashTable *hash_table = boxed;
249 g_hash_table_unref (hash_table);
253 g_hash_table_get_type (void)
255 static GType type_id = 0;
257 type_id = g_boxed_type_register_static (g_intern_static_string ("GHashTable"),
258 hash_table_copy, hash_table_free);
263 g_regex_get_type (void)
265 static GType type_id = 0;
269 type_id = g_boxed_type_register_static (g_intern_static_string ("GRegex"),
270 (GBoxedCopyFunc) g_regex_ref,
271 (GBoxedFreeFunc) g_regex_unref);
278 g_array_get_type (void)
280 static GType type_id = 0;
282 type_id = g_boxed_type_register_static (g_intern_static_string ("GArray"),
283 (GBoxedCopyFunc) g_array_ref,
284 (GBoxedFreeFunc) g_array_unref);
289 g_ptr_array_get_type (void)
291 static GType type_id = 0;
293 type_id = g_boxed_type_register_static (g_intern_static_string ("GPtrArray"),
294 (GBoxedCopyFunc) g_ptr_array_ref,
295 (GBoxedFreeFunc) g_ptr_array_unref);
300 g_byte_array_get_type (void)
302 static GType type_id = 0;
304 type_id = g_boxed_type_register_static (g_intern_static_string ("GByteArray"),
305 (GBoxedCopyFunc) g_byte_array_ref,
306 (GBoxedFreeFunc) g_byte_array_unref);
311 boxed_proxy_value_init (GValue *value)
313 value->data[0].v_pointer = NULL;
317 boxed_proxy_value_free (GValue *value)
319 if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
321 BoxedNode key, *node;
323 key.type = G_VALUE_TYPE (value);
324 node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
325 node->free (value->data[0].v_pointer);
330 boxed_proxy_value_copy (const GValue *src_value,
333 if (src_value->data[0].v_pointer)
335 BoxedNode key, *node;
337 key.type = G_VALUE_TYPE (src_value);
338 node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
339 dest_value->data[0].v_pointer = node->copy (src_value->data[0].v_pointer);
342 dest_value->data[0].v_pointer = src_value->data[0].v_pointer;
346 boxed_proxy_value_peek_pointer (const GValue *value)
348 return value->data[0].v_pointer;
352 boxed_proxy_collect_value (GValue *value,
353 guint n_collect_values,
354 GTypeCValue *collect_values,
357 BoxedNode key, *node;
359 key.type = G_VALUE_TYPE (value);
360 node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
362 if (!collect_values[0].v_pointer)
363 value->data[0].v_pointer = NULL;
366 if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
368 value->data[0].v_pointer = collect_values[0].v_pointer;
369 value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
372 value->data[0].v_pointer = node->copy (collect_values[0].v_pointer);
379 boxed_proxy_lcopy_value (const GValue *value,
380 guint n_collect_values,
381 GTypeCValue *collect_values,
384 gpointer *boxed_p = collect_values[0].v_pointer;
387 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
389 if (!value->data[0].v_pointer)
391 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
392 *boxed_p = value->data[0].v_pointer;
395 BoxedNode key, *node;
397 key.type = G_VALUE_TYPE (value);
398 node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
399 *boxed_p = node->copy (value->data[0].v_pointer);
406 * g_boxed_type_register_static:
407 * @name: Name of the new boxed type.
408 * @boxed_copy: Boxed structure copy function.
409 * @boxed_free: Boxed structure free function.
411 * This function creates a new %G_TYPE_BOXED derived type id for a new
412 * boxed type with name @name. Boxed type handling functions have to be
413 * provided to copy and free opaque boxed structures of this type.
415 * Returns: New %G_TYPE_BOXED derived type id for @name.
418 g_boxed_type_register_static (const gchar *name,
419 GBoxedCopyFunc boxed_copy,
420 GBoxedFreeFunc boxed_free)
422 static const GTypeValueTable vtable = {
423 boxed_proxy_value_init,
424 boxed_proxy_value_free,
425 boxed_proxy_value_copy,
426 boxed_proxy_value_peek_pointer,
428 boxed_proxy_collect_value,
430 boxed_proxy_lcopy_value,
432 static const GTypeInfo type_info = {
434 NULL, /* base_init */
435 NULL, /* base_finalize */
436 NULL, /* class_init */
437 NULL, /* class_finalize */
438 NULL, /* class_data */
439 0, /* instance_size */
441 NULL, /* instance_init */
442 &vtable, /* value_table */
446 g_return_val_if_fail (name != NULL, 0);
447 g_return_val_if_fail (boxed_copy != NULL, 0);
448 g_return_val_if_fail (boxed_free != NULL, 0);
449 g_return_val_if_fail (g_type_from_name (name) == 0, 0);
451 type = g_type_register_static (G_TYPE_BOXED, name, &type_info, 0);
453 /* install proxy functions upon successfull registration */
459 key.copy = boxed_copy;
460 key.free = boxed_free;
461 boxed_bsa = g_bsearch_array_insert (boxed_bsa, &boxed_bconfig, &key);
469 * @boxed_type: The type of @src_boxed.
470 * @src_boxed: The boxed structure to be copied.
472 * Provide a copy of a boxed structure @src_boxed which is of type @boxed_type.
474 * Returns: The newly created copy of the boxed structure.
477 g_boxed_copy (GType boxed_type,
478 gconstpointer src_boxed)
480 GTypeValueTable *value_table;
483 g_return_val_if_fail (G_TYPE_IS_BOXED (boxed_type), NULL);
484 g_return_val_if_fail (G_TYPE_IS_ABSTRACT (boxed_type) == FALSE, NULL);
485 g_return_val_if_fail (src_boxed != NULL, NULL);
487 value_table = g_type_value_table_peek (boxed_type);
489 g_return_val_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type), NULL);
491 /* check if our proxying implementation is used, we can short-cut here */
492 if (value_table->value_copy == boxed_proxy_value_copy)
494 BoxedNode key, *node;
496 key.type = boxed_type;
497 node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
498 dest_boxed = node->copy ((gpointer) src_boxed);
502 GValue src_value, dest_value;
504 /* we heavily rely on third-party boxed type value vtable
505 * implementations to follow normal boxed value storage
506 * (data[0].v_pointer is the boxed struct, and
507 * data[1].v_uint holds the G_VALUE_NOCOPY_CONTENTS flag,
509 * but then, we can expect that since we layed out the
511 * data[1].v_uint&G_VALUE_NOCOPY_CONTENTS shouldn't be set
514 /* equiv. to g_value_set_static_boxed() */
515 value_meminit (&src_value, boxed_type);
516 src_value.data[0].v_pointer = (gpointer) src_boxed;
517 src_value.data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
519 /* call third-party code copy function, fingers-crossed */
520 value_meminit (&dest_value, boxed_type);
521 value_table->value_copy (&src_value, &dest_value);
523 /* double check and grouse if things went wrong */
524 if (dest_value.data[1].v_ulong)
525 g_warning ("the copy_value() implementation of type `%s' seems to make use of reserved GValue fields",
526 g_type_name (boxed_type));
528 dest_boxed = dest_value.data[0].v_pointer;
536 * @boxed_type: The type of @boxed.
537 * @boxed: The boxed structure to be freed.
539 * Free the boxed structure @boxed which is of type @boxed_type.
542 g_boxed_free (GType boxed_type,
545 GTypeValueTable *value_table;
547 g_return_if_fail (G_TYPE_IS_BOXED (boxed_type));
548 g_return_if_fail (G_TYPE_IS_ABSTRACT (boxed_type) == FALSE);
549 g_return_if_fail (boxed != NULL);
551 value_table = g_type_value_table_peek (boxed_type);
553 g_return_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type));
555 /* check if our proxying implementation is used, we can short-cut here */
556 if (value_table->value_free == boxed_proxy_value_free)
558 BoxedNode key, *node;
560 key.type = boxed_type;
561 node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
568 /* see g_boxed_copy() on why we think we can do this */
569 value_meminit (&value, boxed_type);
570 value.data[0].v_pointer = boxed;
571 value_table->value_free (&value);
577 * @value: a valid #GValue of %G_TYPE_BOXED derived type
579 * Get the contents of a %G_TYPE_BOXED derived #GValue.
581 * Returns: boxed contents of @value
584 g_value_get_boxed (const GValue *value)
586 g_return_val_if_fail (G_VALUE_HOLDS_BOXED (value), NULL);
587 g_return_val_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)), NULL);
589 return value->data[0].v_pointer;
594 * @value: a valid #GValue of %G_TYPE_BOXED derived type
596 * Get the contents of a %G_TYPE_BOXED derived #GValue. Upon getting,
597 * the boxed value is duplicated and needs to be later freed with
598 * g_boxed_free(), e.g. like: g_boxed_free (G_VALUE_TYPE (@value),
601 * Returns: boxed contents of @value
604 g_value_dup_boxed (const GValue *value)
606 g_return_val_if_fail (G_VALUE_HOLDS_BOXED (value), NULL);
607 g_return_val_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)), NULL);
609 return value->data[0].v_pointer ? g_boxed_copy (G_VALUE_TYPE (value), value->data[0].v_pointer) : NULL;
613 value_set_boxed_internal (GValue *value,
614 gconstpointer const_boxed,
618 BoxedNode key, *node;
619 gpointer boxed = (gpointer) const_boxed;
623 /* just resetting to NULL might not be desired, need to
624 * have value reinitialized also (for values defaulting
625 * to other default value states than a NULL data pointer),
626 * g_value_reset() will handle this
628 g_value_reset (value);
632 key.type = G_VALUE_TYPE (value);
633 node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
637 /* we proxy this type, free contents and copy right away */
638 if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
639 node->free (value->data[0].v_pointer);
640 value->data[1].v_uint = need_free ? 0 : G_VALUE_NOCOPY_CONTENTS;
641 value->data[0].v_pointer = need_copy ? node->copy (boxed) : boxed;
645 /* we don't handle this type, free contents and let g_boxed_copy()
646 * figure what's required
648 if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
649 g_boxed_free (G_VALUE_TYPE (value), value->data[0].v_pointer);
650 value->data[1].v_uint = need_free ? 0 : G_VALUE_NOCOPY_CONTENTS;
651 value->data[0].v_pointer = need_copy ? g_boxed_copy (G_VALUE_TYPE (value), boxed) : boxed;
657 * @value: a valid #GValue of %G_TYPE_BOXED derived type
658 * @v_boxed: boxed value to be set
660 * Set the contents of a %G_TYPE_BOXED derived #GValue to @v_boxed.
663 g_value_set_boxed (GValue *value,
666 g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
667 g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
669 value_set_boxed_internal (value, boxed, TRUE, TRUE);
673 * g_value_set_static_boxed:
674 * @value: a valid #GValue of %G_TYPE_BOXED derived type
675 * @v_boxed: static boxed value to be set
677 * Set the contents of a %G_TYPE_BOXED derived #GValue to @v_boxed.
678 * The boxed value is assumed to be static, and is thus not duplicated
679 * when setting the #GValue.
682 g_value_set_static_boxed (GValue *value,
685 g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
686 g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
688 value_set_boxed_internal (value, boxed, FALSE, FALSE);
692 * g_value_set_boxed_take_ownership:
693 * @value: a valid #GValue of %G_TYPE_BOXED derived type
694 * @v_boxed: duplicated unowned boxed value to be set
696 * This is an internal function introduced mainly for C marshallers.
698 * Deprecated: 2.4: Use g_value_take_boxed() instead.
701 g_value_set_boxed_take_ownership (GValue *value,
704 g_value_take_boxed (value, boxed);
708 * g_value_take_boxed:
709 * @value: a valid #GValue of %G_TYPE_BOXED derived type
710 * @v_boxed: duplicated unowned boxed value to be set
712 * Sets the contents of a %G_TYPE_BOXED derived #GValue to @v_boxed
713 * and takes over the ownership of the callers reference to @v_boxed;
714 * the caller doesn't have to unref it any more.
719 g_value_take_boxed (GValue *value,
722 g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
723 g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
725 value_set_boxed_internal (value, boxed, FALSE, TRUE);
728 #define __G_BOXED_C__
729 #include "gobjectaliasdef.c"