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.
27 #include "gvaluecollector.h"
28 #include "gbsearcharray.h"
29 #include "gobjectalias.h"
32 /* --- typedefs & structures --- */
40 /* --- prototypes --- */
41 static gint transform_entries_cmp (gconstpointer bsearch_node1,
42 gconstpointer bsearch_node2);
45 /* --- variables --- */
46 static GBSearchArray *transform_array = NULL;
47 static GBSearchConfig transform_bconfig = {
48 sizeof (TransformEntry),
49 transform_entries_cmp,
54 /* --- functions --- */
58 transform_array = g_bsearch_array_create (&transform_bconfig);
61 static inline void /* keep this function in sync with gvaluecollector.h and gboxed.c */
62 value_meminit (GValue *value,
65 value->g_type = value_type;
66 memset (value->data, 0, sizeof (value->data));
70 g_value_init (GValue *value,
73 /* g_return_val_if_fail (G_TYPE_IS_VALUE (g_type), NULL); be more elaborate below */
74 g_return_val_if_fail (value != NULL, NULL);
75 /* g_return_val_if_fail (G_VALUE_TYPE (value) == 0, NULL); be more elaborate below */
77 if (G_TYPE_IS_VALUE (g_type) && G_VALUE_TYPE (value) == 0)
79 GTypeValueTable *value_table = g_type_value_table_peek (g_type);
82 value_meminit (value, g_type);
83 value_table->value_init (value);
85 else if (G_VALUE_TYPE (value))
86 g_warning ("%s: cannot initialize GValue with type `%s', the value has already been initialized as `%s'",
89 g_type_name (G_VALUE_TYPE (value)));
90 else /* !G_TYPE_IS_VALUE (g_type) */
91 g_warning ("%s: cannot initialize GValue with type `%s', %s",
94 g_type_value_table_peek (g_type) ?
95 "this type is abstract with regards to GValue use, use a more specific (derived) type" :
96 "this type has no GTypeValueTable implementation");
101 g_value_copy (const GValue *src_value,
104 g_return_if_fail (G_IS_VALUE (src_value));
105 g_return_if_fail (G_IS_VALUE (dest_value));
106 g_return_if_fail (g_value_type_compatible (G_VALUE_TYPE (src_value), G_VALUE_TYPE (dest_value)));
108 if (src_value != dest_value)
110 GType dest_type = G_VALUE_TYPE (dest_value);
111 GTypeValueTable *value_table = g_type_value_table_peek (dest_type);
113 /* make sure dest_value's value is free()d */
114 if (value_table->value_free)
115 value_table->value_free (dest_value);
118 value_meminit (dest_value, dest_type);
119 value_table->value_copy (src_value, dest_value);
124 g_value_reset (GValue *value)
126 GTypeValueTable *value_table;
129 g_return_val_if_fail (G_IS_VALUE (value), NULL);
131 g_type = G_VALUE_TYPE (value);
132 value_table = g_type_value_table_peek (g_type);
134 /* make sure value's value is free()d */
135 if (value_table->value_free)
136 value_table->value_free (value);
139 value_meminit (value, g_type);
140 value_table->value_init (value);
146 g_value_unset (GValue *value)
148 GTypeValueTable *value_table;
150 g_return_if_fail (G_IS_VALUE (value));
152 value_table = g_type_value_table_peek (G_VALUE_TYPE (value));
154 if (value_table->value_free)
155 value_table->value_free (value);
156 memset (value, 0, sizeof (*value));
160 g_value_fits_pointer (const GValue *value)
162 GTypeValueTable *value_table;
164 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
166 value_table = g_type_value_table_peek (G_VALUE_TYPE (value));
168 return value_table->value_peek_pointer != NULL;
172 g_value_peek_pointer (const GValue *value)
174 GTypeValueTable *value_table;
176 g_return_val_if_fail (G_IS_VALUE (value), NULL);
178 value_table = g_type_value_table_peek (G_VALUE_TYPE (value));
179 if (!value_table->value_peek_pointer)
181 g_return_val_if_fail (g_value_fits_pointer (value) == TRUE, NULL);
185 return value_table->value_peek_pointer (value);
189 g_value_set_instance (GValue *value,
193 GTypeValueTable *value_table;
197 g_return_if_fail (G_IS_VALUE (value));
200 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
201 g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (instance), G_VALUE_TYPE (value)));
204 g_type = G_VALUE_TYPE (value);
205 value_table = g_type_value_table_peek (g_type);
207 g_return_if_fail (strcmp (value_table->collect_format, "p") == 0);
209 memset (&cvalue, 0, sizeof (cvalue));
210 cvalue.v_pointer = instance;
212 /* make sure value's value is free()d */
213 if (value_table->value_free)
214 value_table->value_free (value);
216 /* setup and collect */
217 value_meminit (value, g_type);
218 error_msg = value_table->collect_value (value, 1, &cvalue, 0);
221 g_warning ("%s: %s", G_STRLOC, error_msg);
224 /* we purposely leak the value here, it might not be
225 * in a sane state if an error condition occoured
227 value_meminit (value, g_type);
228 value_table->value_init (value);
232 static GValueTransform
233 transform_func_lookup (GType src_type,
236 TransformEntry entry;
238 entry.src_type = src_type;
241 entry.dest_type = dest_type;
246 e = g_bsearch_array_lookup (transform_array, &transform_bconfig, &entry);
249 /* need to check that there hasn't been a change in value handling */
250 if (g_type_value_table_peek (entry.dest_type) == g_type_value_table_peek (dest_type) &&
251 g_type_value_table_peek (entry.src_type) == g_type_value_table_peek (src_type))
254 entry.dest_type = g_type_parent (entry.dest_type);
256 while (entry.dest_type);
258 entry.src_type = g_type_parent (entry.src_type);
260 while (entry.src_type);
266 transform_entries_cmp (gconstpointer bsearch_node1,
267 gconstpointer bsearch_node2)
269 const TransformEntry *e1 = bsearch_node1;
270 const TransformEntry *e2 = bsearch_node2;
271 gint cmp = G_BSEARCH_ARRAY_CMP (e1->src_type, e2->src_type);
276 return G_BSEARCH_ARRAY_CMP (e1->dest_type, e2->dest_type);
280 g_value_register_transform_func (GType src_type,
282 GValueTransform transform_func)
284 TransformEntry entry;
286 /* these checks won't pass for dynamic types.
287 * g_return_if_fail (G_TYPE_HAS_VALUE_TABLE (src_type));
288 * g_return_if_fail (G_TYPE_HAS_VALUE_TABLE (dest_type));
290 g_return_if_fail (transform_func != NULL);
292 entry.src_type = src_type;
293 entry.dest_type = dest_type;
295 #if 0 /* let transform function replacement be a valid operation */
296 if (g_bsearch_array_lookup (transform_array, &transform_bconfig, &entry))
297 g_warning ("reregistering value transformation function (%p) for `%s' to `%s'",
299 g_type_name (src_type),
300 g_type_name (dest_type));
303 entry.func = transform_func;
304 transform_array = g_bsearch_array_replace (transform_array, &transform_bconfig, &entry);
308 g_value_type_transformable (GType src_type,
311 g_return_val_if_fail (G_TYPE_IS_VALUE (src_type), FALSE);
312 g_return_val_if_fail (G_TYPE_IS_VALUE (dest_type), FALSE);
314 return (g_value_type_compatible (src_type, dest_type) ||
315 transform_func_lookup (src_type, dest_type) != NULL);
319 g_value_type_compatible (GType src_type,
322 g_return_val_if_fail (G_TYPE_IS_VALUE (src_type), FALSE);
323 g_return_val_if_fail (G_TYPE_IS_VALUE (dest_type), FALSE);
325 return (g_type_is_a (src_type, dest_type) &&
326 g_type_value_table_peek (dest_type) == g_type_value_table_peek (src_type));
330 g_value_transform (const GValue *src_value,
335 g_return_val_if_fail (G_IS_VALUE (src_value), FALSE);
336 g_return_val_if_fail (G_IS_VALUE (dest_value), FALSE);
338 dest_type = G_VALUE_TYPE (dest_value);
339 if (g_value_type_compatible (G_VALUE_TYPE (src_value), dest_type))
341 g_value_copy (src_value, dest_value);
347 GValueTransform transform = transform_func_lookup (G_VALUE_TYPE (src_value), dest_type);
351 g_value_unset (dest_value);
353 /* setup and transform */
354 value_meminit (dest_value, dest_type);
355 transform (src_value, dest_value);
363 #define __G_VALUE_C__
364 #include "gobjectaliasdef.c"