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 extern void g_object_type_init (void);
35 static void g_object_base_class_init (GObjectClass *class);
36 static void g_object_base_class_finalize (GObjectClass *class);
37 static void g_object_do_class_init (GObjectClass *class);
38 static void g_object_do_init (GObject *object);
39 static void g_object_do_queue_param_changed (GObject *object,
41 static void g_object_do_dispatch_param_changed (GObject *object,
43 static void g_object_last_unref (GObject *object);
44 static void g_object_do_shutdown (GObject *object);
45 static void g_object_do_finalize (GObject *object);
48 /* --- variables --- */
49 static GQuark quark_param_id = 0;
50 static GQuark quark_param_changed_queue = 0;
51 static GHashTable *param_spec_hash_table = NULL;
54 /* --- functions --- */
56 static guint debug_objects_count = 0;
57 static GHashTable *debug_objects_ht = NULL;
59 debug_objects_foreach (gpointer key,
63 GObject *object = value;
65 g_message ("[%p] stale %s\tref_count=%u",
67 G_OBJECT_TYPE_NAME (object),
71 debug_objects_atexit (void)
75 g_message ("stale GObjects: %u", debug_objects_count);
76 g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL);
82 g_object_type_init (void) /* sync with glib-gtype.c */
84 static gboolean initialized = FALSE;
85 static const GTypeFundamentalInfo finfo = {
86 G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE,
87 0 /* n_collect_bytes */,
88 NULL /* GTypeParamCollector */,
90 static GTypeInfo info = {
91 sizeof (GObjectClass),
92 (GBaseInitFunc) g_object_base_class_init,
93 (GBaseFinalizeFunc) g_object_base_class_finalize,
94 (GClassInitFunc) g_object_do_class_init,
95 NULL /* class_destroy */,
96 NULL /* class_data */,
99 (GInstanceInitFunc) g_object_do_init,
103 g_return_if_fail (initialized == FALSE);
108 type = g_type_register_fundamental (G_TYPE_OBJECT, "GObject", &finfo, &info);
109 g_assert (type == G_TYPE_OBJECT);
112 g_atexit (debug_objects_atexit);
117 g_object_base_class_init (GObjectClass *class)
119 /* reset instance specific fields and methods that don't get inherited */
120 class->n_param_specs = 0;
121 class->param_specs = NULL;
122 class->get_param = NULL;
123 class->set_param = NULL;
127 g_object_base_class_finalize (GObjectClass *class)
131 g_message ("finallizing base class of %s", G_OBJECT_CLASS_NAME (class));
133 for (i = 0; i < class->n_param_specs; i++)
135 GParamSpec *pspec = class->param_specs[i];
137 g_param_spec_hash_table_remove (param_spec_hash_table, pspec);
138 g_param_spec_set_qdata (pspec, quark_param_id, NULL);
139 g_param_spec_unref (pspec);
141 class->n_param_specs = 0;
142 g_free (class->param_specs);
143 class->param_specs = NULL;
147 g_object_do_class_init (GObjectClass *class)
149 quark_param_id = g_quark_from_static_string ("glib-object-param-id");
150 quark_param_changed_queue = g_quark_from_static_string ("glib-object-param-changed-queue");
151 param_spec_hash_table = g_param_spec_hash_table_new ();
153 class->queue_param_changed = g_object_do_queue_param_changed;
154 class->dispatch_param_changed = g_object_do_dispatch_param_changed;
155 class->shutdown = g_object_do_shutdown;
156 class->finalize = g_object_do_finalize;
160 g_object_class_install_param (GObjectClass *class,
162 GParamSpec *pspec /* 1 ref_count taken over */)
166 g_return_if_fail (G_IS_OBJECT_CLASS (class));
167 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
168 if (pspec->flags & G_PARAM_WRITABLE)
169 g_return_if_fail (class->set_param != NULL);
170 if (pspec->flags & G_PARAM_READABLE)
171 g_return_if_fail (class->get_param != NULL);
172 g_return_if_fail (param_id > 0);
173 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
175 /* expensive paranoia checks ;( */
176 for (i = 0; i < class->n_param_specs; i++)
177 if (PARAM_SPEC_PARAM_ID (class->param_specs[i]) == param_id)
179 g_warning (G_STRLOC ": class `%s' already contains a parameter `%s' with id %u, "
180 "cannot install parameter `%s'",
181 G_OBJECT_CLASS_NAME (class),
182 class->param_specs[i]->name,
187 if (g_object_class_find_param_spec (class, pspec->name))
189 g_warning (G_STRLOC ": class `%s' already contains a parameter named `%s'",
190 G_OBJECT_CLASS_NAME (class),
195 g_param_spec_set_qdata (pspec, quark_param_id, GUINT_TO_POINTER (param_id));
196 g_param_spec_hash_table_insert (param_spec_hash_table, pspec, G_OBJECT_CLASS_TYPE (class));
197 i = class->n_param_specs++;
198 class->param_specs = g_renew (GParamSpec*, class->param_specs, class->n_param_specs);
199 class->param_specs[i] = pspec;
203 g_object_class_find_param_spec (GObjectClass *class,
204 const gchar *param_name)
206 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
207 g_return_val_if_fail (param_name != NULL, NULL);
209 return g_param_spec_hash_table_lookup (param_spec_hash_table,
211 G_OBJECT_CLASS_TYPE (class),
216 g_object_do_init (GObject *object)
218 object->ref_count = 1;
219 object->qdata = NULL;
222 if (!debug_objects_ht)
223 debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
224 debug_objects_count++;
225 g_hash_table_insert (debug_objects_ht, object, object);
230 g_object_last_unref (GObject *object)
232 g_return_if_fail (object->ref_count > 0);
234 if (object->ref_count == 1) /* may have been re-referenced meanwhile */
235 G_OBJECT_GET_CLASS (object)->shutdown (object);
237 object->ref_count -= 1;
239 if (object->ref_count == 0) /* may have been re-referenced meanwhile */
240 G_OBJECT_GET_CLASS (object)->finalize (object);
244 g_object_do_shutdown (GObject *object)
246 /* this function needs to be always present for unconditional
247 * chaining, we also might add some code here later.
248 * beware though, subclasses may invoke shutdown() arbitrarily.
253 g_object_do_finalize (GObject *object)
255 g_datalist_clear (&object->qdata);
258 g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
260 g_hash_table_remove (debug_objects_ht, object);
261 debug_objects_count--;
264 g_type_free_instance ((GTypeInstance*) object);
268 g_object_new (GType object_type,
269 const gchar *first_param_name,
275 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
277 va_start (var_args, first_param_name);
278 object = g_object_new_valist (object_type, first_param_name, var_args);
285 g_object_new_valist (GType object_type,
286 const gchar *first_param_name,
291 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
293 object = (GObject*) g_type_create_instance (object_type);
294 if (first_param_name)
295 g_object_set_valist (object, first_param_name, var_args);
301 g_object_do_dispatch_param_changed (GObject *object,
304 g_message ("NOTIFICATION: parameter `%s' changed on object `%s'",
306 G_OBJECT_TYPE_NAME (object));
310 notify_param_changed_handler (gpointer data)
316 /* FIXME: need GDK_THREADS lock */
318 object = G_OBJECT (data);
319 class = G_OBJECT_GET_CLASS (object);
320 slist = g_datalist_id_get_data (&object->qdata, quark_param_changed_queue);
322 /* a reference count is still being held */
324 for (; slist; slist = slist->next)
327 GParamSpec *pspec = slist->data;
330 class->dispatch_param_changed (object, pspec);
333 g_datalist_id_set_data (&object->qdata, quark_param_changed_queue, NULL);
339 g_object_do_queue_param_changed (GObject *object,
342 GSList *slist, *last = NULL;
344 /* if this is a recursive call on this object (i.e. pspecs are queued
345 * for notification, while param_changed notification is currently in
346 * progress), we simply add them to the queue that is currently being
347 * dispatched. otherwise, we later dispatch parameter changed notification
348 * asyncronously from an idle handler untill the queue is completely empty.
351 slist = g_datalist_id_get_data (&object->qdata, quark_param_changed_queue);
352 for (; slist; last = slist, slist = last->next)
353 if (slist->data == pspec)
358 g_object_ref (object);
359 g_idle_add_full (G_NOTIFY_PRIORITY,
360 notify_param_changed_handler,
362 (GDestroyNotify) g_object_unref);
363 g_object_set_qdata_full (object,
364 quark_param_changed_queue,
365 g_slist_prepend (NULL, pspec),
366 (GDestroyNotify) g_slist_free);
369 last->next = g_slist_prepend (NULL, pspec);
373 object_get_param (GObject *object,
376 const gchar *trailer)
380 g_return_if_fail (g_type_is_a (G_OBJECT_TYPE (object), pspec->owner_type)); /* paranoid */
382 class = g_type_class_peek (pspec->owner_type);
384 class->get_param (object, PARAM_SPEC_PARAM_ID (pspec), value, pspec, trailer);
388 object_set_param (GObject *object,
391 const gchar *trailer)
395 g_return_if_fail (g_type_is_a (G_OBJECT_TYPE (object), pspec->owner_type)); /* paranoid */
397 class = g_type_class_peek (pspec->owner_type);
399 class->set_param (object, PARAM_SPEC_PARAM_ID (pspec), value, pspec, trailer);
401 class->queue_param_changed (object, pspec);
405 g_object_set_valist (GObject *object,
406 const gchar *first_param_name,
411 g_return_if_fail (G_IS_OBJECT (object));
413 g_object_ref (object);
415 name = first_param_name;
419 const gchar *trailer = NULL;
420 GValue value = { 0, };
424 pspec = g_param_spec_hash_table_lookup (param_spec_hash_table,
426 G_OBJECT_TYPE (object),
431 g_warning ("%s: object class `%s' has no parameter named `%s'",
433 G_OBJECT_TYPE_NAME (object),
437 if (!(pspec->flags & G_PARAM_WRITABLE))
439 g_warning ("%s: parameter `%s' of object class `%s' is not writable",
442 G_OBJECT_TYPE_NAME (object));
446 g_value_init (&value, G_PARAM_SPEC_TYPE (pspec));
448 G_PARAM_COLLECT_VALUE (&value, pspec, var_args, &error);
451 g_warning ("%s: %s", G_STRLOC, error);
454 /* we purposely leak the value here, it might not be
455 * in a sane state if an error condition occoured
460 object_set_param (object, &value, pspec, trailer);
462 g_value_unset (&value);
464 name = va_arg (var_args, gchar*);
467 g_object_unref (object);
471 g_object_get_valist (GObject *object,
472 const gchar *first_param_name,
477 g_return_if_fail (G_IS_OBJECT (object));
479 g_object_ref (object);
481 name = first_param_name;
485 const gchar *trailer = NULL;
486 GValue value = { 0, };
490 pspec = g_param_spec_hash_table_lookup (param_spec_hash_table,
492 G_OBJECT_TYPE (object),
497 g_warning ("%s: object class `%s' has no parameter named `%s'",
499 G_OBJECT_TYPE_NAME (object),
503 if (!(pspec->flags & G_PARAM_READABLE))
505 g_warning ("%s: parameter `%s' of object class `%s' is not readable",
508 G_OBJECT_TYPE_NAME (object));
512 g_value_init (&value, G_PARAM_SPEC_TYPE (pspec));
514 object_get_param (object, &value, pspec, trailer);
516 G_PARAM_LCOPY_VALUE (&value, pspec, var_args, &error);
519 g_warning ("%s: %s", G_STRLOC, error);
522 /* we purposely leak the value here, it might not be
523 * in a sane state if an error condition occoured
528 g_value_unset (&value);
530 name = va_arg (var_args, gchar*);
533 g_object_unref (object);
537 g_object_set (GObject *object,
538 const gchar *first_param_name,
543 g_return_if_fail (G_IS_OBJECT (object));
545 va_start (var_args, first_param_name);
546 g_object_set_valist (object, first_param_name, var_args);
551 g_object_get (GObject *object,
552 const gchar *first_param_name,
557 g_return_if_fail (G_IS_OBJECT (object));
559 va_start (var_args, first_param_name);
560 g_object_get_valist (object, first_param_name, var_args);
565 g_object_set_param (GObject *object,
566 const gchar *param_name,
570 const gchar *trailer;
572 g_return_if_fail (G_IS_OBJECT (object));
573 g_return_if_fail (param_name != NULL);
574 g_return_if_fail (G_IS_VALUE (value));
576 g_object_ref (object);
578 pspec = g_param_spec_hash_table_lookup (param_spec_hash_table,
580 G_OBJECT_TYPE (object),
584 g_warning ("%s: object class `%s' has no parameter named `%s'",
586 G_OBJECT_TYPE_NAME (object),
590 GValue tmp_value = { 0, };
592 /* provide a copy to work from and convert if necessary */
593 g_value_init (&tmp_value, G_PARAM_SPEC_TYPE (pspec));
595 if (!g_value_convert (value, &tmp_value) ||
596 g_value_validate (&tmp_value, pspec))
597 g_warning ("%s: can't convert `%s' value to parameter `%s' of type `%s'",
599 G_VALUE_TYPE_NAME (value),
601 G_PARAM_SPEC_TYPE_NAME (pspec));
603 object_set_param (object, &tmp_value, pspec, trailer);
605 g_value_unset (&tmp_value);
608 g_object_unref (object);
612 g_object_get_param (GObject *object,
613 const gchar *param_name,
617 const gchar *trailer;
619 g_return_if_fail (G_IS_OBJECT (object));
620 g_return_if_fail (param_name != NULL);
621 g_return_if_fail (G_IS_VALUE (value));
623 g_object_ref (object);
625 pspec = g_param_spec_hash_table_lookup (param_spec_hash_table,
627 G_OBJECT_TYPE (object),
631 g_warning ("%s: object class `%s' has no parameter named `%s'",
633 G_OBJECT_TYPE_NAME (object),
637 GValue tmp_value = { 0, };
639 /* provide a copy to work from and later convert if necessary, so
640 * _get_param() implementations need *not* care about freeing values
641 * that might be already set in the parameter to get.
642 * (though, at this point, GValue should exclusively be modified
643 * through the accessor functions anyways)
645 g_value_init (&tmp_value, G_PARAM_SPEC_TYPE (pspec));
647 if (!g_value_types_exchangable (G_VALUE_TYPE (value), G_PARAM_SPEC_TYPE (pspec)))
648 g_warning ("%s: can't retrive parameter `%s' of type `%s' as value of type `%s'",
651 G_PARAM_SPEC_TYPE_NAME (pspec),
652 G_VALUE_TYPE_NAME (value));
655 object_get_param (object, &tmp_value, pspec, trailer);
656 g_value_convert (&tmp_value, value);
657 /* g_value_validate (value, pspec); */
660 g_value_unset (&tmp_value);
663 g_object_unref (object);
667 g_object_queue_param_changed (GObject *object,
668 const gchar *param_name)
672 g_return_if_fail (G_IS_OBJECT (object));
673 g_return_if_fail (param_name != NULL);
675 pspec = g_param_spec_hash_table_lookup (param_spec_hash_table,
677 G_OBJECT_TYPE (object),
680 g_warning ("%s: object class `%s' has no parameter named `%s'",
682 G_OBJECT_TYPE_NAME (object),
685 G_OBJECT_GET_CLASS (object)->queue_param_changed (object, pspec);
689 g_object_ref (GObject *object)
691 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
692 g_return_val_if_fail (object->ref_count > 0, NULL);
694 object->ref_count += 1;
700 g_object_unref (GObject *object)
702 g_return_if_fail (G_IS_OBJECT (object));
703 g_return_if_fail (object->ref_count > 0);
705 if (object->ref_count > 1)
706 object->ref_count -= 1;
708 g_object_last_unref (object);
712 g_object_get_qdata (GObject *object,
715 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
717 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
721 g_object_set_qdata (GObject *object,
725 g_return_if_fail (G_IS_OBJECT (object));
726 g_return_if_fail (quark > 0);
728 g_datalist_id_set_data (&object->qdata, quark, data);
732 g_object_set_qdata_full (GObject *object,
735 GDestroyNotify destroy)
737 g_return_if_fail (G_IS_OBJECT (object));
738 g_return_if_fail (quark > 0);
740 g_datalist_id_set_data_full (&object->qdata, quark, data, data ? destroy : NULL);
744 g_object_steal_qdata (GObject *object,
747 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
748 g_return_val_if_fail (quark > 0, NULL);
750 return g_datalist_id_remove_no_notify (&object->qdata, quark);