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