Fix #117925 (Dov Grobgeld):
[glib.git] / gobject / gboxed.c
blobee6e432ea7ae49ba17abda9a6ee6e52b27323bad
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.
19 #include "gboxed.h"
21 #include "gbsearcharray.h"
22 #include "gvalue.h"
23 #include "gvaluearray.h"
24 #include "gclosure.h"
25 #include "gvaluecollector.h"
27 #include <string.h>
29 /* --- typedefs & structures --- */
30 typedef struct
32 GType type;
33 GBoxedCopyFunc copy;
34 GBoxedFreeFunc free;
35 } BoxedNode;
38 /* --- prototypes --- */
39 static gint boxed_nodes_cmp (gconstpointer p1,
40 gconstpointer p2);
43 /* --- variables --- */
44 static GBSearchArray *boxed_bsa = NULL;
45 static GBSearchConfig boxed_bconfig = G_STATIC_BCONFIG (sizeof (BoxedNode), boxed_nodes_cmp, 0);
48 /* --- functions --- */
49 static gint
50 boxed_nodes_cmp (gconstpointer p1,
51 gconstpointer p2)
53 const BoxedNode *node1 = p1, *node2 = p2;
55 return G_BSEARCH_ARRAY_CMP (node1->type, node2->type);
58 static inline void /* keep this function in sync with gvalue.c */
59 value_meminit (GValue *value,
60 GType value_type)
62 value->g_type = value_type;
63 memset (value->data, 0, sizeof (value->data));
66 static gpointer
67 value_copy (gpointer boxed)
69 const GValue *src_value = boxed;
70 GValue *dest_value = g_new0 (GValue, 1);
72 if (G_VALUE_TYPE (src_value))
74 g_value_init (dest_value, G_VALUE_TYPE (src_value));
75 g_value_copy (src_value, dest_value);
77 return dest_value;
80 static void
81 value_free (gpointer boxed)
83 GValue *value = boxed;
85 if (G_VALUE_TYPE (value))
86 g_value_unset (value);
87 g_free (value);
90 static gpointer
91 gstring_copy (gpointer boxed)
93 const GString *src_gstring = boxed;
95 return g_string_new_len (src_gstring->str, src_gstring->len);
98 static void
99 gstring_free (gpointer boxed)
101 GString *gstring = boxed;
103 g_string_free (gstring, TRUE);
106 void
107 g_boxed_type_init (void) /* sync with gtype.c */
109 static const GTypeInfo info = {
110 0, /* class_size */
111 NULL, /* base_init */
112 NULL, /* base_destroy */
113 NULL, /* class_init */
114 NULL, /* class_destroy */
115 NULL, /* class_data */
116 0, /* instance_size */
117 0, /* n_preallocs */
118 NULL, /* instance_init */
119 NULL, /* value_table */
121 const GTypeFundamentalInfo finfo = { G_TYPE_FLAG_DERIVABLE, };
122 GType type;
124 boxed_bsa = g_bsearch_array_new (&boxed_bconfig);
126 /* G_TYPE_BOXED
128 type = g_type_register_fundamental (G_TYPE_BOXED, "GBoxed", &info, &finfo,
129 G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT);
130 g_assert (type == G_TYPE_BOXED);
133 GType
134 g_closure_get_type (void)
136 static GType type_id = 0;
138 if (!type_id)
139 type_id = g_boxed_type_register_static ("GClosure",
140 (GBoxedCopyFunc) g_closure_ref,
141 (GBoxedFreeFunc) g_closure_unref);
142 return type_id;
145 GType
146 g_value_get_type (void)
148 static GType type_id = 0;
150 if (!type_id)
151 type_id = g_boxed_type_register_static ("GValue",
152 value_copy,
153 value_free);
154 return type_id;
157 GType
158 g_value_array_get_type (void)
160 static GType type_id = 0;
162 if (!type_id)
163 type_id = g_boxed_type_register_static ("GValueArray",
164 (GBoxedCopyFunc) g_value_array_copy,
165 (GBoxedFreeFunc) g_value_array_free);
166 return type_id;
169 GType
170 g_gstring_get_type (void)
172 static GType type_id = 0;
174 if (!type_id)
175 type_id = g_boxed_type_register_static ("GString", /* the naming is a bit odd, but GString is obviously not G_TYPE_STRING */
176 gstring_copy,
177 gstring_free);
178 return type_id;
181 static void
182 boxed_proxy_value_init (GValue *value)
184 value->data[0].v_pointer = NULL;
187 static void
188 boxed_proxy_value_free (GValue *value)
190 if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
192 BoxedNode key, *node;
194 key.type = G_VALUE_TYPE (value);
195 node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
196 node->free (value->data[0].v_pointer);
200 static void
201 boxed_proxy_value_copy (const GValue *src_value,
202 GValue *dest_value)
204 if (src_value->data[0].v_pointer)
206 BoxedNode key, *node;
208 key.type = G_VALUE_TYPE (src_value);
209 node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
210 dest_value->data[0].v_pointer = node->copy (src_value->data[0].v_pointer);
212 else
213 dest_value->data[0].v_pointer = src_value->data[0].v_pointer;
216 static gpointer
217 boxed_proxy_value_peek_pointer (const GValue *value)
219 return value->data[0].v_pointer;
222 static gchar*
223 boxed_proxy_collect_value (GValue *value,
224 guint n_collect_values,
225 GTypeCValue *collect_values,
226 guint collect_flags)
228 BoxedNode key, *node;
230 key.type = G_VALUE_TYPE (value);
231 node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
233 if (!collect_values[0].v_pointer)
234 value->data[0].v_pointer = NULL;
235 else
237 if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
239 value->data[0].v_pointer = collect_values[0].v_pointer;
240 value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
242 else
243 value->data[0].v_pointer = node->copy (collect_values[0].v_pointer);
246 return NULL;
249 static gchar*
250 boxed_proxy_lcopy_value (const GValue *value,
251 guint n_collect_values,
252 GTypeCValue *collect_values,
253 guint collect_flags)
255 gpointer *boxed_p = collect_values[0].v_pointer;
257 if (!boxed_p)
258 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
260 if (!value->data[0].v_pointer)
261 *boxed_p = NULL;
262 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
263 *boxed_p = value->data[0].v_pointer;
264 else
266 BoxedNode key, *node;
268 key.type = G_VALUE_TYPE (value);
269 node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
270 *boxed_p = node->copy (value->data[0].v_pointer);
273 return NULL;
276 GType
277 g_boxed_type_register_static (const gchar *name,
278 GBoxedCopyFunc boxed_copy,
279 GBoxedFreeFunc boxed_free)
281 static const GTypeValueTable vtable = {
282 boxed_proxy_value_init,
283 boxed_proxy_value_free,
284 boxed_proxy_value_copy,
285 boxed_proxy_value_peek_pointer,
286 "p",
287 boxed_proxy_collect_value,
288 "p",
289 boxed_proxy_lcopy_value,
291 static const GTypeInfo type_info = {
292 0, /* class_size */
293 NULL, /* base_init */
294 NULL, /* base_finalize */
295 NULL, /* class_init */
296 NULL, /* class_finalize */
297 NULL, /* class_data */
298 0, /* instance_size */
299 0, /* n_preallocs */
300 NULL, /* instance_init */
301 &vtable, /* value_table */
303 GType type;
305 g_return_val_if_fail (name != NULL, 0);
306 g_return_val_if_fail (boxed_copy != NULL, 0);
307 g_return_val_if_fail (boxed_free != NULL, 0);
308 g_return_val_if_fail (g_type_from_name (name) == 0, 0);
310 type = g_type_register_static (G_TYPE_BOXED, name, &type_info, 0);
312 /* install proxy functions upon successfull registration */
313 if (type)
315 BoxedNode key;
317 key.type = type;
318 key.copy = boxed_copy;
319 key.free = boxed_free;
320 boxed_bsa = g_bsearch_array_insert (boxed_bsa, &boxed_bconfig, &key, TRUE);
323 return type;
326 gpointer
327 g_boxed_copy (GType boxed_type,
328 gconstpointer src_boxed)
330 GTypeValueTable *value_table;
331 gpointer dest_boxed;
333 g_return_val_if_fail (G_TYPE_IS_BOXED (boxed_type), NULL);
334 g_return_val_if_fail (G_TYPE_IS_ABSTRACT (boxed_type) == FALSE, NULL);
335 g_return_val_if_fail (src_boxed != NULL, NULL);
337 value_table = g_type_value_table_peek (boxed_type);
338 if (!value_table)
339 g_return_val_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type), NULL);
341 /* check if our proxying implementation is used, we can short-cut here */
342 if (value_table->value_copy == boxed_proxy_value_copy)
344 BoxedNode key, *node;
346 key.type = boxed_type;
347 node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
348 dest_boxed = node->copy ((gpointer) src_boxed);
350 else
352 GValue src_value, dest_value;
354 /* we heavily rely on third-party boxed type value vtable
355 * implementations to follow normal boxed value storage
356 * (data[0].v_pointer is the boxed struct, and
357 * data[1].v_uint holds the G_VALUE_NOCOPY_CONTENTS flag,
358 * rest zero).
359 * but then, we can expect that since we layed out the
360 * g_boxed_*() API.
361 * data[1].v_uint&G_VALUE_NOCOPY_CONTENTS shouldn't be set
362 * after a copy.
364 /* equiv. to g_value_set_static_boxed() */
365 value_meminit (&src_value, boxed_type);
366 src_value.data[0].v_pointer = (gpointer) src_boxed;
367 src_value.data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
369 /* call third-party code copy function, fingers-crossed */
370 value_meminit (&dest_value, boxed_type);
371 value_table->value_copy (&src_value, &dest_value);
373 /* double check and grouse if things went wrong */
374 if (dest_value.data[1].v_ulong)
375 g_warning ("the copy_value() implementation of type `%s' seems to make use of reserved GValue fields",
376 g_type_name (boxed_type));
378 dest_boxed = dest_value.data[0].v_pointer;
381 return dest_boxed;
384 void
385 g_boxed_free (GType boxed_type,
386 gpointer boxed)
388 GTypeValueTable *value_table;
390 g_return_if_fail (G_TYPE_IS_BOXED (boxed_type));
391 g_return_if_fail (G_TYPE_IS_ABSTRACT (boxed_type) == FALSE);
392 g_return_if_fail (boxed != NULL);
394 value_table = g_type_value_table_peek (boxed_type);
395 if (!value_table)
396 g_return_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type));
398 /* check if our proxying implementation is used, we can short-cut here */
399 if (value_table->value_free == boxed_proxy_value_free)
401 BoxedNode key, *node;
403 key.type = boxed_type;
404 node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
405 node->free (boxed);
407 else
409 GValue value;
411 /* see g_boxed_copy() on why we think we can do this */
412 value_meminit (&value, boxed_type);
413 value.data[0].v_pointer = boxed;
414 value_table->value_free (&value);
418 gpointer
419 g_value_get_boxed (const GValue *value)
421 g_return_val_if_fail (G_VALUE_HOLDS_BOXED (value), NULL);
422 g_return_val_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)), NULL);
424 return value->data[0].v_pointer;
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 const_boxed,
439 gboolean need_copy,
440 gboolean need_free)
442 BoxedNode key, *node;
443 gpointer boxed = (gpointer) const_boxed;
445 if (!boxed)
447 /* just resetting to NULL might not be desired, need to
448 * have value reinitialized also (for values defaulting
449 * to other default value states than a NULL data pointer),
450 * g_value_reset() will handle this
452 g_value_reset (value);
453 return;
456 key.type = G_VALUE_TYPE (value);
457 node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key);
459 if (node)
461 /* we proxy this type, free contents and copy right away */
462 if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
463 node->free (value->data[0].v_pointer);
464 value->data[1].v_uint = need_free ? 0 : G_VALUE_NOCOPY_CONTENTS;
465 value->data[0].v_pointer = need_copy ? node->copy (boxed) : boxed;
467 else
469 /* we don't handle this type, free contents and let g_boxed_copy()
470 * figure what's required
472 if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
473 g_boxed_free (G_VALUE_TYPE (value), value->data[0].v_pointer);
474 value->data[1].v_uint = need_free ? 0 : G_VALUE_NOCOPY_CONTENTS;
475 value->data[0].v_pointer = need_copy ? g_boxed_copy (G_VALUE_TYPE (value), boxed) : boxed;
479 void
480 g_value_set_boxed (GValue *value,
481 gconstpointer boxed)
483 g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
484 g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
486 value_set_boxed_internal (value, boxed, TRUE, TRUE);
489 void
490 g_value_set_static_boxed (GValue *value,
491 gconstpointer boxed)
493 g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
494 g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
496 value_set_boxed_internal (value, boxed, FALSE, FALSE);
499 void
500 g_value_set_boxed_take_ownership (GValue *value,
501 gconstpointer boxed)
503 g_return_if_fail (G_VALUE_HOLDS_BOXED (value));
504 g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)));
506 value_set_boxed_internal (value, boxed, FALSE, TRUE);