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.
28 * The #GValue structure is basically a variable container that consists
29 * of a type identifier and a specific value of that type.
30 * The type identifier within a #GValue structure always determines the
31 * type of the associated value.
32 * To create a undefined #GValue structure, simply create a zero-filled
33 * #GValue structure. To initialize the #GValue, use the g_value_init()
34 * function. A #GValue cannot be used until it is initialized.
35 * The basic type operations (such as freeing and copying) are determined
36 * by the #GTypeValueTable associated with the type ID stored in the #GValue.
37 * Other #GValue operations (such as converting values between types) are
38 * provided by this interface.
48 #include "gvaluecollector.h"
49 #include "gbsearcharray.h"
50 #include "gobjectalias.h"
53 /* --- typedefs & structures --- */
61 /* --- prototypes --- */
62 static gint transform_entries_cmp (gconstpointer bsearch_node1,
63 gconstpointer bsearch_node2);
66 /* --- variables --- */
67 static GBSearchArray *transform_array = NULL;
68 static GBSearchConfig transform_bconfig = {
69 sizeof (TransformEntry),
70 transform_entries_cmp,
75 /* --- functions --- */
79 transform_array = g_bsearch_array_create (&transform_bconfig);
82 static inline void /* keep this function in sync with gvaluecollector.h and gboxed.c */
83 value_meminit (GValue *value,
86 value->g_type = value_type;
87 memset (value->data, 0, sizeof (value->data));
92 * @value: A zero-filled (uninitialized) #GValue structure.
93 * @g_type: Type the #GValue should hold values of.
95 * Initializes @value with the default value of @type.
97 * Returns: the #GValue structure that has been passed in
100 g_value_init (GValue *value,
103 /* g_return_val_if_fail (G_TYPE_IS_VALUE (g_type), NULL); be more elaborate below */
104 g_return_val_if_fail (value != NULL, NULL);
105 /* g_return_val_if_fail (G_VALUE_TYPE (value) == 0, NULL); be more elaborate below */
107 if (G_TYPE_IS_VALUE (g_type) && G_VALUE_TYPE (value) == 0)
109 GTypeValueTable *value_table = g_type_value_table_peek (g_type);
112 value_meminit (value, g_type);
113 value_table->value_init (value);
115 else if (G_VALUE_TYPE (value))
116 g_warning ("%s: cannot initialize GValue with type `%s', the value has already been initialized as `%s'",
118 g_type_name (g_type),
119 g_type_name (G_VALUE_TYPE (value)));
120 else /* !G_TYPE_IS_VALUE (g_type) */
121 g_warning ("%s: cannot initialize GValue with type `%s', %s",
123 g_type_name (g_type),
124 g_type_value_table_peek (g_type) ?
125 "this type is abstract with regards to GValue use, use a more specific (derived) type" :
126 "this type has no GTypeValueTable implementation");
132 * @src_value: An initialized #GValue structure.
133 * @dest_value: An initialized #GValue structure of the same type as @src_value.
135 * Copies the value of @src_value into @dest_value.
138 g_value_copy (const GValue *src_value,
141 g_return_if_fail (G_IS_VALUE (src_value));
142 g_return_if_fail (G_IS_VALUE (dest_value));
143 g_return_if_fail (g_value_type_compatible (G_VALUE_TYPE (src_value), G_VALUE_TYPE (dest_value)));
145 if (src_value != dest_value)
147 GType dest_type = G_VALUE_TYPE (dest_value);
148 GTypeValueTable *value_table = g_type_value_table_peek (dest_type);
150 /* make sure dest_value's value is free()d */
151 if (value_table->value_free)
152 value_table->value_free (dest_value);
155 value_meminit (dest_value, dest_type);
156 value_table->value_copy (src_value, dest_value);
162 * @value: An initialized #GValue structure.
164 * Clears the current value in @value and resets it to the default value
165 * (as if the value had just been initialized).
167 * Returns: the #GValue structure that has been passed in
170 g_value_reset (GValue *value)
172 GTypeValueTable *value_table;
175 g_return_val_if_fail (G_IS_VALUE (value), NULL);
177 g_type = G_VALUE_TYPE (value);
178 value_table = g_type_value_table_peek (g_type);
180 /* make sure value's value is free()d */
181 if (value_table->value_free)
182 value_table->value_free (value);
185 value_meminit (value, g_type);
186 value_table->value_init (value);
193 * @value: An initialized #GValue structure.
195 * Clears the current value in @value and "unsets" the type,
196 * this releases all resources associated with this GValue.
197 * An unset value is the same as an uninitialized (zero-filled)
201 g_value_unset (GValue *value)
203 GTypeValueTable *value_table;
205 g_return_if_fail (G_IS_VALUE (value));
207 value_table = g_type_value_table_peek (G_VALUE_TYPE (value));
209 if (value_table->value_free)
210 value_table->value_free (value);
211 memset (value, 0, sizeof (*value));
215 * g_value_fits_pointer:
216 * @value: An initialized #GValue structure.
218 * Determines if @value will fit inside the size of a pointer value.
219 * This is an internal function introduced mainly for C marshallers.
221 * Returns: %TRUE if @value will fit inside a pointer value.
224 g_value_fits_pointer (const GValue *value)
226 GTypeValueTable *value_table;
228 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
230 value_table = g_type_value_table_peek (G_VALUE_TYPE (value));
232 return value_table->value_peek_pointer != NULL;
236 * g_value_peek_pointer:
237 * @value: An initialized #GValue structure.
239 * Return the value contents as pointer. This function asserts that
240 * g_value_fits_pointer() returned %TRUE for the passed in value.
241 * This is an internal function introduced mainly for C marshallers.
243 * Returns: %TRUE if @value will fit inside a pointer value.
246 g_value_peek_pointer (const GValue *value)
248 GTypeValueTable *value_table;
250 g_return_val_if_fail (G_IS_VALUE (value), NULL);
252 value_table = g_type_value_table_peek (G_VALUE_TYPE (value));
253 if (!value_table->value_peek_pointer)
255 g_return_val_if_fail (g_value_fits_pointer (value) == TRUE, NULL);
259 return value_table->value_peek_pointer (value);
263 * g_value_set_instance:
264 * @value: An initialized #GValue structure.
265 * @instance: the instance
267 * Sets @value from an instantiatable type via the
268 * value_table's collect_value() function.
271 g_value_set_instance (GValue *value,
275 GTypeValueTable *value_table;
279 g_return_if_fail (G_IS_VALUE (value));
282 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
283 g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (instance), G_VALUE_TYPE (value)));
286 g_type = G_VALUE_TYPE (value);
287 value_table = g_type_value_table_peek (g_type);
289 g_return_if_fail (strcmp (value_table->collect_format, "p") == 0);
291 memset (&cvalue, 0, sizeof (cvalue));
292 cvalue.v_pointer = instance;
294 /* make sure value's value is free()d */
295 if (value_table->value_free)
296 value_table->value_free (value);
298 /* setup and collect */
299 value_meminit (value, g_type);
300 error_msg = value_table->collect_value (value, 1, &cvalue, 0);
303 g_warning ("%s: %s", G_STRLOC, error_msg);
306 /* we purposely leak the value here, it might not be
307 * in a sane state if an error condition occoured
309 value_meminit (value, g_type);
310 value_table->value_init (value);
314 static GValueTransform
315 transform_func_lookup (GType src_type,
318 TransformEntry entry;
320 entry.src_type = src_type;
323 entry.dest_type = dest_type;
328 e = g_bsearch_array_lookup (transform_array, &transform_bconfig, &entry);
331 /* need to check that there hasn't been a change in value handling */
332 if (g_type_value_table_peek (entry.dest_type) == g_type_value_table_peek (dest_type) &&
333 g_type_value_table_peek (entry.src_type) == g_type_value_table_peek (src_type))
336 entry.dest_type = g_type_parent (entry.dest_type);
338 while (entry.dest_type);
340 entry.src_type = g_type_parent (entry.src_type);
342 while (entry.src_type);
348 transform_entries_cmp (gconstpointer bsearch_node1,
349 gconstpointer bsearch_node2)
351 const TransformEntry *e1 = bsearch_node1;
352 const TransformEntry *e2 = bsearch_node2;
353 gint cmp = G_BSEARCH_ARRAY_CMP (e1->src_type, e2->src_type);
358 return G_BSEARCH_ARRAY_CMP (e1->dest_type, e2->dest_type);
362 * g_value_register_transform_func:
363 * @src_type: Source type.
364 * @dest_type: Target type.
365 * @transform_func: a function which transforms values of type @src_type
366 * into value of type @dest_type
368 * Registers a value transformation function for use in g_value_transform().
369 * A previously registered transformation function for @src_type and @dest_type
373 g_value_register_transform_func (GType src_type,
375 GValueTransform transform_func)
377 TransformEntry entry;
379 /* these checks won't pass for dynamic types.
380 * g_return_if_fail (G_TYPE_HAS_VALUE_TABLE (src_type));
381 * g_return_if_fail (G_TYPE_HAS_VALUE_TABLE (dest_type));
383 g_return_if_fail (transform_func != NULL);
385 entry.src_type = src_type;
386 entry.dest_type = dest_type;
388 #if 0 /* let transform function replacement be a valid operation */
389 if (g_bsearch_array_lookup (transform_array, &transform_bconfig, &entry))
390 g_warning ("reregistering value transformation function (%p) for `%s' to `%s'",
392 g_type_name (src_type),
393 g_type_name (dest_type));
396 entry.func = transform_func;
397 transform_array = g_bsearch_array_replace (transform_array, &transform_bconfig, &entry);
401 * g_value_type_transformable:
402 * @src_type: Source type.
403 * @dest_type: Target type.
405 * Check whether g_value_transform() is able to transform values
406 * of type @src_type into values of type @dest_type.
408 * Returns: %TRUE if the transformation is possible, %FALSE otherwise.
411 g_value_type_transformable (GType src_type,
414 g_return_val_if_fail (G_TYPE_IS_VALUE (src_type), FALSE);
415 g_return_val_if_fail (G_TYPE_IS_VALUE (dest_type), FALSE);
417 return (g_value_type_compatible (src_type, dest_type) ||
418 transform_func_lookup (src_type, dest_type) != NULL);
422 * g_value_type_compatible:
423 * @src_type: source type to be copied.
424 * @dest_type: destination type for copying.
426 * Returns whether a #GValue of type @src_type can be copied into
427 * a #GValue of type @dest_type.
429 * Returns: %TRUE if g_value_copy() is possible with @src_type and @dest_type.
432 g_value_type_compatible (GType src_type,
435 g_return_val_if_fail (G_TYPE_IS_VALUE (src_type), FALSE);
436 g_return_val_if_fail (G_TYPE_IS_VALUE (dest_type), FALSE);
438 return (g_type_is_a (src_type, dest_type) &&
439 g_type_value_table_peek (dest_type) == g_type_value_table_peek (src_type));
444 * @src_value: Source value.
445 * @dest_value: Target value.
447 * Tries to cast the contents of @src_value into a type appropriate
448 * to store in @dest_value, e.g. to transform a %G_TYPE_INT value
449 * into a %G_TYPE_FLOAT value. Performing transformations between
450 * value types might incur precision lossage. Especially
451 * transformations into strings might reveal seemingly arbitrary
452 * results and shouldn't be relied upon for production code (such
453 * as rcfile value or object property serialization).
455 * Returns: Whether a transformation rule was found and could be applied.
456 * Upon failing transformations, @dest_value is left untouched.
459 g_value_transform (const GValue *src_value,
464 g_return_val_if_fail (G_IS_VALUE (src_value), FALSE);
465 g_return_val_if_fail (G_IS_VALUE (dest_value), FALSE);
467 dest_type = G_VALUE_TYPE (dest_value);
468 if (g_value_type_compatible (G_VALUE_TYPE (src_value), dest_type))
470 g_value_copy (src_value, dest_value);
476 GValueTransform transform = transform_func_lookup (G_VALUE_TYPE (src_value), dest_type);
480 g_value_unset (dest_value);
482 /* setup and transform */
483 value_meminit (dest_value, dest_type);
484 transform (src_value, dest_value);
492 #define __G_VALUE_C__
493 #include "gobjectaliasdef.c"