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);
100 #endif /* DEBUG_OBJECTS */
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);
143 #endif /* DEBUG_OBJECTS */
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);
259 #endif /* DEBUG_OBJECTS */
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 */
274 G_OBJECT_GET_CLASS (object)->finalize (object);
275 g_type_free_instance ((GTypeInstance*) object);
280 g_object_do_shutdown (GObject *object)
282 /* this function needs to be always present for unconditional
283 * chaining, we also might add some code here later.
284 * beware though, subclasses may invoke shutdown() arbitrarily.
289 g_object_do_finalize (GObject *object)
291 g_datalist_clear (&object->qdata);
294 if (glib_debug_objects)
296 g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
298 g_hash_table_remove (debug_objects_ht, object);
299 debug_objects_count--;
301 #endif /* DEBUG_OBJECTS */
305 g_object_new (GType object_type,
306 const gchar *first_param_name,
312 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
314 va_start (var_args, first_param_name);
315 object = g_object_new_valist (object_type, first_param_name, var_args);
322 g_object_new_valist (GType object_type,
323 const gchar *first_param_name,
328 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
330 object = (GObject*) g_type_create_instance (object_type);
331 if (first_param_name)
332 g_object_set_valist (object, first_param_name, var_args);
338 g_object_do_dispatch_param_changed (GObject *object,
341 g_message ("NOTIFICATION: parameter `%s' changed on object `%s'",
343 G_OBJECT_TYPE_NAME (object));
347 notify_param_changed_handler (gpointer data)
353 /* FIXME: need GDK_THREADS lock */
355 object = G_OBJECT (data);
356 class = G_OBJECT_GET_CLASS (object);
357 slist = g_datalist_id_get_data (&object->qdata, quark_param_changed_queue);
359 /* a reference count is still being held */
361 for (; slist; slist = slist->next)
364 GParamSpec *pspec = slist->data;
367 class->dispatch_param_changed (object, pspec);
370 g_datalist_id_set_data (&object->qdata, quark_param_changed_queue, NULL);
376 g_object_do_queue_param_changed (GObject *object,
379 GSList *slist, *last = NULL;
381 /* if this is a recursive call on this object (i.e. pspecs are queued
382 * for notification, while param_changed notification is currently in
383 * progress), we simply add them to the queue that is currently being
384 * dispatched. otherwise, we later dispatch parameter changed notification
385 * asyncronously from an idle handler untill the queue is completely empty.
388 slist = g_datalist_id_get_data (&object->qdata, quark_param_changed_queue);
389 for (; slist; last = slist, slist = last->next)
390 if (slist->data == pspec)
395 g_object_ref (object);
396 g_idle_add_full (G_NOTIFY_PRIORITY,
397 notify_param_changed_handler,
399 (GDestroyNotify) g_object_unref);
400 g_object_set_qdata_full (object,
401 quark_param_changed_queue,
402 g_slist_prepend (NULL, pspec),
403 (GDestroyNotify) g_slist_free);
406 last->next = g_slist_prepend (NULL, pspec);
410 object_get_param (GObject *object,
413 const gchar *trailer)
417 g_return_if_fail (g_type_is_a (G_OBJECT_TYPE (object), pspec->owner_type)); /* paranoid */
419 class = g_type_class_peek (pspec->owner_type);
421 class->get_param (object, PARAM_SPEC_PARAM_ID (pspec), value, pspec, trailer);
425 object_set_param (GObject *object,
428 const gchar *trailer)
432 g_return_if_fail (g_type_is_a (G_OBJECT_TYPE (object), pspec->owner_type)); /* paranoid */
434 class = g_type_class_peek (pspec->owner_type);
436 class->set_param (object, PARAM_SPEC_PARAM_ID (pspec), value, pspec, trailer);
438 class->queue_param_changed (object, pspec);
442 g_object_set_valist (GObject *object,
443 const gchar *first_param_name,
448 g_return_if_fail (G_IS_OBJECT (object));
450 g_object_ref (object);
452 name = first_param_name;
456 const gchar *trailer = NULL;
457 GValue value = { 0, };
461 pspec = g_param_spec_hash_table_lookup (param_spec_hash_table,
463 G_OBJECT_TYPE (object),
468 g_warning ("%s: object class `%s' has no parameter named `%s'",
470 G_OBJECT_TYPE_NAME (object),
474 if (!(pspec->flags & G_PARAM_WRITABLE))
476 g_warning ("%s: parameter `%s' of object class `%s' is not writable",
479 G_OBJECT_TYPE_NAME (object));
483 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
485 G_VALUE_COLLECT (&value, var_args, &error);
488 g_warning ("%s: %s", G_STRLOC, error);
491 /* we purposely leak the value here, it might not be
492 * in a sane state if an error condition occoured
497 object_set_param (object, &value, pspec, trailer);
499 g_value_unset (&value);
501 name = va_arg (var_args, gchar*);
504 g_object_unref (object);
508 g_object_get_valist (GObject *object,
509 const gchar *first_param_name,
514 g_return_if_fail (G_IS_OBJECT (object));
516 g_object_ref (object);
518 name = first_param_name;
522 const gchar *trailer = NULL;
523 GValue value = { 0, };
527 pspec = g_param_spec_hash_table_lookup (param_spec_hash_table,
529 G_OBJECT_TYPE (object),
534 g_warning ("%s: object class `%s' has no parameter named `%s'",
536 G_OBJECT_TYPE_NAME (object),
540 if (!(pspec->flags & G_PARAM_READABLE))
542 g_warning ("%s: parameter `%s' of object class `%s' is not readable",
545 G_OBJECT_TYPE_NAME (object));
549 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
551 object_get_param (object, &value, pspec, trailer);
553 G_VALUE_LCOPY (&value, var_args, &error);
556 g_warning ("%s: %s", G_STRLOC, error);
559 /* we purposely leak the value here, it might not be
560 * in a sane state if an error condition occoured
565 g_value_unset (&value);
567 name = va_arg (var_args, gchar*);
570 g_object_unref (object);
574 g_object_set (GObject *object,
575 const gchar *first_param_name,
580 g_return_if_fail (G_IS_OBJECT (object));
582 va_start (var_args, first_param_name);
583 g_object_set_valist (object, first_param_name, var_args);
588 g_object_get (GObject *object,
589 const gchar *first_param_name,
594 g_return_if_fail (G_IS_OBJECT (object));
596 va_start (var_args, first_param_name);
597 g_object_get_valist (object, first_param_name, var_args);
602 g_object_set_param (GObject *object,
603 const gchar *param_name,
607 const gchar *trailer;
609 g_return_if_fail (G_IS_OBJECT (object));
610 g_return_if_fail (param_name != NULL);
611 g_return_if_fail (G_IS_VALUE (value));
613 g_object_ref (object);
615 pspec = g_param_spec_hash_table_lookup (param_spec_hash_table,
617 G_OBJECT_TYPE (object),
621 g_warning ("%s: object class `%s' has no parameter named `%s'",
623 G_OBJECT_TYPE_NAME (object),
627 GValue tmp_value = { 0, };
629 /* provide a copy to work from and convert if necessary */
630 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
632 if (!g_value_convert (value, &tmp_value) ||
633 g_param_value_validate (pspec, &tmp_value))
634 g_warning ("%s: cannot convert `%s' value to parameter `%s' value of type `%s'",
636 G_VALUE_TYPE_NAME (value),
638 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
640 object_set_param (object, &tmp_value, pspec, trailer);
642 g_value_unset (&tmp_value);
645 g_object_unref (object);
649 g_object_get_param (GObject *object,
650 const gchar *param_name,
654 const gchar *trailer;
656 g_return_if_fail (G_IS_OBJECT (object));
657 g_return_if_fail (param_name != NULL);
658 g_return_if_fail (G_IS_VALUE (value));
660 g_object_ref (object);
662 pspec = g_param_spec_hash_table_lookup (param_spec_hash_table,
664 G_OBJECT_TYPE (object),
668 g_warning ("%s: object class `%s' has no parameter named `%s'",
670 G_OBJECT_TYPE_NAME (object),
674 GValue tmp_value = { 0, };
676 /* provide a copy to work from and later convert if necessary, so
677 * _get_param() implementations need *not* care about freeing values
678 * that might be already set in the parameter to get.
679 * (though, at this point, GValue should exclusively be modified
680 * through the accessor functions anyways)
682 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
684 if (!g_value_types_exchangable (G_VALUE_TYPE (value), G_PARAM_SPEC_VALUE_TYPE (pspec)))
685 g_warning ("%s: can't retrive parameter `%s' value of type `%s' as value of type `%s'",
688 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
689 G_VALUE_TYPE_NAME (value));
692 object_get_param (object, &tmp_value, pspec, trailer);
693 g_value_convert (&tmp_value, value);
694 /* g_value_validate (value, pspec); */
697 g_value_unset (&tmp_value);
700 g_object_unref (object);
704 g_object_queue_param_changed (GObject *object,
705 const gchar *param_name)
709 g_return_if_fail (G_IS_OBJECT (object));
710 g_return_if_fail (param_name != NULL);
712 pspec = g_param_spec_hash_table_lookup (param_spec_hash_table,
714 G_OBJECT_TYPE (object),
717 g_warning ("%s: object class `%s' has no parameter named `%s'",
719 G_OBJECT_TYPE_NAME (object),
722 G_OBJECT_GET_CLASS (object)->queue_param_changed (object, pspec);
726 g_object_ref (GObject *object)
728 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
729 g_return_val_if_fail (object->ref_count > 0, NULL);
731 object->ref_count += 1;
737 g_object_unref (GObject *object)
739 g_return_if_fail (G_IS_OBJECT (object));
740 g_return_if_fail (object->ref_count > 0);
742 if (object->ref_count > 1)
743 object->ref_count -= 1;
745 g_object_last_unref (object);
749 g_object_get_qdata (GObject *object,
752 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
754 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
758 g_object_set_qdata (GObject *object,
762 g_return_if_fail (G_IS_OBJECT (object));
763 g_return_if_fail (quark > 0);
765 g_datalist_id_set_data (&object->qdata, quark, data);
769 g_object_set_qdata_full (GObject *object,
772 GDestroyNotify destroy)
774 g_return_if_fail (G_IS_OBJECT (object));
775 g_return_if_fail (quark > 0);
777 g_datalist_id_set_data_full (&object->qdata, quark, data, data ? destroy : NULL);
781 g_object_steal_qdata (GObject *object,
784 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
785 g_return_val_if_fail (quark > 0, NULL);
787 return g_datalist_id_remove_no_notify (&object->qdata, quark);
791 g_value_object_init (GValue *value)
793 value->data[0].v_pointer = NULL;
797 g_value_object_free_value (GValue *value)
799 if (value->data[0].v_pointer)
800 g_object_unref (value->data[0].v_pointer);
804 g_value_object_copy_value (const GValue *src_value,
807 if (src_value->data[0].v_pointer)
808 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
810 dest_value->data[0].v_pointer = NULL;
814 g_value_object_collect_value (GValue *value,
817 GTypeCValue *collect_value)
819 if (collect_value->v_pointer)
821 GObject *object = collect_value->v_pointer;
823 if (object->g_type_instance.g_class == NULL)
824 return g_strconcat ("invalid unclassed object pointer for value type `",
825 G_VALUE_TYPE_NAME (value),
828 else if (!g_type_is_a (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
829 return g_strconcat ("invalid object type `",
830 G_OBJECT_TYPE_NAME (object),
831 "' for value type `",
832 G_VALUE_TYPE_NAME (value),
835 value->data[0].v_pointer = g_object_ref (object);
838 value->data[0].v_pointer = NULL;
845 g_value_object_lcopy_value (const GValue *value,
848 GTypeCValue *collect_value)
850 GObject **object_p = collect_value->v_pointer;
853 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
855 *object_p = value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
862 g_value_set_object (GValue *value,
865 g_return_if_fail (G_IS_VALUE_OBJECT (value));
867 g_return_if_fail (G_IS_OBJECT (v_object));
869 if (value->data[0].v_pointer)
870 g_object_unref (value->data[0].v_pointer);
871 value->data[0].v_pointer = v_object;
872 if (value->data[0].v_pointer)
873 g_object_ref (value->data[0].v_pointer);
877 g_value_get_object (GValue *value)
879 g_return_val_if_fail (G_IS_VALUE_OBJECT (value), NULL);
881 return value->data[0].v_pointer;
885 g_value_dup_object (GValue *value)
887 g_return_val_if_fail (G_IS_VALUE_OBJECT (value), NULL);
889 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;