1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 1997-1999, 2000-2001 Tim Janik and Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
20 * SECTION:generic_values
21 * @Short_description: A polymorphic type that can hold values of any other type
22 * @See_also: The fundamental types which all support #GValue operations and
23 * thus can be used as a type initializer for g_value_init() are defined by
24 * a separate interface. See the <link
25 * linkend="gobject-Standard-Parameter-and-Value-Types">Standard Values
26 * API</link> for details.
27 * @Title: Generic values
29 * The #GValue structure is basically a variable container that consists
30 * of a type identifier and a specific value of that type.
31 * The type identifier within a #GValue structure always determines the
32 * type of the associated value.
33 * To create a undefined #GValue structure, simply create a zero-filled
34 * #GValue structure. To initialize the #GValue, use the g_value_init()
35 * function. A #GValue cannot be used until it is initialized.
36 * The basic type operations (such as freeing and copying) are determined
37 * by the #GTypeValueTable associated with the type ID stored in the #GValue.
38 * Other #GValue operations (such as converting values between types) are
39 * provided by this interface.
49 #include "gvaluecollector.h"
50 #include "gbsearcharray.h"
51 #include "gobjectalias.h"
54 /* --- typedefs & structures --- */
62 /* --- prototypes --- */
63 static gint transform_entries_cmp (gconstpointer bsearch_node1,
64 gconstpointer bsearch_node2);
67 /* --- variables --- */
68 static GBSearchArray *transform_array = NULL;
69 static GBSearchConfig transform_bconfig = {
70 sizeof (TransformEntry),
71 transform_entries_cmp,
76 /* --- functions --- */
80 transform_array = g_bsearch_array_create (&transform_bconfig);
83 static inline void /* keep this function in sync with gvaluecollector.h and gboxed.c */
84 value_meminit (GValue *value,
87 value->g_type = value_type;
88 memset (value->data, 0, sizeof (value->data));
93 * @value: A zero-filled (uninitialized) #GValue structure.
94 * @g_type: Type the #GValue should hold values of.
96 * Initializes @value with the default value of @type.
98 * Returns: the #GValue structure that has been passed in
101 g_value_init (GValue *value,
104 /* g_return_val_if_fail (G_TYPE_IS_VALUE (g_type), NULL); be more elaborate below */
105 g_return_val_if_fail (value != NULL, NULL);
106 /* g_return_val_if_fail (G_VALUE_TYPE (value) == 0, NULL); be more elaborate below */
108 if (G_TYPE_IS_VALUE (g_type) && G_VALUE_TYPE (value) == 0)
110 GTypeValueTable *value_table = g_type_value_table_peek (g_type);
113 value_meminit (value, g_type);
114 value_table->value_init (value);
116 else if (G_VALUE_TYPE (value))
117 g_warning ("%s: cannot initialize GValue with type `%s', the value has already been initialized as `%s'",
119 g_type_name (g_type),
120 g_type_name (G_VALUE_TYPE (value)));
121 else /* !G_TYPE_IS_VALUE (g_type) */
122 g_warning ("%s: cannot initialize GValue with type `%s', %s",
124 g_type_name (g_type),
125 g_type_value_table_peek (g_type) ?
126 "this type is abstract with regards to GValue use, use a more specific (derived) type" :
127 "this type has no GTypeValueTable implementation");
133 * @src_value: An initialized #GValue structure.
134 * @dest_value: An initialized #GValue structure of the same type as @src_value.
136 * Copies the value of @src_value into @dest_value.
139 g_value_copy (const GValue *src_value,
142 g_return_if_fail (G_IS_VALUE (src_value));
143 g_return_if_fail (G_IS_VALUE (dest_value));
144 g_return_if_fail (g_value_type_compatible (G_VALUE_TYPE (src_value), G_VALUE_TYPE (dest_value)));
146 if (src_value != dest_value)
148 GType dest_type = G_VALUE_TYPE (dest_value);
149 GTypeValueTable *value_table = g_type_value_table_peek (dest_type);
151 /* make sure dest_value's value is free()d */
152 if (value_table->value_free)
153 value_table->value_free (dest_value);
156 value_meminit (dest_value, dest_type);
157 value_table->value_copy (src_value, dest_value);
163 * @value: An initialized #GValue structure.
165 * Clears the current value in @value and resets it to the default value
166 * (as if the value had just been initialized).
168 * Returns: the #GValue structure that has been passed in
171 g_value_reset (GValue *value)
173 GTypeValueTable *value_table;
176 g_return_val_if_fail (G_IS_VALUE (value), NULL);
178 g_type = G_VALUE_TYPE (value);
179 value_table = g_type_value_table_peek (g_type);
181 /* make sure value's value is free()d */
182 if (value_table->value_free)
183 value_table->value_free (value);
186 value_meminit (value, g_type);
187 value_table->value_init (value);
194 * @value: An initialized #GValue structure.
196 * Clears the current value in @value and "unsets" the type,
197 * this releases all resources associated with this GValue.
198 * An unset value is the same as an uninitialized (zero-filled)
202 g_value_unset (GValue *value)
204 GTypeValueTable *value_table;
206 g_return_if_fail (G_IS_VALUE (value));
208 value_table = g_type_value_table_peek (G_VALUE_TYPE (value));
210 if (value_table->value_free)
211 value_table->value_free (value);
212 memset (value, 0, sizeof (*value));
216 * g_value_fits_pointer:
217 * @value: An initialized #GValue structure.
219 * Determines if @value will fit inside the size of a pointer value.
220 * This is an internal function introduced mainly for C marshallers.
222 * Returns: %TRUE if @value will fit inside a pointer value.
225 g_value_fits_pointer (const GValue *value)
227 GTypeValueTable *value_table;
229 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
231 value_table = g_type_value_table_peek (G_VALUE_TYPE (value));
233 return value_table->value_peek_pointer != NULL;
237 * g_value_peek_pointer:
238 * @value: An initialized #GValue structure.
240 * Return the value contents as pointer. This function asserts that
241 * g_value_fits_pointer() returned %TRUE for the passed in value.
242 * This is an internal function introduced mainly for C marshallers.
244 * Returns: %TRUE if @value will fit inside a pointer value.
247 g_value_peek_pointer (const GValue *value)
249 GTypeValueTable *value_table;
251 g_return_val_if_fail (G_IS_VALUE (value), NULL);
253 value_table = g_type_value_table_peek (G_VALUE_TYPE (value));
254 if (!value_table->value_peek_pointer)
256 g_return_val_if_fail (g_value_fits_pointer (value) == TRUE, NULL);
260 return value_table->value_peek_pointer (value);
264 * g_value_set_instance:
265 * @value: An initialized #GValue structure.
266 * @instance: the instance
268 * Sets @value from an instantiatable type via the
269 * value_table's collect_value() function.
272 g_value_set_instance (GValue *value,
276 GTypeValueTable *value_table;
280 g_return_if_fail (G_IS_VALUE (value));
283 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
284 g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (instance), G_VALUE_TYPE (value)));
287 g_type = G_VALUE_TYPE (value);
288 value_table = g_type_value_table_peek (g_type);
290 g_return_if_fail (strcmp (value_table->collect_format, "p") == 0);
292 memset (&cvalue, 0, sizeof (cvalue));
293 cvalue.v_pointer = instance;
295 /* make sure value's value is free()d */
296 if (value_table->value_free)
297 value_table->value_free (value);
299 /* setup and collect */
300 value_meminit (value, g_type);
301 error_msg = value_table->collect_value (value, 1, &cvalue, 0);
304 g_warning ("%s: %s", G_STRLOC, error_msg);
307 /* we purposely leak the value here, it might not be
308 * in a sane state if an error condition occoured
310 value_meminit (value, g_type);
311 value_table->value_init (value);
315 static GValueTransform
316 transform_func_lookup (GType src_type,
319 TransformEntry entry;
321 entry.src_type = src_type;
324 entry.dest_type = dest_type;
329 e = g_bsearch_array_lookup (transform_array, &transform_bconfig, &entry);
332 /* need to check that there hasn't been a change in value handling */
333 if (g_type_value_table_peek (entry.dest_type) == g_type_value_table_peek (dest_type) &&
334 g_type_value_table_peek (entry.src_type) == g_type_value_table_peek (src_type))
337 entry.dest_type = g_type_parent (entry.dest_type);
339 while (entry.dest_type);
341 entry.src_type = g_type_parent (entry.src_type);
343 while (entry.src_type);
349 transform_entries_cmp (gconstpointer bsearch_node1,
350 gconstpointer bsearch_node2)
352 const TransformEntry *e1 = bsearch_node1;
353 const TransformEntry *e2 = bsearch_node2;
354 gint cmp = G_BSEARCH_ARRAY_CMP (e1->src_type, e2->src_type);
359 return G_BSEARCH_ARRAY_CMP (e1->dest_type, e2->dest_type);
363 * g_value_register_transform_func:
364 * @src_type: Source type.
365 * @dest_type: Target type.
366 * @transform_func: a function which transforms values of type @src_type
367 * into value of type @dest_type
369 * Registers a value transformation function for use in g_value_transform().
370 * A previously registered transformation function for @src_type and @dest_type
374 g_value_register_transform_func (GType src_type,
376 GValueTransform transform_func)
378 TransformEntry entry;
380 /* these checks won't pass for dynamic types.
381 * g_return_if_fail (G_TYPE_HAS_VALUE_TABLE (src_type));
382 * g_return_if_fail (G_TYPE_HAS_VALUE_TABLE (dest_type));
384 g_return_if_fail (transform_func != NULL);
386 entry.src_type = src_type;
387 entry.dest_type = dest_type;
389 #if 0 /* let transform function replacement be a valid operation */
390 if (g_bsearch_array_lookup (transform_array, &transform_bconfig, &entry))
391 g_warning ("reregistering value transformation function (%p) for `%s' to `%s'",
393 g_type_name (src_type),
394 g_type_name (dest_type));
397 entry.func = transform_func;
398 transform_array = g_bsearch_array_replace (transform_array, &transform_bconfig, &entry);
402 * g_value_type_transformable:
403 * @src_type: Source type.
404 * @dest_type: Target type.
406 * Check whether g_value_transform() is able to transform values
407 * of type @src_type into values of type @dest_type.
409 * Returns: %TRUE if the transformation is possible, %FALSE otherwise.
412 g_value_type_transformable (GType src_type,
415 g_return_val_if_fail (G_TYPE_IS_VALUE (src_type), FALSE);
416 g_return_val_if_fail (G_TYPE_IS_VALUE (dest_type), FALSE);
418 return (g_value_type_compatible (src_type, dest_type) ||
419 transform_func_lookup (src_type, dest_type) != NULL);
423 * g_value_type_compatible:
424 * @src_type: source type to be copied.
425 * @dest_type: destination type for copying.
427 * Returns whether a #GValue of type @src_type can be copied into
428 * a #GValue of type @dest_type.
430 * Returns: %TRUE if g_value_copy() is possible with @src_type and @dest_type.
433 g_value_type_compatible (GType src_type,
436 g_return_val_if_fail (G_TYPE_IS_VALUE (src_type), FALSE);
437 g_return_val_if_fail (G_TYPE_IS_VALUE (dest_type), FALSE);
439 return (g_type_is_a (src_type, dest_type) &&
440 g_type_value_table_peek (dest_type) == g_type_value_table_peek (src_type));
445 * @src_value: Source value.
446 * @dest_value: Target value.
448 * Tries to cast the contents of @src_value into a type appropriate
449 * to store in @dest_value, e.g. to transform a %G_TYPE_INT value
450 * into a %G_TYPE_FLOAT value. Performing transformations between
451 * value types might incur precision lossage. Especially
452 * transformations into strings might reveal seemingly arbitrary
453 * results and shouldn't be relied upon for production code (such
454 * as rcfile value or object property serialization).
456 * Returns: Whether a transformation rule was found and could be applied.
457 * Upon failing transformations, @dest_value is left untouched.
460 g_value_transform (const GValue *src_value,
465 g_return_val_if_fail (G_IS_VALUE (src_value), FALSE);
466 g_return_val_if_fail (G_IS_VALUE (dest_value), FALSE);
468 dest_type = G_VALUE_TYPE (dest_value);
469 if (g_value_type_compatible (G_VALUE_TYPE (src_value), dest_type))
471 g_value_copy (src_value, dest_value);
477 GValueTransform transform = transform_func_lookup (G_VALUE_TYPE (src_value), dest_type);
481 g_value_unset (dest_value);
483 /* setup and transform */
484 value_meminit (dest_value, dest_type);
485 transform (src_value, dest_value);
493 #define __G_VALUE_C__
494 #include "gobjectaliasdef.c"