gstreamer_core/gst/gstinterface.c
changeset 0 0e761a78d257
child 7 567bb019e3e3
equal deleted inserted replaced
-1:000000000000 0:0e761a78d257
       
     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 availabillity
       
    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 #ifdef __SYMBIAN32__
       
    47 EXPORT_C
       
    48 #endif
       
    49 
       
    50 
       
    51 GType
       
    52 gst_implements_interface_get_type (void)
       
    53 {
       
    54   static GType gst_interface_type = 0;
       
    55 
       
    56   if (!gst_interface_type) {
       
    57     static const GTypeInfo gst_interface_info = {
       
    58       sizeof (GstImplementsInterfaceClass),
       
    59       (GBaseInitFunc) gst_implements_interface_class_init,
       
    60       NULL,
       
    61       NULL,
       
    62       NULL,
       
    63       NULL,
       
    64       0,
       
    65       0,
       
    66       NULL,
       
    67       NULL
       
    68     };
       
    69 
       
    70     gst_interface_type = g_type_register_static (G_TYPE_INTERFACE,
       
    71         "GstImplementsInterface", &gst_interface_info, 0);
       
    72 
       
    73     g_type_interface_add_prerequisite (gst_interface_type, GST_TYPE_ELEMENT);
       
    74   }
       
    75 
       
    76   return gst_interface_type;
       
    77 }
       
    78 
       
    79 static void
       
    80 gst_implements_interface_class_init (GstImplementsInterfaceClass * klass)
       
    81 {
       
    82   klass->supported = gst_implements_interface_supported_default;
       
    83 }
       
    84 
       
    85 static gboolean
       
    86 gst_implements_interface_supported_default (GstImplementsInterface * interface,
       
    87     GType iface_type)
       
    88 {
       
    89   /* Well, if someone didn't set the virtual function,
       
    90    * then something is clearly wrong. So big no-no here */
       
    91 
       
    92   return FALSE;
       
    93 }
       
    94 
       
    95 /**
       
    96  * gst_element_implements_interface:
       
    97  * @element: #GstElement to check for the implementation of the interface
       
    98  * @iface_type: (final) type of the interface which we want to be implemented
       
    99  *
       
   100  * Test whether the given element implements a certain interface of type
       
   101  * iface_type, and test whether it is supported for this specific instance.
       
   102  *
       
   103  * Returns: whether or not the element implements the interface.
       
   104  */
       
   105 #ifdef __SYMBIAN32__
       
   106 EXPORT_C
       
   107 #endif
       
   108 
       
   109 
       
   110 gboolean
       
   111 gst_element_implements_interface (GstElement * element, GType iface_type)
       
   112 {
       
   113   g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
       
   114 
       
   115   if (G_TYPE_CHECK_INSTANCE_TYPE (G_OBJECT (element), iface_type)) {
       
   116     GstImplementsInterface *iface;
       
   117     GstImplementsInterfaceClass *ifclass;
       
   118 
       
   119     iface = G_TYPE_CHECK_INSTANCE_CAST (G_OBJECT (element),
       
   120         iface_type, GstImplementsInterface);
       
   121     ifclass = GST_IMPLEMENTS_INTERFACE_GET_CLASS (iface);
       
   122 
       
   123     /* element implements iface_type but not GstImplementsInterface, so
       
   124      * just assume the other interface is implemented unconditionally */
       
   125     if (ifclass == NULL)
       
   126       return TRUE;
       
   127 
       
   128     if (ifclass->supported != NULL &&
       
   129         ifclass->supported (iface, iface_type) == TRUE) {
       
   130       return TRUE;
       
   131     }
       
   132   }
       
   133 
       
   134   return FALSE;
       
   135 }
       
   136 
       
   137 /**
       
   138  * gst_implements_interface_cast:
       
   139  * @from: the object (any sort) from which to cast to the interface
       
   140  * @type: the interface type to cast to
       
   141  *
       
   142  * cast a given object to an interface type, and check whether this
       
   143  * interface is supported for this specific instance.
       
   144  *
       
   145  * Returns: a gpointer to the interface type
       
   146  */
       
   147 #ifdef __SYMBIAN32__
       
   148 EXPORT_C
       
   149 #endif
       
   150 
       
   151 
       
   152 gpointer
       
   153 gst_implements_interface_cast (gpointer from, GType iface_type)
       
   154 {
       
   155   GstImplementsInterface *iface;
       
   156 
       
   157   /* check cast, give warning+fail if it's invalid */
       
   158   if (!(iface = G_TYPE_CHECK_INSTANCE_CAST (from, iface_type,
       
   159               GstImplementsInterface))) {
       
   160     return NULL;
       
   161   }
       
   162 
       
   163   /* if we're an element, take care that this interface
       
   164    * is actually implemented */
       
   165   if (GST_IS_ELEMENT (from)) {
       
   166     g_return_val_if_fail (gst_element_implements_interface (GST_ELEMENT (from),
       
   167             iface_type), NULL);
       
   168   }
       
   169 
       
   170   return iface;
       
   171 }
       
   172 
       
   173 /**
       
   174  * gst_implements_interface_check:
       
   175  * @from: the object (any sort) from which to check from for the interface
       
   176  * @type: the interface type to check for
       
   177  *
       
   178  * check a given object for an interface implementation, and check
       
   179  * whether this interface is supported for this specific instance.
       
   180  *
       
   181  * Returns: whether or not the object implements the given interface
       
   182  */
       
   183 #ifdef __SYMBIAN32__
       
   184 EXPORT_C
       
   185 #endif
       
   186 
       
   187 
       
   188 gboolean
       
   189 gst_implements_interface_check (gpointer from, GType type)
       
   190 {
       
   191   GstImplementsInterface *iface;
       
   192 
       
   193   /* check cast, return FALSE if it fails, don't give a warning... */
       
   194   if (!G_TYPE_CHECK_INSTANCE_TYPE (from, type)) {
       
   195     return FALSE;
       
   196   }
       
   197 
       
   198   iface = G_TYPE_CHECK_INSTANCE_CAST (from, type, GstImplementsInterface);
       
   199 
       
   200   /* now, if we're an element (or derivative), is this thing
       
   201    * actually implemented for real? */
       
   202   if (GST_IS_ELEMENT (from)) {
       
   203     if (!gst_element_implements_interface (GST_ELEMENT (from), type)) {
       
   204       return FALSE;
       
   205     }
       
   206   }
       
   207 
       
   208   return TRUE;
       
   209 }