1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 1998, 1999, 2000 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.
23 #include "gvaluecollector.h"
30 #define PARAM_SPEC_PARAM_ID(pspec) (GPOINTER_TO_UINT (g_param_spec_get_qdata ((pspec), quark_param_id)))
33 /* --- prototypes --- */
34 static void g_object_base_class_init (GObjectClass *class);
35 static void g_object_base_class_finalize (GObjectClass *class);
36 static void g_object_do_class_init (GObjectClass *class);
37 static void g_object_do_init (GObject *object);
38 static void g_object_do_queue_param_changed (GObject *object,
40 static void g_object_do_dispatch_param_changed (GObject *object,
42 static void g_object_last_unref (GObject *object);
43 static void g_object_do_shutdown (GObject *object);
44 static void g_object_do_finalize (GObject *object);
45 static void g_value_object_init (GValue *value);
46 static void g_value_object_free_value (GValue *value);
47 static void g_value_object_copy_value (const GValue *src_value,
49 static gchar* g_value_object_collect_value (GValue *value,
52 GTypeCValue *collect_value);
53 static gchar* g_value_object_lcopy_value (const GValue *value,
56 GTypeCValue *collect_value);
59 /* --- variables --- */
60 static GQuark quark_param_id = 0;
61 static GQuark quark_param_changed_queue = 0;
62 static GHashTable *param_spec_hash_table = NULL;
65 /* --- functions --- */
68 /* We need an actual method for handling debug keys in GLib.
69 * For now, we'll simply use, as a method
70 * 'extern gboolean glib_debug_objects'
72 gboolean glib_debug_objects = FALSE;
74 static guint debug_objects_count = 0;
75 static GHashTable *debug_objects_ht = NULL;
77 debug_objects_foreach (gpointer key,
81 GObject *object = value;
83 g_message ("[%p] stale %s\tref_count=%u",
85 G_OBJECT_TYPE_NAME (object),
89 debug_objects_atexit (void)
91 if (glib_debug_objects)
95 g_message ("stale GObjects: %u", debug_objects_count);
96 g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL);
103 g_object_type_init (void) /* sync with gtype.c */
105 static gboolean initialized = FALSE;
106 static const GTypeFundamentalInfo finfo = {
107 G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE,
109 static GTypeInfo info = {
110 sizeof (GObjectClass),
111 (GBaseInitFunc) g_object_base_class_init,
112 (GBaseFinalizeFunc) g_object_base_class_finalize,
113 (GClassInitFunc) g_object_do_class_init,
114 NULL /* class_destroy */,
115 NULL /* class_data */,
118 (GInstanceInitFunc) g_object_do_init,
119 NULL, /* value_table */
121 static const GTypeValueTable value_table = {
122 g_value_object_init, /* value_init */
123 g_value_object_free_value, /* value_free */
124 g_value_object_copy_value, /* value_copy */
125 G_VALUE_COLLECT_POINTER, /* collect_type */
126 g_value_object_collect_value, /* collect_value */
127 G_VALUE_COLLECT_POINTER, /* lcopy_type */
128 g_value_object_lcopy_value, /* lcopy_value */
132 g_return_if_fail (initialized == FALSE);
137 info.value_table = &value_table;
138 type = g_type_register_fundamental (G_TYPE_OBJECT, "GObject", &info, &finfo);
139 g_assert (type == G_TYPE_OBJECT);
142 g_atexit (debug_objects_atexit);
147 g_object_base_class_init (GObjectClass *class)
149 /* reset instance specific fields and methods that don't get inherited */
150 class->n_param_specs = 0;
151 class->param_specs = NULL;
152 class->get_param = NULL;
153 class->set_param = NULL;
157 g_object_base_class_finalize (GObjectClass *class)
161 g_message ("finallizing base class of %s", G_OBJECT_CLASS_NAME (class));
163 for (i = 0; i < class->n_param_specs; i++)
165 GParamSpec *pspec = class->param_specs[i];
167 g_param_spec_hash_table_remove (param_spec_hash_table, pspec);
168 g_param_spec_set_qdata (pspec, quark_param_id, NULL);
169 g_param_spec_unref (pspec);
171 class->n_param_specs = 0;
172 g_free (class->param_specs);
173 class->param_specs = NULL;
177 g_object_do_class_init (GObjectClass *class)
179 quark_param_id = g_quark_from_static_string ("glib-object-param-id");
180 quark_param_changed_queue = g_quark_from_static_string ("glib-object-param-changed-queue");
181 param_spec_hash_table = g_param_spec_hash_table_new ();
183 class->queue_param_changed = g_object_do_queue_param_changed;
184 class->dispatch_param_changed = g_object_do_dispatch_param_changed;
185 class->shutdown = g_object_do_shutdown;
186 class->finalize = g_object_do_finalize;
190 g_object_class_install_param (GObjectClass *class,
192 GParamSpec *pspec /* 1 ref_count taken over */)
196 g_return_if_fail (G_IS_OBJECT_CLASS (class));
197 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
198 if (pspec->flags & G_PARAM_WRITABLE)
199 g_return_if_fail (class->set_param != NULL);
200 if (pspec->flags & G_PARAM_READABLE)
201 g_return_if_fail (class->get_param != NULL);
202 g_return_if_fail (param_id > 0);
203 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
205 /* expensive paranoia checks ;( */
206 for (i = 0; i < class->n_param_specs; i++)
207 if (PARAM_SPEC_PARAM_ID (class->param_specs[i]) == param_id)
209 g_warning (G_STRLOC ": class `%s' already contains a parameter `%s' with id %u, "
210 "cannot install parameter `%s'",
211 G_OBJECT_CLASS_NAME (class),
212 class->param_specs[i]->name,
217 if (g_object_class_find_param_spec (class, pspec->name))
219 g_warning (G_STRLOC ": class `%s' already contains a parameter named `%s'",
220 G_OBJECT_CLASS_NAME (class),
225 g_param_spec_set_qdata (pspec, quark_param_id, GUINT_TO_POINTER (param_id));
226 g_param_spec_hash_table_insert (param_spec_hash_table, pspec, G_OBJECT_CLASS_TYPE (class));
227 i = class->n_param_specs++;
228 class->param_specs = g_renew (GParamSpec*, class->param_specs, class->n_param_specs);
229 class->param_specs[i] = pspec;
233 g_object_class_find_param_spec (GObjectClass *class,
234 const gchar *param_name)
236 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
237 g_return_val_if_fail (param_name != NULL, NULL);
239 return g_param_spec_hash_table_lookup (param_spec_hash_table,
241 G_OBJECT_CLASS_TYPE (class),
246 g_object_do_init (GObject *object)
248 object->ref_count = 1;
249 object->qdata = NULL;
252 if (glib_debug_objects)
254 if (!debug_objects_ht)
255 debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
256 debug_objects_count++;
257 g_hash_table_insert (debug_objects_ht, object, object);
263 g_object_last_unref (GObject *object)
265 g_return_if_fail (object->ref_count > 0);
267 if (object->ref_count == 1) /* may have been re-referenced meanwhile */
268 G_OBJECT_GET_CLASS (object)->shutdown (object);
270 object->ref_count -= 1;
272 if (object->ref_count == 0) /* may have been re-referenced meanwhile */
273 G_OBJECT_GET_CLASS (object)->finalize (object);
277 g_object_do_shutdown (GObject *object)
279 /* this function needs to be always present for unconditional
280 * chaining, we also might add some code here later.
281 * beware though, subclasses may invoke shutdown() arbitrarily.
286 g_object_do_finalize (GObject *object)
288 g_datalist_clear (&object->qdata);
291 if (glib_debug_objects)
293 g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
295 g_hash_table_remove (debug_objects_ht, object);
296 debug_objects_count--;
300 g_type_free_instance ((GTypeInstance*) object);
304 g_object_new (GType object_type,
305 const gchar *first_param_name,
311 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
313 va_start (var_args, first_param_name);
314 object = g_object_new_valist (object_type, first_param_name, var_args);
321 g_object_new_valist (GType object_type,
322 const gchar *first_param_name,
327 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
329 object = (GObject*) g_type_create_instance (object_type);
330 if (first_param_name)
331 g_object_set_valist (object, first_param_name, var_args);
337 g_object_do_dispatch_param_changed (GObject *object,
340 g_message ("NOTIFICATION: parameter `%s' changed on object `%s'",
342 G_OBJECT_TYPE_NAME (object));
346 notify_param_changed_handler (gpointer data)
352 /* FIXME: need GDK_THREADS lock */
354 object = G_OBJECT (data);
355 class = G_OBJECT_GET_CLASS (object);
356 slist = g_datalist_id_get_data (&object->qdata, quark_param_changed_queue);
358 /* a reference count is still being held */
360 for (; slist; slist = slist->next)
363 GParamSpec *pspec = slist->data;
366 class->dispatch_param_changed (object, pspec);
369 g_datalist_id_set_data (&object->qdata, quark_param_changed_queue, NULL);
375 g_object_do_queue_param_changed (GObject *object,
378 GSList *slist, *last = NULL;
380 /* if this is a recursive call on this object (i.e. pspecs are queued
381 * for notification, while param_changed notification is currently in
382 * progress), we simply add them to the queue that is currently being
383 * dispatched. otherwise, we later dispatch parameter changed notification
384 * asyncronously from an idle handler untill the queue is completely empty.
387 slist = g_datalist_id_get_data (&object->qdata, quark_param_changed_queue);
388 for (; slist; last = slist, slist = last->next)
389 if (slist->data == pspec)
394 g_object_ref (object);
395 g_idle_add_full (G_NOTIFY_PRIORITY,
396 notify_param_changed_handler,
398 (GDestroyNotify) g_object_unref);
399 g_object_set_qdata_full (object,
400 quark_param_changed_queue,
401 g_slist_prepend (NULL, pspec),
402 (GDestroyNotify) g_slist_free);
405 last->next = g_slist_prepend (NULL, pspec);
409 object_get_param (GObject *object,
412 const gchar *trailer)
416 g_return_if_fail (g_type_is_a (G_OBJECT_TYPE (object), pspec->owner_type)); /* paranoid */
418 class = g_type_class_peek (pspec->owner_type);
420 class->get_param (object, PARAM_SPEC_PARAM_ID (pspec), value, pspec, trailer);
424 object_set_param (GObject *object,
427 const gchar *trailer)
431 g_return_if_fail (g_type_is_a (G_OBJECT_TYPE (object), pspec->owner_type)); /* paranoid */
433 class = g_type_class_peek (pspec->owner_type);
435 class->set_param (object, PARAM_SPEC_PARAM_ID (pspec), value, pspec, trailer);
437 class->queue_param_changed (object, pspec);
441 g_object_set_valist (GObject *object,
442 const gchar *first_param_name,
447 g_return_if_fail (G_IS_OBJECT (object));
449 g_object_ref (object);
451 name = first_param_name;
455 const gchar *trailer = NULL;
456 GValue value = { 0, };
460 pspec = g_param_spec_hash_table_lookup (param_spec_hash_table,
462 G_OBJECT_TYPE (object),
467 g_warning ("%s: object class `%s' has no parameter named `%s'",
469 G_OBJECT_TYPE_NAME (object),
473 if (!(pspec->flags & G_PARAM_WRITABLE))
475 g_warning ("%s: parameter `%s' of object class `%s' is not writable",
478 G_OBJECT_TYPE_NAME (object));
482 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
484 G_VALUE_COLLECT (&value, var_args, &error);
487 g_warning ("%s: %s", G_STRLOC, error);
490 /* we purposely leak the value here, it might not be
491 * in a sane state if an error condition occoured
496 object_set_param (object, &value, pspec, trailer);
498 g_value_unset (&value);
500 name = va_arg (var_args, gchar*);
503 g_object_unref (object);
507 g_object_get_valist (GObject *object,
508 const gchar *first_param_name,
513 g_return_if_fail (G_IS_OBJECT (object));
515 g_object_ref (object);
517 name = first_param_name;
521 const gchar *trailer = NULL;
522 GValue value = { 0, };
526 pspec = g_param_spec_hash_table_lookup (param_spec_hash_table,
528 G_OBJECT_TYPE (object),
533 g_warning ("%s: object class `%s' has no parameter named `%s'",
535 G_OBJECT_TYPE_NAME (object),
539 if (!(pspec->flags & G_PARAM_READABLE))
541 g_warning ("%s: parameter `%s' of object class `%s' is not readable",
544 G_OBJECT_TYPE_NAME (object));
548 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
550 object_get_param (object, &value, pspec, trailer);
552 G_VALUE_LCOPY (&value, var_args, &error);
555 g_warning ("%s: %s", G_STRLOC, error);
558 /* we purposely leak the value here, it might not be
559 * in a sane state if an error condition occoured
564 g_value_unset (&value);
566 name = va_arg (var_args, gchar*);
569 g_object_unref (object);
573 g_object_set (GObject *object,
574 const gchar *first_param_name,
579 g_return_if_fail (G_IS_OBJECT (object));
581 va_start (var_args, first_param_name);
582 g_object_set_valist (object, first_param_name, var_args);
587 g_object_get (GObject *object,
588 const gchar *first_param_name,
593 g_return_if_fail (G_IS_OBJECT (object));
595 va_start (var_args, first_param_name);
596 g_object_get_valist (object, first_param_name, var_args);
601 g_object_set_param (GObject *object,
602 const gchar *param_name,
606 const gchar *trailer;
608 g_return_if_fail (G_IS_OBJECT (object));
609 g_return_if_fail (param_name != NULL);
610 g_return_if_fail (G_IS_VALUE (value));
612 g_object_ref (object);
614 pspec = g_param_spec_hash_table_lookup (param_spec_hash_table,
616 G_OBJECT_TYPE (object),
620 g_warning ("%s: object class `%s' has no parameter named `%s'",
622 G_OBJECT_TYPE_NAME (object),
626 GValue tmp_value = { 0, };
628 /* provide a copy to work from and convert if necessary */
629 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
631 if (!g_value_convert (value, &tmp_value) ||
632 g_param_value_validate (pspec, &tmp_value))
633 g_warning ("%s: cannot convert `%s' value to parameter `%s' value of type `%s'",
635 G_VALUE_TYPE_NAME (value),
637 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
639 object_set_param (object, &tmp_value, pspec, trailer);
641 g_value_unset (&tmp_value);
644 g_object_unref (object);
648 g_object_get_param (GObject *object,
649 const gchar *param_name,
653 const gchar *trailer;
655 g_return_if_fail (G_IS_OBJECT (object));
656 g_return_if_fail (param_name != NULL);
657 g_return_if_fail (G_IS_VALUE (value));
659 g_object_ref (object);
661 pspec = g_param_spec_hash_table_lookup (param_spec_hash_table,
663 G_OBJECT_TYPE (object),
667 g_warning ("%s: object class `%s' has no parameter named `%s'",
669 G_OBJECT_TYPE_NAME (object),
673 GValue tmp_value = { 0, };
675 /* provide a copy to work from and later convert if necessary, so
676 * _get_param() implementations need *not* care about freeing values
677 * that might be already set in the parameter to get.
678 * (though, at this point, GValue should exclusively be modified
679 * through the accessor functions anyways)
681 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
683 if (!g_value_types_exchangable (G_VALUE_TYPE (value), G_PARAM_SPEC_VALUE_TYPE (pspec)))
684 g_warning ("%s: can't retrive parameter `%s' value of type `%s' as value of type `%s'",
687 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
688 G_VALUE_TYPE_NAME (value));
691 object_get_param (object, &tmp_value, pspec, trailer);
692 g_value_convert (&tmp_value, value);
693 /* g_value_validate (value, pspec); */
696 g_value_unset (&tmp_value);
699 g_object_unref (object);
703 g_object_queue_param_changed (GObject *object,
704 const gchar *param_name)
708 g_return_if_fail (G_IS_OBJECT (object));
709 g_return_if_fail (param_name != NULL);
711 pspec = g_param_spec_hash_table_lookup (param_spec_hash_table,
713 G_OBJECT_TYPE (object),
716 g_warning ("%s: object class `%s' has no parameter named `%s'",
718 G_OBJECT_TYPE_NAME (object),
721 G_OBJECT_GET_CLASS (object)->queue_param_changed (object, pspec);
725 g_object_ref (GObject *object)
727 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
728 g_return_val_if_fail (object->ref_count > 0, NULL);
730 object->ref_count += 1;
736 g_object_unref (GObject *object)
738 g_return_if_fail (G_IS_OBJECT (object));
739 g_return_if_fail (object->ref_count > 0);
741 if (object->ref_count > 1)
742 object->ref_count -= 1;
744 g_object_last_unref (object);
748 g_object_get_qdata (GObject *object,
751 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
753 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
757 g_object_set_qdata (GObject *object,
761 g_return_if_fail (G_IS_OBJECT (object));
762 g_return_if_fail (quark > 0);
764 g_datalist_id_set_data (&object->qdata, quark, data);
768 g_object_set_qdata_full (GObject *object,
771 GDestroyNotify destroy)
773 g_return_if_fail (G_IS_OBJECT (object));
774 g_return_if_fail (quark > 0);
776 g_datalist_id_set_data_full (&object->qdata, quark, data, data ? destroy : NULL);
780 g_object_steal_qdata (GObject *object,
783 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
784 g_return_val_if_fail (quark > 0, NULL);
786 return g_datalist_id_remove_no_notify (&object->qdata, quark);
790 g_value_object_init (GValue *value)
792 value->data[0].v_pointer = NULL;
796 g_value_object_free_value (GValue *value)
798 if (value->data[0].v_pointer)
799 g_object_unref (value->data[0].v_pointer);
803 g_value_object_copy_value (const GValue *src_value,
806 if (src_value->data[0].v_pointer)
807 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
809 dest_value->data[0].v_pointer = NULL;
813 g_value_object_collect_value (GValue *value,
816 GTypeCValue *collect_value)
818 if (collect_value->v_pointer)
820 GObject *object = collect_value->v_pointer;
822 if (object->g_type_instance.g_class == NULL)
823 return g_strconcat ("invalid unclassed object pointer for value type `",
824 G_VALUE_TYPE_NAME (value),
827 else if (!g_type_is_a (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
828 return g_strconcat ("invalid object type `",
829 G_OBJECT_TYPE_NAME (object),
830 "' for value type `",
831 G_VALUE_TYPE_NAME (value),
834 value->data[0].v_pointer = g_object_ref (object);
837 value->data[0].v_pointer = NULL;
844 g_value_object_lcopy_value (const GValue *value,
847 GTypeCValue *collect_value)
849 GObject **object_p = collect_value->v_pointer;
852 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
854 *object_p = value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
861 g_value_set_object (GValue *value,
864 g_return_if_fail (G_IS_VALUE_OBJECT (value));
866 g_return_if_fail (G_IS_OBJECT (v_object));
868 if (value->data[0].v_pointer)
869 g_object_unref (value->data[0].v_pointer);
870 value->data[0].v_pointer = v_object;
871 if (value->data[0].v_pointer)
872 g_object_ref (value->data[0].v_pointer);
876 g_value_get_object (GValue *value)
878 g_return_val_if_fail (G_IS_VALUE_OBJECT (value), NULL);
880 return value->data[0].v_pointer;
884 g_value_dup_object (GValue *value)
886 g_return_val_if_fail (G_IS_VALUE_OBJECT (value), NULL);
888 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;