2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * 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.
21 #include <gst/gstobject.h>
22 #include <gst/gstbin.h>
25 /* Object signals and args */
37 static void gst_object_class_init(GstObjectClass *klass);
38 static void gst_object_init(GstObject *object);
41 static GtkObjectClass *parent_class = NULL;
42 static guint gst_object_signals[LAST_SIGNAL] = { 0 };
44 GtkType gst_object_get_type(void) {
45 static GtkType object_type = 0;
48 static const GtkTypeInfo object_info = {
51 sizeof(GstObjectClass),
52 (GtkClassInitFunc)gst_object_class_init,
53 (GtkObjectInitFunc)gst_object_init,
56 (GtkClassInitFunc)NULL,
58 object_type = gtk_type_unique(gtk_object_get_type(),&object_info);
63 static void gst_object_class_init(GstObjectClass *klass) {
64 GtkObjectClass *gtkobject_class;
66 gtkobject_class = (GtkObjectClass*)klass;
68 parent_class = gtk_type_class(gtk_object_get_type());
70 gst_object_signals[PARENT_SET] =
71 gtk_signal_new("parent_set",GTK_RUN_LAST,gtkobject_class->type,
72 GTK_SIGNAL_OFFSET(GstObjectClass,parent_set),
73 gtk_marshal_NONE__POINTER,GTK_TYPE_NONE,1,
75 gtk_object_class_add_signals(gtkobject_class,gst_object_signals,LAST_SIGNAL);
78 static void gst_object_init(GstObject *object) {
79 object->lock = g_mutex_new();
81 atomic_set(&(object->refcount),1);
85 object->parent = NULL;
91 * Create a new object with
95 GstObject *gst_object_new() {
96 return GST_OBJECT(gtk_type_new(gst_object_get_type()));
100 * gst_object_set_parent:
101 * @object: GstObject to set parent of
102 * @parent: new parent of object
104 * Set the parent of the object. The object's reference count is
107 void gst_object_set_parent(GstObject *object,GstObject *parent) {
108 g_return_if_fail(object != NULL);
109 g_return_if_fail(GST_IS_OBJECT(object));
110 g_return_if_fail(object->parent == NULL);
111 g_return_if_fail(parent != NULL);
112 g_return_if_fail(GST_IS_OBJECT(parent));
113 g_return_if_fail(object != parent);
115 gst_object_ref(object);
116 gst_object_sink(object);
117 object->parent = parent;
119 gtk_signal_emit(GTK_OBJECT(object),gst_object_signals[PARENT_SET],parent);
123 * gst_object_get_parent:
124 * @object: GstObject to get parent of
126 * Return the parent of the object.
128 * Returns: parent of the object
130 GstObject *gst_object_get_parent(GstObject *object) {
131 g_return_if_fail(object != NULL);
132 g_return_if_fail(GST_IS_OBJECT(object));
134 return object->parent;
138 * gst_object_unparent:
139 * @object: GstObject to unparent
141 * Clear the parent of the object, removing the associated reference.
143 void gst_object_unparent(GstObject *object) {
144 g_return_if_fail(object != NULL);
145 g_return_if_fail(GST_IS_OBJECT(object));
146 if (object->parent == NULL)
149 object->parent = NULL;
150 gst_object_unref(object);
153 #ifndef gst_object_ref
154 void gst_object_ref (GstObject *object) {
155 g_return_if_fail(object != NULL);
156 g_return_if_fail(GST_IS_OBJECT(object));
159 g_return_if_fail(atomic_read(&(object->refcount)) > 0);
160 atomic_inc(&(object->refcount))
162 g_return_if_fail(object->refcount > 0);
168 #endif /* gst_object_ref */
170 #ifndef gst_object_unref
171 void gst_object_unref (GstObject *object) {
174 g_return_if_fail(object != NULL);
175 g_return_if_fail(GST_IS_OBJECT(object));
178 g_return_if_fail(atomic_read(&(object->refcount)) > 0);
179 reftest = atomic_dec_and_test(&(object->refcount))
181 g_return_if_fail(object->refcount > 0);
184 reftest = (object->refcount == 0);
188 /* if we ended up with the refcount at zero */
190 /* get the count to 1 for gtk_object_destroy() */
192 atomic_set(&(object->refcount),1);
194 object->refcount = 1;
197 gtk_object_destroy(GTK_OBJECT(object));
198 /* drop the refcount back to zero */
200 atomic_set(&(object->refcount),0);
202 object->refcount = 0;
204 /* finalize the object */
205 // FIXME this is an evil hack that should be killed
206 // FIXMEFIXMEFIXMEFIXME
207 // gtk_object_finalize(GTK_OBJECT(object));
210 #endif /* gst_object_unref */
212 #ifndef gst_object_sink
213 void gst_object_sink(GstObject *object) {
214 g_return_if_fail(object != NULL);
215 g_return_if_fail(GST_IS_OBJECT(object));
217 if (GTK_OBJECT_FLOATING(object)) {
218 GTK_OBJECT_UNSET_FLAGS(object, GTK_FLOATING);
219 gst_object_unref(object);
222 #endif /* gst_object_sink */