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, empty object. Not very useful, should never be used.
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
106 * signals the parent-set signal
108 void gst_object_set_parent(GstObject *object,GstObject *parent) {
109 g_return_if_fail(object != NULL);
110 g_return_if_fail(GST_IS_OBJECT(object));
111 g_return_if_fail(object->parent == NULL);
112 g_return_if_fail(parent != NULL);
113 g_return_if_fail(GST_IS_OBJECT(parent));
114 g_return_if_fail(object != parent);
116 gst_object_ref(object);
117 gst_object_sink(object);
118 object->parent = parent;
120 gtk_signal_emit(GTK_OBJECT(object),gst_object_signals[PARENT_SET],parent);
124 * gst_object_get_parent:
125 * @object: GstObject to get parent of
127 * Return the parent of the object.
129 * Returns: parent of the object
131 GstObject *gst_object_get_parent(GstObject *object) {
132 g_return_val_if_fail(object != NULL, NULL);
133 g_return_val_if_fail(GST_IS_OBJECT(object), NULL);
135 return object->parent;
139 * gst_object_unparent:
140 * @object: GstObject to unparent
142 * Clear the parent of the object, removing the associated reference.
144 void gst_object_unparent(GstObject *object) {
145 g_return_if_fail(object != NULL);
146 g_return_if_fail(GST_IS_OBJECT(object));
147 if (object->parent == NULL)
150 object->parent = NULL;
151 gst_object_unref(object);
156 * @object: GstObject to reference
158 * Increments the refence count on the object.
160 #ifndef gst_object_ref
161 void gst_object_ref (GstObject *object) {
162 g_return_if_fail(object != NULL);
163 g_return_if_fail(GST_IS_OBJECT(object));
166 g_return_if_fail(atomic_read(&(object->refcount)) > 0);
167 atomic_inc(&(object->refcount))
169 g_return_if_fail(object->refcount > 0);
175 #endif /* gst_object_ref */
179 * @object: GstObject to unreference
181 * Decrements the refence count on the object. If reference count hits
182 * zero, destroy the object.
184 #ifndef gst_object_unref
185 void gst_object_unref (GstObject *object) {
188 g_return_if_fail(object != NULL);
189 g_return_if_fail(GST_IS_OBJECT(object));
192 g_return_if_fail(atomic_read(&(object->refcount)) > 0);
193 reftest = atomic_dec_and_test(&(object->refcount))
195 g_return_if_fail(object->refcount > 0);
198 reftest = (object->refcount == 0);
202 /* if we ended up with the refcount at zero */
204 /* get the count to 1 for gtk_object_destroy() */
206 atomic_set(&(object->refcount),1);
208 object->refcount = 1;
211 gtk_object_destroy(GTK_OBJECT(object));
212 /* drop the refcount back to zero */
214 atomic_set(&(object->refcount),0);
216 object->refcount = 0;
218 /* finalize the object */
219 // FIXME this is an evil hack that should be killed
220 // FIXMEFIXMEFIXMEFIXME
221 // gtk_object_finalize(GTK_OBJECT(object));
224 #endif /* gst_object_unref */
228 * @object: GstObject to sink
230 * Removes floating reference on an object. Any newly created object has
231 * a refcount of 1 and is FLOATING. This function should be used when
232 * creating a new object to symbolically 'take ownership of' the object.
234 #ifndef gst_object_sink
235 void gst_object_sink(GstObject *object) {
236 g_return_if_fail(object != NULL);
237 g_return_if_fail(GST_IS_OBJECT(object));
239 if (GTK_OBJECT_FLOATING(object)) {
240 GTK_OBJECT_UNSET_FLAGS(object, GTK_FLOATING);
241 gst_object_unref(object);
244 #endif /* gst_object_sink */