• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 1997-1999, 2000-2001 Tim Janik and 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 /*
21  * MT safe
22  */
23 
24 #include "config.h"
25 
26 #include <string.h>
27 #include <stdlib.h> /* qsort() */
28 
29 #include "gvaluetypes.h"
30 #include "gvaluecollector.h"
31 #include "gobject.h"
32 #include "gparam.h"
33 #include "gboxed.h"
34 #include "genums.h"
35 #include "gobjectalias.h"
36 
37 
38 /* --- value functions --- */
39 static void
value_init_long0(GValue * value)40 value_init_long0 (GValue *value)
41 {
42   value->data[0].v_long = 0;
43 }
44 
45 static void
value_copy_long0(const GValue * src_value,GValue * dest_value)46 value_copy_long0 (const GValue *src_value,
47 		  GValue       *dest_value)
48 {
49   dest_value->data[0].v_long = src_value->data[0].v_long;
50 }
51 
52 static gchar*
value_lcopy_char(const GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)53 value_lcopy_char (const GValue *value,
54 		  guint         n_collect_values,
55 		  GTypeCValue  *collect_values,
56 		  guint         collect_flags)
57 {
58   gint8 *int8_p = collect_values[0].v_pointer;
59 
60   if (!int8_p)
61     return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
62 
63   *int8_p = value->data[0].v_int;
64 
65   return NULL;
66 }
67 
68 static gchar*
value_lcopy_boolean(const GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)69 value_lcopy_boolean (const GValue *value,
70 		     guint         n_collect_values,
71 		     GTypeCValue  *collect_values,
72 		     guint         collect_flags)
73 {
74   gboolean *bool_p = collect_values[0].v_pointer;
75 
76   if (!bool_p)
77     return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
78 
79   *bool_p = value->data[0].v_int;
80 
81   return NULL;
82 }
83 
84 static gchar*
value_collect_int(GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)85 value_collect_int (GValue      *value,
86 		   guint        n_collect_values,
87 		   GTypeCValue *collect_values,
88 		   guint        collect_flags)
89 {
90   value->data[0].v_int = collect_values[0].v_int;
91 
92   return NULL;
93 }
94 
95 static gchar*
value_lcopy_int(const GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)96 value_lcopy_int (const GValue *value,
97 		 guint         n_collect_values,
98 		 GTypeCValue  *collect_values,
99 		 guint         collect_flags)
100 {
101   gint *int_p = collect_values[0].v_pointer;
102 
103   if (!int_p)
104     return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
105 
106   *int_p = value->data[0].v_int;
107 
108   return NULL;
109 }
110 
111 static gchar*
value_collect_long(GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)112 value_collect_long (GValue      *value,
113 		    guint        n_collect_values,
114 		    GTypeCValue *collect_values,
115 		    guint        collect_flags)
116 {
117   value->data[0].v_long = collect_values[0].v_long;
118 
119   return NULL;
120 }
121 
122 static gchar*
value_lcopy_long(const GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)123 value_lcopy_long (const GValue *value,
124 		  guint         n_collect_values,
125 		  GTypeCValue  *collect_values,
126 		  guint         collect_flags)
127 {
128   glong *long_p = collect_values[0].v_pointer;
129 
130   if (!long_p)
131     return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
132 
133   *long_p = value->data[0].v_long;
134 
135   return NULL;
136 }
137 
138 static void
value_init_int64(GValue * value)139 value_init_int64 (GValue *value)
140 {
141   value->data[0].v_int64 = 0;
142 }
143 
144 static void
value_copy_int64(const GValue * src_value,GValue * dest_value)145 value_copy_int64 (const GValue *src_value,
146 		  GValue       *dest_value)
147 {
148   dest_value->data[0].v_int64 = src_value->data[0].v_int64;
149 }
150 
151 static gchar*
value_collect_int64(GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)152 value_collect_int64 (GValue      *value,
153 		     guint        n_collect_values,
154 		     GTypeCValue *collect_values,
155 		     guint        collect_flags)
156 {
157   value->data[0].v_int64 = collect_values[0].v_int64;
158 
159   return NULL;
160 }
161 
162 static gchar*
value_lcopy_int64(const GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)163 value_lcopy_int64 (const GValue *value,
164 		   guint         n_collect_values,
165 		   GTypeCValue  *collect_values,
166 		   guint         collect_flags)
167 {
168   gint64 *int64_p = collect_values[0].v_pointer;
169 
170   if (!int64_p)
171     return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
172 
173   *int64_p = value->data[0].v_int64;
174 
175   return NULL;
176 }
177 
178 static void
value_init_float(GValue * value)179 value_init_float (GValue *value)
180 {
181   value->data[0].v_float = 0.0;
182 }
183 
184 static void
value_copy_float(const GValue * src_value,GValue * dest_value)185 value_copy_float (const GValue *src_value,
186 		  GValue       *dest_value)
187 {
188   dest_value->data[0].v_float = src_value->data[0].v_float;
189 }
190 
191 static gchar*
value_collect_float(GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)192 value_collect_float (GValue      *value,
193 		     guint        n_collect_values,
194 		     GTypeCValue *collect_values,
195 		     guint        collect_flags)
196 {
197   value->data[0].v_float = collect_values[0].v_double;
198 
199   return NULL;
200 }
201 
202 static gchar*
value_lcopy_float(const GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)203 value_lcopy_float (const GValue *value,
204 		   guint         n_collect_values,
205 		   GTypeCValue  *collect_values,
206 		   guint         collect_flags)
207 {
208   gfloat *float_p = collect_values[0].v_pointer;
209 
210   if (!float_p)
211     return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
212 
213   *float_p = value->data[0].v_float;
214 
215   return NULL;
216 }
217 
218 static void
value_init_double(GValue * value)219 value_init_double (GValue *value)
220 {
221   value->data[0].v_double = 0.0;
222 }
223 
224 static void
value_copy_double(const GValue * src_value,GValue * dest_value)225 value_copy_double (const GValue *src_value,
226 		   GValue	*dest_value)
227 {
228   dest_value->data[0].v_double = src_value->data[0].v_double;
229 }
230 
231 static gchar*
value_collect_double(GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)232 value_collect_double (GValue	  *value,
233 		      guint        n_collect_values,
234 		      GTypeCValue *collect_values,
235 		      guint        collect_flags)
236 {
237   value->data[0].v_double = collect_values[0].v_double;
238 
239   return NULL;
240 }
241 
242 static gchar*
value_lcopy_double(const GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)243 value_lcopy_double (const GValue *value,
244 		    guint         n_collect_values,
245 		    GTypeCValue  *collect_values,
246 		    guint         collect_flags)
247 {
248   gdouble *double_p = collect_values[0].v_pointer;
249 
250   if (!double_p)
251     return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
252 
253   *double_p = value->data[0].v_double;
254 
255   return NULL;
256 }
257 
258 static void
value_init_string(GValue * value)259 value_init_string (GValue *value)
260 {
261   value->data[0].v_pointer = NULL;
262 }
263 
264 static void
value_free_string(GValue * value)265 value_free_string (GValue *value)
266 {
267   if (!(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
268     g_free (value->data[0].v_pointer);
269 }
270 
271 static void
value_copy_string(const GValue * src_value,GValue * dest_value)272 value_copy_string (const GValue *src_value,
273 		   GValue	*dest_value)
274 {
275   dest_value->data[0].v_pointer = g_strdup (src_value->data[0].v_pointer);
276 }
277 
278 static gchar*
value_collect_string(GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)279 value_collect_string (GValue	  *value,
280 		      guint        n_collect_values,
281 		      GTypeCValue *collect_values,
282 		      guint        collect_flags)
283 {
284   if (!collect_values[0].v_pointer)
285     value->data[0].v_pointer = NULL;
286   else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
287     {
288       value->data[0].v_pointer = collect_values[0].v_pointer;
289       value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
290     }
291   else
292     value->data[0].v_pointer = g_strdup (collect_values[0].v_pointer);
293 
294   return NULL;
295 }
296 
297 static gchar*
value_lcopy_string(const GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)298 value_lcopy_string (const GValue *value,
299 		    guint         n_collect_values,
300 		    GTypeCValue  *collect_values,
301 		    guint         collect_flags)
302 {
303   gchar **string_p = collect_values[0].v_pointer;
304 
305   if (!string_p)
306     return 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   if (!pointer_p)
357     return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
358 
359   *pointer_p = value->data[0].v_pointer;
360 
361   return NULL;
362 }
363 
364 
365 /* --- type initialization --- */
366 void
g_value_types_init(void)367 g_value_types_init (void)
368 {
369   GTypeInfo info = {
370     0,				/* class_size */
371     NULL,			/* base_init */
372     NULL,			/* base_destroy */
373     NULL,			/* class_init */
374     NULL,			/* class_destroy */
375     NULL,			/* class_data */
376     0,				/* instance_size */
377     0,				/* n_preallocs */
378     NULL,			/* instance_init */
379     NULL,			/* value_table */
380   };
381   const GTypeFundamentalInfo finfo = { G_TYPE_FLAG_DERIVABLE, };
382   GType type;
383 
384   /* G_TYPE_CHAR / G_TYPE_UCHAR
385    */
386   {
387     static const GTypeValueTable value_table = {
388       value_init_long0,		/* value_init */
389       NULL,			/* value_free */
390       value_copy_long0,		/* value_copy */
391       NULL,			/* value_peek_pointer */
392       "i",			/* collect_format */
393       value_collect_int,	/* collect_value */
394       "p",			/* lcopy_format */
395       value_lcopy_char,		/* lcopy_value */
396     };
397     info.value_table = &value_table;
398     type = g_type_register_fundamental (G_TYPE_CHAR, g_intern_static_string ("gchar"), &info, &finfo, 0);
399     g_assert (type == G_TYPE_CHAR);
400     type = g_type_register_fundamental (G_TYPE_UCHAR, g_intern_static_string ("guchar"), &info, &finfo, 0);
401     g_assert (type == G_TYPE_UCHAR);
402   }
403 
404   /* G_TYPE_BOOLEAN
405    */
406   {
407     static const GTypeValueTable value_table = {
408       value_init_long0,		 /* value_init */
409       NULL,			 /* value_free */
410       value_copy_long0,		 /* value_copy */
411       NULL,                      /* value_peek_pointer */
412       "i",			 /* collect_format */
413       value_collect_int,	 /* collect_value */
414       "p",			 /* lcopy_format */
415       value_lcopy_boolean,	 /* lcopy_value */
416     };
417     info.value_table = &value_table;
418     type = g_type_register_fundamental (G_TYPE_BOOLEAN, g_intern_static_string ("gboolean"), &info, &finfo, 0);
419     g_assert (type == G_TYPE_BOOLEAN);
420   }
421 
422   /* G_TYPE_INT / G_TYPE_UINT
423    */
424   {
425     static const GTypeValueTable value_table = {
426       value_init_long0,		/* value_init */
427       NULL,			/* value_free */
428       value_copy_long0,		/* value_copy */
429       NULL,                     /* value_peek_pointer */
430       "i",			/* collect_format */
431       value_collect_int,	/* collect_value */
432       "p",			/* lcopy_format */
433       value_lcopy_int,		/* lcopy_value */
434     };
435     info.value_table = &value_table;
436     type = g_type_register_fundamental (G_TYPE_INT, g_intern_static_string ("gint"), &info, &finfo, 0);
437     g_assert (type == G_TYPE_INT);
438     type = g_type_register_fundamental (G_TYPE_UINT, g_intern_static_string ("guint"), &info, &finfo, 0);
439     g_assert (type == G_TYPE_UINT);
440   }
441 
442   /* G_TYPE_LONG / G_TYPE_ULONG
443    */
444   {
445     static const GTypeValueTable value_table = {
446       value_init_long0,		/* value_init */
447       NULL,			/* value_free */
448       value_copy_long0,		/* value_copy */
449       NULL,                     /* value_peek_pointer */
450       "l",			/* collect_format */
451       value_collect_long,	/* collect_value */
452       "p",			/* lcopy_format */
453       value_lcopy_long,		/* lcopy_value */
454     };
455     info.value_table = &value_table;
456     type = g_type_register_fundamental (G_TYPE_LONG, g_intern_static_string ("glong"), &info, &finfo, 0);
457     g_assert (type == G_TYPE_LONG);
458     type = g_type_register_fundamental (G_TYPE_ULONG, g_intern_static_string ("gulong"), &info, &finfo, 0);
459     g_assert (type == G_TYPE_ULONG);
460   }
461 
462   /* G_TYPE_INT64 / G_TYPE_UINT64
463    */
464   {
465     static const GTypeValueTable value_table = {
466       value_init_int64,		/* value_init */
467       NULL,			/* value_free */
468       value_copy_int64,		/* value_copy */
469       NULL,                     /* value_peek_pointer */
470       "q",			/* collect_format */
471       value_collect_int64,	/* collect_value */
472       "p",			/* lcopy_format */
473       value_lcopy_int64,	/* lcopy_value */
474     };
475     info.value_table = &value_table;
476     type = g_type_register_fundamental (G_TYPE_INT64, g_intern_static_string ("gint64"), &info, &finfo, 0);
477     g_assert (type == G_TYPE_INT64);
478     type = g_type_register_fundamental (G_TYPE_UINT64, g_intern_static_string ("guint64"), &info, &finfo, 0);
479     g_assert (type == G_TYPE_UINT64);
480   }
481 
482   /* G_TYPE_FLOAT
483    */
484   {
485     static const GTypeValueTable value_table = {
486       value_init_float,		 /* value_init */
487       NULL,			 /* value_free */
488       value_copy_float,		 /* value_copy */
489       NULL,                      /* value_peek_pointer */
490       "d",			 /* collect_format */
491       value_collect_float,	 /* collect_value */
492       "p",			 /* lcopy_format */
493       value_lcopy_float,	 /* lcopy_value */
494     };
495     info.value_table = &value_table;
496     type = g_type_register_fundamental (G_TYPE_FLOAT, g_intern_static_string ("gfloat"), &info, &finfo, 0);
497     g_assert (type == G_TYPE_FLOAT);
498   }
499 
500   /* G_TYPE_DOUBLE
501    */
502   {
503     static const GTypeValueTable value_table = {
504       value_init_double,	/* value_init */
505       NULL,			/* value_free */
506       value_copy_double,	/* value_copy */
507       NULL,                     /* value_peek_pointer */
508       "d",			/* collect_format */
509       value_collect_double,	/* collect_value */
510       "p",			/* lcopy_format */
511       value_lcopy_double,	/* lcopy_value */
512     };
513     info.value_table = &value_table;
514     type = g_type_register_fundamental (G_TYPE_DOUBLE, g_intern_static_string ("gdouble"), &info, &finfo, 0);
515     g_assert (type == G_TYPE_DOUBLE);
516   }
517 
518   /* G_TYPE_STRING
519    */
520   {
521     static const GTypeValueTable value_table = {
522       value_init_string,	/* value_init */
523       value_free_string,	/* value_free */
524       value_copy_string,	/* value_copy */
525       value_peek_pointer0,	/* value_peek_pointer */
526       "p",			/* collect_format */
527       value_collect_string,	/* collect_value */
528       "p",			/* lcopy_format */
529       value_lcopy_string,	/* lcopy_value */
530     };
531     info.value_table = &value_table;
532     type = g_type_register_fundamental (G_TYPE_STRING, g_intern_static_string ("gchararray"), &info, &finfo, 0);
533     g_assert (type == G_TYPE_STRING);
534   }
535 
536   /* G_TYPE_POINTER
537    */
538   {
539     static const GTypeValueTable value_table = {
540       value_init_pointer,	/* value_init */
541       NULL,			/* value_free */
542       value_copy_pointer,	/* value_copy */
543       value_peek_pointer0,	/* value_peek_pointer */
544       "p",			/* collect_format */
545       value_collect_pointer,	/* collect_value */
546       "p",			/* lcopy_format */
547       value_lcopy_pointer,	/* lcopy_value */
548     };
549     info.value_table = &value_table;
550     type = g_type_register_fundamental (G_TYPE_POINTER, g_intern_static_string ("gpointer"), &info, &finfo, 0);
551     g_assert (type == G_TYPE_POINTER);
552   }
553 }
554 
555 
556 /* --- GValue functions --- */
557 /**
558  * g_value_set_char:
559  * @value: a valid #GValue of type %G_TYPE_CHAR
560  * @v_char: character value to be set
561  *
562  * Set the contents of a %G_TYPE_CHAR #GValue to @v_char.
563  */
564 void
g_value_set_char(GValue * value,gchar v_char)565 g_value_set_char (GValue *value,
566 		  gchar	  v_char)
567 {
568   g_return_if_fail (G_VALUE_HOLDS_CHAR (value));
569 
570   value->data[0].v_int = v_char;
571 }
572 
573 /**
574  * g_value_get_char:
575  * @value: a valid #GValue of type %G_TYPE_CHAR
576  *
577  * Get the contents of a %G_TYPE_CHAR #GValue.
578  *
579  * Returns: character contents of @value
580  */
581 gchar
g_value_get_char(const GValue * value)582 g_value_get_char (const GValue *value)
583 {
584   g_return_val_if_fail (G_VALUE_HOLDS_CHAR (value), 0);
585 
586   return value->data[0].v_int;
587 }
588 
589 /**
590  * g_value_set_uchar:
591  * @value: a valid #GValue of type %G_TYPE_UCHAR
592  * @v_uchar: unsigned character value to be set
593  *
594  * Set the contents of a %G_TYPE_UCHAR #GValue to @v_uchar.
595  */
596 void
g_value_set_uchar(GValue * value,guchar v_uchar)597 g_value_set_uchar (GValue *value,
598 		   guchar  v_uchar)
599 {
600   g_return_if_fail (G_VALUE_HOLDS_UCHAR (value));
601 
602   value->data[0].v_uint = v_uchar;
603 }
604 
605 /**
606  * g_value_get_uchar:
607  * @value: a valid #GValue of type %G_TYPE_UCHAR
608  *
609  * Get the contents of a %G_TYPE_UCHAR #GValue.
610  *
611  * Returns: unsigned character contents of @value
612  */
613 guchar
g_value_get_uchar(const GValue * value)614 g_value_get_uchar (const GValue *value)
615 {
616   g_return_val_if_fail (G_VALUE_HOLDS_UCHAR (value), 0);
617 
618   return value->data[0].v_uint;
619 }
620 
621 /**
622  * g_value_set_boolean:
623  * @value: a valid #GValue of type %G_TYPE_BOOLEAN
624  * @v_boolean: boolean value to be set
625  *
626  * Set the contents of a %G_TYPE_BOOLEAN #GValue to @v_boolean.
627  */
628 void
g_value_set_boolean(GValue * value,gboolean v_boolean)629 g_value_set_boolean (GValue  *value,
630 		     gboolean v_boolean)
631 {
632   g_return_if_fail (G_VALUE_HOLDS_BOOLEAN (value));
633 
634   value->data[0].v_int = v_boolean != FALSE;
635 }
636 
637 /**
638  * g_value_get_boolean:
639  * @value: a valid #GValue of type %G_TYPE_BOOLEAN
640  *
641  * Get the contents of a %G_TYPE_BOOLEAN #GValue.
642  *
643  * Returns: boolean contents of @value
644  */
645 gboolean
g_value_get_boolean(const GValue * value)646 g_value_get_boolean (const GValue *value)
647 {
648   g_return_val_if_fail (G_VALUE_HOLDS_BOOLEAN (value), 0);
649 
650   return value->data[0].v_int;
651 }
652 
653 /**
654  * g_value_set_int:
655  * @value: a valid #GValue of type %G_TYPE_INT
656  * @v_int: integer value to be set
657  *
658  * Set the contents of a %G_TYPE_INT #GValue to @v_int.
659  */
660 void
g_value_set_int(GValue * value,gint v_int)661 g_value_set_int (GValue *value,
662 		 gint	 v_int)
663 {
664   g_return_if_fail (G_VALUE_HOLDS_INT (value));
665 
666   value->data[0].v_int = v_int;
667 }
668 
669 /**
670  * g_value_get_int:
671  * @value: a valid #GValue of type %G_TYPE_INT
672  *
673  * Get the contents of a %G_TYPE_INT #GValue.
674  *
675  * Returns: integer contents of @value
676  */
677 gint
g_value_get_int(const GValue * value)678 g_value_get_int (const GValue *value)
679 {
680   g_return_val_if_fail (G_VALUE_HOLDS_INT (value), 0);
681 
682   return value->data[0].v_int;
683 }
684 
685 /**
686  * g_value_set_uint:
687  * @value: a valid #GValue of type %G_TYPE_UINT
688  * @v_uint: unsigned integer value to be set
689  *
690  * Set the contents of a %G_TYPE_UINT #GValue to @v_uint.
691  */
692 void
g_value_set_uint(GValue * value,guint v_uint)693 g_value_set_uint (GValue *value,
694 		  guint	  v_uint)
695 {
696   g_return_if_fail (G_VALUE_HOLDS_UINT (value));
697 
698   value->data[0].v_uint = v_uint;
699 }
700 
701 /**
702  * g_value_get_uint:
703  * @value: a valid #GValue of type %G_TYPE_UINT
704  *
705  * Get the contents of a %G_TYPE_UINT #GValue.
706  *
707  * Returns: unsigned integer contents of @value
708  */
709 guint
g_value_get_uint(const GValue * value)710 g_value_get_uint (const GValue *value)
711 {
712   g_return_val_if_fail (G_VALUE_HOLDS_UINT (value), 0);
713 
714   return value->data[0].v_uint;
715 }
716 
717 /**
718  * g_value_set_long:
719  * @value: a valid #GValue of type %G_TYPE_LONG
720  * @v_long: long integer value to be set
721  *
722  * Set the contents of a %G_TYPE_LONG #GValue to @v_long.
723  */
724 void
g_value_set_long(GValue * value,glong v_long)725 g_value_set_long (GValue *value,
726 		  glong	  v_long)
727 {
728   g_return_if_fail (G_VALUE_HOLDS_LONG (value));
729 
730   value->data[0].v_long = v_long;
731 }
732 
733 /**
734  * g_value_get_long:
735  * @value: a valid #GValue of type %G_TYPE_LONG
736  *
737  * Get the contents of a %G_TYPE_LONG #GValue.
738  *
739  * Returns: long integer contents of @value
740  */
741 glong
g_value_get_long(const GValue * value)742 g_value_get_long (const GValue *value)
743 {
744   g_return_val_if_fail (G_VALUE_HOLDS_LONG (value), 0);
745 
746   return value->data[0].v_long;
747 }
748 
749 /**
750  * g_value_set_ulong:
751  * @value: a valid #GValue of type %G_TYPE_ULONG
752  * @v_ulong: unsigned long integer value to be set
753  *
754  * Set the contents of a %G_TYPE_ULONG #GValue to @v_ulong.
755  */
756 void
g_value_set_ulong(GValue * value,gulong v_ulong)757 g_value_set_ulong (GValue *value,
758 		   gulong  v_ulong)
759 {
760   g_return_if_fail (G_VALUE_HOLDS_ULONG (value));
761 
762   value->data[0].v_ulong = v_ulong;
763 }
764 
765 /**
766  * g_value_get_ulong:
767  * @value: a valid #GValue of type %G_TYPE_ULONG
768  *
769  * Get the contents of a %G_TYPE_ULONG #GValue.
770  *
771  * Returns: unsigned long integer contents of @value
772  */
773 gulong
g_value_get_ulong(const GValue * value)774 g_value_get_ulong (const GValue *value)
775 {
776   g_return_val_if_fail (G_VALUE_HOLDS_ULONG (value), 0);
777 
778   return value->data[0].v_ulong;
779 }
780 
781 /**
782  * g_value_get_int64:
783  * @value: a valid #GValue of type %G_TYPE_INT64
784  *
785  * Get the contents of a %G_TYPE_INT64 #GValue.
786  *
787  * Returns: 64bit integer contents of @value
788  */
789 void
g_value_set_int64(GValue * value,gint64 v_int64)790 g_value_set_int64 (GValue *value,
791 		   gint64  v_int64)
792 {
793   g_return_if_fail (G_VALUE_HOLDS_INT64 (value));
794 
795   value->data[0].v_int64 = v_int64;
796 }
797 
798 /**
799  * g_value_set_int64:
800  * @value: a valid #GValue of type %G_TYPE_INT64
801  * @v_int64: 64bit integer value to be set
802  *
803  * Set the contents of a %G_TYPE_INT64 #GValue to @v_int64.
804  */
805 gint64
g_value_get_int64(const GValue * value)806 g_value_get_int64 (const GValue *value)
807 {
808   g_return_val_if_fail (G_VALUE_HOLDS_INT64 (value), 0);
809 
810   return value->data[0].v_int64;
811 }
812 
813 /**
814  * g_value_set_uint64:
815  * @value: a valid #GValue of type %G_TYPE_UINT64
816  * @v_uint64: unsigned 64bit integer value to be set
817  *
818  * Set the contents of a %G_TYPE_UINT64 #GValue to @v_uint64.
819  */
820 void
g_value_set_uint64(GValue * value,guint64 v_uint64)821 g_value_set_uint64 (GValue *value,
822 		    guint64 v_uint64)
823 {
824   g_return_if_fail (G_VALUE_HOLDS_UINT64 (value));
825 
826   value->data[0].v_uint64 = v_uint64;
827 }
828 
829 /**
830  * g_value_get_uint64:
831  * @value: a valid #GValue of type %G_TYPE_UINT64
832  *
833  * Get the contents of a %G_TYPE_UINT64 #GValue.
834  *
835  * Returns: unsigned 64bit integer contents of @value
836  */
837 guint64
g_value_get_uint64(const GValue * value)838 g_value_get_uint64 (const GValue *value)
839 {
840   g_return_val_if_fail (G_VALUE_HOLDS_UINT64 (value), 0);
841 
842   return value->data[0].v_uint64;
843 }
844 
845 /**
846  * g_value_set_float:
847  * @value: a valid #GValue of type %G_TYPE_FLOAT
848  * @v_float: float value to be set
849  *
850  * Set the contents of a %G_TYPE_FLOAT #GValue to @v_float.
851  */
852 void
g_value_set_float(GValue * value,gfloat v_float)853 g_value_set_float (GValue *value,
854 		   gfloat  v_float)
855 {
856   g_return_if_fail (G_VALUE_HOLDS_FLOAT (value));
857 
858   value->data[0].v_float = v_float;
859 }
860 
861 /**
862  * g_value_get_float:
863  * @value: a valid #GValue of type %G_TYPE_FLOAT
864  *
865  * Get the contents of a %G_TYPE_FLOAT #GValue.
866  *
867  * Returns: float contents of @value
868  */
869 gfloat
g_value_get_float(const GValue * value)870 g_value_get_float (const GValue *value)
871 {
872   g_return_val_if_fail (G_VALUE_HOLDS_FLOAT (value), 0);
873 
874   return value->data[0].v_float;
875 }
876 
877 /**
878  * g_value_set_double:
879  * @value: a valid #GValue of type %G_TYPE_DOUBLE
880  * @v_double: double value to be set
881  *
882  * Set the contents of a %G_TYPE_DOUBLE #GValue to @v_double.
883  */
884 void
g_value_set_double(GValue * value,gdouble v_double)885 g_value_set_double (GValue *value,
886 		    gdouble v_double)
887 {
888   g_return_if_fail (G_VALUE_HOLDS_DOUBLE (value));
889 
890   value->data[0].v_double = v_double;
891 }
892 
893 /**
894  * g_value_get_double:
895  * @value: a valid #GValue of type %G_TYPE_DOUBLE
896  *
897  * Get the contents of a %G_TYPE_DOUBLE #GValue.
898  *
899  * Returns: double contents of @value
900  */
901 gdouble
g_value_get_double(const GValue * value)902 g_value_get_double (const GValue *value)
903 {
904   g_return_val_if_fail (G_VALUE_HOLDS_DOUBLE (value), 0);
905 
906   return value->data[0].v_double;
907 }
908 
909 /**
910  * g_value_set_string:
911  * @value: a valid #GValue of type %G_TYPE_STRING
912  * @v_string: caller-owned string to be duplicated for the #GValue
913  *
914  * Set the contents of a %G_TYPE_STRING #GValue to @v_string.
915  */
916 void
g_value_set_string(GValue * value,const gchar * v_string)917 g_value_set_string (GValue	*value,
918 		    const gchar *v_string)
919 {
920   gchar *new_val;
921 
922   g_return_if_fail (G_VALUE_HOLDS_STRING (value));
923 
924   new_val = g_strdup (v_string);
925 
926   if (value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS)
927     value->data[1].v_uint = 0;
928   else
929     g_free (value->data[0].v_pointer);
930 
931   value->data[0].v_pointer = new_val;
932 }
933 
934 /**
935  * g_value_set_static_string:
936  * @value: a valid #GValue of type %G_TYPE_STRING
937  * @v_string: static string to be set
938  *
939  * Set the contents of a %G_TYPE_STRING #GValue to @v_string.
940  * The string is assumed to be static, and is thus not duplicated
941  * when setting the #GValue.
942  */
943 void
g_value_set_static_string(GValue * value,const gchar * v_string)944 g_value_set_static_string (GValue      *value,
945 			   const gchar *v_string)
946 {
947   g_return_if_fail (G_VALUE_HOLDS_STRING (value));
948 
949   if (!(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
950     g_free (value->data[0].v_pointer);
951   value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
952   value->data[0].v_pointer = (gchar*) v_string;
953 }
954 
955 /**
956  * g_value_set_string_take_ownership:
957  * @value: a valid #GValue of type %G_TYPE_STRING
958  * @v_string: duplicated unowned string to be set
959  *
960  * This is an internal function introduced mainly for C marshallers.
961  *
962  * Deprecated: 2.4: Use g_value_take_string() instead.
963  */
964 void
g_value_set_string_take_ownership(GValue * value,gchar * v_string)965 g_value_set_string_take_ownership (GValue *value,
966 				   gchar  *v_string)
967 {
968   g_value_take_string (value, v_string);
969 }
970 
971 /**
972  * g_value_take_string:
973  * @value: a valid #GValue of type %G_TYPE_STRING
974  * @v_string: string to take ownership of
975  *
976  * Sets the contents of a %G_TYPE_STRING #GValue to @v_string.
977  *
978  * Since: 2.4
979  */
980 void
g_value_take_string(GValue * value,gchar * v_string)981 g_value_take_string (GValue *value,
982 		     gchar  *v_string)
983 {
984   g_return_if_fail (G_VALUE_HOLDS_STRING (value));
985 
986   if (value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS)
987     value->data[1].v_uint = 0;
988   else
989     g_free (value->data[0].v_pointer);
990   value->data[0].v_pointer = v_string;
991 }
992 
993 /**
994  * g_value_get_string:
995  * @value: a valid #GValue of type %G_TYPE_STRING
996  *
997  * Get the contents of a %G_TYPE_STRING #GValue.
998  *
999  * Returns: string content of @value
1000  */
1001 G_CONST_RETURN gchar*
g_value_get_string(const GValue * value)1002 g_value_get_string (const GValue *value)
1003 {
1004   g_return_val_if_fail (G_VALUE_HOLDS_STRING (value), NULL);
1005 
1006   return value->data[0].v_pointer;
1007 }
1008 
1009 /**
1010  * g_value_dup_string:
1011  * @value: a valid #GValue of type %G_TYPE_STRING
1012  *
1013  * Get a copy the contents of a %G_TYPE_STRING #GValue.
1014  *
1015  * Returns: a newly allocated copy of the string content of @value
1016  */
1017 gchar*
g_value_dup_string(const GValue * value)1018 g_value_dup_string (const GValue *value)
1019 {
1020   g_return_val_if_fail (G_VALUE_HOLDS_STRING (value), NULL);
1021 
1022   return g_strdup (value->data[0].v_pointer);
1023 }
1024 
1025 /**
1026  * g_value_set_pointer:
1027  * @value: a valid #GValue of %G_TYPE_POINTER
1028  * @v_pointer: pointer value to be set
1029  *
1030  * Set the contents of a pointer #GValue to @v_pointer.
1031  */
1032 void
g_value_set_pointer(GValue * value,gpointer v_pointer)1033 g_value_set_pointer (GValue  *value,
1034 		     gpointer v_pointer)
1035 {
1036   g_return_if_fail (G_VALUE_HOLDS_POINTER (value));
1037 
1038   value->data[0].v_pointer = v_pointer;
1039 }
1040 
1041 /**
1042  * g_value_get_pointer:
1043  * @value: a valid #GValue of %G_TYPE_POINTER
1044  *
1045  * Get the contents of a pointer #GValue.
1046  *
1047  * Returns: pointer contents of @value
1048  */
1049 gpointer
g_value_get_pointer(const GValue * value)1050 g_value_get_pointer (const GValue *value)
1051 {
1052   g_return_val_if_fail (G_VALUE_HOLDS_POINTER (value), NULL);
1053 
1054   return value->data[0].v_pointer;
1055 }
1056 
1057 GType
g_gtype_get_type(void)1058 g_gtype_get_type (void)
1059 {
1060   static const GTypeInfo type_info = { 0, };
1061   static GType type;
1062   if (!type)
1063     type = g_type_register_static (G_TYPE_POINTER, g_intern_static_string ("GType"), &type_info, 0);
1064   return type;
1065 }
1066 
1067 /**
1068  * g_value_set_gtype:
1069  * @value: a valid #GValue of type %G_TYPE_GTYPE
1070  * @v_gtype: #GType to be set
1071  *
1072  * Set the contents of a %G_TYPE_GTYPE #GValue to @v_gtype.
1073  *
1074  * Since: 2.12
1075  */
1076 void
g_value_set_gtype(GValue * value,GType v_gtype)1077 g_value_set_gtype (GValue *value,
1078 		   GType   v_gtype)
1079 {
1080   g_return_if_fail (G_VALUE_HOLDS_GTYPE (value));
1081 
1082   value->data[0].v_long = v_gtype;
1083 
1084 }
1085 
1086 /**
1087  * g_value_get_gtype:
1088  * @value: a valid #GValue of type %G_TYPE_GTYPE
1089  *
1090  * Get the contents of a %G_TYPE_GTYPE #GValue.
1091  *
1092  * Since: 2.12
1093  *
1094  * Returns: the #GType stored in @value
1095  */
1096 GType
g_value_get_gtype(const GValue * value)1097 g_value_get_gtype (const GValue *value)
1098 {
1099   g_return_val_if_fail (G_VALUE_HOLDS_GTYPE (value), 0);
1100 
1101   return value->data[0].v_long;
1102 }
1103 
1104 /**
1105  * g_strdup_value_contents:
1106  * @value: #GValue which contents are to be described.
1107  *
1108  * Return a newly allocated string, which describes the contents of a
1109  * #GValue.  The main purpose of this function is to describe #GValue
1110  * contents for debugging output, the way in which the contents are
1111  * described may change between different GLib versions.
1112  *
1113  * Returns: Newly allocated string.
1114  */
1115 gchar*
g_strdup_value_contents(const GValue * value)1116 g_strdup_value_contents (const GValue *value)
1117 {
1118   const gchar *src;
1119   gchar *contents;
1120 
1121   g_return_val_if_fail (G_IS_VALUE (value), NULL);
1122 
1123   if (G_VALUE_HOLDS_STRING (value))
1124     {
1125       src = g_value_get_string (value);
1126 
1127       if (!src)
1128 	contents = g_strdup ("NULL");
1129       else
1130 	{
1131 	  gchar *s = g_strescape (src, NULL);
1132 
1133 	  contents = g_strdup_printf ("\"%s\"", s);
1134 	  g_free (s);
1135 	}
1136     }
1137   else if (g_value_type_transformable (G_VALUE_TYPE (value), G_TYPE_STRING))
1138     {
1139       GValue tmp_value = { 0, };
1140       gchar *s;
1141 
1142       g_value_init (&tmp_value, G_TYPE_STRING);
1143       g_value_transform (value, &tmp_value);
1144       s = g_strescape (g_value_get_string (&tmp_value), NULL);
1145       g_value_unset (&tmp_value);
1146       if (G_VALUE_HOLDS_ENUM (value) || G_VALUE_HOLDS_FLAGS (value))
1147 	contents = g_strdup_printf ("((%s) %s)",
1148 				    g_type_name (G_VALUE_TYPE (value)),
1149 				    s);
1150       else
1151 	contents = g_strdup (s ? s : "NULL");
1152       g_free (s);
1153     }
1154   else if (g_value_fits_pointer (value))
1155     {
1156       gpointer p = g_value_peek_pointer (value);
1157 
1158       if (!p)
1159 	contents = g_strdup ("NULL");
1160       else if (G_VALUE_HOLDS_OBJECT (value))
1161 	contents = g_strdup_printf ("((%s*) %p)", G_OBJECT_TYPE_NAME (p), p);
1162       else if (G_VALUE_HOLDS_PARAM (value))
1163 	contents = g_strdup_printf ("((%s*) %p)", G_PARAM_SPEC_TYPE_NAME (p), p);
1164       else if (G_VALUE_HOLDS_BOXED (value))
1165 	contents = g_strdup_printf ("((%s*) %p)", g_type_name (G_VALUE_TYPE (value)), p);
1166       else if (G_VALUE_HOLDS_POINTER (value))
1167 	contents = g_strdup_printf ("((gpointer) %p)", p);
1168       else
1169 	contents = g_strdup ("???");
1170     }
1171   else
1172     contents = g_strdup ("???");
1173 
1174   return contents;
1175 }
1176 
1177 /**
1178  * g_pointer_type_register_static:
1179  * @name: the name of the new pointer type.
1180  *
1181  * Creates a new %G_TYPE_POINTER derived type id for a new
1182  * pointer type with name @name.
1183  *
1184  * Returns: a new %G_TYPE_POINTER derived type id for @name.
1185  */
1186 GType
g_pointer_type_register_static(const gchar * name)1187 g_pointer_type_register_static (const gchar *name)
1188 {
1189   static const GTypeInfo type_info = {
1190     0,			/* class_size */
1191     NULL,		/* base_init */
1192     NULL,		/* base_finalize */
1193     NULL,		/* class_init */
1194     NULL,		/* class_finalize */
1195     NULL,		/* class_data */
1196     0,			/* instance_size */
1197     0,			/* n_preallocs */
1198     NULL,		/* instance_init */
1199     NULL		/* value_table */
1200   };
1201   GType type;
1202 
1203   g_return_val_if_fail (name != NULL, 0);
1204   g_return_val_if_fail (g_type_from_name (name) == 0, 0);
1205 
1206   type = g_type_register_static (G_TYPE_POINTER, name, &type_info, 0);
1207 
1208   return type;
1209 }
1210 
1211 #define __G_VALUETYPES_C__
1212 #include "gobjectaliasdef.c"
1213