1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 1997-1999, 2000-2001 Tim Janik and Red Hat, Inc.
3 * Copyright © 2010 Christian Persch
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.1 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, see <http://www.gnu.org/licenses/>.
17 */
18
19 /*
20 * MT safe
21 */
22
23 #include "config.h"
24
25 #include <string.h>
26 #include <stdlib.h> /* qsort() */
27
28 #include "gvaluetypes.h"
29 #include "gtype-private.h"
30 #include "gvaluecollector.h"
31 #include "gobject.h"
32 #include "gparam.h"
33 #include "gboxed.h"
34 #include "genums.h"
35
36
37 /* --- value functions --- */
38 static void
value_init_long0(GValue * value)39 value_init_long0 (GValue *value)
40 {
41 value->data[0].v_long = 0;
42 }
43
44 static void
value_copy_long0(const GValue * src_value,GValue * dest_value)45 value_copy_long0 (const GValue *src_value,
46 GValue *dest_value)
47 {
48 dest_value->data[0].v_long = src_value->data[0].v_long;
49 }
50
51 static gchar*
value_lcopy_char(const GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)52 value_lcopy_char (const GValue *value,
53 guint n_collect_values,
54 GTypeCValue *collect_values,
55 guint collect_flags)
56 {
57 gint8 *int8_p = collect_values[0].v_pointer;
58
59 g_return_val_if_fail (int8_p != NULL, g_strdup_printf ("value location for '%s' passed as NULL", G_VALUE_TYPE_NAME (value)));
60
61 *int8_p = value->data[0].v_int;
62
63 return NULL;
64 }
65
66 static gchar*
value_lcopy_boolean(const GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)67 value_lcopy_boolean (const GValue *value,
68 guint n_collect_values,
69 GTypeCValue *collect_values,
70 guint collect_flags)
71 {
72 gboolean *bool_p = collect_values[0].v_pointer;
73
74 g_return_val_if_fail (bool_p != NULL, g_strdup_printf ("value location for '%s' passed as NULL", G_VALUE_TYPE_NAME (value)));
75
76 *bool_p = value->data[0].v_int;
77
78 return NULL;
79 }
80
81 static gchar*
value_collect_int(GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)82 value_collect_int (GValue *value,
83 guint n_collect_values,
84 GTypeCValue *collect_values,
85 guint collect_flags)
86 {
87 value->data[0].v_int = collect_values[0].v_int;
88
89 return NULL;
90 }
91
92 static gchar*
value_lcopy_int(const GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)93 value_lcopy_int (const GValue *value,
94 guint n_collect_values,
95 GTypeCValue *collect_values,
96 guint collect_flags)
97 {
98 gint *int_p = collect_values[0].v_pointer;
99
100 g_return_val_if_fail (int_p != NULL, g_strdup_printf ("value location for '%s' passed as NULL", G_VALUE_TYPE_NAME (value)));
101
102 *int_p = value->data[0].v_int;
103
104 return NULL;
105 }
106
107 static gchar*
value_collect_long(GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)108 value_collect_long (GValue *value,
109 guint n_collect_values,
110 GTypeCValue *collect_values,
111 guint collect_flags)
112 {
113 value->data[0].v_long = collect_values[0].v_long;
114
115 return NULL;
116 }
117
118 static gchar*
value_lcopy_long(const GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)119 value_lcopy_long (const GValue *value,
120 guint n_collect_values,
121 GTypeCValue *collect_values,
122 guint collect_flags)
123 {
124 glong *long_p = collect_values[0].v_pointer;
125
126 g_return_val_if_fail (long_p != NULL, g_strdup_printf ("value location for '%s' passed as NULL", G_VALUE_TYPE_NAME (value)));
127
128 *long_p = value->data[0].v_long;
129
130 return NULL;
131 }
132
133 static void
value_init_int64(GValue * value)134 value_init_int64 (GValue *value)
135 {
136 value->data[0].v_int64 = 0;
137 }
138
139 static void
value_copy_int64(const GValue * src_value,GValue * dest_value)140 value_copy_int64 (const GValue *src_value,
141 GValue *dest_value)
142 {
143 dest_value->data[0].v_int64 = src_value->data[0].v_int64;
144 }
145
146 static gchar*
value_collect_int64(GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)147 value_collect_int64 (GValue *value,
148 guint n_collect_values,
149 GTypeCValue *collect_values,
150 guint collect_flags)
151 {
152 value->data[0].v_int64 = collect_values[0].v_int64;
153
154 return NULL;
155 }
156
157 static gchar*
value_lcopy_int64(const GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)158 value_lcopy_int64 (const GValue *value,
159 guint n_collect_values,
160 GTypeCValue *collect_values,
161 guint collect_flags)
162 {
163 gint64 *int64_p = collect_values[0].v_pointer;
164
165 g_return_val_if_fail (int64_p != NULL, g_strdup_printf ("value location for '%s' passed as NULL", G_VALUE_TYPE_NAME (value)));
166
167 *int64_p = value->data[0].v_int64;
168
169 return NULL;
170 }
171
172 static void
value_init_float(GValue * value)173 value_init_float (GValue *value)
174 {
175 value->data[0].v_float = 0.0;
176 }
177
178 static void
value_copy_float(const GValue * src_value,GValue * dest_value)179 value_copy_float (const GValue *src_value,
180 GValue *dest_value)
181 {
182 dest_value->data[0].v_float = src_value->data[0].v_float;
183 }
184
185 static gchar*
value_collect_float(GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)186 value_collect_float (GValue *value,
187 guint n_collect_values,
188 GTypeCValue *collect_values,
189 guint collect_flags)
190 {
191 value->data[0].v_float = collect_values[0].v_double;
192
193 return NULL;
194 }
195
196 static gchar*
value_lcopy_float(const GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)197 value_lcopy_float (const GValue *value,
198 guint n_collect_values,
199 GTypeCValue *collect_values,
200 guint collect_flags)
201 {
202 gfloat *float_p = collect_values[0].v_pointer;
203
204 g_return_val_if_fail (float_p != NULL, g_strdup_printf ("value location for '%s' passed as NULL", G_VALUE_TYPE_NAME (value)));
205
206 *float_p = value->data[0].v_float;
207
208 return NULL;
209 }
210
211 static void
value_init_double(GValue * value)212 value_init_double (GValue *value)
213 {
214 value->data[0].v_double = 0.0;
215 }
216
217 static void
value_copy_double(const GValue * src_value,GValue * dest_value)218 value_copy_double (const GValue *src_value,
219 GValue *dest_value)
220 {
221 dest_value->data[0].v_double = src_value->data[0].v_double;
222 }
223
224 static gchar*
value_collect_double(GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)225 value_collect_double (GValue *value,
226 guint n_collect_values,
227 GTypeCValue *collect_values,
228 guint collect_flags)
229 {
230 value->data[0].v_double = collect_values[0].v_double;
231
232 return NULL;
233 }
234
235 static gchar*
value_lcopy_double(const GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)236 value_lcopy_double (const GValue *value,
237 guint n_collect_values,
238 GTypeCValue *collect_values,
239 guint collect_flags)
240 {
241 gdouble *double_p = collect_values[0].v_pointer;
242
243 g_return_val_if_fail (double_p != NULL, g_strdup_printf ("value location for '%s' passed as NULL", G_VALUE_TYPE_NAME (value)));
244
245 *double_p = value->data[0].v_double;
246
247 return NULL;
248 }
249
250 static void
value_init_string(GValue * value)251 value_init_string (GValue *value)
252 {
253 value->data[0].v_pointer = NULL;
254 }
255
256 static void
value_free_string(GValue * value)257 value_free_string (GValue *value)
258 {
259 if (!(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
260 g_free (value->data[0].v_pointer);
261 }
262
263 static void
value_copy_string(const GValue * src_value,GValue * dest_value)264 value_copy_string (const GValue *src_value,
265 GValue *dest_value)
266 {
267 if (src_value->data[1].v_uint & G_VALUE_INTERNED_STRING)
268 {
269 dest_value->data[0].v_pointer = src_value->data[0].v_pointer;
270 dest_value->data[1].v_uint = src_value->data[1].v_uint;
271 }
272 else
273 {
274 dest_value->data[0].v_pointer = g_strdup (src_value->data[0].v_pointer);
275 /* Don't copy over *any* flags, we're restarting from scratch */
276 }
277 }
278
279 static gchar*
value_collect_string(GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)280 value_collect_string (GValue *value,
281 guint n_collect_values,
282 GTypeCValue *collect_values,
283 guint collect_flags)
284 {
285 if (!collect_values[0].v_pointer)
286 value->data[0].v_pointer = NULL;
287 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
288 {
289 value->data[0].v_pointer = collect_values[0].v_pointer;
290 value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
291 }
292 else
293 value->data[0].v_pointer = g_strdup (collect_values[0].v_pointer);
294
295 return NULL;
296 }
297
298 static gchar*
value_lcopy_string(const GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)299 value_lcopy_string (const GValue *value,
300 guint n_collect_values,
301 GTypeCValue *collect_values,
302 guint collect_flags)
303 {
304 gchar **string_p = collect_values[0].v_pointer;
305
306 g_return_val_if_fail (string_p != NULL, g_strdup_printf ("value location for '%s' passed as NULL", G_VALUE_TYPE_NAME (value)));
307
308 if (!value->data[0].v_pointer)
309 *string_p = NULL;
310 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
311 *string_p = value->data[0].v_pointer;
312 else
313 *string_p = g_strdup (value->data[0].v_pointer);
314
315 return NULL;
316 }
317
318 static void
value_init_pointer(GValue * value)319 value_init_pointer (GValue *value)
320 {
321 value->data[0].v_pointer = NULL;
322 }
323
324 static void
value_copy_pointer(const GValue * src_value,GValue * dest_value)325 value_copy_pointer (const GValue *src_value,
326 GValue *dest_value)
327 {
328 dest_value->data[0].v_pointer = src_value->data[0].v_pointer;
329 }
330
331 static gpointer
value_peek_pointer0(const GValue * value)332 value_peek_pointer0 (const GValue *value)
333 {
334 return value->data[0].v_pointer;
335 }
336
337 static gchar*
value_collect_pointer(GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)338 value_collect_pointer (GValue *value,
339 guint n_collect_values,
340 GTypeCValue *collect_values,
341 guint collect_flags)
342 {
343 value->data[0].v_pointer = collect_values[0].v_pointer;
344
345 return NULL;
346 }
347
348 static gchar*
value_lcopy_pointer(const GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)349 value_lcopy_pointer (const GValue *value,
350 guint n_collect_values,
351 GTypeCValue *collect_values,
352 guint collect_flags)
353 {
354 gpointer *pointer_p = collect_values[0].v_pointer;
355
356 g_return_val_if_fail (pointer_p != NULL, g_strdup_printf ("value location for '%s' passed as NULL", G_VALUE_TYPE_NAME (value)));
357
358 *pointer_p = value->data[0].v_pointer;
359
360 return NULL;
361 }
362
363 static void
value_free_variant(GValue * value)364 value_free_variant (GValue *value)
365 {
366 if (!(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS) &&
367 value->data[0].v_pointer)
368 g_variant_unref (value->data[0].v_pointer);
369 }
370
371 static void
value_copy_variant(const GValue * src_value,GValue * dest_value)372 value_copy_variant (const GValue *src_value,
373 GValue *dest_value)
374 {
375 if (src_value->data[0].v_pointer)
376 dest_value->data[0].v_pointer = g_variant_ref_sink (src_value->data[0].v_pointer);
377 else
378 dest_value->data[0].v_pointer = NULL;
379 }
380
381 static gchar*
value_collect_variant(GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)382 value_collect_variant (GValue *value,
383 guint n_collect_values,
384 GTypeCValue *collect_values,
385 guint collect_flags)
386 {
387 if (!collect_values[0].v_pointer)
388 value->data[0].v_pointer = NULL;
389 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
390 {
391 value->data[0].v_pointer = collect_values[0].v_pointer;
392 value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
393 }
394 else
395 value->data[0].v_pointer = g_variant_ref_sink (collect_values[0].v_pointer);
396
397 return NULL;
398 }
399
400 static gchar*
value_lcopy_variant(const GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)401 value_lcopy_variant (const GValue *value,
402 guint n_collect_values,
403 GTypeCValue *collect_values,
404 guint collect_flags)
405 {
406 GVariant **variant_p = collect_values[0].v_pointer;
407
408 g_return_val_if_fail (variant_p != NULL, g_strdup_printf ("value location for '%s' passed as NULL", G_VALUE_TYPE_NAME (value)));
409
410 if (!value->data[0].v_pointer)
411 *variant_p = NULL;
412 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
413 *variant_p = value->data[0].v_pointer;
414 else
415 *variant_p = g_variant_ref_sink (value->data[0].v_pointer);
416
417 return NULL;
418 }
419
420 /* --- type initialization --- */
421 void
_g_value_types_init(void)422 _g_value_types_init (void)
423 {
424 GTypeInfo info = {
425 0, /* class_size */
426 NULL, /* base_init */
427 NULL, /* base_destroy */
428 NULL, /* class_init */
429 NULL, /* class_destroy */
430 NULL, /* class_data */
431 0, /* instance_size */
432 0, /* n_preallocs */
433 NULL, /* instance_init */
434 NULL, /* value_table */
435 };
436 const GTypeFundamentalInfo finfo = { G_TYPE_FLAG_DERIVABLE, };
437 GType type G_GNUC_UNUSED /* when compiling with G_DISABLE_ASSERT */;
438
439 /* G_TYPE_CHAR / G_TYPE_UCHAR
440 */
441 {
442 static const GTypeValueTable value_table = {
443 value_init_long0, /* value_init */
444 NULL, /* value_free */
445 value_copy_long0, /* value_copy */
446 NULL, /* value_peek_pointer */
447 "i", /* collect_format */
448 value_collect_int, /* collect_value */
449 "p", /* lcopy_format */
450 value_lcopy_char, /* lcopy_value */
451 };
452 info.value_table = &value_table;
453 type = g_type_register_fundamental (G_TYPE_CHAR, g_intern_static_string ("gchar"), &info, &finfo, 0);
454 g_assert (type == G_TYPE_CHAR);
455 type = g_type_register_fundamental (G_TYPE_UCHAR, g_intern_static_string ("guchar"), &info, &finfo, 0);
456 g_assert (type == G_TYPE_UCHAR);
457 }
458
459 /* G_TYPE_BOOLEAN
460 */
461 {
462 static const GTypeValueTable value_table = {
463 value_init_long0, /* value_init */
464 NULL, /* value_free */
465 value_copy_long0, /* value_copy */
466 NULL, /* value_peek_pointer */
467 "i", /* collect_format */
468 value_collect_int, /* collect_value */
469 "p", /* lcopy_format */
470 value_lcopy_boolean, /* lcopy_value */
471 };
472 info.value_table = &value_table;
473 type = g_type_register_fundamental (G_TYPE_BOOLEAN, g_intern_static_string ("gboolean"), &info, &finfo, 0);
474 g_assert (type == G_TYPE_BOOLEAN);
475 }
476
477 /* G_TYPE_INT / G_TYPE_UINT
478 */
479 {
480 static const GTypeValueTable value_table = {
481 value_init_long0, /* value_init */
482 NULL, /* value_free */
483 value_copy_long0, /* value_copy */
484 NULL, /* value_peek_pointer */
485 "i", /* collect_format */
486 value_collect_int, /* collect_value */
487 "p", /* lcopy_format */
488 value_lcopy_int, /* lcopy_value */
489 };
490 info.value_table = &value_table;
491 type = g_type_register_fundamental (G_TYPE_INT, g_intern_static_string ("gint"), &info, &finfo, 0);
492 g_assert (type == G_TYPE_INT);
493 type = g_type_register_fundamental (G_TYPE_UINT, g_intern_static_string ("guint"), &info, &finfo, 0);
494 g_assert (type == G_TYPE_UINT);
495 }
496
497 /* G_TYPE_LONG / G_TYPE_ULONG
498 */
499 {
500 static const GTypeValueTable value_table = {
501 value_init_long0, /* value_init */
502 NULL, /* value_free */
503 value_copy_long0, /* value_copy */
504 NULL, /* value_peek_pointer */
505 "l", /* collect_format */
506 value_collect_long, /* collect_value */
507 "p", /* lcopy_format */
508 value_lcopy_long, /* lcopy_value */
509 };
510 info.value_table = &value_table;
511 type = g_type_register_fundamental (G_TYPE_LONG, g_intern_static_string ("glong"), &info, &finfo, 0);
512 g_assert (type == G_TYPE_LONG);
513 type = g_type_register_fundamental (G_TYPE_ULONG, g_intern_static_string ("gulong"), &info, &finfo, 0);
514 g_assert (type == G_TYPE_ULONG);
515 }
516
517 /* G_TYPE_INT64 / G_TYPE_UINT64
518 */
519 {
520 static const GTypeValueTable value_table = {
521 value_init_int64, /* value_init */
522 NULL, /* value_free */
523 value_copy_int64, /* value_copy */
524 NULL, /* value_peek_pointer */
525 "q", /* collect_format */
526 value_collect_int64, /* collect_value */
527 "p", /* lcopy_format */
528 value_lcopy_int64, /* lcopy_value */
529 };
530 info.value_table = &value_table;
531 type = g_type_register_fundamental (G_TYPE_INT64, g_intern_static_string ("gint64"), &info, &finfo, 0);
532 g_assert (type == G_TYPE_INT64);
533 type = g_type_register_fundamental (G_TYPE_UINT64, g_intern_static_string ("guint64"), &info, &finfo, 0);
534 g_assert (type == G_TYPE_UINT64);
535 }
536
537 /* G_TYPE_FLOAT
538 */
539 {
540 static const GTypeValueTable value_table = {
541 value_init_float, /* value_init */
542 NULL, /* value_free */
543 value_copy_float, /* value_copy */
544 NULL, /* value_peek_pointer */
545 "d", /* collect_format */
546 value_collect_float, /* collect_value */
547 "p", /* lcopy_format */
548 value_lcopy_float, /* lcopy_value */
549 };
550 info.value_table = &value_table;
551 type = g_type_register_fundamental (G_TYPE_FLOAT, g_intern_static_string ("gfloat"), &info, &finfo, 0);
552 g_assert (type == G_TYPE_FLOAT);
553 }
554
555 /* G_TYPE_DOUBLE
556 */
557 {
558 static const GTypeValueTable value_table = {
559 value_init_double, /* value_init */
560 NULL, /* value_free */
561 value_copy_double, /* value_copy */
562 NULL, /* value_peek_pointer */
563 "d", /* collect_format */
564 value_collect_double, /* collect_value */
565 "p", /* lcopy_format */
566 value_lcopy_double, /* lcopy_value */
567 };
568 info.value_table = &value_table;
569 type = g_type_register_fundamental (G_TYPE_DOUBLE, g_intern_static_string ("gdouble"), &info, &finfo, 0);
570 g_assert (type == G_TYPE_DOUBLE);
571 }
572
573 /* G_TYPE_STRING
574 */
575 {
576 static const GTypeValueTable value_table = {
577 value_init_string, /* value_init */
578 value_free_string, /* value_free */
579 value_copy_string, /* value_copy */
580 value_peek_pointer0, /* value_peek_pointer */
581 "p", /* collect_format */
582 value_collect_string, /* collect_value */
583 "p", /* lcopy_format */
584 value_lcopy_string, /* lcopy_value */
585 };
586 info.value_table = &value_table;
587 type = g_type_register_fundamental (G_TYPE_STRING, g_intern_static_string ("gchararray"), &info, &finfo, 0);
588 g_assert (type == G_TYPE_STRING);
589 }
590
591 /* G_TYPE_POINTER
592 */
593 {
594 static const GTypeValueTable value_table = {
595 value_init_pointer, /* value_init */
596 NULL, /* value_free */
597 value_copy_pointer, /* value_copy */
598 value_peek_pointer0, /* value_peek_pointer */
599 "p", /* collect_format */
600 value_collect_pointer, /* collect_value */
601 "p", /* lcopy_format */
602 value_lcopy_pointer, /* lcopy_value */
603 };
604 info.value_table = &value_table;
605 type = g_type_register_fundamental (G_TYPE_POINTER, g_intern_static_string ("gpointer"), &info, &finfo, 0);
606 g_assert (type == G_TYPE_POINTER);
607 }
608
609 /* G_TYPE_VARIANT
610 */
611 {
612 static const GTypeValueTable value_table = {
613 value_init_pointer, /* value_init */
614 value_free_variant, /* value_free */
615 value_copy_variant, /* value_copy */
616 value_peek_pointer0, /* value_peek_pointer */
617 "p", /* collect_format */
618 value_collect_variant, /* collect_value */
619 "p", /* lcopy_format */
620 value_lcopy_variant, /* lcopy_value */
621 };
622 info.value_table = &value_table;
623 type = g_type_register_fundamental (G_TYPE_VARIANT, g_intern_static_string ("GVariant"), &info, &finfo, 0);
624 g_assert (type == G_TYPE_VARIANT);
625 }
626 }
627
628
629 /* --- GValue functions --- */
630 /**
631 * g_value_set_char:
632 * @value: a valid #GValue of type %G_TYPE_CHAR
633 * @v_char: character value to be set
634 *
635 * Set the contents of a %G_TYPE_CHAR #GValue to @v_char.
636 * Deprecated: 2.32: This function's input type is broken, see g_value_set_schar()
637 */
638 void
g_value_set_char(GValue * value,gchar v_char)639 g_value_set_char (GValue *value,
640 gchar v_char)
641 {
642 g_return_if_fail (G_VALUE_HOLDS_CHAR (value));
643
644 value->data[0].v_int = v_char;
645 }
646
647 /**
648 * g_value_get_char:
649 * @value: a valid #GValue of type %G_TYPE_CHAR
650 *
651 * Do not use this function; it is broken on platforms where the %char
652 * type is unsigned, such as ARM and PowerPC. See g_value_get_schar().
653 *
654 * Get the contents of a %G_TYPE_CHAR #GValue.
655 *
656 * Returns: character contents of @value
657 * Deprecated: 2.32: This function's return type is broken, see g_value_get_schar()
658 */
659 gchar
g_value_get_char(const GValue * value)660 g_value_get_char (const GValue *value)
661 {
662 g_return_val_if_fail (G_VALUE_HOLDS_CHAR (value), 0);
663
664 return value->data[0].v_int;
665 }
666
667 /**
668 * g_value_set_schar:
669 * @value: a valid #GValue of type %G_TYPE_CHAR
670 * @v_char: signed 8 bit integer to be set
671 *
672 * Set the contents of a %G_TYPE_CHAR #GValue to @v_char.
673 *
674 * Since: 2.32
675 */
676 void
g_value_set_schar(GValue * value,gint8 v_char)677 g_value_set_schar (GValue *value,
678 gint8 v_char)
679 {
680 g_return_if_fail (G_VALUE_HOLDS_CHAR (value));
681
682 value->data[0].v_int = v_char;
683 }
684
685 /**
686 * g_value_get_schar:
687 * @value: a valid #GValue of type %G_TYPE_CHAR
688 *
689 * Get the contents of a %G_TYPE_CHAR #GValue.
690 *
691 * Returns: signed 8 bit integer contents of @value
692 * Since: 2.32
693 */
694 gint8
g_value_get_schar(const GValue * value)695 g_value_get_schar (const GValue *value)
696 {
697 g_return_val_if_fail (G_VALUE_HOLDS_CHAR (value), 0);
698
699 return value->data[0].v_int;
700 }
701
702 /**
703 * g_value_set_uchar:
704 * @value: a valid #GValue of type %G_TYPE_UCHAR
705 * @v_uchar: unsigned character value to be set
706 *
707 * Set the contents of a %G_TYPE_UCHAR #GValue to @v_uchar.
708 */
709 void
g_value_set_uchar(GValue * value,guchar v_uchar)710 g_value_set_uchar (GValue *value,
711 guchar v_uchar)
712 {
713 g_return_if_fail (G_VALUE_HOLDS_UCHAR (value));
714
715 value->data[0].v_uint = v_uchar;
716 }
717
718 /**
719 * g_value_get_uchar:
720 * @value: a valid #GValue of type %G_TYPE_UCHAR
721 *
722 * Get the contents of a %G_TYPE_UCHAR #GValue.
723 *
724 * Returns: unsigned character contents of @value
725 */
726 guchar
g_value_get_uchar(const GValue * value)727 g_value_get_uchar (const GValue *value)
728 {
729 g_return_val_if_fail (G_VALUE_HOLDS_UCHAR (value), 0);
730
731 return value->data[0].v_uint;
732 }
733
734 /**
735 * g_value_set_boolean:
736 * @value: a valid #GValue of type %G_TYPE_BOOLEAN
737 * @v_boolean: boolean value to be set
738 *
739 * Set the contents of a %G_TYPE_BOOLEAN #GValue to @v_boolean.
740 */
741 void
g_value_set_boolean(GValue * value,gboolean v_boolean)742 g_value_set_boolean (GValue *value,
743 gboolean v_boolean)
744 {
745 g_return_if_fail (G_VALUE_HOLDS_BOOLEAN (value));
746
747 value->data[0].v_int = v_boolean != FALSE;
748 }
749
750 /**
751 * g_value_get_boolean:
752 * @value: a valid #GValue of type %G_TYPE_BOOLEAN
753 *
754 * Get the contents of a %G_TYPE_BOOLEAN #GValue.
755 *
756 * Returns: boolean contents of @value
757 */
758 gboolean
g_value_get_boolean(const GValue * value)759 g_value_get_boolean (const GValue *value)
760 {
761 g_return_val_if_fail (G_VALUE_HOLDS_BOOLEAN (value), 0);
762
763 return value->data[0].v_int;
764 }
765
766 /**
767 * g_value_set_int:
768 * @value: a valid #GValue of type %G_TYPE_INT
769 * @v_int: integer value to be set
770 *
771 * Set the contents of a %G_TYPE_INT #GValue to @v_int.
772 */
773 void
g_value_set_int(GValue * value,gint v_int)774 g_value_set_int (GValue *value,
775 gint v_int)
776 {
777 g_return_if_fail (G_VALUE_HOLDS_INT (value));
778
779 value->data[0].v_int = v_int;
780 }
781
782 /**
783 * g_value_get_int:
784 * @value: a valid #GValue of type %G_TYPE_INT
785 *
786 * Get the contents of a %G_TYPE_INT #GValue.
787 *
788 * Returns: integer contents of @value
789 */
790 gint
g_value_get_int(const GValue * value)791 g_value_get_int (const GValue *value)
792 {
793 g_return_val_if_fail (G_VALUE_HOLDS_INT (value), 0);
794
795 return value->data[0].v_int;
796 }
797
798 /**
799 * g_value_set_uint:
800 * @value: a valid #GValue of type %G_TYPE_UINT
801 * @v_uint: unsigned integer value to be set
802 *
803 * Set the contents of a %G_TYPE_UINT #GValue to @v_uint.
804 */
805 void
g_value_set_uint(GValue * value,guint v_uint)806 g_value_set_uint (GValue *value,
807 guint v_uint)
808 {
809 g_return_if_fail (G_VALUE_HOLDS_UINT (value));
810
811 value->data[0].v_uint = v_uint;
812 }
813
814 /**
815 * g_value_get_uint:
816 * @value: a valid #GValue of type %G_TYPE_UINT
817 *
818 * Get the contents of a %G_TYPE_UINT #GValue.
819 *
820 * Returns: unsigned integer contents of @value
821 */
822 guint
g_value_get_uint(const GValue * value)823 g_value_get_uint (const GValue *value)
824 {
825 g_return_val_if_fail (G_VALUE_HOLDS_UINT (value), 0);
826
827 return value->data[0].v_uint;
828 }
829
830 /**
831 * g_value_set_long:
832 * @value: a valid #GValue of type %G_TYPE_LONG
833 * @v_long: long integer value to be set
834 *
835 * Set the contents of a %G_TYPE_LONG #GValue to @v_long.
836 */
837 void
g_value_set_long(GValue * value,glong v_long)838 g_value_set_long (GValue *value,
839 glong v_long)
840 {
841 g_return_if_fail (G_VALUE_HOLDS_LONG (value));
842
843 value->data[0].v_long = v_long;
844 }
845
846 /**
847 * g_value_get_long:
848 * @value: a valid #GValue of type %G_TYPE_LONG
849 *
850 * Get the contents of a %G_TYPE_LONG #GValue.
851 *
852 * Returns: long integer contents of @value
853 */
854 glong
g_value_get_long(const GValue * value)855 g_value_get_long (const GValue *value)
856 {
857 g_return_val_if_fail (G_VALUE_HOLDS_LONG (value), 0);
858
859 return value->data[0].v_long;
860 }
861
862 /**
863 * g_value_set_ulong:
864 * @value: a valid #GValue of type %G_TYPE_ULONG
865 * @v_ulong: unsigned long integer value to be set
866 *
867 * Set the contents of a %G_TYPE_ULONG #GValue to @v_ulong.
868 */
869 void
g_value_set_ulong(GValue * value,gulong v_ulong)870 g_value_set_ulong (GValue *value,
871 gulong v_ulong)
872 {
873 g_return_if_fail (G_VALUE_HOLDS_ULONG (value));
874
875 value->data[0].v_ulong = v_ulong;
876 }
877
878 /**
879 * g_value_get_ulong:
880 * @value: a valid #GValue of type %G_TYPE_ULONG
881 *
882 * Get the contents of a %G_TYPE_ULONG #GValue.
883 *
884 * Returns: unsigned long integer contents of @value
885 */
886 gulong
g_value_get_ulong(const GValue * value)887 g_value_get_ulong (const GValue *value)
888 {
889 g_return_val_if_fail (G_VALUE_HOLDS_ULONG (value), 0);
890
891 return value->data[0].v_ulong;
892 }
893
894 /**
895 * g_value_get_int64:
896 * @value: a valid #GValue of type %G_TYPE_INT64
897 *
898 * Get the contents of a %G_TYPE_INT64 #GValue.
899 *
900 * Returns: 64bit integer contents of @value
901 */
902 void
g_value_set_int64(GValue * value,gint64 v_int64)903 g_value_set_int64 (GValue *value,
904 gint64 v_int64)
905 {
906 g_return_if_fail (G_VALUE_HOLDS_INT64 (value));
907
908 value->data[0].v_int64 = v_int64;
909 }
910
911 /**
912 * g_value_set_int64:
913 * @value: a valid #GValue of type %G_TYPE_INT64
914 * @v_int64: 64bit integer value to be set
915 *
916 * Set the contents of a %G_TYPE_INT64 #GValue to @v_int64.
917 */
918 gint64
g_value_get_int64(const GValue * value)919 g_value_get_int64 (const GValue *value)
920 {
921 g_return_val_if_fail (G_VALUE_HOLDS_INT64 (value), 0);
922
923 return value->data[0].v_int64;
924 }
925
926 /**
927 * g_value_set_uint64:
928 * @value: a valid #GValue of type %G_TYPE_UINT64
929 * @v_uint64: unsigned 64bit integer value to be set
930 *
931 * Set the contents of a %G_TYPE_UINT64 #GValue to @v_uint64.
932 */
933 void
g_value_set_uint64(GValue * value,guint64 v_uint64)934 g_value_set_uint64 (GValue *value,
935 guint64 v_uint64)
936 {
937 g_return_if_fail (G_VALUE_HOLDS_UINT64 (value));
938
939 value->data[0].v_uint64 = v_uint64;
940 }
941
942 /**
943 * g_value_get_uint64:
944 * @value: a valid #GValue of type %G_TYPE_UINT64
945 *
946 * Get the contents of a %G_TYPE_UINT64 #GValue.
947 *
948 * Returns: unsigned 64bit integer contents of @value
949 */
950 guint64
g_value_get_uint64(const GValue * value)951 g_value_get_uint64 (const GValue *value)
952 {
953 g_return_val_if_fail (G_VALUE_HOLDS_UINT64 (value), 0);
954
955 return value->data[0].v_uint64;
956 }
957
958 /**
959 * g_value_set_float:
960 * @value: a valid #GValue of type %G_TYPE_FLOAT
961 * @v_float: float value to be set
962 *
963 * Set the contents of a %G_TYPE_FLOAT #GValue to @v_float.
964 */
965 void
g_value_set_float(GValue * value,gfloat v_float)966 g_value_set_float (GValue *value,
967 gfloat v_float)
968 {
969 g_return_if_fail (G_VALUE_HOLDS_FLOAT (value));
970
971 value->data[0].v_float = v_float;
972 }
973
974 /**
975 * g_value_get_float:
976 * @value: a valid #GValue of type %G_TYPE_FLOAT
977 *
978 * Get the contents of a %G_TYPE_FLOAT #GValue.
979 *
980 * Returns: float contents of @value
981 */
982 gfloat
g_value_get_float(const GValue * value)983 g_value_get_float (const GValue *value)
984 {
985 g_return_val_if_fail (G_VALUE_HOLDS_FLOAT (value), 0);
986
987 return value->data[0].v_float;
988 }
989
990 /**
991 * g_value_set_double:
992 * @value: a valid #GValue of type %G_TYPE_DOUBLE
993 * @v_double: double value to be set
994 *
995 * Set the contents of a %G_TYPE_DOUBLE #GValue to @v_double.
996 */
997 void
g_value_set_double(GValue * value,gdouble v_double)998 g_value_set_double (GValue *value,
999 gdouble v_double)
1000 {
1001 g_return_if_fail (G_VALUE_HOLDS_DOUBLE (value));
1002
1003 value->data[0].v_double = v_double;
1004 }
1005
1006 /**
1007 * g_value_get_double:
1008 * @value: a valid #GValue of type %G_TYPE_DOUBLE
1009 *
1010 * Get the contents of a %G_TYPE_DOUBLE #GValue.
1011 *
1012 * Returns: double contents of @value
1013 */
1014 gdouble
g_value_get_double(const GValue * value)1015 g_value_get_double (const GValue *value)
1016 {
1017 g_return_val_if_fail (G_VALUE_HOLDS_DOUBLE (value), 0);
1018
1019 return value->data[0].v_double;
1020 }
1021
1022 /**
1023 * g_value_set_string:
1024 * @value: a valid #GValue of type %G_TYPE_STRING
1025 * @v_string: (nullable): caller-owned string to be duplicated for the #GValue
1026 *
1027 * Set the contents of a %G_TYPE_STRING #GValue to @v_string.
1028 */
1029 void
g_value_set_string(GValue * value,const gchar * v_string)1030 g_value_set_string (GValue *value,
1031 const gchar *v_string)
1032 {
1033 gchar *new_val;
1034
1035 g_return_if_fail (G_VALUE_HOLDS_STRING (value));
1036
1037 new_val = g_strdup (v_string);
1038
1039 if (value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS)
1040 value->data[1].v_uint = 0;
1041 else
1042 g_free (value->data[0].v_pointer);
1043
1044 value->data[0].v_pointer = new_val;
1045 }
1046
1047 /**
1048 * g_value_set_static_string:
1049 * @value: a valid #GValue of type %G_TYPE_STRING
1050 * @v_string: (nullable): static string to be set
1051 *
1052 * Set the contents of a %G_TYPE_STRING #GValue to @v_string.
1053 * The string is assumed to be static, and is thus not duplicated
1054 * when setting the #GValue.
1055 *
1056 * If the the string is a canonical string, using g_value_set_interned_string()
1057 * is more appropriate.
1058 */
1059 void
g_value_set_static_string(GValue * value,const gchar * v_string)1060 g_value_set_static_string (GValue *value,
1061 const gchar *v_string)
1062 {
1063 g_return_if_fail (G_VALUE_HOLDS_STRING (value));
1064
1065 if (!(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
1066 g_free (value->data[0].v_pointer);
1067 value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
1068 value->data[0].v_pointer = (gchar*) v_string;
1069 }
1070
1071 /**
1072 * g_value_set_interned_string:
1073 * @value: a valid #GValue of type %G_TYPE_STRING
1074 * @v_string: (nullable): static string to be set
1075 *
1076 * Set the contents of a %G_TYPE_STRING #GValue to @v_string. The string is
1077 * assumed to be static and interned (canonical, for example from
1078 * g_intern_string()), and is thus not duplicated when setting the #GValue.
1079 *
1080 * Since: 2.66
1081 */
1082 void
g_value_set_interned_string(GValue * value,const gchar * v_string)1083 g_value_set_interned_string (GValue *value,
1084 const gchar *v_string)
1085 {
1086 g_return_if_fail (G_VALUE_HOLDS_STRING (value));
1087
1088 if (!(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
1089 g_free (value->data[0].v_pointer);
1090 value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS | G_VALUE_INTERNED_STRING;
1091 value->data[0].v_pointer = (gchar *) v_string;
1092 }
1093
1094 /**
1095 * g_value_set_string_take_ownership:
1096 * @value: a valid #GValue of type %G_TYPE_STRING
1097 * @v_string: (nullable): duplicated unowned string to be set
1098 *
1099 * This is an internal function introduced mainly for C marshallers.
1100 *
1101 * Deprecated: 2.4: Use g_value_take_string() instead.
1102 */
1103 void
g_value_set_string_take_ownership(GValue * value,gchar * v_string)1104 g_value_set_string_take_ownership (GValue *value,
1105 gchar *v_string)
1106 {
1107 g_value_take_string (value, v_string);
1108 }
1109
1110 /**
1111 * g_value_take_string:
1112 * @value: a valid #GValue of type %G_TYPE_STRING
1113 * @v_string: (nullable): string to take ownership of
1114 *
1115 * Sets the contents of a %G_TYPE_STRING #GValue to @v_string.
1116 *
1117 * Since: 2.4
1118 */
1119 void
g_value_take_string(GValue * value,gchar * v_string)1120 g_value_take_string (GValue *value,
1121 gchar *v_string)
1122 {
1123 g_return_if_fail (G_VALUE_HOLDS_STRING (value));
1124
1125 if (value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS)
1126 value->data[1].v_uint = 0;
1127 else
1128 g_free (value->data[0].v_pointer);
1129 value->data[0].v_pointer = v_string;
1130 }
1131
1132 /**
1133 * g_value_get_string:
1134 * @value: a valid #GValue of type %G_TYPE_STRING
1135 *
1136 * Get the contents of a %G_TYPE_STRING #GValue.
1137 *
1138 * Returns: string content of @value
1139 */
1140 const gchar*
g_value_get_string(const GValue * value)1141 g_value_get_string (const GValue *value)
1142 {
1143 g_return_val_if_fail (G_VALUE_HOLDS_STRING (value), NULL);
1144
1145 return value->data[0].v_pointer;
1146 }
1147
1148 /**
1149 * g_value_dup_string:
1150 * @value: a valid #GValue of type %G_TYPE_STRING
1151 *
1152 * Get a copy the contents of a %G_TYPE_STRING #GValue.
1153 *
1154 * Returns: a newly allocated copy of the string content of @value
1155 */
1156 gchar*
g_value_dup_string(const GValue * value)1157 g_value_dup_string (const GValue *value)
1158 {
1159 g_return_val_if_fail (G_VALUE_HOLDS_STRING (value), NULL);
1160
1161 return g_strdup (value->data[0].v_pointer);
1162 }
1163
1164 /**
1165 * g_value_set_pointer:
1166 * @value: a valid #GValue of %G_TYPE_POINTER
1167 * @v_pointer: pointer value to be set
1168 *
1169 * Set the contents of a pointer #GValue to @v_pointer.
1170 */
1171 void
g_value_set_pointer(GValue * value,gpointer v_pointer)1172 g_value_set_pointer (GValue *value,
1173 gpointer v_pointer)
1174 {
1175 g_return_if_fail (G_VALUE_HOLDS_POINTER (value));
1176
1177 value->data[0].v_pointer = v_pointer;
1178 }
1179
1180 /**
1181 * g_value_get_pointer:
1182 * @value: a valid #GValue of %G_TYPE_POINTER
1183 *
1184 * Get the contents of a pointer #GValue.
1185 *
1186 * Returns: (transfer none): pointer contents of @value
1187 */
1188 gpointer
g_value_get_pointer(const GValue * value)1189 g_value_get_pointer (const GValue *value)
1190 {
1191 g_return_val_if_fail (G_VALUE_HOLDS_POINTER (value), NULL);
1192
1193 return value->data[0].v_pointer;
1194 }
1195
G_DEFINE_POINTER_TYPE(GType,g_gtype)1196 G_DEFINE_POINTER_TYPE (GType, g_gtype)
1197
1198 /**
1199 * g_value_set_gtype:
1200 * @value: a valid #GValue of type %G_TYPE_GTYPE
1201 * @v_gtype: #GType to be set
1202 *
1203 * Set the contents of a %G_TYPE_GTYPE #GValue to @v_gtype.
1204 *
1205 * Since: 2.12
1206 */
1207 void
1208 g_value_set_gtype (GValue *value,
1209 GType v_gtype)
1210 {
1211 g_return_if_fail (G_VALUE_HOLDS_GTYPE (value));
1212
1213 value->data[0].v_pointer = GSIZE_TO_POINTER (v_gtype);
1214
1215 }
1216
1217 /**
1218 * g_value_get_gtype:
1219 * @value: a valid #GValue of type %G_TYPE_GTYPE
1220 *
1221 * Get the contents of a %G_TYPE_GTYPE #GValue.
1222 *
1223 * Since: 2.12
1224 *
1225 * Returns: the #GType stored in @value
1226 */
1227 GType
g_value_get_gtype(const GValue * value)1228 g_value_get_gtype (const GValue *value)
1229 {
1230 g_return_val_if_fail (G_VALUE_HOLDS_GTYPE (value), 0);
1231
1232 return GPOINTER_TO_SIZE (value->data[0].v_pointer);
1233 }
1234
1235 /**
1236 * g_value_set_variant:
1237 * @value: a valid #GValue of type %G_TYPE_VARIANT
1238 * @variant: (nullable): a #GVariant, or %NULL
1239 *
1240 * Set the contents of a variant #GValue to @variant.
1241 * If the variant is floating, it is consumed.
1242 *
1243 * Since: 2.26
1244 */
1245 void
g_value_set_variant(GValue * value,GVariant * variant)1246 g_value_set_variant (GValue *value,
1247 GVariant *variant)
1248 {
1249 GVariant *old_variant;
1250
1251 g_return_if_fail (G_VALUE_HOLDS_VARIANT (value));
1252
1253 old_variant = value->data[0].v_pointer;
1254
1255 if (variant)
1256 value->data[0].v_pointer = g_variant_ref_sink (variant);
1257 else
1258 value->data[0].v_pointer = NULL;
1259
1260 if (old_variant)
1261 g_variant_unref (old_variant);
1262 }
1263
1264 /**
1265 * g_value_take_variant:
1266 * @value: a valid #GValue of type %G_TYPE_VARIANT
1267 * @variant: (nullable) (transfer full): a #GVariant, or %NULL
1268 *
1269 * Set the contents of a variant #GValue to @variant, and takes over
1270 * the ownership of the caller's reference to @variant;
1271 * the caller doesn't have to unref it any more (i.e. the reference
1272 * count of the variant is not increased).
1273 *
1274 * If @variant was floating then its floating reference is converted to
1275 * a hard reference.
1276 *
1277 * If you want the #GValue to hold its own reference to @variant, use
1278 * g_value_set_variant() instead.
1279 *
1280 * This is an internal function introduced mainly for C marshallers.
1281 *
1282 * Since: 2.26
1283 */
1284 void
g_value_take_variant(GValue * value,GVariant * variant)1285 g_value_take_variant (GValue *value,
1286 GVariant *variant)
1287 {
1288 GVariant *old_variant;
1289
1290 g_return_if_fail (G_VALUE_HOLDS_VARIANT (value));
1291
1292 old_variant = value->data[0].v_pointer;
1293
1294 if (variant)
1295 value->data[0].v_pointer = g_variant_take_ref (variant);
1296 else
1297 value->data[0].v_pointer = NULL;
1298
1299 if (old_variant)
1300 g_variant_unref (old_variant);
1301 }
1302
1303 /**
1304 * g_value_get_variant:
1305 * @value: a valid #GValue of type %G_TYPE_VARIANT
1306 *
1307 * Get the contents of a variant #GValue.
1308 *
1309 * Returns: (transfer none) (nullable): variant contents of @value (may be %NULL)
1310 *
1311 * Since: 2.26
1312 */
1313 GVariant*
g_value_get_variant(const GValue * value)1314 g_value_get_variant (const GValue *value)
1315 {
1316 g_return_val_if_fail (G_VALUE_HOLDS_VARIANT (value), NULL);
1317
1318 return value->data[0].v_pointer;
1319 }
1320
1321 /**
1322 * g_value_dup_variant:
1323 * @value: a valid #GValue of type %G_TYPE_VARIANT
1324 *
1325 * Get the contents of a variant #GValue, increasing its refcount. The returned
1326 * #GVariant is never floating.
1327 *
1328 * Returns: (transfer full) (nullable): variant contents of @value (may be %NULL);
1329 * should be unreffed using g_variant_unref() when no longer needed
1330 *
1331 * Since: 2.26
1332 */
1333 GVariant*
g_value_dup_variant(const GValue * value)1334 g_value_dup_variant (const GValue *value)
1335 {
1336 GVariant *variant;
1337
1338 g_return_val_if_fail (G_VALUE_HOLDS_VARIANT (value), NULL);
1339
1340 variant = value->data[0].v_pointer;
1341 if (variant)
1342 g_variant_ref_sink (variant);
1343
1344 return variant;
1345 }
1346
1347 /**
1348 * g_strdup_value_contents:
1349 * @value: #GValue which contents are to be described.
1350 *
1351 * Return a newly allocated string, which describes the contents of a
1352 * #GValue. The main purpose of this function is to describe #GValue
1353 * contents for debugging output, the way in which the contents are
1354 * described may change between different GLib versions.
1355 *
1356 * Returns: Newly allocated string.
1357 */
1358 gchar*
g_strdup_value_contents(const GValue * value)1359 g_strdup_value_contents (const GValue *value)
1360 {
1361 const gchar *src;
1362 gchar *contents;
1363
1364 g_return_val_if_fail (G_IS_VALUE (value), NULL);
1365
1366 if (G_VALUE_HOLDS_STRING (value))
1367 {
1368 src = g_value_get_string (value);
1369
1370 if (!src)
1371 contents = g_strdup ("NULL");
1372 else
1373 {
1374 gchar *s = g_strescape (src, NULL);
1375
1376 contents = g_strdup_printf ("\"%s\"", s);
1377 g_free (s);
1378 }
1379 }
1380 else if (g_value_type_transformable (G_VALUE_TYPE (value), G_TYPE_STRING))
1381 {
1382 GValue tmp_value = G_VALUE_INIT;
1383 gchar *s;
1384
1385 g_value_init (&tmp_value, G_TYPE_STRING);
1386 g_value_transform (value, &tmp_value);
1387 s = g_strescape (g_value_get_string (&tmp_value), NULL);
1388 g_value_unset (&tmp_value);
1389 if (G_VALUE_HOLDS_ENUM (value) || G_VALUE_HOLDS_FLAGS (value))
1390 contents = g_strdup_printf ("((%s) %s)",
1391 g_type_name (G_VALUE_TYPE (value)),
1392 s);
1393 else
1394 contents = g_strdup (s ? s : "NULL");
1395 g_free (s);
1396 }
1397 else if (g_value_fits_pointer (value))
1398 {
1399 gpointer p = g_value_peek_pointer (value);
1400
1401 if (!p)
1402 contents = g_strdup ("NULL");
1403 else if (G_VALUE_HOLDS_OBJECT (value))
1404 contents = g_strdup_printf ("((%s*) %p)", G_OBJECT_TYPE_NAME (p), p);
1405 else if (G_VALUE_HOLDS_PARAM (value))
1406 contents = g_strdup_printf ("((%s*) %p)", G_PARAM_SPEC_TYPE_NAME (p), p);
1407 else if (G_VALUE_HOLDS (value, G_TYPE_STRV))
1408 {
1409 GStrv strv = g_value_get_boxed (value);
1410 GString *tmp = g_string_new ("[");
1411
1412 while (*strv != NULL)
1413 {
1414 gchar *escaped = g_strescape (*strv, NULL);
1415
1416 g_string_append_printf (tmp, "\"%s\"", escaped);
1417 g_free (escaped);
1418
1419 if (*++strv != NULL)
1420 g_string_append (tmp, ", ");
1421 }
1422
1423 g_string_append (tmp, "]");
1424 contents = g_string_free (tmp, FALSE);
1425 }
1426 else if (G_VALUE_HOLDS_BOXED (value))
1427 contents = g_strdup_printf ("((%s*) %p)", g_type_name (G_VALUE_TYPE (value)), p);
1428 else if (G_VALUE_HOLDS_POINTER (value))
1429 contents = g_strdup_printf ("((gpointer) %p)", p);
1430 else
1431 contents = g_strdup ("???");
1432 }
1433 else
1434 contents = g_strdup ("???");
1435
1436 return contents;
1437 }
1438
1439 /**
1440 * g_pointer_type_register_static:
1441 * @name: the name of the new pointer type.
1442 *
1443 * Creates a new %G_TYPE_POINTER derived type id for a new
1444 * pointer type with name @name.
1445 *
1446 * Returns: a new %G_TYPE_POINTER derived type id for @name.
1447 */
1448 GType
g_pointer_type_register_static(const gchar * name)1449 g_pointer_type_register_static (const gchar *name)
1450 {
1451 const GTypeInfo type_info = {
1452 0, /* class_size */
1453 NULL, /* base_init */
1454 NULL, /* base_finalize */
1455 NULL, /* class_init */
1456 NULL, /* class_finalize */
1457 NULL, /* class_data */
1458 0, /* instance_size */
1459 0, /* n_preallocs */
1460 NULL, /* instance_init */
1461 NULL /* value_table */
1462 };
1463 GType type;
1464
1465 g_return_val_if_fail (name != NULL, 0);
1466 g_return_val_if_fail (g_type_from_name (name) == 0, 0);
1467
1468 type = g_type_register_static (G_TYPE_POINTER, name, &type_info, 0);
1469
1470 return type;
1471 }
1472