cleanup
[platform/upstream/glib.git] / gobject / gboxed.c
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 2000-2001 Red Hat, Inc.
3  *
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.
8  *
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.
13  *
14  * You should have received a copy of the GNU Lesser General
15  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "config.h"
19
20 #include <string.h>
21
22 /* for GValueArray */
23 #define GLIB_DISABLE_DEPRECATION_WARNINGS
24
25 #include "gboxed.h"
26 #include "gclosure.h"
27 #include "gtype-private.h"
28 #include "gvalue.h"
29 #include "gvaluearray.h"
30 #include "gvaluecollector.h"
31
32
33 /**
34  * SECTION:gboxed
35  * @short_description: A mechanism to wrap opaque C structures registered
36  *     by the type system
37  * @see_also: #GParamSpecBoxed, g_param_spec_boxed()
38  * @title: Boxed Types
39  *
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.
43  *
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
46  * based libraries.
47  */
48
49 static inline void              /* keep this function in sync with gvalue.c */
50 value_meminit (GValue *value,
51                GType   value_type)
52 {
53   value->g_type = value_type;
54   memset (value->data, 0, sizeof (value->data));
55 }
56
57 static GValue *
58 value_copy (GValue *src_value)
59 {
60   GValue *dest_value = g_new0 (GValue, 1);
61
62   if (G_VALUE_TYPE (src_value))
63     {
64       g_value_init (dest_value, G_VALUE_TYPE (src_value));
65       g_value_copy (src_value, dest_value);
66     }
67   return dest_value;
68 }
69
70 static void
71 value_free (GValue *value)
72 {
73   if (G_VALUE_TYPE (value))
74     g_value_unset (value);
75   g_free (value);
76 }
77
78 static GPollFD *
79 pollfd_copy (GPollFD *src)
80 {
81   GPollFD *dest = g_new0 (GPollFD, 1);
82   /* just a couple of integers */
83   memcpy (dest, src, sizeof (GPollFD));
84   return dest;
85 }
86
87 void
88 _g_boxed_type_init (void)
89 {
90   const GTypeInfo info = {
91     0,                          /* class_size */
92     NULL,                       /* base_init */
93     NULL,                       /* base_destroy */
94     NULL,                       /* class_init */
95     NULL,                       /* class_destroy */
96     NULL,                       /* class_data */
97     0,                          /* instance_size */
98     0,                          /* n_preallocs */
99     NULL,                       /* instance_init */
100     NULL,                       /* value_table */
101   };
102   const GTypeFundamentalInfo finfo = { G_TYPE_FLAG_DERIVABLE, };
103   GType type;
104
105   /* G_TYPE_BOXED
106    */
107   type = g_type_register_fundamental (G_TYPE_BOXED, g_intern_static_string ("GBoxed"), &info, &finfo,
108                                       G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT);
109   g_assert (type == G_TYPE_BOXED);
110 }
111
112 static GDate *
113 gdate_copy (GDate *date)
114 {
115   return g_date_new_julian (g_date_get_julian (date));
116 }
117
118 static GString *
119 gstring_copy (GString *src_gstring)
120 {
121   return g_string_new_len (src_gstring->str, src_gstring->len);
122 }
123
124 static void
125 gstring_free (GString *gstring)
126 {
127   g_string_free (gstring, TRUE);
128 }
129
130 G_DEFINE_BOXED_TYPE (GClosure, g_closure, g_closure_ref, g_closure_unref)
131 G_DEFINE_BOXED_TYPE (GValue, g_value, value_copy, value_free)
132 G_DEFINE_BOXED_TYPE (GValueArray, g_value_array, g_value_array_copy, g_value_array_free)
133 G_DEFINE_BOXED_TYPE (GDate, g_date, gdate_copy, g_date_free)
134 /* the naming is a bit odd, but GString is obviously not G_TYPE_STRING */
135 G_DEFINE_BOXED_TYPE (GString, g_gstring, gstring_copy, gstring_free)
136 G_DEFINE_BOXED_TYPE (GHashTable, g_hash_table, g_hash_table_ref, g_hash_table_unref)
137 G_DEFINE_BOXED_TYPE (GArray, g_array, g_array_ref, g_array_unref)
138 G_DEFINE_BOXED_TYPE (GPtrArray, g_ptr_array,g_ptr_array_ref, g_ptr_array_unref)
139 G_DEFINE_BOXED_TYPE (GByteArray, g_byte_array, g_byte_array_ref, g_byte_array_unref)
140 G_DEFINE_BOXED_TYPE (GBytes, g_bytes, g_bytes_ref, g_bytes_unref);
141
142 G_DEFINE_BOXED_TYPE (GRegex, g_regex, g_regex_ref, g_regex_unref)
143 G_DEFINE_BOXED_TYPE (GMatchInfo, g_match_info, g_match_info_ref, g_match_info_unref)
144
145 #define g_variant_type_get_type g_variant_type_get_gtype
146 G_DEFINE_BOXED_TYPE (GVariantType, g_variant_type, g_variant_type_copy, g_variant_type_free)
147 #undef g_variant_type_get_type
148
149 G_DEFINE_BOXED_TYPE (GVariantBuilder, g_variant_builder, g_variant_builder_ref, g_variant_builder_unref)
150 G_DEFINE_BOXED_TYPE (GVariantDict, g_variant_dict, g_variant_dict_ref, g_variant_dict_unref)
151
152 G_DEFINE_BOXED_TYPE (GError, g_error, g_error_copy, g_error_free)
153
154 G_DEFINE_BOXED_TYPE (GDateTime, g_date_time, g_date_time_ref, g_date_time_unref);
155 G_DEFINE_BOXED_TYPE (GTimeZone, g_time_zone, g_time_zone_ref, g_time_zone_unref);
156 G_DEFINE_BOXED_TYPE (GKeyFile, g_key_file, g_key_file_ref, g_key_file_unref)
157 G_DEFINE_BOXED_TYPE (GMappedFile, g_mapped_file, g_mapped_file_ref, g_mapped_file_unref)
158
159 G_DEFINE_BOXED_TYPE (GMainLoop, g_main_loop, g_main_loop_ref, g_main_loop_unref)
160 G_DEFINE_BOXED_TYPE (GMainContext, g_main_context, g_main_context_ref, g_main_context_unref)
161 G_DEFINE_BOXED_TYPE (GSource, g_source, g_source_ref, g_source_unref)
162 G_DEFINE_BOXED_TYPE (GPollFD, g_pollfd, pollfd_copy, g_free)
163 G_DEFINE_BOXED_TYPE (GMarkupParseContext, g_markup_parse_context, g_markup_parse_context_ref, g_markup_parse_context_unref)
164
165 G_DEFINE_BOXED_TYPE (GThread, g_thread, g_thread_ref, g_thread_unref)
166 G_DEFINE_BOXED_TYPE (GChecksum, g_checksum, g_checksum_copy, g_checksum_free)
167
168 /* This one can't use G_DEFINE_BOXED_TYPE (GStrv, g_strv, g_strdupv, g_strfreev) */
169 GType
170 g_strv_get_type (void)
171 {
172   static volatile gsize g_define_type_id__volatile = 0;
173
174   if (g_once_init_enter (&g_define_type_id__volatile))
175     {
176       GType g_define_type_id =
177         g_boxed_type_register_static (g_intern_static_string ("GStrv"),
178                                       (GBoxedCopyFunc) g_strdupv,
179                                       (GBoxedFreeFunc) g_strfreev);
180
181       g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
182     }
183
184   return g_define_type_id__volatile;
185 }
186
187 /**
188  * g_variant_get_gtype:
189  *
190  * Since: 2.24
191  * Deprecated: 2.26
192  */
193 GType
194 g_variant_get_gtype (void)
195 {
196   return G_TYPE_VARIANT;
197 }
198
199 static void
200 boxed_proxy_value_init (GValue *value)
201 {
202   value->data[0].v_pointer = NULL;
203 }
204
205 static void
206 boxed_proxy_value_free (GValue *value)
207 {
208   if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
209     _g_type_boxed_free (G_VALUE_TYPE (value), value->data[0].v_pointer);
210 }
211
212 static void
213 boxed_proxy_value_copy (const GValue *src_value,
214                         GValue       *dest_value)
215 {
216   if (src_value->data[0].v_pointer)
217     dest_value->data[0].v_pointer = _g_type_boxed_copy (G_VALUE_TYPE (src_value), src_value->data[0].v_pointer);
218   else
219     dest_value->data[0].v_pointer = src_value->data[0].v_pointer;
220 }
221
222 static gpointer
223 boxed_proxy_value_peek_pointer (const GValue *value)
224 {
225   return value->data[0].v_pointer;
226 }
227
228 static gchar*
229 boxed_proxy_collect_value (GValue      *value,
230                            guint        n_collect_values,
231                            GTypeCValue *collect_values,
232                            guint        collect_flags)
233 {
234   if (!collect_values[0].v_pointer)
235     value->data[0].v_pointer = NULL;
236   else
237     {
238       if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
239         {
240           value->data[0].v_pointer = collect_values[0].v_pointer;
241           value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
242         }
243       else
244         value->data[0].v_pointer = _g_type_boxed_copy (G_VALUE_TYPE (value), collect_values[0].v_pointer);
245     }
246
247   return NULL;
248 }
249
250 static gchar*
251 boxed_proxy_lcopy_value (const GValue *value,
252                          guint         n_collect_values,
253                          GTypeCValue  *collect_values,
254                          guint         collect_flags)
255 {
256   gpointer *boxed_p = collect_values[0].v_pointer;
257
258   if (!boxed_p)
259     return g_strdup_printf ("value location for '%s' passed as NULL", G_VALUE_TYPE_NAME (value));
260
261   if (!value->data[0].v_pointer)
262     *boxed_p = NULL;
263   else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
264     *boxed_p = value->data[0].v_pointer;
265   else
266     *boxed_p = _g_type_boxed_copy (G_VALUE_TYPE (value), value->data[0].v_pointer);
267
268   return NULL;
269 }
270
271 /**
272  * g_boxed_type_register_static:
273  * @name: Name of the new boxed type.
274  * @boxed_copy: Boxed structure copy function.
275  * @boxed_free: Boxed structure free function.
276  *
277  * This function creates a new %G_TYPE_BOXED derived type id for a new
278  * boxed type with name @name. Boxed type handling functions have to be
279  * provided to copy and free opaque boxed structures of this type.
280  *
281  * Returns: New %G_TYPE_BOXED derived type id for @name.
282  */
283 GType
284 g_boxed_type_register_static (const gchar   *name,
285                               GBoxedCopyFunc boxed_copy,
286                               GBoxedFreeFunc boxed_free)
287 {
288   static const GTypeValueTable vtable = {
289     boxed_proxy_value_init,
290     boxed_proxy_value_free,
291     boxed_proxy_value_copy,
292     boxed_proxy_value_peek_pointer,
293     "p",
294     boxed_proxy_collect_value,
295     "p",
296     boxed_proxy_lcopy_value,
297   };
298   GTypeInfo type_info = {
299     0,                  /* class_size */
300     NULL,               /* base_init */
301     NULL,               /* base_finalize */
302     NULL,               /* class_init */
303     NULL,               /* class_finalize */
304     NULL,               /* class_data */
305     0,                  /* instance_size */
306     0,                  /* n_preallocs */
307     NULL,               /* instance_init */
308     &vtable,            /* value_table */
309   };
310   GType type;
311
312   g_return_val_if_fail (name != NULL, 0);
313   g_return_val_if_fail (boxed_copy != NULL, 0);
314   g_return_val_if_fail (boxed_free != NULL, 0);
315   g_return_val_if_fail (g_type_from_name (name) == 0, 0);
316
317   type = g_type_register_static (G_TYPE_BOXED, name, &type_info, 0);
318
319   /* install proxy functions upon successful registration */
320   if (type)
321     _g_type_boxed_init (type, boxed_copy, boxed_free);
322
323   return type;
324 }
325
326 /**
327  * g_boxed_copy:
328  * @boxed_type: The type of @src_boxed.
329  * @src_boxed: The boxed structure to be copied.
330  * 
331  * Provide a copy of a boxed structure @src_boxed which is of type @boxed_type.
332  * 
333  * Returns: (transfer full): The newly created copy of the boxed structure.
334  */
335 gpointer
336 g_boxed_copy (GType         boxed_type,
337               gconstpointer src_boxed)
338 {
339   GTypeValueTable *value_table;
340   gpointer dest_boxed;
341
342   g_return_val_if_fail (G_TYPE_IS_BOXED (boxed_type), NULL);
343   g_return_val_if_fail (G_TYPE_IS_ABSTRACT (boxed_type) == FALSE, NULL);
344   g_return_val_if_fail (src_boxed != NULL, NULL);
345
346   value_table = g_type_value_table_peek (boxed_type);
347   if (!value_table)
348     g_return_val_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type), NULL);
349
350   /* check if our proxying implementation is used, we can short-cut here */
351   if (value_table->value_copy == boxed_proxy_value_copy)
352     dest_boxed = _g_type_boxed_copy (boxed_type, (gpointer) src_boxed);
353   else
354     {
355       GValue src_value, dest_value;
356
357       /* we heavily rely on third-party boxed type value vtable
358        * implementations to follow normal boxed value storage
359        * (data[0].v_pointer is the boxed struct, and
360        * data[1].v_uint holds the G_VALUE_NOCOPY_CONTENTS flag,
361        * rest zero).
362        * but then, we can expect that since we laid out the
363        * g_boxed_*() API.
364        * data[1].v_uint&G_VALUE_NOCOPY_CONTENTS shouldn't be set
365        * after a copy.
366        */
367       /* equiv. to g_value_set_static_boxed() */
368       value_meminit (&src_value, boxed_type);
369       src_value.data[0].v_pointer = (gpointer) src_boxed;
370       src_value.data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
371
372       /* call third-party code copy function, fingers-crossed */
373       value_meminit (&dest_value, boxed_type);
374       value_table->value_copy (&src_value, &dest_value);
375
376       /* double check and grouse if things went wrong */
377       if (dest_value.data[1].v_ulong)
378         g_warning ("the copy_value() implementation of type '%s' seems to make use of reserved GValue fields",
379                    g_type_name (boxed_type));
380
381       dest_boxed = dest_value.data[0].v_pointer;
382     }
383
384   return dest_boxed;
385 }
386
387 /**
388  * g_boxed_free:
389  * @boxed_type: The type of @boxed.
390  * @boxed: The boxed structure to be freed.
391  *
392  * Free the boxed structure @boxed which is of type @boxed_type.
393  */
394 void
395 g_boxed_free (GType    boxed_type,
396               gpointer boxed)
397 {
398   GTypeValueTable *value_table;
399
400   g_return_if_fail (G_TYPE_IS_BOXED (boxed_type));
401   g_return_if_fail (G_TYPE_IS_ABSTRACT (boxed_type) == FALSE);
402   g_return_if_fail (boxed != NULL);
403
404   value_table = g_type_value_table_peek (boxed_type);
405   if (!value_table)
406     g_return_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type));
407
408   /* check if our proxying implementation is used, we can short-cut here */
409   if (value_table->value_free == boxed_proxy_value_free)
410     _g_type_boxed_free (boxed_type, boxed);
411   else
412     {
413       GValue value;
414
415       /* see g_boxed_copy() on why we think we can do this */
416       value_meminit (&value, boxed_type);
417       value.data[0].v_pointer = boxed;
418       value_table->value_free (&value);
419     }
420 }
421
422 /**
423  * g_value_get_boxed:
424  * @value: a valid #GValue of %G_TYPE_BOXED derived type
425  *
426  * Get the contents of a %G_TYPE_BOXED derived #GValue.
427  *
428  * Returns: (transfer none): boxed contents of @value
429  */
430 gpointer
431 g_value_get_boxed (const GValue *value)
432 {
433   g_return_val_if_fail (G_VALUE_HOLDS_BOXED (value), NULL);
434   g_return_val_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)), NULL);
435
436   return value->data[0].v_pointer;
437 }
438
439 /**
440  * g_value_dup_boxed: (skip)
441  * @value: a valid #GValue of %G_TYPE_BOXED derived type
442  *
443  * Get the contents of a %G_TYPE_BOXED derived #GValue.  Upon getting,
444  * the boxed value is duplicated and needs to be later freed with
445  * g_boxed_free(), e.g. like: g_boxed_free (G_VALUE_TYPE (@value),
446  * return_value);
447  *
448  * Returns: boxed contents of @value
449  */
450 gpointer
451 g_value_dup_boxed (const GValue *value)
452 {
453   g_return_val_if_fail (G_VALUE_HOLDS_BOXED (value), NULL);
454   g_return_val_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)), NULL);
455
456   return value->data[0].v_pointer ? g_boxed_copy (G_VALUE_TYPE (value), value->data[0].v_pointer) : NULL;
457 }
458
459 static inline void
460 value_set_boxed_internal (GValue       *value,
461                           gconstpointer boxed,
462                           gboolean      need_copy,
463                           gboolean      need_free)
464 {
465   if (!boxed)
466     {
467       /* just resetting to NULL might not be desired, need to
468        * have value reinitialized also (for values defaulting
469        * to other default value states than a NULL data pointer),
470        * g_value_reset() will handle this
471        */
472       g_value_reset (value);
473       return;
474     }
475
476   if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
477     g_boxed_free (G_VALUE_TYPE (value), value->data[0].v_pointer);
478   value->data[1].v_uint = need_free ? 0 : G_VALUE_NOCOPY_CONTENTS;
479   value->data[0].v_pointer = need_copy ? g_boxed_copy (G_VALUE_TYPE (value), boxed) : (gpointer) boxed;
480 }
481
482 /**
483  * g_value_set_boxed:
484  * @value: a valid #GValue of %G_TYPE_BOXED derived type
485  * @v_boxed: (allow-none): boxed value to be set
486  *
487  * Set the contents of a %G_TYPE_BOXED derived #GValue to @v_boxed.
488  */
489 void
490 g_value_set_boxed (GValue       *value,
491                    gconstpointer boxed)
492 {
493   g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
494   g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
495
496   value_set_boxed_internal (value, boxed, TRUE, TRUE);
497 }
498
499 /**
500  * g_value_set_static_boxed:
501  * @value: a valid #GValue of %G_TYPE_BOXED derived type
502  * @v_boxed: (allow-none): static boxed value to be set
503  *
504  * Set the contents of a %G_TYPE_BOXED derived #GValue to @v_boxed.
505  * The boxed value is assumed to be static, and is thus not duplicated
506  * when setting the #GValue.
507  */
508 void
509 g_value_set_static_boxed (GValue       *value,
510                           gconstpointer boxed)
511 {
512   g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
513   g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
514
515   value_set_boxed_internal (value, boxed, FALSE, FALSE);
516 }
517
518 /**
519  * g_value_set_boxed_take_ownership:
520  * @value: a valid #GValue of %G_TYPE_BOXED derived type
521  * @v_boxed: (allow-none): duplicated unowned boxed value to be set
522  *
523  * This is an internal function introduced mainly for C marshallers.
524  *
525  * Deprecated: 2.4: Use g_value_take_boxed() instead.
526  */
527 void
528 g_value_set_boxed_take_ownership (GValue       *value,
529                                   gconstpointer boxed)
530 {
531   g_value_take_boxed (value, boxed);
532 }
533
534 /**
535  * g_value_take_boxed:
536  * @value: a valid #GValue of %G_TYPE_BOXED derived type
537  * @v_boxed: (allow-none): duplicated unowned boxed value to be set
538  *
539  * Sets the contents of a %G_TYPE_BOXED derived #GValue to @v_boxed
540  * and takes over the ownership of the callers reference to @v_boxed;
541  * the caller doesn't have to unref it any more.
542  *
543  * Since: 2.4
544  */
545 void
546 g_value_take_boxed (GValue       *value,
547                     gconstpointer boxed)
548 {
549   g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
550   g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
551
552   value_set_boxed_internal (value, boxed, FALSE, TRUE);
553 }