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 "gtype-private.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 static inline void /* keep this function in sync with gvalue.c */
50 value_meminit (GValue *value,
53 value->g_type = value_type;
54 memset (value->data, 0, sizeof (value->data));
58 value_copy (gpointer boxed)
60 const GValue *src_value = boxed;
61 GValue *dest_value = g_new0 (GValue, 1);
63 if (G_VALUE_TYPE (src_value))
65 g_value_init (dest_value, G_VALUE_TYPE (src_value));
66 g_value_copy (src_value, dest_value);
72 value_free (gpointer boxed)
74 GValue *value = boxed;
76 if (G_VALUE_TYPE (value))
77 g_value_unset (value);
82 g_boxed_type_init (void)
84 static const GTypeInfo info = {
87 NULL, /* base_destroy */
88 NULL, /* class_init */
89 NULL, /* class_destroy */
90 NULL, /* class_data */
91 0, /* instance_size */
93 NULL, /* instance_init */
94 NULL, /* value_table */
96 const GTypeFundamentalInfo finfo = { G_TYPE_FLAG_DERIVABLE, };
101 type = g_type_register_fundamental (G_TYPE_BOXED, g_intern_static_string ("GBoxed"), &info, &finfo,
102 G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT);
103 g_assert (type == G_TYPE_BOXED);
107 g_closure_get_type (void)
109 static GType type_id = 0;
112 type_id = g_boxed_type_register_static (g_intern_static_string ("GClosure"),
113 (GBoxedCopyFunc) g_closure_ref,
114 (GBoxedFreeFunc) g_closure_unref);
119 g_value_get_type (void)
121 static GType type_id = 0;
124 type_id = g_boxed_type_register_static (g_intern_static_string ("GValue"),
131 g_value_array_get_type (void)
133 static GType type_id = 0;
136 type_id = g_boxed_type_register_static (g_intern_static_string ("GValueArray"),
137 (GBoxedCopyFunc) g_value_array_copy,
138 (GBoxedFreeFunc) g_value_array_free);
143 gdate_copy (gpointer boxed)
145 const GDate *date = (const GDate*) boxed;
147 return g_date_new_julian (g_date_get_julian (date));
151 g_date_get_type (void)
153 static GType type_id = 0;
156 type_id = g_boxed_type_register_static (g_intern_static_string ("GDate"),
157 (GBoxedCopyFunc) gdate_copy,
158 (GBoxedFreeFunc) g_date_free);
163 g_strv_get_type (void)
165 static GType type_id = 0;
168 type_id = g_boxed_type_register_static (g_intern_static_string ("GStrv"),
169 (GBoxedCopyFunc) g_strdupv,
170 (GBoxedFreeFunc) g_strfreev);
175 gstring_copy (gpointer boxed)
177 const GString *src_gstring = boxed;
179 return g_string_new_len (src_gstring->str, src_gstring->len);
183 gstring_free (gpointer boxed)
185 GString *gstring = boxed;
187 g_string_free (gstring, TRUE);
191 g_gstring_get_type (void)
193 static GType type_id = 0;
196 type_id = g_boxed_type_register_static (g_intern_static_string ("GString"),
197 /* the naming is a bit odd, but GString is obviously not G_TYPE_STRING */
204 hash_table_copy (gpointer boxed)
206 GHashTable *hash_table = boxed;
207 return g_hash_table_ref (hash_table);
211 hash_table_free (gpointer boxed)
213 GHashTable *hash_table = boxed;
214 g_hash_table_unref (hash_table);
218 g_hash_table_get_type (void)
220 static GType type_id = 0;
222 type_id = g_boxed_type_register_static (g_intern_static_string ("GHashTable"),
223 hash_table_copy, hash_table_free);
228 g_regex_get_type (void)
230 static GType type_id = 0;
234 type_id = g_boxed_type_register_static (g_intern_static_string ("GRegex"),
235 (GBoxedCopyFunc) g_regex_ref,
236 (GBoxedFreeFunc) g_regex_unref);
243 g_array_get_type (void)
245 static GType type_id = 0;
247 type_id = g_boxed_type_register_static (g_intern_static_string ("GArray"),
248 (GBoxedCopyFunc) g_array_ref,
249 (GBoxedFreeFunc) g_array_unref);
254 g_ptr_array_get_type (void)
256 static GType type_id = 0;
258 type_id = g_boxed_type_register_static (g_intern_static_string ("GPtrArray"),
259 (GBoxedCopyFunc) g_ptr_array_ref,
260 (GBoxedFreeFunc) g_ptr_array_unref);
265 g_byte_array_get_type (void)
267 static GType type_id = 0;
269 type_id = g_boxed_type_register_static (g_intern_static_string ("GByteArray"),
270 (GBoxedCopyFunc) g_byte_array_ref,
271 (GBoxedFreeFunc) g_byte_array_unref);
276 boxed_proxy_value_init (GValue *value)
278 value->data[0].v_pointer = NULL;
282 boxed_proxy_value_free (GValue *value)
284 if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
285 _g_type_boxed_free (G_VALUE_TYPE (value), value->data[0].v_pointer);
289 boxed_proxy_value_copy (const GValue *src_value,
292 if (src_value->data[0].v_pointer)
293 dest_value->data[0].v_pointer = _g_type_boxed_copy (G_VALUE_TYPE (src_value), src_value->data[0].v_pointer);
295 dest_value->data[0].v_pointer = src_value->data[0].v_pointer;
299 boxed_proxy_value_peek_pointer (const GValue *value)
301 return value->data[0].v_pointer;
305 boxed_proxy_collect_value (GValue *value,
306 guint n_collect_values,
307 GTypeCValue *collect_values,
310 if (!collect_values[0].v_pointer)
311 value->data[0].v_pointer = NULL;
314 if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
316 value->data[0].v_pointer = collect_values[0].v_pointer;
317 value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
320 value->data[0].v_pointer = _g_type_boxed_copy (G_VALUE_TYPE (value), collect_values[0].v_pointer);
327 boxed_proxy_lcopy_value (const GValue *value,
328 guint n_collect_values,
329 GTypeCValue *collect_values,
332 gpointer *boxed_p = collect_values[0].v_pointer;
335 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
337 if (!value->data[0].v_pointer)
339 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
340 *boxed_p = value->data[0].v_pointer;
342 *boxed_p = _g_type_boxed_copy (G_VALUE_TYPE (value), value->data[0].v_pointer);
348 * g_boxed_type_register_static:
349 * @name: Name of the new boxed type.
350 * @boxed_copy: Boxed structure copy function.
351 * @boxed_free: Boxed structure free function.
353 * This function creates a new %G_TYPE_BOXED derived type id for a new
354 * boxed type with name @name. Boxed type handling functions have to be
355 * provided to copy and free opaque boxed structures of this type.
357 * Returns: New %G_TYPE_BOXED derived type id for @name.
360 g_boxed_type_register_static (const gchar *name,
361 GBoxedCopyFunc boxed_copy,
362 GBoxedFreeFunc boxed_free)
364 static const GTypeValueTable vtable = {
365 boxed_proxy_value_init,
366 boxed_proxy_value_free,
367 boxed_proxy_value_copy,
368 boxed_proxy_value_peek_pointer,
370 boxed_proxy_collect_value,
372 boxed_proxy_lcopy_value,
374 GTypeInfo type_info = {
376 NULL, /* base_init */
377 NULL, /* base_finalize */
378 NULL, /* class_init */
379 NULL, /* class_finalize */
380 NULL, /* class_data */
381 0, /* instance_size */
383 NULL, /* instance_init */
384 &vtable, /* value_table */
388 g_return_val_if_fail (name != NULL, 0);
389 g_return_val_if_fail (boxed_copy != NULL, 0);
390 g_return_val_if_fail (boxed_free != NULL, 0);
391 g_return_val_if_fail (g_type_from_name (name) == 0, 0);
393 type = g_type_register_static (G_TYPE_BOXED, name, &type_info, 0);
395 /* install proxy functions upon successfull registration */
397 _g_type_boxed_init (type, boxed_copy, boxed_free);
404 * @boxed_type: The type of @src_boxed.
405 * @src_boxed: The boxed structure to be copied.
407 * Provide a copy of a boxed structure @src_boxed which is of type @boxed_type.
409 * Returns: The newly created copy of the boxed structure.
412 g_boxed_copy (GType boxed_type,
413 gconstpointer src_boxed)
415 GTypeValueTable *value_table;
418 g_return_val_if_fail (G_TYPE_IS_BOXED (boxed_type), NULL);
419 g_return_val_if_fail (G_TYPE_IS_ABSTRACT (boxed_type) == FALSE, NULL);
420 g_return_val_if_fail (src_boxed != NULL, NULL);
422 value_table = g_type_value_table_peek (boxed_type);
424 g_return_val_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type), NULL);
426 /* check if our proxying implementation is used, we can short-cut here */
427 if (value_table->value_copy == boxed_proxy_value_copy)
428 dest_boxed = _g_type_boxed_copy (boxed_type, (gpointer) src_boxed);
431 GValue src_value, dest_value;
433 /* we heavily rely on third-party boxed type value vtable
434 * implementations to follow normal boxed value storage
435 * (data[0].v_pointer is the boxed struct, and
436 * data[1].v_uint holds the G_VALUE_NOCOPY_CONTENTS flag,
438 * but then, we can expect that since we layed out the
440 * data[1].v_uint&G_VALUE_NOCOPY_CONTENTS shouldn't be set
443 /* equiv. to g_value_set_static_boxed() */
444 value_meminit (&src_value, boxed_type);
445 src_value.data[0].v_pointer = (gpointer) src_boxed;
446 src_value.data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
448 /* call third-party code copy function, fingers-crossed */
449 value_meminit (&dest_value, boxed_type);
450 value_table->value_copy (&src_value, &dest_value);
452 /* double check and grouse if things went wrong */
453 if (dest_value.data[1].v_ulong)
454 g_warning ("the copy_value() implementation of type `%s' seems to make use of reserved GValue fields",
455 g_type_name (boxed_type));
457 dest_boxed = dest_value.data[0].v_pointer;
465 * @boxed_type: The type of @boxed.
466 * @boxed: The boxed structure to be freed.
468 * Free the boxed structure @boxed which is of type @boxed_type.
471 g_boxed_free (GType boxed_type,
474 GTypeValueTable *value_table;
476 g_return_if_fail (G_TYPE_IS_BOXED (boxed_type));
477 g_return_if_fail (G_TYPE_IS_ABSTRACT (boxed_type) == FALSE);
478 g_return_if_fail (boxed != NULL);
480 value_table = g_type_value_table_peek (boxed_type);
482 g_return_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type));
484 /* check if our proxying implementation is used, we can short-cut here */
485 if (value_table->value_free == boxed_proxy_value_free)
486 _g_type_boxed_free (boxed_type, boxed);
491 /* see g_boxed_copy() on why we think we can do this */
492 value_meminit (&value, boxed_type);
493 value.data[0].v_pointer = boxed;
494 value_table->value_free (&value);
500 * @value: a valid #GValue of %G_TYPE_BOXED derived type
502 * Get the contents of a %G_TYPE_BOXED derived #GValue.
504 * Returns: boxed contents of @value
507 g_value_get_boxed (const GValue *value)
509 g_return_val_if_fail (G_VALUE_HOLDS_BOXED (value), NULL);
510 g_return_val_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)), NULL);
512 return value->data[0].v_pointer;
517 * @value: a valid #GValue of %G_TYPE_BOXED derived type
519 * Get the contents of a %G_TYPE_BOXED derived #GValue. Upon getting,
520 * the boxed value is duplicated and needs to be later freed with
521 * g_boxed_free(), e.g. like: g_boxed_free (G_VALUE_TYPE (@value),
524 * Returns: boxed contents of @value
527 g_value_dup_boxed (const GValue *value)
529 g_return_val_if_fail (G_VALUE_HOLDS_BOXED (value), NULL);
530 g_return_val_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)), NULL);
532 return value->data[0].v_pointer ? g_boxed_copy (G_VALUE_TYPE (value), value->data[0].v_pointer) : NULL;
536 value_set_boxed_internal (GValue *value,
543 /* just resetting to NULL might not be desired, need to
544 * have value reinitialized also (for values defaulting
545 * to other default value states than a NULL data pointer),
546 * g_value_reset() will handle this
548 g_value_reset (value);
552 if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
553 g_boxed_free (G_VALUE_TYPE (value), value->data[0].v_pointer);
554 value->data[1].v_uint = need_free ? 0 : G_VALUE_NOCOPY_CONTENTS;
555 value->data[0].v_pointer = need_copy ? g_boxed_copy (G_VALUE_TYPE (value), boxed) : (gpointer) boxed;
560 * @value: a valid #GValue of %G_TYPE_BOXED derived type
561 * @v_boxed: boxed value to be set
563 * Set the contents of a %G_TYPE_BOXED derived #GValue to @v_boxed.
566 g_value_set_boxed (GValue *value,
569 g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
570 g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
572 value_set_boxed_internal (value, boxed, TRUE, TRUE);
576 * g_value_set_static_boxed:
577 * @value: a valid #GValue of %G_TYPE_BOXED derived type
578 * @v_boxed: static boxed value to be set
580 * Set the contents of a %G_TYPE_BOXED derived #GValue to @v_boxed.
581 * The boxed value is assumed to be static, and is thus not duplicated
582 * when setting the #GValue.
585 g_value_set_static_boxed (GValue *value,
588 g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
589 g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
591 value_set_boxed_internal (value, boxed, FALSE, FALSE);
595 * g_value_set_boxed_take_ownership:
596 * @value: a valid #GValue of %G_TYPE_BOXED derived type
597 * @v_boxed: duplicated unowned boxed value to be set
599 * This is an internal function introduced mainly for C marshallers.
601 * Deprecated: 2.4: Use g_value_take_boxed() instead.
604 g_value_set_boxed_take_ownership (GValue *value,
607 g_value_take_boxed (value, boxed);
611 * g_value_take_boxed:
612 * @value: a valid #GValue of %G_TYPE_BOXED derived type
613 * @v_boxed: duplicated unowned boxed value to be set
615 * Sets the contents of a %G_TYPE_BOXED derived #GValue to @v_boxed
616 * and takes over the ownership of the callers reference to @v_boxed;
617 * the caller doesn't have to unref it any more.
622 g_value_take_boxed (GValue *value,
625 g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
626 g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
628 value_set_boxed_internal (value, boxed, FALSE, TRUE);
631 #define __G_BOXED_C__
632 #include "gobjectaliasdef.c"