1 /* Gnome-Streamer
2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 *
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.
8 *
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.
13 *
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.
18 */
21 #include <gst/gstobject.h>
22 #include <gst/gstbin.h>
25 /* Object signals and args */
26 enum {
27 PARENT_SET,
28 LAST_SIGNAL
29 };
31 enum {
32 ARG_0,
33 /* FILL ME */
34 };
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;
47 if (!object_type) {
48 static const GtkTypeInfo object_info = {
49 "GstObject",
50 sizeof(GstObject),
51 sizeof(GstObjectClass),
52 (GtkClassInitFunc)gst_object_class_init,
53 (GtkObjectInitFunc)gst_object_init,
54 (GtkArgSetFunc)NULL,
55 (GtkArgGetFunc)NULL,
56 (GtkClassInitFunc)NULL,
57 };
58 object_type = gtk_type_unique(gtk_object_get_type(),&object_info);
59 }
60 return object_type;
61 }
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,
74 GTK_TYPE_POINTER);
75 gtk_object_class_add_signals(gtkobject_class,gst_object_signals,LAST_SIGNAL);
76 }
78 static void gst_object_init(GstObject *object) {
79 object->lock = g_mutex_new();
80 #ifdef HAVE_ATOMIC_H
81 atomic_set(&(object->refcount),1);
82 #else
83 object->refcount++;
84 #endif
85 object->parent = NULL;
86 }
88 /**
89 * gst_object_new:
90 *
91 * Create a new object with
92 *
93 * Returns: new object
94 */
95 GstObject *gst_object_new() {
96 return GST_OBJECT(gtk_type_new(gst_object_get_type()));
97 }
99 /**
100 * gst_object_set_parent:
101 * @object: GstObject to set parent of
102 * @parent: new parent of object
103 *
104 * Set the parent of the object. The object's reference count is
105 * incremented.
106 */
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);
120 }
122 /**
123 * gst_object_get_parent:
124 * @object: GstObject to get parent of
125 *
126 * Return the parent of the object.
127 *
128 * Returns: parent of the object
129 */
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;
135 }
137 /**
138 * gst_object_unparent:
139 * @object: GstObject to unparent
140 *
141 * Clear the parent of the object, removing the associated reference.
142 */
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)
147 return;
149 object->parent = NULL;
150 gst_object_unref(object);
151 }
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));
158 #ifdef HAVE_ATOMIC_H
159 g_return_if_fail(atomic_read(&(object->refcount)) > 0);
160 atomic_inc(&(object->refcount))
161 #else
162 g_return_if_fail(object->refcount > 0);
163 GST_LOCK(object);
164 object->refcount++;
165 GST_UNLOCK(object);
166 #endif
167 }
168 #endif /* gst_object_ref */
170 #ifndef gst_object_unref
171 void gst_object_unref (GstObject *object) {
172 int reftest;
174 g_return_if_fail(object != NULL);
175 g_return_if_fail(GST_IS_OBJECT(object));
177 #ifdef HAVE_ATOMIC_H
178 g_return_if_fail(atomic_read(&(object->refcount)) > 0);
179 reftest = atomic_dec_and_test(&(object->refcount))
180 #else
181 g_return_if_fail(object->refcount > 0);
182 GST_LOCK(object);
183 object->refcount--;
184 reftest = (object->refcount == 0);
185 GST_UNLOCK(object);
186 #endif
188 /* if we ended up with the refcount at zero */
189 if (reftest) {
190 /* get the count to 1 for gtk_object_destroy() */
191 #ifdef HAVE_ATOMIC_T
192 atomic_set(&(object->refcount),1);
193 #else
194 object->refcount = 1;
195 #endif
196 /* destroy it */
197 gtk_object_destroy(GTK_OBJECT(object));
198 /* drop the refcount back to zero */
199 #ifdef HAVE_ATOMIC_T
200 atomic_set(&(object->refcount),0);
201 #else
202 object->refcount = 0;
203 #endif
204 /* finalize the object */
205 // FIXME this is an evil hack that should be killed
206 // FIXMEFIXMEFIXMEFIXME
207 // gtk_object_finalize(GTK_OBJECT(object));
208 }
209 }
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);
220 }
221 }
222 #endif /* gst_object_sink */