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 #define GLIB_DISABLE_DEPRECATION_WARNINGS
29 #include "gtype-private.h"
31 #include "gvaluearray.h"
32 #include "gvaluecollector.h"
37 * @short_description: A mechanism to wrap opaque C structures registered
39 * @see_also: #GParamSpecBoxed, g_param_spec_boxed()
42 * GBoxed is a generic wrapper mechanism for arbitrary C structures. The only
43 * thing the type system needs to know about the structures is how to copy and
44 * free them, beyond that they are treated as opaque chunks of memory.
46 * Boxed types are useful for simple value-holder structures like rectangles or
47 * points. They can also be used for wrapping structures defined in non-GObject
51 static inline void /* keep this function in sync with gvalue.c */
52 value_meminit (GValue *value,
55 value->g_type = value_type;
56 memset (value->data, 0, sizeof (value->data));
60 value_copy (GValue *src_value)
62 GValue *dest_value = g_new0 (GValue, 1);
64 if (G_VALUE_TYPE (src_value))
66 g_value_init (dest_value, G_VALUE_TYPE (src_value));
67 g_value_copy (src_value, dest_value);
73 value_free (GValue *value)
75 if (G_VALUE_TYPE (value))
76 g_value_unset (value);
81 pollfd_copy (GPollFD *src)
83 GPollFD *dest = g_new0 (GPollFD, 1);
84 /* just a couple of integers */
85 memcpy (dest, src, sizeof (GPollFD));
90 _g_boxed_type_init (void)
92 const GTypeInfo info = {
95 NULL, /* base_destroy */
96 NULL, /* class_init */
97 NULL, /* class_destroy */
98 NULL, /* class_data */
99 0, /* instance_size */
101 NULL, /* instance_init */
102 NULL, /* value_table */
104 const GTypeFundamentalInfo finfo = { G_TYPE_FLAG_DERIVABLE, };
109 type = g_type_register_fundamental (G_TYPE_BOXED, g_intern_static_string ("GBoxed"), &info, &finfo,
110 G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT);
111 g_assert (type == G_TYPE_BOXED);
115 gdate_copy (GDate *date)
117 return g_date_new_julian (g_date_get_julian (date));
121 gstring_copy (GString *src_gstring)
123 return g_string_new_len (src_gstring->str, src_gstring->len);
127 gstring_free (GString *gstring)
129 g_string_free (gstring, TRUE);
132 G_DEFINE_BOXED_TYPE (GClosure, g_closure, g_closure_ref, g_closure_unref)
133 G_DEFINE_BOXED_TYPE (GValue, g_value, value_copy, value_free)
134 G_DEFINE_BOXED_TYPE (GValueArray, g_value_array, g_value_array_copy, g_value_array_free)
135 G_DEFINE_BOXED_TYPE (GDate, g_date, gdate_copy, g_date_free)
136 /* the naming is a bit odd, but GString is obviously not G_TYPE_STRING */
137 G_DEFINE_BOXED_TYPE (GString, g_gstring, gstring_copy, gstring_free)
138 G_DEFINE_BOXED_TYPE (GHashTable, g_hash_table, g_hash_table_ref, g_hash_table_unref)
139 G_DEFINE_BOXED_TYPE (GArray, g_array, g_array_ref, g_array_unref)
140 G_DEFINE_BOXED_TYPE (GPtrArray, g_ptr_array,g_ptr_array_ref, g_ptr_array_unref)
141 G_DEFINE_BOXED_TYPE (GByteArray, g_byte_array, g_byte_array_ref, g_byte_array_unref)
142 G_DEFINE_BOXED_TYPE (GBytes, g_bytes, g_bytes_ref, g_bytes_unref);
144 G_DEFINE_BOXED_TYPE (GRegex, g_regex, g_regex_ref, g_regex_unref)
145 G_DEFINE_BOXED_TYPE (GMatchInfo, g_match_info, g_match_info_ref, g_match_info_unref)
147 #define g_variant_type_get_type g_variant_type_get_gtype
148 G_DEFINE_BOXED_TYPE (GVariantType, g_variant_type, g_variant_type_copy, g_variant_type_free)
149 #undef g_variant_type_get_type
151 G_DEFINE_BOXED_TYPE (GVariantBuilder, g_variant_builder, g_variant_builder_ref, g_variant_builder_unref)
153 G_DEFINE_BOXED_TYPE (GError, g_error, g_error_copy, g_error_free)
155 G_DEFINE_BOXED_TYPE (GDateTime, g_date_time, g_date_time_ref, g_date_time_unref);
156 G_DEFINE_BOXED_TYPE (GTimeZone, g_time_zone, g_time_zone_ref, g_time_zone_unref);
157 G_DEFINE_BOXED_TYPE (GKeyFile, g_key_file, g_key_file_ref, g_key_file_unref)
158 G_DEFINE_BOXED_TYPE (GMappedFile, g_mapped_file, g_mapped_file_ref, g_mapped_file_unref)
160 G_DEFINE_BOXED_TYPE (GMainLoop, g_main_loop, g_main_loop_ref, g_main_loop_unref)
161 G_DEFINE_BOXED_TYPE (GMainContext, g_main_context, g_main_context_ref, g_main_context_unref)
162 G_DEFINE_BOXED_TYPE (GSource, g_source, g_source_ref, g_source_unref)
163 G_DEFINE_BOXED_TYPE (GPollFD, g_pollfd, pollfd_copy, g_free)
164 G_DEFINE_BOXED_TYPE (GMarkupParseContext, g_markup_parse_context, g_markup_parse_context_ref, g_markup_parse_context_unref)
166 G_DEFINE_BOXED_TYPE (GThread, g_thread, g_thread_ref, g_thread_unref)
167 G_DEFINE_BOXED_TYPE (GChecksum, g_checksum, g_checksum_copy, g_checksum_free)
169 /* This one can't use G_DEFINE_BOXED_TYPE (GStrv, g_strv, g_strdupv, g_strfreev) */
171 g_strv_get_type (void)
173 static volatile gsize g_define_type_id__volatile = 0;
175 if (g_once_init_enter (&g_define_type_id__volatile))
177 GType g_define_type_id =
178 g_boxed_type_register_static (g_intern_static_string ("GStrv"),
179 (GBoxedCopyFunc) g_strdupv,
180 (GBoxedFreeFunc) g_strfreev);
182 g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
185 return g_define_type_id__volatile;
189 * g_variant_get_gtype:
195 g_variant_get_gtype (void)
197 return G_TYPE_VARIANT;
201 boxed_proxy_value_init (GValue *value)
203 value->data[0].v_pointer = NULL;
207 boxed_proxy_value_free (GValue *value)
209 if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
210 _g_type_boxed_free (G_VALUE_TYPE (value), value->data[0].v_pointer);
214 boxed_proxy_value_copy (const GValue *src_value,
217 if (src_value->data[0].v_pointer)
218 dest_value->data[0].v_pointer = _g_type_boxed_copy (G_VALUE_TYPE (src_value), src_value->data[0].v_pointer);
220 dest_value->data[0].v_pointer = src_value->data[0].v_pointer;
224 boxed_proxy_value_peek_pointer (const GValue *value)
226 return value->data[0].v_pointer;
230 boxed_proxy_collect_value (GValue *value,
231 guint n_collect_values,
232 GTypeCValue *collect_values,
235 if (!collect_values[0].v_pointer)
236 value->data[0].v_pointer = NULL;
239 if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
241 value->data[0].v_pointer = collect_values[0].v_pointer;
242 value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
245 value->data[0].v_pointer = _g_type_boxed_copy (G_VALUE_TYPE (value), collect_values[0].v_pointer);
252 boxed_proxy_lcopy_value (const GValue *value,
253 guint n_collect_values,
254 GTypeCValue *collect_values,
257 gpointer *boxed_p = collect_values[0].v_pointer;
260 return g_strdup_printf ("value location for '%s' passed as NULL", G_VALUE_TYPE_NAME (value));
262 if (!value->data[0].v_pointer)
264 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
265 *boxed_p = value->data[0].v_pointer;
267 *boxed_p = _g_type_boxed_copy (G_VALUE_TYPE (value), value->data[0].v_pointer);
273 * g_boxed_type_register_static:
274 * @name: Name of the new boxed type.
275 * @boxed_copy: Boxed structure copy function.
276 * @boxed_free: Boxed structure free function.
278 * This function creates a new %G_TYPE_BOXED derived type id for a new
279 * boxed type with name @name. Boxed type handling functions have to be
280 * provided to copy and free opaque boxed structures of this type.
282 * Returns: New %G_TYPE_BOXED derived type id for @name.
285 g_boxed_type_register_static (const gchar *name,
286 GBoxedCopyFunc boxed_copy,
287 GBoxedFreeFunc boxed_free)
289 static const GTypeValueTable vtable = {
290 boxed_proxy_value_init,
291 boxed_proxy_value_free,
292 boxed_proxy_value_copy,
293 boxed_proxy_value_peek_pointer,
295 boxed_proxy_collect_value,
297 boxed_proxy_lcopy_value,
299 GTypeInfo type_info = {
301 NULL, /* base_init */
302 NULL, /* base_finalize */
303 NULL, /* class_init */
304 NULL, /* class_finalize */
305 NULL, /* class_data */
306 0, /* instance_size */
308 NULL, /* instance_init */
309 &vtable, /* value_table */
313 g_return_val_if_fail (name != NULL, 0);
314 g_return_val_if_fail (boxed_copy != NULL, 0);
315 g_return_val_if_fail (boxed_free != NULL, 0);
316 g_return_val_if_fail (g_type_from_name (name) == 0, 0);
318 type = g_type_register_static (G_TYPE_BOXED, name, &type_info, 0);
320 /* install proxy functions upon successful registration */
322 _g_type_boxed_init (type, boxed_copy, boxed_free);
329 * @boxed_type: The type of @src_boxed.
330 * @src_boxed: The boxed structure to be copied.
332 * Provide a copy of a boxed structure @src_boxed which is of type @boxed_type.
334 * Returns: The newly created copy of the boxed structure.
337 g_boxed_copy (GType boxed_type,
338 gconstpointer src_boxed)
340 GTypeValueTable *value_table;
343 g_return_val_if_fail (G_TYPE_IS_BOXED (boxed_type), NULL);
344 g_return_val_if_fail (G_TYPE_IS_ABSTRACT (boxed_type) == FALSE, NULL);
345 g_return_val_if_fail (src_boxed != NULL, NULL);
347 value_table = g_type_value_table_peek (boxed_type);
349 g_return_val_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type), NULL);
351 /* check if our proxying implementation is used, we can short-cut here */
352 if (value_table->value_copy == boxed_proxy_value_copy)
353 dest_boxed = _g_type_boxed_copy (boxed_type, (gpointer) src_boxed);
356 GValue src_value, dest_value;
358 /* we heavily rely on third-party boxed type value vtable
359 * implementations to follow normal boxed value storage
360 * (data[0].v_pointer is the boxed struct, and
361 * data[1].v_uint holds the G_VALUE_NOCOPY_CONTENTS flag,
363 * but then, we can expect that since we laid out the
365 * data[1].v_uint&G_VALUE_NOCOPY_CONTENTS shouldn't be set
368 /* equiv. to g_value_set_static_boxed() */
369 value_meminit (&src_value, boxed_type);
370 src_value.data[0].v_pointer = (gpointer) src_boxed;
371 src_value.data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
373 /* call third-party code copy function, fingers-crossed */
374 value_meminit (&dest_value, boxed_type);
375 value_table->value_copy (&src_value, &dest_value);
377 /* double check and grouse if things went wrong */
378 if (dest_value.data[1].v_ulong)
379 g_warning ("the copy_value() implementation of type '%s' seems to make use of reserved GValue fields",
380 g_type_name (boxed_type));
382 dest_boxed = dest_value.data[0].v_pointer;
390 * @boxed_type: The type of @boxed.
391 * @boxed: The boxed structure to be freed.
393 * Free the boxed structure @boxed which is of type @boxed_type.
396 g_boxed_free (GType boxed_type,
399 GTypeValueTable *value_table;
401 g_return_if_fail (G_TYPE_IS_BOXED (boxed_type));
402 g_return_if_fail (G_TYPE_IS_ABSTRACT (boxed_type) == FALSE);
403 g_return_if_fail (boxed != NULL);
405 value_table = g_type_value_table_peek (boxed_type);
407 g_return_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type));
409 /* check if our proxying implementation is used, we can short-cut here */
410 if (value_table->value_free == boxed_proxy_value_free)
411 _g_type_boxed_free (boxed_type, boxed);
416 /* see g_boxed_copy() on why we think we can do this */
417 value_meminit (&value, boxed_type);
418 value.data[0].v_pointer = boxed;
419 value_table->value_free (&value);
425 * @value: a valid #GValue of %G_TYPE_BOXED derived type
427 * Get the contents of a %G_TYPE_BOXED derived #GValue.
429 * Returns: (transfer none): boxed contents of @value
432 g_value_get_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;
441 * g_value_dup_boxed: (skip)
442 * @value: a valid #GValue of %G_TYPE_BOXED derived type
444 * Get the contents of a %G_TYPE_BOXED derived #GValue. Upon getting,
445 * the boxed value is duplicated and needs to be later freed with
446 * g_boxed_free(), e.g. like: g_boxed_free (G_VALUE_TYPE (@value),
449 * Returns: boxed contents of @value
452 g_value_dup_boxed (const GValue *value)
454 g_return_val_if_fail (G_VALUE_HOLDS_BOXED (value), NULL);
455 g_return_val_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)), NULL);
457 return value->data[0].v_pointer ? g_boxed_copy (G_VALUE_TYPE (value), value->data[0].v_pointer) : NULL;
461 value_set_boxed_internal (GValue *value,
468 /* just resetting to NULL might not be desired, need to
469 * have value reinitialized also (for values defaulting
470 * to other default value states than a NULL data pointer),
471 * g_value_reset() will handle this
473 g_value_reset (value);
477 if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
478 g_boxed_free (G_VALUE_TYPE (value), value->data[0].v_pointer);
479 value->data[1].v_uint = need_free ? 0 : G_VALUE_NOCOPY_CONTENTS;
480 value->data[0].v_pointer = need_copy ? g_boxed_copy (G_VALUE_TYPE (value), boxed) : (gpointer) boxed;
485 * @value: a valid #GValue of %G_TYPE_BOXED derived type
486 * @v_boxed: (allow-none): boxed value to be set
488 * Set the contents of a %G_TYPE_BOXED derived #GValue to @v_boxed.
491 g_value_set_boxed (GValue *value,
494 g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
495 g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
497 value_set_boxed_internal (value, boxed, TRUE, TRUE);
501 * g_value_set_static_boxed:
502 * @value: a valid #GValue of %G_TYPE_BOXED derived type
503 * @v_boxed: (allow-none): static boxed value to be set
505 * Set the contents of a %G_TYPE_BOXED derived #GValue to @v_boxed.
506 * The boxed value is assumed to be static, and is thus not duplicated
507 * when setting the #GValue.
510 g_value_set_static_boxed (GValue *value,
513 g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
514 g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
516 value_set_boxed_internal (value, boxed, FALSE, FALSE);
520 * g_value_set_boxed_take_ownership:
521 * @value: a valid #GValue of %G_TYPE_BOXED derived type
522 * @v_boxed: (allow-none): duplicated unowned boxed value to be set
524 * This is an internal function introduced mainly for C marshallers.
526 * Deprecated: 2.4: Use g_value_take_boxed() instead.
529 g_value_set_boxed_take_ownership (GValue *value,
532 g_value_take_boxed (value, boxed);
536 * g_value_take_boxed:
537 * @value: a valid #GValue of %G_TYPE_BOXED derived type
538 * @v_boxed: (allow-none): duplicated unowned boxed value to be set
540 * Sets the contents of a %G_TYPE_BOXED derived #GValue to @v_boxed
541 * and takes over the ownership of the callers reference to @v_boxed;
542 * the caller doesn't have to unref it any more.
547 g_value_take_boxed (GValue *value,
550 g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
551 g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
553 value_set_boxed_internal (value, boxed, FALSE, TRUE);