|
1 /* GObject - GLib Type, Object, Parameter and Signal Library |
|
2 * Copyright (C) 2000-2001 Red Hat, Inc. |
|
3 * Portions copyright (c) 2006-2009 Nokia Corporation. All rights reserved. |
|
4 * |
|
5 * This library is free software; you can redistribute it and/or |
|
6 * modify it under the terms of the GNU Lesser General Public |
|
7 * License as published by the Free Software Foundation; either |
|
8 * version 2 of the License, or (at your option) any later version. |
|
9 * |
|
10 * This library is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 * Lesser General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU Lesser General |
|
16 * Public License along with this library; if not, write to the |
|
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
|
18 * Boston, MA 02111-1307, USA. |
|
19 */ |
|
20 |
|
21 #include "config.h" |
|
22 |
|
23 #include <string.h> |
|
24 |
|
25 #include "gboxed.h" |
|
26 #include "gbsearcharray.h" |
|
27 #include "gvalue.h" |
|
28 #include "gvaluearray.h" |
|
29 #include "gclosure.h" |
|
30 #include "gvaluecollector.h" |
|
31 #include "gobjectalias.h" |
|
32 |
|
33 #ifdef __SYMBIAN32__ |
|
34 #include "gobject_wsd.h" |
|
35 #endif /* __SYMBIAN32__ */ |
|
36 |
|
37 /** |
|
38 * SECTION:gboxed |
|
39 * @short_description: A mechanism to wrap opaque C structures registered |
|
40 * by the type system |
|
41 * @see_also: #GParamSpecBoxed, g_param_spec_boxed() |
|
42 * @title: Boxed Types |
|
43 * |
|
44 * GBoxed is a generic wrapper mechanism for arbitrary C structures. The only |
|
45 * thing the type system needs to know about the structures is how to copy and |
|
46 * free them, beyond that they are treated as opaque chunks of memory. |
|
47 * |
|
48 * Boxed types are useful for simple value-holder structures like rectangles or |
|
49 * points. They can also be used for wrapping structures defined in non-GObject |
|
50 * based libraries. |
|
51 */ |
|
52 |
|
53 /* --- typedefs & structures --- */ |
|
54 typedef struct |
|
55 { |
|
56 GType type; |
|
57 GBoxedCopyFunc copy; |
|
58 GBoxedFreeFunc free; |
|
59 } BoxedNode; |
|
60 |
|
61 |
|
62 /* --- prototypes --- */ |
|
63 static gint boxed_nodes_cmp (gconstpointer p1, |
|
64 gconstpointer p2); |
|
65 |
|
66 |
|
67 /* --- variables --- */ |
|
68 #if EMULATOR |
|
69 PLS(boxed_bsa,gboxed,GBSearchArray *) |
|
70 #define boxed_bsa (*FUNCTION_NAME(boxed_bsa,gboxed)()) |
|
71 #else |
|
72 static GBSearchArray *boxed_bsa = NULL; |
|
73 #endif /*EMULATOR */ |
|
74 static const GBSearchConfig boxed_bconfig = { |
|
75 sizeof (BoxedNode), |
|
76 boxed_nodes_cmp, |
|
77 0, |
|
78 }; |
|
79 |
|
80 |
|
81 /* --- functions --- */ |
|
82 static gint |
|
83 boxed_nodes_cmp (gconstpointer p1, |
|
84 gconstpointer p2) |
|
85 { |
|
86 const BoxedNode *node1 = p1, *node2 = p2; |
|
87 |
|
88 return G_BSEARCH_ARRAY_CMP (node1->type, node2->type); |
|
89 } |
|
90 |
|
91 static inline void /* keep this function in sync with gvalue.c */ |
|
92 value_meminit (GValue *value, |
|
93 GType value_type) |
|
94 { |
|
95 value->g_type = value_type; |
|
96 memset (value->data, 0, sizeof (value->data)); |
|
97 } |
|
98 |
|
99 static gpointer |
|
100 value_copy (gpointer boxed) |
|
101 { |
|
102 const GValue *src_value = boxed; |
|
103 GValue *dest_value = g_new0 (GValue, 1); |
|
104 |
|
105 if (G_VALUE_TYPE (src_value)) |
|
106 { |
|
107 g_value_init (dest_value, G_VALUE_TYPE (src_value)); |
|
108 g_value_copy (src_value, dest_value); |
|
109 } |
|
110 return dest_value; |
|
111 } |
|
112 |
|
113 static void |
|
114 value_free (gpointer boxed) |
|
115 { |
|
116 GValue *value = boxed; |
|
117 |
|
118 if (G_VALUE_TYPE (value)) |
|
119 g_value_unset (value); |
|
120 g_free (value); |
|
121 } |
|
122 |
|
123 void |
|
124 g_boxed_type_init (void) |
|
125 { |
|
126 static const GTypeInfo info = { |
|
127 0, /* class_size */ |
|
128 NULL, /* base_init */ |
|
129 NULL, /* base_destroy */ |
|
130 NULL, /* class_init */ |
|
131 NULL, /* class_destroy */ |
|
132 NULL, /* class_data */ |
|
133 0, /* instance_size */ |
|
134 0, /* n_preallocs */ |
|
135 NULL, /* instance_init */ |
|
136 NULL, /* value_table */ |
|
137 }; |
|
138 const GTypeFundamentalInfo finfo = { G_TYPE_FLAG_DERIVABLE, }; |
|
139 GType type; |
|
140 |
|
141 boxed_bsa = g_bsearch_array_create (&boxed_bconfig); |
|
142 |
|
143 /* G_TYPE_BOXED |
|
144 */ |
|
145 type = g_type_register_fundamental (G_TYPE_BOXED, g_intern_static_string ("GBoxed"), &info, &finfo, |
|
146 G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT); |
|
147 g_assert (type == G_TYPE_BOXED); |
|
148 } |
|
149 |
|
150 #if EMULATOR |
|
151 PLS(type_id,g_closure_get_type ,GType) |
|
152 #define type_id (*FUNCTION_NAME(type_id,g_closure_get_type )()) |
|
153 #endif /* EMULATOR */ |
|
154 |
|
155 EXPORT_C GType |
|
156 g_closure_get_type (void) |
|
157 { |
|
158 #if !(EMULATOR) |
|
159 static GType type_id = 0; |
|
160 #endif /* EMULATOR */ |
|
161 |
|
162 if (!type_id) |
|
163 type_id = g_boxed_type_register_static (g_intern_static_string ("GClosure"), |
|
164 (GBoxedCopyFunc) g_closure_ref, |
|
165 (GBoxedFreeFunc) g_closure_unref); |
|
166 return type_id; |
|
167 } |
|
168 |
|
169 #if EMULATOR |
|
170 #undef type_id |
|
171 PLS(type_id,g_value_get_type ,GType) |
|
172 #define type_id (*FUNCTION_NAME(type_id,g_value_get_type )()) |
|
173 #endif /* EMULATOR */ |
|
174 |
|
175 EXPORT_C GType |
|
176 g_value_get_type (void) |
|
177 { |
|
178 #if !(EMULATOR) |
|
179 static GType type_id = 0; |
|
180 #endif /* EMULATOR */ |
|
181 |
|
182 if (!type_id) |
|
183 type_id = g_boxed_type_register_static (g_intern_static_string ("GValue"), |
|
184 value_copy, |
|
185 value_free); |
|
186 return type_id; |
|
187 } |
|
188 |
|
189 #if EMULATOR |
|
190 #undef type_id |
|
191 PLS(type_id,g_value_array_get_type ,GType) |
|
192 #define type_id (*FUNCTION_NAME(type_id,g_value_array_get_type )()) |
|
193 #endif /* EMULATOR */ |
|
194 |
|
195 EXPORT_C GType |
|
196 g_value_array_get_type (void) |
|
197 { |
|
198 #if !(EMULATOR) |
|
199 static GType type_id = 0; |
|
200 #endif /* EMULATOR */ |
|
201 |
|
202 if (!type_id) |
|
203 type_id = g_boxed_type_register_static (g_intern_static_string ("GValueArray"), |
|
204 (GBoxedCopyFunc) g_value_array_copy, |
|
205 (GBoxedFreeFunc) g_value_array_free); |
|
206 return type_id; |
|
207 } |
|
208 |
|
209 static gpointer |
|
210 gdate_copy (gpointer boxed) |
|
211 { |
|
212 const GDate *date = (const GDate*) boxed; |
|
213 |
|
214 return g_date_new_julian (g_date_get_julian (date)); |
|
215 } |
|
216 |
|
217 #if EMULATOR |
|
218 #undef type_id |
|
219 PLS(type_id,g_date_get_type ,GType) |
|
220 #define type_id (*FUNCTION_NAME(type_id,g_date_get_type )()) |
|
221 #endif /* EMULATOR */ |
|
222 |
|
223 EXPORT_C GType |
|
224 g_date_get_type (void) |
|
225 { |
|
226 #if !(EMULATOR) |
|
227 static GType type_id = 0; |
|
228 #endif /* EMULATOR */ |
|
229 |
|
230 if (!type_id) |
|
231 type_id = g_boxed_type_register_static (g_intern_static_string ("GDate"), |
|
232 (GBoxedCopyFunc) gdate_copy, |
|
233 (GBoxedFreeFunc) g_date_free); |
|
234 return type_id; |
|
235 } |
|
236 |
|
237 #if EMULATOR |
|
238 #undef type_id |
|
239 PLS(type_id,g_strv_get_type ,GType) |
|
240 #define type_id (*FUNCTION_NAME(type_id,g_strv_get_type )()) |
|
241 #endif /* EMULATOR */ |
|
242 |
|
243 EXPORT_C GType |
|
244 g_strv_get_type (void) |
|
245 { |
|
246 #if !(EMULATOR) |
|
247 static GType type_id = 0; |
|
248 #endif /* EMULATOR */ |
|
249 |
|
250 if (!type_id) |
|
251 type_id = g_boxed_type_register_static (g_intern_static_string ("GStrv"), |
|
252 (GBoxedCopyFunc) g_strdupv, |
|
253 (GBoxedFreeFunc) g_strfreev); |
|
254 return type_id; |
|
255 } |
|
256 |
|
257 static gpointer |
|
258 gstring_copy (gpointer boxed) |
|
259 { |
|
260 const GString *src_gstring = boxed; |
|
261 |
|
262 return g_string_new_len (src_gstring->str, src_gstring->len); |
|
263 } |
|
264 |
|
265 static void |
|
266 gstring_free (gpointer boxed) |
|
267 { |
|
268 GString *gstring = boxed; |
|
269 |
|
270 g_string_free (gstring, TRUE); |
|
271 } |
|
272 |
|
273 #if EMULATOR |
|
274 #undef type_id |
|
275 PLS(type_id,g_gstring_get_type ,GType) |
|
276 #define type_id (*FUNCTION_NAME(type_id,g_gstring_get_type )()) |
|
277 #endif /* EMULATOR */ |
|
278 |
|
279 EXPORT_C GType |
|
280 g_gstring_get_type (void) |
|
281 { |
|
282 #if !(EMULATOR) |
|
283 static GType type_id = 0; |
|
284 #endif /* !(EMULATOR) */ |
|
285 |
|
286 if (!type_id) |
|
287 type_id = g_boxed_type_register_static (g_intern_static_string ("GString"), |
|
288 /* the naming is a bit odd, but GString is obviously not G_TYPE_STRING */ |
|
289 gstring_copy, |
|
290 gstring_free); |
|
291 return type_id; |
|
292 } |
|
293 |
|
294 static gpointer |
|
295 hash_table_copy (gpointer boxed) |
|
296 { |
|
297 GHashTable *hash_table = boxed; |
|
298 return g_hash_table_ref (hash_table); |
|
299 } |
|
300 |
|
301 static void |
|
302 hash_table_free (gpointer boxed) |
|
303 { |
|
304 GHashTable *hash_table = boxed; |
|
305 g_hash_table_unref (hash_table); |
|
306 } |
|
307 |
|
308 #if EMULATOR |
|
309 #undef type_id |
|
310 PLS(type_id,g_hash_table_get_type ,GType) |
|
311 #define type_id (*FUNCTION_NAME(type_id,g_hash_table_get_type )()) |
|
312 #endif /* EMULATOR */ |
|
313 |
|
314 EXPORT_C GType |
|
315 g_hash_table_get_type (void) |
|
316 { |
|
317 #if !(EMULATOR) |
|
318 static GType type_id = 0; |
|
319 #endif */ !(EMULATOR) */ |
|
320 if (!type_id) |
|
321 type_id = g_boxed_type_register_static (g_intern_static_string ("GHashTable"), |
|
322 hash_table_copy, hash_table_free); |
|
323 return type_id; |
|
324 } |
|
325 |
|
326 #if EMULATOR |
|
327 #undef type_id |
|
328 PLS(type_id,g_regex_get_type ,GType) |
|
329 #define type_id (*FUNCTION_NAME(type_id,g_regex_get_type )()) |
|
330 #endif /* EMULATOR */ |
|
331 |
|
332 EXPORT_C GType |
|
333 g_regex_get_type (void) |
|
334 { |
|
335 #if !(EMULATOR) |
|
336 static GType type_id = 0; |
|
337 #endif */ !(EMULATOR) */ |
|
338 |
|
339 #ifdef ENABLE_REGEX |
|
340 if (!type_id) |
|
341 type_id = g_boxed_type_register_static (g_intern_static_string ("GRegex"), |
|
342 (GBoxedCopyFunc) g_regex_ref, |
|
343 (GBoxedFreeFunc) g_regex_unref); |
|
344 #endif |
|
345 |
|
346 return type_id; |
|
347 } |
|
348 |
|
349 #if EMULATOR |
|
350 #undef type_id |
|
351 #endif /* EMULATOR */ |
|
352 |
|
353 static void |
|
354 boxed_proxy_value_init (GValue *value) |
|
355 { |
|
356 value->data[0].v_pointer = NULL; |
|
357 } |
|
358 |
|
359 static void |
|
360 boxed_proxy_value_free (GValue *value) |
|
361 { |
|
362 if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS)) |
|
363 { |
|
364 BoxedNode key, *node; |
|
365 |
|
366 key.type = G_VALUE_TYPE (value); |
|
367 node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key); |
|
368 node->free (value->data[0].v_pointer); |
|
369 } |
|
370 } |
|
371 |
|
372 static void |
|
373 boxed_proxy_value_copy (const GValue *src_value, |
|
374 GValue *dest_value) |
|
375 { |
|
376 if (src_value->data[0].v_pointer) |
|
377 { |
|
378 BoxedNode key, *node; |
|
379 |
|
380 key.type = G_VALUE_TYPE (src_value); |
|
381 node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key); |
|
382 dest_value->data[0].v_pointer = node->copy (src_value->data[0].v_pointer); |
|
383 } |
|
384 else |
|
385 dest_value->data[0].v_pointer = src_value->data[0].v_pointer; |
|
386 } |
|
387 |
|
388 static gpointer |
|
389 boxed_proxy_value_peek_pointer (const GValue *value) |
|
390 { |
|
391 return value->data[0].v_pointer; |
|
392 } |
|
393 |
|
394 static gchar* |
|
395 boxed_proxy_collect_value (GValue *value, |
|
396 guint n_collect_values, |
|
397 GTypeCValue *collect_values, |
|
398 guint collect_flags) |
|
399 { |
|
400 BoxedNode key, *node; |
|
401 |
|
402 key.type = G_VALUE_TYPE (value); |
|
403 node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key); |
|
404 |
|
405 if (!collect_values[0].v_pointer) |
|
406 value->data[0].v_pointer = NULL; |
|
407 else |
|
408 { |
|
409 if (collect_flags & G_VALUE_NOCOPY_CONTENTS) |
|
410 { |
|
411 value->data[0].v_pointer = collect_values[0].v_pointer; |
|
412 value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS; |
|
413 } |
|
414 else |
|
415 value->data[0].v_pointer = node->copy (collect_values[0].v_pointer); |
|
416 } |
|
417 |
|
418 return NULL; |
|
419 } |
|
420 |
|
421 static gchar* |
|
422 boxed_proxy_lcopy_value (const GValue *value, |
|
423 guint n_collect_values, |
|
424 GTypeCValue *collect_values, |
|
425 guint collect_flags) |
|
426 { |
|
427 gpointer *boxed_p = collect_values[0].v_pointer; |
|
428 |
|
429 if (!boxed_p) |
|
430 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value)); |
|
431 |
|
432 if (!value->data[0].v_pointer) |
|
433 *boxed_p = NULL; |
|
434 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS) |
|
435 *boxed_p = value->data[0].v_pointer; |
|
436 else |
|
437 { |
|
438 BoxedNode key, *node; |
|
439 |
|
440 key.type = G_VALUE_TYPE (value); |
|
441 node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key); |
|
442 *boxed_p = node->copy (value->data[0].v_pointer); |
|
443 } |
|
444 |
|
445 return NULL; |
|
446 } |
|
447 |
|
448 /** |
|
449 * g_boxed_type_register_static: |
|
450 * @name: Name of the new boxed type. |
|
451 * @boxed_copy: Boxed structure copy function. |
|
452 * @boxed_free: Boxed structure free function. |
|
453 * |
|
454 * This function creates a new %G_TYPE_BOXED derived type id for a new |
|
455 * boxed type with name @name. Boxed type handling functions have to be |
|
456 * provided to copy and free opaque boxed structures of this type. |
|
457 * |
|
458 * Returns: New %G_TYPE_BOXED derived type id for @name. |
|
459 */ |
|
460 EXPORT_C GType |
|
461 g_boxed_type_register_static (const gchar *name, |
|
462 GBoxedCopyFunc boxed_copy, |
|
463 GBoxedFreeFunc boxed_free) |
|
464 { |
|
465 static const GTypeValueTable vtable = { |
|
466 boxed_proxy_value_init, |
|
467 boxed_proxy_value_free, |
|
468 boxed_proxy_value_copy, |
|
469 boxed_proxy_value_peek_pointer, |
|
470 "p", |
|
471 boxed_proxy_collect_value, |
|
472 "p", |
|
473 boxed_proxy_lcopy_value, |
|
474 }; |
|
475 static const GTypeInfo type_info = { |
|
476 0, /* class_size */ |
|
477 NULL, /* base_init */ |
|
478 NULL, /* base_finalize */ |
|
479 NULL, /* class_init */ |
|
480 NULL, /* class_finalize */ |
|
481 NULL, /* class_data */ |
|
482 0, /* instance_size */ |
|
483 0, /* n_preallocs */ |
|
484 NULL, /* instance_init */ |
|
485 &vtable, /* value_table */ |
|
486 }; |
|
487 GType type; |
|
488 |
|
489 g_return_val_if_fail (name != NULL, 0); |
|
490 g_return_val_if_fail (boxed_copy != NULL, 0); |
|
491 g_return_val_if_fail (boxed_free != NULL, 0); |
|
492 g_return_val_if_fail (g_type_from_name (name) == 0, 0); |
|
493 |
|
494 type = g_type_register_static (G_TYPE_BOXED, name, &type_info, 0); |
|
495 |
|
496 /* install proxy functions upon successfull registration */ |
|
497 if (type) |
|
498 { |
|
499 BoxedNode key; |
|
500 |
|
501 key.type = type; |
|
502 key.copy = boxed_copy; |
|
503 key.free = boxed_free; |
|
504 boxed_bsa = g_bsearch_array_insert (boxed_bsa, &boxed_bconfig, &key); |
|
505 } |
|
506 |
|
507 return type; |
|
508 } |
|
509 |
|
510 /** |
|
511 * g_boxed_copy: |
|
512 * @boxed_type: The type of @src_boxed. |
|
513 * @src_boxed: The boxed structure to be copied. |
|
514 * |
|
515 * Provide a copy of a boxed structure @src_boxed which is of type @boxed_type. |
|
516 * |
|
517 * Returns: The newly created copy of the boxed structure. |
|
518 */ |
|
519 EXPORT_C gpointer |
|
520 g_boxed_copy (GType boxed_type, |
|
521 gconstpointer src_boxed) |
|
522 { |
|
523 GTypeValueTable *value_table; |
|
524 gpointer dest_boxed; |
|
525 |
|
526 g_return_val_if_fail (G_TYPE_IS_BOXED (boxed_type), NULL); |
|
527 g_return_val_if_fail (G_TYPE_IS_ABSTRACT (boxed_type) == FALSE, NULL); |
|
528 g_return_val_if_fail (src_boxed != NULL, NULL); |
|
529 |
|
530 value_table = g_type_value_table_peek (boxed_type); |
|
531 if (!value_table) |
|
532 g_return_val_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type), NULL); |
|
533 |
|
534 /* check if our proxying implementation is used, we can short-cut here */ |
|
535 if (value_table->value_copy == boxed_proxy_value_copy) |
|
536 { |
|
537 BoxedNode key, *node; |
|
538 |
|
539 key.type = boxed_type; |
|
540 node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key); |
|
541 dest_boxed = node->copy ((gpointer) src_boxed); |
|
542 } |
|
543 else |
|
544 { |
|
545 GValue src_value, dest_value; |
|
546 |
|
547 /* we heavily rely on third-party boxed type value vtable |
|
548 * implementations to follow normal boxed value storage |
|
549 * (data[0].v_pointer is the boxed struct, and |
|
550 * data[1].v_uint holds the G_VALUE_NOCOPY_CONTENTS flag, |
|
551 * rest zero). |
|
552 * but then, we can expect that since we layed out the |
|
553 * g_boxed_*() API. |
|
554 * data[1].v_uint&G_VALUE_NOCOPY_CONTENTS shouldn't be set |
|
555 * after a copy. |
|
556 */ |
|
557 /* equiv. to g_value_set_static_boxed() */ |
|
558 value_meminit (&src_value, boxed_type); |
|
559 src_value.data[0].v_pointer = (gpointer) src_boxed; |
|
560 src_value.data[1].v_uint = G_VALUE_NOCOPY_CONTENTS; |
|
561 |
|
562 /* call third-party code copy function, fingers-crossed */ |
|
563 value_meminit (&dest_value, boxed_type); |
|
564 value_table->value_copy (&src_value, &dest_value); |
|
565 |
|
566 /* double check and grouse if things went wrong */ |
|
567 if (dest_value.data[1].v_ulong) |
|
568 g_warning ("the copy_value() implementation of type `%s' seems to make use of reserved GValue fields", |
|
569 g_type_name (boxed_type)); |
|
570 |
|
571 dest_boxed = dest_value.data[0].v_pointer; |
|
572 } |
|
573 |
|
574 return dest_boxed; |
|
575 } |
|
576 |
|
577 /** |
|
578 * g_boxed_free: |
|
579 * @boxed_type: The type of @boxed. |
|
580 * @boxed: The boxed structure to be freed. |
|
581 * |
|
582 * Free the boxed structure @boxed which is of type @boxed_type. |
|
583 */ |
|
584 EXPORT_C void |
|
585 g_boxed_free (GType boxed_type, |
|
586 gpointer boxed) |
|
587 { |
|
588 GTypeValueTable *value_table; |
|
589 |
|
590 g_return_if_fail (G_TYPE_IS_BOXED (boxed_type)); |
|
591 g_return_if_fail (G_TYPE_IS_ABSTRACT (boxed_type) == FALSE); |
|
592 g_return_if_fail (boxed != NULL); |
|
593 |
|
594 value_table = g_type_value_table_peek (boxed_type); |
|
595 if (!value_table) |
|
596 g_return_if_fail (G_TYPE_IS_VALUE_TYPE (boxed_type)); |
|
597 |
|
598 /* check if our proxying implementation is used, we can short-cut here */ |
|
599 if (value_table->value_free == boxed_proxy_value_free) |
|
600 { |
|
601 BoxedNode key, *node; |
|
602 |
|
603 key.type = boxed_type; |
|
604 node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key); |
|
605 node->free (boxed); |
|
606 } |
|
607 else |
|
608 { |
|
609 GValue value; |
|
610 |
|
611 /* see g_boxed_copy() on why we think we can do this */ |
|
612 value_meminit (&value, boxed_type); |
|
613 value.data[0].v_pointer = boxed; |
|
614 value_table->value_free (&value); |
|
615 } |
|
616 } |
|
617 |
|
618 /** |
|
619 * g_value_get_boxed: |
|
620 * @value: a valid #GValue of %G_TYPE_BOXED derived type |
|
621 * |
|
622 * Get the contents of a %G_TYPE_BOXED derived #GValue. |
|
623 * |
|
624 * Returns: boxed contents of @value |
|
625 */ |
|
626 EXPORT_C gpointer |
|
627 g_value_get_boxed (const GValue *value) |
|
628 { |
|
629 g_return_val_if_fail (G_VALUE_HOLDS_BOXED (value), NULL); |
|
630 g_return_val_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)), NULL); |
|
631 |
|
632 return value->data[0].v_pointer; |
|
633 } |
|
634 |
|
635 /** |
|
636 * g_value_dup_boxed: |
|
637 * @value: a valid #GValue of %G_TYPE_BOXED derived type |
|
638 * |
|
639 * Get the contents of a %G_TYPE_BOXED derived #GValue. Upon getting, |
|
640 * the boxed value is duplicated and needs to be later freed with |
|
641 * g_boxed_free(), e.g. like: g_boxed_free (G_VALUE_TYPE (@value), |
|
642 * return_value); |
|
643 * |
|
644 * Returns: boxed contents of @value |
|
645 */ |
|
646 EXPORT_C gpointer |
|
647 g_value_dup_boxed (const GValue *value) |
|
648 { |
|
649 g_return_val_if_fail (G_VALUE_HOLDS_BOXED (value), NULL); |
|
650 g_return_val_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value)), NULL); |
|
651 |
|
652 return value->data[0].v_pointer ? g_boxed_copy (G_VALUE_TYPE (value), value->data[0].v_pointer) : NULL; |
|
653 } |
|
654 |
|
655 static inline void |
|
656 value_set_boxed_internal (GValue *value, |
|
657 gconstpointer const_boxed, |
|
658 gboolean need_copy, |
|
659 gboolean need_free) |
|
660 { |
|
661 BoxedNode key, *node; |
|
662 gpointer boxed = (gpointer) const_boxed; |
|
663 |
|
664 if (!boxed) |
|
665 { |
|
666 /* just resetting to NULL might not be desired, need to |
|
667 * have value reinitialized also (for values defaulting |
|
668 * to other default value states than a NULL data pointer), |
|
669 * g_value_reset() will handle this |
|
670 */ |
|
671 g_value_reset (value); |
|
672 return; |
|
673 } |
|
674 |
|
675 key.type = G_VALUE_TYPE (value); |
|
676 node = g_bsearch_array_lookup (boxed_bsa, &boxed_bconfig, &key); |
|
677 |
|
678 if (node) |
|
679 { |
|
680 /* we proxy this type, free contents and copy right away */ |
|
681 if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS)) |
|
682 node->free (value->data[0].v_pointer); |
|
683 value->data[1].v_uint = need_free ? 0 : G_VALUE_NOCOPY_CONTENTS; |
|
684 value->data[0].v_pointer = need_copy ? node->copy (boxed) : boxed; |
|
685 } |
|
686 else |
|
687 { |
|
688 /* we don't handle this type, free contents and let g_boxed_copy() |
|
689 * figure what's required |
|
690 */ |
|
691 if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS)) |
|
692 g_boxed_free (G_VALUE_TYPE (value), value->data[0].v_pointer); |
|
693 value->data[1].v_uint = need_free ? 0 : G_VALUE_NOCOPY_CONTENTS; |
|
694 value->data[0].v_pointer = need_copy ? g_boxed_copy (G_VALUE_TYPE (value), boxed) : boxed; |
|
695 } |
|
696 } |
|
697 |
|
698 /** |
|
699 * g_value_set_boxed: |
|
700 * @value: a valid #GValue of %G_TYPE_BOXED derived type |
|
701 * @v_boxed: boxed value to be set |
|
702 * |
|
703 * Set the contents of a %G_TYPE_BOXED derived #GValue to @v_boxed. |
|
704 */ |
|
705 EXPORT_C void |
|
706 g_value_set_boxed (GValue *value, |
|
707 gconstpointer boxed) |
|
708 { |
|
709 g_return_if_fail (G_VALUE_HOLDS_BOXED (value)); |
|
710 g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value))); |
|
711 |
|
712 value_set_boxed_internal (value, boxed, TRUE, TRUE); |
|
713 } |
|
714 |
|
715 /** |
|
716 * g_value_set_static_boxed: |
|
717 * @value: a valid #GValue of %G_TYPE_BOXED derived type |
|
718 * @v_boxed: static boxed value to be set |
|
719 * |
|
720 * Set the contents of a %G_TYPE_BOXED derived #GValue to @v_boxed. |
|
721 * The boxed value is assumed to be static, and is thus not duplicated |
|
722 * when setting the #GValue. |
|
723 */ |
|
724 EXPORT_C void |
|
725 g_value_set_static_boxed (GValue *value, |
|
726 gconstpointer boxed) |
|
727 { |
|
728 g_return_if_fail (G_VALUE_HOLDS_BOXED (value)); |
|
729 g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value))); |
|
730 |
|
731 value_set_boxed_internal (value, boxed, FALSE, FALSE); |
|
732 } |
|
733 |
|
734 /** |
|
735 * g_value_set_boxed_take_ownership: |
|
736 * @value: a valid #GValue of %G_TYPE_BOXED derived type |
|
737 * @v_boxed: duplicated unowned boxed value to be set |
|
738 * |
|
739 * This is an internal function introduced mainly for C marshallers. |
|
740 * |
|
741 * Deprecated: 2.4: Use g_value_take_boxed() instead. |
|
742 */ |
|
743 EXPORT_C void |
|
744 g_value_set_boxed_take_ownership (GValue *value, |
|
745 gconstpointer boxed) |
|
746 { |
|
747 g_value_take_boxed (value, boxed); |
|
748 } |
|
749 |
|
750 /** |
|
751 * g_value_take_boxed: |
|
752 * @value: a valid #GValue of %G_TYPE_BOXED derived type |
|
753 * @v_boxed: duplicated unowned boxed value to be set |
|
754 * |
|
755 * Sets the contents of a %G_TYPE_BOXED derived #GValue to @v_boxed |
|
756 * and takes over the ownership of the callers reference to @v_boxed; |
|
757 * the caller doesn't have to unref it any more. |
|
758 * |
|
759 * Since: 2.4 |
|
760 */ |
|
761 EXPORT_C void |
|
762 g_value_take_boxed (GValue *value, |
|
763 gconstpointer boxed) |
|
764 { |
|
765 g_return_if_fail (G_VALUE_HOLDS_BOXED (value)); |
|
766 g_return_if_fail (G_TYPE_IS_VALUE (G_VALUE_TYPE (value))); |
|
767 |
|
768 value_set_boxed_internal (value, boxed, FALSE, TRUE); |
|
769 } |
|
770 |
|
771 #define __G_BOXED_C__ |
|
772 #include "gobjectaliasdef.c" |