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