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"
31 /* --- typedefs & structures --- */
39 /* --- prototypes --- */
40 static gint transform_entries_cmp (gconstpointer bsearch_node1,
41 gconstpointer bsearch_node2);
44 /* --- variables --- */
45 static GBSearchArray *transform_array = NULL;
46 static GBSearchConfig transform_bconfig = G_STATIC_BCONFIG (sizeof (TransformEntry),
47 transform_entries_cmp,
51 /* --- functions --- */
53 g_value_c_init (void) /* sync with gtype.c */
55 transform_array = g_bsearch_array_new (&transform_bconfig);
58 static inline void /* keep this function in sync with gvaluecollector.h and gboxed.c */
59 value_meminit (GValue *value,
62 value->g_type = value_type;
63 memset (value->data, 0, sizeof (value->data));
67 g_value_init (GValue *value,
70 /* g_return_val_if_fail (G_TYPE_IS_VALUE (g_type), NULL); be more elaborate below */
71 g_return_val_if_fail (value != NULL, NULL);
72 /* g_return_val_if_fail (G_VALUE_TYPE (value) == 0, NULL); be more elaborate below */
74 if (G_TYPE_IS_VALUE (g_type) && G_VALUE_TYPE (value) == 0)
76 GTypeValueTable *value_table = g_type_value_table_peek (g_type);
79 value_meminit (value, g_type);
80 value_table->value_init (value);
82 else if (G_VALUE_TYPE (value))
83 g_warning ("%s: cannot initialize GValue with type `%s', the value has already been initialized as `%s'",
86 g_type_name (G_VALUE_TYPE (value)));
87 else /* !G_TYPE_IS_VALUE (g_type) */
88 g_warning ("%s: cannot initialize GValue with type `%s', %s",
91 g_type_value_table_peek (g_type) ?
92 "this type is abstract with regards to GValue use, use a more specific (derived) type" :
93 "this type has no GTypeValueTable implementation");
98 g_value_copy (const GValue *src_value,
101 g_return_if_fail (G_IS_VALUE (src_value));
102 g_return_if_fail (G_IS_VALUE (dest_value));
103 g_return_if_fail (g_value_type_compatible (G_VALUE_TYPE (src_value), G_VALUE_TYPE (dest_value)));
105 if (src_value != dest_value)
107 GType dest_type = G_VALUE_TYPE (dest_value);
108 GTypeValueTable *value_table = g_type_value_table_peek (dest_type);
110 /* make sure dest_value's value is free()d */
111 if (value_table->value_free)
112 value_table->value_free (dest_value);
115 value_meminit (dest_value, dest_type);
116 value_table->value_copy (src_value, dest_value);
121 g_value_reset (GValue *value)
123 GTypeValueTable *value_table;
126 g_return_val_if_fail (G_IS_VALUE (value), NULL);
128 g_type = G_VALUE_TYPE (value);
129 value_table = g_type_value_table_peek (g_type);
131 /* make sure value's value is free()d */
132 if (value_table->value_free)
133 value_table->value_free (value);
136 value_meminit (value, g_type);
137 value_table->value_init (value);
143 g_value_unset (GValue *value)
145 GTypeValueTable *value_table;
147 g_return_if_fail (G_IS_VALUE (value));
149 value_table = g_type_value_table_peek (G_VALUE_TYPE (value));
151 if (value_table->value_free)
152 value_table->value_free (value);
153 memset (value, 0, sizeof (*value));
157 g_value_fits_pointer (const GValue *value)
159 GTypeValueTable *value_table;
161 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
163 value_table = g_type_value_table_peek (G_VALUE_TYPE (value));
165 return value_table->value_peek_pointer != NULL;
169 g_value_peek_pointer (const GValue *value)
171 GTypeValueTable *value_table;
173 g_return_val_if_fail (G_IS_VALUE (value), NULL);
175 value_table = g_type_value_table_peek (G_VALUE_TYPE (value));
176 if (!value_table->value_peek_pointer)
177 g_return_val_if_fail (g_value_fits_pointer (value) == TRUE, NULL);
179 return value_table->value_peek_pointer (value);
183 g_value_set_instance (GValue *value,
187 GTypeValueTable *value_table;
191 g_return_if_fail (G_IS_VALUE (value));
194 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
195 g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (instance), G_VALUE_TYPE (value)));
198 g_type = G_VALUE_TYPE (value);
199 value_table = g_type_value_table_peek (g_type);
201 g_return_if_fail (strcmp (value_table->collect_format, "p") == 0);
203 memset (&cvalue, 0, sizeof (cvalue));
204 cvalue.v_pointer = instance;
206 /* make sure value's value is free()d */
207 if (value_table->value_free)
208 value_table->value_free (value);
210 /* setup and collect */
211 value_meminit (value, g_type);
212 error_msg = value_table->collect_value (value, 1, &cvalue, 0);
215 g_warning ("%s: %s", G_STRLOC, error_msg);
218 /* we purposely leak the value here, it might not be
219 * in a sane state if an error condition occoured
221 value_meminit (value, g_type);
222 value_table->value_init (value);
226 static GValueTransform
227 transform_func_lookup (GType src_type,
230 TransformEntry entry;
232 entry.src_type = src_type;
235 entry.dest_type = dest_type;
240 e = g_bsearch_array_lookup (transform_array, &transform_bconfig, &entry);
243 /* need to check that there hasn't been a change in value handling */
244 if (g_type_value_table_peek (entry.dest_type) == g_type_value_table_peek (dest_type) &&
245 g_type_value_table_peek (entry.src_type) == g_type_value_table_peek (src_type))
248 entry.dest_type = g_type_parent (entry.dest_type);
250 while (entry.dest_type);
252 entry.src_type = g_type_parent (entry.src_type);
254 while (entry.src_type);
260 transform_entries_cmp (gconstpointer bsearch_node1,
261 gconstpointer bsearch_node2)
263 const TransformEntry *e1 = bsearch_node1;
264 const TransformEntry *e2 = bsearch_node2;
265 gint cmp = G_BSEARCH_ARRAY_CMP (e1->src_type, e2->src_type);
270 return G_BSEARCH_ARRAY_CMP (e1->dest_type, e2->dest_type);
274 g_value_register_transform_func (GType src_type,
276 GValueTransform transform_func)
278 TransformEntry entry;
280 /* these checks won't pass for dynamic types.
281 * g_return_if_fail (G_TYPE_HAS_VALUE_TABLE (src_type));
282 * g_return_if_fail (G_TYPE_HAS_VALUE_TABLE (dest_type));
284 g_return_if_fail (transform_func != NULL);
286 entry.src_type = src_type;
287 entry.dest_type = dest_type;
289 if (g_bsearch_array_lookup (transform_array, &transform_bconfig, &entry))
290 g_warning ("reregistering value transformation function (%p) for `%s' to `%s'",
292 g_type_name (src_type),
293 g_type_name (dest_type));
295 entry.func = transform_func;
297 transform_array = g_bsearch_array_insert (transform_array, &transform_bconfig, &entry, TRUE);
301 g_value_type_transformable (GType src_type,
304 g_return_val_if_fail (G_TYPE_IS_VALUE (src_type), FALSE);
305 g_return_val_if_fail (G_TYPE_IS_VALUE (dest_type), FALSE);
307 return (g_value_type_compatible (src_type, dest_type) ||
308 transform_func_lookup (src_type, dest_type) != NULL);
312 g_value_type_compatible (GType src_type,
315 g_return_val_if_fail (G_TYPE_IS_VALUE (src_type), FALSE);
316 g_return_val_if_fail (G_TYPE_IS_VALUE (dest_type), FALSE);
318 return (g_type_is_a (src_type, dest_type) &&
319 g_type_value_table_peek (dest_type) == g_type_value_table_peek (src_type));
323 g_value_transform (const GValue *src_value,
328 g_return_val_if_fail (G_IS_VALUE (src_value), FALSE);
329 g_return_val_if_fail (G_IS_VALUE (dest_value), FALSE);
331 dest_type = G_VALUE_TYPE (dest_value);
332 if (g_value_type_compatible (G_VALUE_TYPE (src_value), dest_type))
334 g_value_copy (src_value, dest_value);
340 GValueTransform transform = transform_func_lookup (G_VALUE_TYPE (src_value), dest_type);
344 g_value_unset (dest_value);
346 /* setup and transform */
347 value_meminit (dest_value, dest_type);
348 transform (src_value, dest_value);