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