Don't use -w in shebang line
[glib.git] / gobject / gboxed.c
blob06b3cf2934465cc28ae10b76ee7d413d41abce0f
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.
20 #include "config.h"
22 #include <string.h>
24 #include "gboxed.h"
25 #include "gtype-private.h"
26 #include "gvalue.h"
27 #include "gvaluearray.h"
28 #include "gclosure.h"
29 #include "gvaluecollector.h"
32 /**
33 * SECTION:gboxed
34 * @short_description: A mechanism to wrap opaque C structures registered
35 * by the type system
36 * @see_also: #GParamSpecBoxed, g_param_spec_boxed()
37 * @title: Boxed Types
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
45 * based libraries.
48 static inline void /* keep this function in sync with gvalue.c */
49 value_meminit (GValue *value,
50 GType value_type)
52 value->g_type = value_type;
53 memset (value->data, 0, sizeof (value->data));
56 static GValue *
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);
66 return dest_value;
69 static void
70 value_free (GValue *value)
72 if (G_VALUE_TYPE (value))
73 g_value_unset (value);
74 g_free (value);
77 void
78 g_boxed_type_init (void)
80 static const GTypeInfo info = {
81 0, /* class_size */
82 NULL, /* base_init */
83 NULL, /* base_destroy */
84 NULL, /* class_init */
85 NULL, /* class_destroy */
86 NULL, /* class_data */
87 0, /* instance_size */
88 0, /* n_preallocs */
89 NULL, /* instance_init */
90 NULL, /* value_table */
92 const GTypeFundamentalInfo finfo = { G_TYPE_FLAG_DERIVABLE, };
93 GType type;
95 /* G_TYPE_BOXED
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);
102 static GDate *
103 gdate_copy (GDate *date)
105 return g_date_new_julian (g_date_get_julian (date));
108 static GString *
109 gstring_copy (GString *src_gstring)
111 return g_string_new_len (src_gstring->str, src_gstring->len);
114 static void
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)
131 #ifdef ENABLE_REGEX
132 G_DEFINE_BOXED_TYPE (GRegex, g_regex, g_regex_ref, g_regex_unref)
133 #else
134 GType g_regex_get_type (void) { return G_TYPE_INVALID; }
135 #endif /* ENABLE_REGEX */
137 #define g_variant_type_get_type g_variant_type_get_gtype
138 G_DEFINE_BOXED_TYPE (GVariantType, g_variant_type, g_variant_type_copy, g_variant_type_free)
139 #undef g_variant_type_get_type
141 G_DEFINE_BOXED_TYPE (GError, g_error, g_error_copy, g_error_free)
143 G_DEFINE_BOXED_TYPE (GDateTime, g_date_time, g_date_time_ref, g_date_time_unref);
145 /* This one can't use G_DEFINE_BOXED_TYPE (GStrv, g_strv, g_strdupv, g_strfreev) */
146 GType
147 g_strv_get_type (void)
149 static volatile gsize g_define_type_id__volatile = 0;
151 if (g_once_init_enter (&g_define_type_id__volatile))
153 GType g_define_type_id =
154 g_boxed_type_register_static (g_intern_static_string ("GStrv"),
155 (GBoxedCopyFunc) g_strdupv,
156 (GBoxedFreeFunc) g_strfreev);
158 g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
161 return g_define_type_id__volatile;
165 * g_variant_get_gtype:
167 * Since: 2.24
168 * Deprecated: 2.26
170 GType
171 g_variant_get_gtype (void)
173 return G_TYPE_VARIANT;
176 static void
177 boxed_proxy_value_init (GValue *value)
179 value->data[0].v_pointer = NULL;
182 static void
183 boxed_proxy_value_free (GValue *value)
185 if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
186 _g_type_boxed_free (G_VALUE_TYPE (value), value->data[0].v_pointer);
189 static void
190 boxed_proxy_value_copy (const GValue *src_value,
191 GValue *dest_value)
193 if (src_value->data[0].v_pointer)
194 dest_value->data[0].v_pointer = _g_type_boxed_copy (G_VALUE_TYPE (src_value), src_value->data[0].v_pointer);
195 else
196 dest_value->data[0].v_pointer = src_value->data[0].v_pointer;
199 static gpointer
200 boxed_proxy_value_peek_pointer (const GValue *value)
202 return value->data[0].v_pointer;
205 static gchar*
206 boxed_proxy_collect_value (GValue *value,
207 guint n_collect_values,
208 GTypeCValue *collect_values,
209 guint collect_flags)
211 if (!collect_values[0].v_pointer)
212 value->data[0].v_pointer = NULL;
213 else
215 if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
217 value->data[0].v_pointer = collect_values[0].v_pointer;
218 value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
220 else
221 value->data[0].v_pointer = _g_type_boxed_copy (G_VALUE_TYPE (value), collect_values[0].v_pointer);
224 return NULL;
227 static gchar*
228 boxed_proxy_lcopy_value (const GValue *value,
229 guint n_collect_values,
230 GTypeCValue *collect_values,
231 guint collect_flags)
233 gpointer *boxed_p = collect_values[0].v_pointer;
235 if (!boxed_p)
236 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
238 if (!value->data[0].v_pointer)
239 *boxed_p = NULL;
240 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
241 *boxed_p = value->data[0].v_pointer;
242 else
243 *boxed_p = _g_type_boxed_copy (G_VALUE_TYPE (value), value->data[0].v_pointer);
245 return NULL;
249 * g_boxed_type_register_static:
250 * @name: Name of the new boxed type.
251 * @boxed_copy: Boxed structure copy function.
252 * @boxed_free: Boxed structure free function.
254 * This function creates a new %G_TYPE_BOXED derived type id for a new
255 * boxed type with name @name. Boxed type handling functions have to be
256 * provided to copy and free opaque boxed structures of this type.
258 * Returns: New %G_TYPE_BOXED derived type id for @name.
260 GType
261 g_boxed_type_register_static (const gchar *name,
262 GBoxedCopyFunc boxed_copy,
263 GBoxedFreeFunc boxed_free)
265 static const GTypeValueTable vtable = {
266 boxed_proxy_value_init,
267 boxed_proxy_value_free,
268 boxed_proxy_value_copy,
269 boxed_proxy_value_peek_pointer,
270 "p",
271 boxed_proxy_collect_value,
272 "p",
273 boxed_proxy_lcopy_value,
275 GTypeInfo type_info = {
276 0, /* class_size */
277 NULL, /* base_init */
278 NULL, /* base_finalize */
279 NULL, /* class_init */
280 NULL, /* class_finalize */
281 NULL, /* class_data */
282 0, /* instance_size */
283 0, /* n_preallocs */
284 NULL, /* instance_init */
285 &vtable, /* value_table */
287 GType type;
289 g_return_val_if_fail (name != NULL, 0);
290 g_return_val_if_fail (boxed_copy != NULL, 0);
291 g_return_val_if_fail (boxed_free != NULL, 0);
292 g_return_val_if_fail (g_type_from_name (name) == 0, 0);
294 type = g_type_register_static (G_TYPE_BOXED, name, &type_info, 0);
296 /* install proxy functions upon successfull registration */
297 if (type)
298 _g_type_boxed_init (type, boxed_copy, boxed_free);
300 return type;
304 * g_boxed_copy:
305 * @boxed_type: The type of @src_boxed.
306 * @src_boxed: The boxed structure to be copied.
308 * Provide a copy of a boxed structure @src_boxed which is of type @boxed_type.
310 * Returns: The newly created copy of the boxed structure.
312 gpointer
313 g_boxed_copy (GType boxed_type,
314 gconstpointer src_boxed)
316 GTypeValueTable *value_table;
317 gpointer dest_boxed;
319 g_return_val_if_fail (G_TYPE_IS_BOXED (boxed_type), NULL);
320 g_return_val_if_fail (G_TYPE_IS_ABSTRACT (boxed_type) == FALSE, NULL);
321 g_return_val_if_fail (src_boxed != NULL, NULL);
323 value_table = g_type_value_table_peek (boxed_type);
324 if (!value_table)
325 g_return_val_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type), NULL);
327 /* check if our proxying implementation is used, we can short-cut here */
328 if (value_table->value_copy == boxed_proxy_value_copy)
329 dest_boxed = _g_type_boxed_copy (boxed_type, (gpointer) src_boxed);
330 else
332 GValue src_value, dest_value;
334 /* we heavily rely on third-party boxed type value vtable
335 * implementations to follow normal boxed value storage
336 * (data[0].v_pointer is the boxed struct, and
337 * data[1].v_uint holds the G_VALUE_NOCOPY_CONTENTS flag,
338 * rest zero).
339 * but then, we can expect that since we layed out the
340 * g_boxed_*() API.
341 * data[1].v_uint&G_VALUE_NOCOPY_CONTENTS shouldn't be set
342 * after a copy.
344 /* equiv. to g_value_set_static_boxed() */
345 value_meminit (&src_value, boxed_type);
346 src_value.data[0].v_pointer = (gpointer) src_boxed;
347 src_value.data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
349 /* call third-party code copy function, fingers-crossed */
350 value_meminit (&dest_value, boxed_type);
351 value_table->value_copy (&src_value, &dest_value);
353 /* double check and grouse if things went wrong */
354 if (dest_value.data[1].v_ulong)
355 g_warning ("the copy_value() implementation of type `%s' seems to make use of reserved GValue fields",
356 g_type_name (boxed_type));
358 dest_boxed = dest_value.data[0].v_pointer;
361 return dest_boxed;
365 * g_boxed_free:
366 * @boxed_type: The type of @boxed.
367 * @boxed: The boxed structure to be freed.
369 * Free the boxed structure @boxed which is of type @boxed_type.
371 void
372 g_boxed_free (GType boxed_type,
373 gpointer boxed)
375 GTypeValueTable *value_table;
377 g_return_if_fail (G_TYPE_IS_BOXED (boxed_type));
378 g_return_if_fail (G_TYPE_IS_ABSTRACT (boxed_type) == FALSE);
379 g_return_if_fail (boxed != NULL);
381 value_table = g_type_value_table_peek (boxed_type);
382 if (!value_table)
383 g_return_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type));
385 /* check if our proxying implementation is used, we can short-cut here */
386 if (value_table->value_free == boxed_proxy_value_free)
387 _g_type_boxed_free (boxed_type, boxed);
388 else
390 GValue value;
392 /* see g_boxed_copy() on why we think we can do this */
393 value_meminit (&value, boxed_type);
394 value.data[0].v_pointer = boxed;
395 value_table->value_free (&value);
400 * g_value_get_boxed:
401 * @value: a valid #GValue of %G_TYPE_BOXED derived type
403 * Get the contents of a %G_TYPE_BOXED derived #GValue.
405 * Returns: (transfer none): boxed contents of @value
407 gpointer
408 g_value_get_boxed (const GValue *value)
410 g_return_val_if_fail (G_VALUE_HOLDS_BOXED (value), NULL);
411 g_return_val_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)), NULL);
413 return value->data[0].v_pointer;
417 * g_value_dup_boxed: (skip)
418 * @value: a valid #GValue of %G_TYPE_BOXED derived type
420 * Get the contents of a %G_TYPE_BOXED derived #GValue. Upon getting,
421 * the boxed value is duplicated and needs to be later freed with
422 * g_boxed_free(), e.g. like: g_boxed_free (G_VALUE_TYPE (@value),
423 * return_value);
425 * Returns: boxed contents of @value
427 gpointer
428 g_value_dup_boxed (const GValue *value)
430 g_return_val_if_fail (G_VALUE_HOLDS_BOXED (value), NULL);
431 g_return_val_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)), NULL);
433 return value->data[0].v_pointer ? g_boxed_copy (G_VALUE_TYPE (value), value->data[0].v_pointer) : NULL;
436 static inline void
437 value_set_boxed_internal (GValue *value,
438 gconstpointer boxed,
439 gboolean need_copy,
440 gboolean need_free)
442 if (!boxed)
444 /* just resetting to NULL might not be desired, need to
445 * have value reinitialized also (for values defaulting
446 * to other default value states than a NULL data pointer),
447 * g_value_reset() will handle this
449 g_value_reset (value);
450 return;
453 if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
454 g_boxed_free (G_VALUE_TYPE (value), value->data[0].v_pointer);
455 value->data[1].v_uint = need_free ? 0 : G_VALUE_NOCOPY_CONTENTS;
456 value->data[0].v_pointer = need_copy ? g_boxed_copy (G_VALUE_TYPE (value), boxed) : (gpointer) boxed;
460 * g_value_set_boxed:
461 * @value: a valid #GValue of %G_TYPE_BOXED derived type
462 * @v_boxed: boxed value to be set
464 * Set the contents of a %G_TYPE_BOXED derived #GValue to @v_boxed.
466 void
467 g_value_set_boxed (GValue *value,
468 gconstpointer boxed)
470 g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
471 g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
473 value_set_boxed_internal (value, boxed, TRUE, TRUE);
477 * g_value_set_static_boxed:
478 * @value: a valid #GValue of %G_TYPE_BOXED derived type
479 * @v_boxed: static boxed value to be set
481 * Set the contents of a %G_TYPE_BOXED derived #GValue to @v_boxed.
482 * The boxed value is assumed to be static, and is thus not duplicated
483 * when setting the #GValue.
485 void
486 g_value_set_static_boxed (GValue *value,
487 gconstpointer boxed)
489 g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
490 g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
492 value_set_boxed_internal (value, boxed, FALSE, FALSE);
496 * g_value_set_boxed_take_ownership:
497 * @value: a valid #GValue of %G_TYPE_BOXED derived type
498 * @v_boxed: duplicated unowned boxed value to be set
500 * This is an internal function introduced mainly for C marshallers.
502 * Deprecated: 2.4: Use g_value_take_boxed() instead.
504 void
505 g_value_set_boxed_take_ownership (GValue *value,
506 gconstpointer boxed)
508 g_value_take_boxed (value, boxed);
512 * g_value_take_boxed:
513 * @value: a valid #GValue of %G_TYPE_BOXED derived type
514 * @v_boxed: duplicated unowned boxed value to be set
516 * Sets the contents of a %G_TYPE_BOXED derived #GValue to @v_boxed
517 * and takes over the ownership of the callers reference to @v_boxed;
518 * the caller doesn't have to unref it any more.
520 * Since: 2.4
522 void
523 g_value_take_boxed (GValue *value,
524 gconstpointer boxed)
526 g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
527 g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
529 value_set_boxed_internal (value, boxed, FALSE, TRUE);