uri: fix wrong G_GNUC_MALLOC
[platform/upstream/gstreamer.git] / gst / gstinterface.c
1 /* GStreamer
2  *
3  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
4  *                    2000 Wim Taymans <wtay@chello.be>
5  *
6  * gstinterface.c: Interface functions
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 /**
25  * SECTION:gstimplementsinterface
26  * @short_description: Core interface implemented by #GstElement instances that
27  * allows runtime querying of interface availability
28  * @see_also: #GstElement
29  *
30  * Provides interface functionality on per instance basis and not per class
31  * basis, which is the case for gobject.
32  */
33
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37
38 #include "gst_private.h"
39 #include "gstinterface.h"
40
41 static void
42 gst_implements_interface_class_init (GstImplementsInterfaceClass * ifklass);
43 static gboolean
44 gst_implements_interface_supported_default (GstImplementsInterface * iface,
45     GType iface_type);
46
47 GType
48 gst_implements_interface_get_type (void)
49 {
50   static volatile gsize gst_interface_type = 0;
51
52   if (g_once_init_enter (&gst_interface_type)) {
53     GType _type;
54     static const GTypeInfo gst_interface_info = {
55       sizeof (GstImplementsInterfaceClass),
56       (GBaseInitFunc) gst_implements_interface_class_init,
57       NULL,
58       NULL,
59       NULL,
60       NULL,
61       0,
62       0,
63       NULL,
64       NULL
65     };
66
67     _type = g_type_register_static (G_TYPE_INTERFACE,
68         "GstImplementsInterface", &gst_interface_info, 0);
69
70     g_type_interface_add_prerequisite (_type, GST_TYPE_ELEMENT);
71     g_once_init_leave (&gst_interface_type, _type);
72   }
73
74   return gst_interface_type;
75 }
76
77 static void
78 gst_implements_interface_class_init (GstImplementsInterfaceClass * klass)
79 {
80   klass->supported = gst_implements_interface_supported_default;
81 }
82
83 static gboolean
84 gst_implements_interface_supported_default (GstImplementsInterface * interface,
85     GType iface_type)
86 {
87   /* Well, if someone didn't set the virtual function,
88    * then something is clearly wrong. So big no-no here */
89
90   return FALSE;
91 }
92
93 /**
94  * gst_element_implements_interface:
95  * @element: #GstElement to check for the implementation of the interface
96  * @iface_type: (final) type of the interface which we want to be implemented
97  *
98  * Test whether the given element implements a certain interface of type
99  * iface_type, and test whether it is supported for this specific instance.
100  *
101  * Returns: whether or not the element implements the interface.
102  */
103
104 gboolean
105 gst_element_implements_interface (GstElement * element, GType iface_type)
106 {
107   g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
108
109   if (G_TYPE_CHECK_INSTANCE_TYPE (G_OBJECT (element), iface_type)) {
110     GstImplementsInterface *iface;
111     GstImplementsInterfaceClass *ifclass;
112
113     iface = G_TYPE_CHECK_INSTANCE_CAST (G_OBJECT (element),
114         iface_type, GstImplementsInterface);
115     ifclass = GST_IMPLEMENTS_INTERFACE_GET_CLASS (iface);
116
117     /* element implements iface_type but not GstImplementsInterface, so
118      * just assume the other interface is implemented unconditionally */
119     if (ifclass == NULL)
120       return TRUE;
121
122     if (ifclass->supported != NULL &&
123         ifclass->supported (iface, iface_type) == TRUE) {
124       return TRUE;
125     }
126   }
127
128   return FALSE;
129 }
130
131 /**
132  * gst_implements_interface_cast:
133  * @from: the object (any sort) from which to cast to the interface
134  * @type: the interface type to cast to
135  *
136  * cast a given object to an interface type, and check whether this
137  * interface is supported for this specific instance.
138  *
139  * Returns: (transfer none): a gpointer to the interface type
140  */
141
142 gpointer
143 gst_implements_interface_cast (gpointer from, GType iface_type)
144 {
145   GstImplementsInterface *iface;
146
147   /* check cast, give warning+fail if it's invalid */
148   if (!(iface = G_TYPE_CHECK_INSTANCE_CAST (from, iface_type,
149               GstImplementsInterface))) {
150     return NULL;
151   }
152
153   /* if we're an element, take care that this interface
154    * is actually implemented */
155   if (GST_IS_ELEMENT (from)) {
156     g_return_val_if_fail (gst_element_implements_interface (GST_ELEMENT (from),
157             iface_type), NULL);
158   }
159
160   return iface;
161 }
162
163 /**
164  * gst_implements_interface_check:
165  * @from: the object (any sort) from which to check from for the interface
166  * @type: the interface type to check for
167  *
168  * check a given object for an interface implementation, and check
169  * whether this interface is supported for this specific instance.
170  *
171  * Returns: whether or not the object implements the given interface
172  */
173
174 gboolean
175 gst_implements_interface_check (gpointer from, GType type)
176 {
177   /* check cast, return FALSE if it fails, don't give a warning... */
178   if (!G_TYPE_CHECK_INSTANCE_TYPE (from, type)) {
179     return FALSE;
180   }
181
182   /* now, if we're an element (or derivative), is this thing
183    * actually implemented for real? */
184   if (GST_IS_ELEMENT (from)) {
185     if (!gst_element_implements_interface (GST_ELEMENT (from), type)) {
186       return FALSE;
187     }
188   }
189
190   return TRUE;
191 }