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;
267 type_id = g_boxed_type_register_static (g_intern_static_string ("GRegex"),
268 (GBoxedCopyFunc) g_regex_ref,
269 (GBoxedFreeFunc) g_regex_unref);
274 boxed_proxy_value_init (GValue *value)
276 value->data[0].v_pointer = NULL;
280 boxed_proxy_value_free (GValue *value)
282 if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
284 BoxedNode key, *node;
286 key.type = G_VALUE_TYPE (value);
287 node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
288 node->free (value->data[0].v_pointer);
293 boxed_proxy_value_copy (const GValue *src_value,
296 if (src_value->data[0].v_pointer)
298 BoxedNode key, *node;
300 key.type = G_VALUE_TYPE (src_value);
301 node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
302 dest_value->data[0].v_pointer = node->copy (src_value->data[0].v_pointer);
305 dest_value->data[0].v_pointer = src_value->data[0].v_pointer;
309 boxed_proxy_value_peek_pointer (const GValue *value)
311 return value->data[0].v_pointer;
315 boxed_proxy_collect_value (GValue *value,
316 guint n_collect_values,
317 GTypeCValue *collect_values,
320 BoxedNode key, *node;
322 key.type = G_VALUE_TYPE (value);
323 node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
325 if (!collect_values[0].v_pointer)
326 value->data[0].v_pointer = NULL;
329 if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
331 value->data[0].v_pointer = collect_values[0].v_pointer;
332 value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
335 value->data[0].v_pointer = node->copy (collect_values[0].v_pointer);
342 boxed_proxy_lcopy_value (const GValue *value,
343 guint n_collect_values,
344 GTypeCValue *collect_values,
347 gpointer *boxed_p = collect_values[0].v_pointer;
350 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
352 if (!value->data[0].v_pointer)
354 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
355 *boxed_p = value->data[0].v_pointer;
358 BoxedNode key, *node;
360 key.type = G_VALUE_TYPE (value);
361 node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
362 *boxed_p = node->copy (value->data[0].v_pointer);
369 * g_boxed_type_register_static:
370 * @name: Name of the new boxed type.
371 * @boxed_copy: Boxed structure copy function.
372 * @boxed_free: Boxed structure free function.
374 * This function creates a new %G_TYPE_BOXED derived type id for a new
375 * boxed type with name @name. Boxed type handling functions have to be
376 * provided to copy and free opaque boxed structures of this type.
378 * Returns: New %G_TYPE_BOXED derived type id for @name.
381 g_boxed_type_register_static (const gchar *name,
382 GBoxedCopyFunc boxed_copy,
383 GBoxedFreeFunc boxed_free)
385 static const GTypeValueTable vtable = {
386 boxed_proxy_value_init,
387 boxed_proxy_value_free,
388 boxed_proxy_value_copy,
389 boxed_proxy_value_peek_pointer,
391 boxed_proxy_collect_value,
393 boxed_proxy_lcopy_value,
395 static const GTypeInfo type_info = {
397 NULL, /* base_init */
398 NULL, /* base_finalize */
399 NULL, /* class_init */
400 NULL, /* class_finalize */
401 NULL, /* class_data */
402 0, /* instance_size */
404 NULL, /* instance_init */
405 &vtable, /* value_table */
409 g_return_val_if_fail (name != NULL, 0);
410 g_return_val_if_fail (boxed_copy != NULL, 0);
411 g_return_val_if_fail (boxed_free != NULL, 0);
412 g_return_val_if_fail (g_type_from_name (name) == 0, 0);
414 type = g_type_register_static (G_TYPE_BOXED, name, &type_info, 0);
416 /* install proxy functions upon successfull registration */
422 key.copy = boxed_copy;
423 key.free = boxed_free;
424 boxed_bsa = g_bsearch_array_insert (boxed_bsa, &boxed_bconfig, &key);
432 * @boxed_type: The type of @src_boxed.
433 * @src_boxed: The boxed structure to be copied.
435 * Provide a copy of a boxed structure @src_boxed which is of type @boxed_type.
437 * Returns: The newly created copy of the boxed structure.
440 g_boxed_copy (GType boxed_type,
441 gconstpointer src_boxed)
443 GTypeValueTable *value_table;
446 g_return_val_if_fail (G_TYPE_IS_BOXED (boxed_type), NULL);
447 g_return_val_if_fail (G_TYPE_IS_ABSTRACT (boxed_type) == FALSE, NULL);
448 g_return_val_if_fail (src_boxed != NULL, NULL);
450 value_table = g_type_value_table_peek (boxed_type);
452 g_return_val_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type), NULL);
454 /* check if our proxying implementation is used, we can short-cut here */
455 if (value_table->value_copy == boxed_proxy_value_copy)
457 BoxedNode key, *node;
459 key.type = boxed_type;
460 node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
461 dest_boxed = node->copy ((gpointer) src_boxed);
465 GValue src_value, dest_value;
467 /* we heavily rely on third-party boxed type value vtable
468 * implementations to follow normal boxed value storage
469 * (data[0].v_pointer is the boxed struct, and
470 * data[1].v_uint holds the G_VALUE_NOCOPY_CONTENTS flag,
472 * but then, we can expect that since we layed out the
474 * data[1].v_uint&G_VALUE_NOCOPY_CONTENTS shouldn't be set
477 /* equiv. to g_value_set_static_boxed() */
478 value_meminit (&src_value, boxed_type);
479 src_value.data[0].v_pointer = (gpointer) src_boxed;
480 src_value.data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
482 /* call third-party code copy function, fingers-crossed */
483 value_meminit (&dest_value, boxed_type);
484 value_table->value_copy (&src_value, &dest_value);
486 /* double check and grouse if things went wrong */
487 if (dest_value.data[1].v_ulong)
488 g_warning ("the copy_value() implementation of type `%s' seems to make use of reserved GValue fields",
489 g_type_name (boxed_type));
491 dest_boxed = dest_value.data[0].v_pointer;
499 * @boxed_type: The type of @boxed.
500 * @boxed: The boxed structure to be freed.
502 * Free the boxed structure @boxed which is of type @boxed_type.
505 g_boxed_free (GType boxed_type,
508 GTypeValueTable *value_table;
510 g_return_if_fail (G_TYPE_IS_BOXED (boxed_type));
511 g_return_if_fail (G_TYPE_IS_ABSTRACT (boxed_type) == FALSE);
512 g_return_if_fail (boxed != NULL);
514 value_table = g_type_value_table_peek (boxed_type);
516 g_return_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type));
518 /* check if our proxying implementation is used, we can short-cut here */
519 if (value_table->value_free == boxed_proxy_value_free)
521 BoxedNode key, *node;
523 key.type = boxed_type;
524 node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
531 /* see g_boxed_copy() on why we think we can do this */
532 value_meminit (&value, boxed_type);
533 value.data[0].v_pointer = boxed;
534 value_table->value_free (&value);
540 * @value: a valid #GValue of %G_TYPE_BOXED derived type
542 * Get the contents of a %G_TYPE_BOXED derived #GValue.
544 * Returns: boxed contents of @value
547 g_value_get_boxed (const GValue *value)
549 g_return_val_if_fail (G_VALUE_HOLDS_BOXED (value), NULL);
550 g_return_val_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)), NULL);
552 return value->data[0].v_pointer;
557 * @value: a valid #GValue of %G_TYPE_BOXED derived type
559 * Get the contents of a %G_TYPE_BOXED derived #GValue. Upon getting,
560 * the boxed value is duplicated and needs to be later freed with
561 * g_boxed_free(), e.g. like: g_boxed_free (G_VALUE_TYPE (@value),
564 * Returns: boxed contents of @value
567 g_value_dup_boxed (const GValue *value)
569 g_return_val_if_fail (G_VALUE_HOLDS_BOXED (value), NULL);
570 g_return_val_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)), NULL);
572 return value->data[0].v_pointer ? g_boxed_copy (G_VALUE_TYPE (value), value->data[0].v_pointer) : NULL;
576 value_set_boxed_internal (GValue *value,
577 gconstpointer const_boxed,
581 BoxedNode key, *node;
582 gpointer boxed = (gpointer) const_boxed;
586 /* just resetting to NULL might not be desired, need to
587 * have value reinitialized also (for values defaulting
588 * to other default value states than a NULL data pointer),
589 * g_value_reset() will handle this
591 g_value_reset (value);
595 key.type = G_VALUE_TYPE (value);
596 node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
600 /* we proxy this type, free contents and copy right away */
601 if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
602 node->free (value->data[0].v_pointer);
603 value->data[1].v_uint = need_free ? 0 : G_VALUE_NOCOPY_CONTENTS;
604 value->data[0].v_pointer = need_copy ? node->copy (boxed) : boxed;
608 /* we don't handle this type, free contents and let g_boxed_copy()
609 * figure what's required
611 if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
612 g_boxed_free (G_VALUE_TYPE (value), value->data[0].v_pointer);
613 value->data[1].v_uint = need_free ? 0 : G_VALUE_NOCOPY_CONTENTS;
614 value->data[0].v_pointer = need_copy ? g_boxed_copy (G_VALUE_TYPE (value), boxed) : boxed;
620 * @value: a valid #GValue of %G_TYPE_BOXED derived type
621 * @v_boxed: boxed value to be set
623 * Set the contents of a %G_TYPE_BOXED derived #GValue to @v_boxed.
626 g_value_set_boxed (GValue *value,
629 g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
630 g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
632 value_set_boxed_internal (value, boxed, TRUE, TRUE);
636 * g_value_set_static_boxed:
637 * @value: a valid #GValue of %G_TYPE_BOXED derived type
638 * @v_boxed: static boxed value to be set
640 * Set the contents of a %G_TYPE_BOXED derived #GValue to @v_boxed.
641 * The boxed value is assumed to be static, and is thus not duplicated
642 * when setting the #GValue.
645 g_value_set_static_boxed (GValue *value,
648 g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
649 g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
651 value_set_boxed_internal (value, boxed, FALSE, FALSE);
655 * g_value_set_boxed_take_ownership:
656 * @value: a valid #GValue of %G_TYPE_BOXED derived type
657 * @v_boxed: duplicated unowned boxed value to be set
659 * This is an internal function introduced mainly for C marshallers.
661 * Deprecated: 2.4: Use g_value_take_boxed() instead.
664 g_value_set_boxed_take_ownership (GValue *value,
667 g_value_take_boxed (value, boxed);
671 * g_value_take_boxed:
672 * @value: a valid #GValue of %G_TYPE_BOXED derived type
673 * @v_boxed: duplicated unowned boxed value to be set
675 * Sets the contents of a %G_TYPE_BOXED derived #GValue to @v_boxed
676 * and takes over the ownership of the callers reference to @v_boxed;
677 * the caller doesn't have to unref it any more.
682 g_value_take_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, TRUE);
691 #define __G_BOXED_C__
692 #include "gobjectaliasdef.c"