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"
34 * @short_description: A mechanism to wrap opaque C structures registered
36 * @see_also: #GParamSpecBoxed, g_param_spec_boxed()
39 * GBoxed is a generic wrapper mechanism for arbitrary C structures. The only
40 * thing the type system needs to know about the structures is how to copy and
41 * free them, beyond that they are treated as opaque chunks of memory.
43 * Boxed types are useful for simple value-holder structures like rectangles or
44 * points. They can also be used for wrapping structures defined in non-GObject
48 static inline void /* keep this function in sync with gvalue.c */
49 value_meminit (GValue *value,
52 value->g_type = value_type;
53 memset (value->data, 0, sizeof (value->data));
57 value_copy (GValue *src_value)
59 GValue *dest_value = g_new0 (GValue, 1);
61 if (G_VALUE_TYPE (src_value))
63 g_value_init (dest_value, G_VALUE_TYPE (src_value));
64 g_value_copy (src_value, dest_value);
70 value_free (GValue *value)
72 if (G_VALUE_TYPE (value))
73 g_value_unset (value);
78 _g_boxed_type_init (void)
80 static const GTypeInfo info = {
83 NULL, /* base_destroy */
84 NULL, /* class_init */
85 NULL, /* class_destroy */
86 NULL, /* class_data */
87 0, /* instance_size */
89 NULL, /* instance_init */
90 NULL, /* value_table */
92 const GTypeFundamentalInfo finfo = { G_TYPE_FLAG_DERIVABLE, };
97 type = g_type_register_fundamental (G_TYPE_BOXED, g_intern_static_string ("GBoxed"), &info, &finfo,
98 G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT);
99 g_assert (type == G_TYPE_BOXED);
103 gdate_copy (GDate *date)
105 return g_date_new_julian (g_date_get_julian (date));
109 gstring_copy (GString *src_gstring)
111 return g_string_new_len (src_gstring->str, src_gstring->len);
115 gstring_free (GString *gstring)
117 g_string_free (gstring, TRUE);
120 G_DEFINE_BOXED_TYPE (GClosure, g_closure, g_closure_ref, g_closure_unref)
121 G_DEFINE_BOXED_TYPE (GValue, g_value, value_copy, value_free)
122 G_DEFINE_BOXED_TYPE (GValueArray, g_value_array, g_value_array_copy, g_value_array_free)
123 G_DEFINE_BOXED_TYPE (GDate, g_date, gdate_copy, g_date_free)
124 /* the naming is a bit odd, but GString is obviously not G_TYPE_STRING */
125 G_DEFINE_BOXED_TYPE (GString, g_gstring, gstring_copy, gstring_free)
126 G_DEFINE_BOXED_TYPE (GHashTable, g_hash_table, g_hash_table_ref, g_hash_table_unref)
127 G_DEFINE_BOXED_TYPE (GArray, g_array, g_array_ref, g_array_unref)
128 G_DEFINE_BOXED_TYPE (GPtrArray, g_ptr_array,g_ptr_array_ref, g_ptr_array_unref)
129 G_DEFINE_BOXED_TYPE (GByteArray, g_byte_array, g_byte_array_ref, g_byte_array_unref)
132 G_DEFINE_BOXED_TYPE (GRegex, g_regex, g_regex_ref, g_regex_unref)
133 G_DEFINE_BOXED_TYPE (GMatchInfo, g_match_info, g_match_info_ref, g_match_info_unref)
135 GType g_regex_get_type (void) { return G_TYPE_INVALID; }
136 GType g_match_info_get_type (void) { return G_TYPE_INVALID; }
137 #endif /* ENABLE_REGEX */
139 #define g_variant_type_get_type g_variant_type_get_gtype
140 G_DEFINE_BOXED_TYPE (GVariantType, g_variant_type, g_variant_type_copy, g_variant_type_free)
141 #undef g_variant_type_get_type
143 G_DEFINE_BOXED_TYPE (GVariantBuilder, g_variant_builder, g_variant_builder_ref, g_variant_builder_unref)
145 G_DEFINE_BOXED_TYPE (GError, g_error, g_error_copy, g_error_free)
147 G_DEFINE_BOXED_TYPE (GDateTime, g_date_time, g_date_time_ref, g_date_time_unref);
149 /* This one can't use G_DEFINE_BOXED_TYPE (GStrv, g_strv, g_strdupv, g_strfreev) */
151 g_strv_get_type (void)
153 static volatile gsize g_define_type_id__volatile = 0;
155 if (g_once_init_enter (&g_define_type_id__volatile))
157 GType g_define_type_id =
158 g_boxed_type_register_static (g_intern_static_string ("GStrv"),
159 (GBoxedCopyFunc) g_strdupv,
160 (GBoxedFreeFunc) g_strfreev);
162 g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
165 return g_define_type_id__volatile;
169 * g_variant_get_gtype:
175 g_variant_get_gtype (void)
177 return G_TYPE_VARIANT;
181 boxed_proxy_value_init (GValue *value)
183 value->data[0].v_pointer = NULL;
187 boxed_proxy_value_free (GValue *value)
189 if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
190 _g_type_boxed_free (G_VALUE_TYPE (value), value->data[0].v_pointer);
194 boxed_proxy_value_copy (const GValue *src_value,
197 if (src_value->data[0].v_pointer)
198 dest_value->data[0].v_pointer = _g_type_boxed_copy (G_VALUE_TYPE (src_value), src_value->data[0].v_pointer);
200 dest_value->data[0].v_pointer = src_value->data[0].v_pointer;
204 boxed_proxy_value_peek_pointer (const GValue *value)
206 return value->data[0].v_pointer;
210 boxed_proxy_collect_value (GValue *value,
211 guint n_collect_values,
212 GTypeCValue *collect_values,
215 if (!collect_values[0].v_pointer)
216 value->data[0].v_pointer = NULL;
219 if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
221 value->data[0].v_pointer = collect_values[0].v_pointer;
222 value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
225 value->data[0].v_pointer = _g_type_boxed_copy (G_VALUE_TYPE (value), collect_values[0].v_pointer);
232 boxed_proxy_lcopy_value (const GValue *value,
233 guint n_collect_values,
234 GTypeCValue *collect_values,
237 gpointer *boxed_p = collect_values[0].v_pointer;
240 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
242 if (!value->data[0].v_pointer)
244 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
245 *boxed_p = value->data[0].v_pointer;
247 *boxed_p = _g_type_boxed_copy (G_VALUE_TYPE (value), value->data[0].v_pointer);
253 * g_boxed_type_register_static:
254 * @name: Name of the new boxed type.
255 * @boxed_copy: Boxed structure copy function.
256 * @boxed_free: Boxed structure free function.
258 * This function creates a new %G_TYPE_BOXED derived type id for a new
259 * boxed type with name @name. Boxed type handling functions have to be
260 * provided to copy and free opaque boxed structures of this type.
262 * Returns: New %G_TYPE_BOXED derived type id for @name.
265 g_boxed_type_register_static (const gchar *name,
266 GBoxedCopyFunc boxed_copy,
267 GBoxedFreeFunc boxed_free)
269 static const GTypeValueTable vtable = {
270 boxed_proxy_value_init,
271 boxed_proxy_value_free,
272 boxed_proxy_value_copy,
273 boxed_proxy_value_peek_pointer,
275 boxed_proxy_collect_value,
277 boxed_proxy_lcopy_value,
279 GTypeInfo type_info = {
281 NULL, /* base_init */
282 NULL, /* base_finalize */
283 NULL, /* class_init */
284 NULL, /* class_finalize */
285 NULL, /* class_data */
286 0, /* instance_size */
288 NULL, /* instance_init */
289 &vtable, /* value_table */
293 g_return_val_if_fail (name != NULL, 0);
294 g_return_val_if_fail (boxed_copy != NULL, 0);
295 g_return_val_if_fail (boxed_free != NULL, 0);
296 g_return_val_if_fail (g_type_from_name (name) == 0, 0);
298 type = g_type_register_static (G_TYPE_BOXED, name, &type_info, 0);
300 /* install proxy functions upon successfull registration */
302 _g_type_boxed_init (type, boxed_copy, boxed_free);
309 * @boxed_type: The type of @src_boxed.
310 * @src_boxed: The boxed structure to be copied.
312 * Provide a copy of a boxed structure @src_boxed which is of type @boxed_type.
314 * Returns: The newly created copy of the boxed structure.
317 g_boxed_copy (GType boxed_type,
318 gconstpointer src_boxed)
320 GTypeValueTable *value_table;
323 g_return_val_if_fail (G_TYPE_IS_BOXED (boxed_type), NULL);
324 g_return_val_if_fail (G_TYPE_IS_ABSTRACT (boxed_type) == FALSE, NULL);
325 g_return_val_if_fail (src_boxed != NULL, NULL);
327 value_table = g_type_value_table_peek (boxed_type);
329 g_return_val_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type), NULL);
331 /* check if our proxying implementation is used, we can short-cut here */
332 if (value_table->value_copy == boxed_proxy_value_copy)
333 dest_boxed = _g_type_boxed_copy (boxed_type, (gpointer) src_boxed);
336 GValue src_value, dest_value;
338 /* we heavily rely on third-party boxed type value vtable
339 * implementations to follow normal boxed value storage
340 * (data[0].v_pointer is the boxed struct, and
341 * data[1].v_uint holds the G_VALUE_NOCOPY_CONTENTS flag,
343 * but then, we can expect that since we layed out the
345 * data[1].v_uint&G_VALUE_NOCOPY_CONTENTS shouldn't be set
348 /* equiv. to g_value_set_static_boxed() */
349 value_meminit (&src_value, boxed_type);
350 src_value.data[0].v_pointer = (gpointer) src_boxed;
351 src_value.data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
353 /* call third-party code copy function, fingers-crossed */
354 value_meminit (&dest_value, boxed_type);
355 value_table->value_copy (&src_value, &dest_value);
357 /* double check and grouse if things went wrong */
358 if (dest_value.data[1].v_ulong)
359 g_warning ("the copy_value() implementation of type `%s' seems to make use of reserved GValue fields",
360 g_type_name (boxed_type));
362 dest_boxed = dest_value.data[0].v_pointer;
370 * @boxed_type: The type of @boxed.
371 * @boxed: The boxed structure to be freed.
373 * Free the boxed structure @boxed which is of type @boxed_type.
376 g_boxed_free (GType boxed_type,
379 GTypeValueTable *value_table;
381 g_return_if_fail (G_TYPE_IS_BOXED (boxed_type));
382 g_return_if_fail (G_TYPE_IS_ABSTRACT (boxed_type) == FALSE);
383 g_return_if_fail (boxed != NULL);
385 value_table = g_type_value_table_peek (boxed_type);
387 g_return_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type));
389 /* check if our proxying implementation is used, we can short-cut here */
390 if (value_table->value_free == boxed_proxy_value_free)
391 _g_type_boxed_free (boxed_type, boxed);
396 /* see g_boxed_copy() on why we think we can do this */
397 value_meminit (&value, boxed_type);
398 value.data[0].v_pointer = boxed;
399 value_table->value_free (&value);
405 * @value: a valid #GValue of %G_TYPE_BOXED derived type
407 * Get the contents of a %G_TYPE_BOXED derived #GValue.
409 * Returns: (transfer none): boxed contents of @value
412 g_value_get_boxed (const GValue *value)
414 g_return_val_if_fail (G_VALUE_HOLDS_BOXED (value), NULL);
415 g_return_val_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)), NULL);
417 return value->data[0].v_pointer;
421 * g_value_dup_boxed: (skip)
422 * @value: a valid #GValue of %G_TYPE_BOXED derived type
424 * Get the contents of a %G_TYPE_BOXED derived #GValue. Upon getting,
425 * the boxed value is duplicated and needs to be later freed with
426 * g_boxed_free(), e.g. like: g_boxed_free (G_VALUE_TYPE (@value),
429 * Returns: boxed contents of @value
432 g_value_dup_boxed (const GValue *value)
434 g_return_val_if_fail (G_VALUE_HOLDS_BOXED (value), NULL);
435 g_return_val_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)), NULL);
437 return value->data[0].v_pointer ? g_boxed_copy (G_VALUE_TYPE (value), value->data[0].v_pointer) : NULL;
441 value_set_boxed_internal (GValue *value,
448 /* just resetting to NULL might not be desired, need to
449 * have value reinitialized also (for values defaulting
450 * to other default value states than a NULL data pointer),
451 * g_value_reset() will handle this
453 g_value_reset (value);
457 if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
458 g_boxed_free (G_VALUE_TYPE (value), value->data[0].v_pointer);
459 value->data[1].v_uint = need_free ? 0 : G_VALUE_NOCOPY_CONTENTS;
460 value->data[0].v_pointer = need_copy ? g_boxed_copy (G_VALUE_TYPE (value), boxed) : (gpointer) boxed;
465 * @value: a valid #GValue of %G_TYPE_BOXED derived type
466 * @v_boxed: boxed value to be set
468 * Set the contents of a %G_TYPE_BOXED derived #GValue to @v_boxed.
471 g_value_set_boxed (GValue *value,
474 g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
475 g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
477 value_set_boxed_internal (value, boxed, TRUE, TRUE);
481 * g_value_set_static_boxed:
482 * @value: a valid #GValue of %G_TYPE_BOXED derived type
483 * @v_boxed: static boxed value to be set
485 * Set the contents of a %G_TYPE_BOXED derived #GValue to @v_boxed.
486 * The boxed value is assumed to be static, and is thus not duplicated
487 * when setting the #GValue.
490 g_value_set_static_boxed (GValue *value,
493 g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
494 g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
496 value_set_boxed_internal (value, boxed, FALSE, FALSE);
500 * g_value_set_boxed_take_ownership:
501 * @value: a valid #GValue of %G_TYPE_BOXED derived type
502 * @v_boxed: duplicated unowned boxed value to be set
504 * This is an internal function introduced mainly for C marshallers.
506 * Deprecated: 2.4: Use g_value_take_boxed() instead.
509 g_value_set_boxed_take_ownership (GValue *value,
512 g_value_take_boxed (value, boxed);
516 * g_value_take_boxed:
517 * @value: a valid #GValue of %G_TYPE_BOXED derived type
518 * @v_boxed: duplicated unowned boxed value to be set
520 * Sets the contents of a %G_TYPE_BOXED derived #GValue to @v_boxed
521 * and takes over the ownership of the callers reference to @v_boxed;
522 * the caller doesn't have to unref it any more.
527 g_value_take_boxed (GValue *value,
530 g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
531 g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
533 value_set_boxed_internal (value, boxed, FALSE, TRUE);