Use new GLib API for generically implementing GObject interfaces

This commit is contained in:
Sebastian Dröge 2018-12-19 01:15:23 +02:00
parent 89d69b0dea
commit 93d1349d78
2 changed files with 40 additions and 75 deletions

View file

@ -16,8 +16,6 @@ use glib::translate::*;
use libc;
use std::ptr;
use ChildProxy;
pub trait ChildProxyImpl: super::element::ElementImpl + Send + Sync + 'static {
@ -46,6 +44,21 @@ pub trait ChildProxyImpl: super::element::ElementImpl + Send + Sync + 'static {
fn child_removed(&self, object: &ChildProxy, child: &glib::Object, name: &str);
}
unsafe impl<T: ObjectSubclass + ChildProxyImpl> IsImplementable<T> for ChildProxy {
unsafe extern "C" fn interface_init(
iface: glib_ffi::gpointer,
_iface_data: glib_ffi::gpointer,
) {
let child_proxy_iface = &mut *(iface as *mut ffi::GstChildProxyInterface);
child_proxy_iface.get_child_by_name = Some(child_proxy_get_child_by_name::<T>);
child_proxy_iface.get_child_by_index = Some(child_proxy_get_child_by_index::<T>);
child_proxy_iface.get_children_count = Some(child_proxy_get_children_count::<T>);
child_proxy_iface.child_added = Some(child_proxy_child_added::<T>);
child_proxy_iface.child_removed = Some(child_proxy_child_removed::<T>);
}
}
unsafe extern "C" fn child_proxy_get_child_by_name<T: ObjectSubclass>(
child_proxy: *mut ffi::GstChildProxy,
name: *const libc::c_char,
@ -127,33 +140,3 @@ unsafe extern "C" fn child_proxy_child_removed<T: ObjectSubclass>(
String::from_glib_none(name).as_str(),
)
}
unsafe extern "C" fn child_proxy_init<T: ObjectSubclass>(
iface: glib_ffi::gpointer,
_iface_data: glib_ffi::gpointer,
) where
T: ChildProxyImpl,
{
let child_proxy_iface = &mut *(iface as *mut ffi::GstChildProxyInterface);
child_proxy_iface.get_child_by_name = Some(child_proxy_get_child_by_name::<T>);
child_proxy_iface.get_child_by_index = Some(child_proxy_get_child_by_index::<T>);
child_proxy_iface.get_children_count = Some(child_proxy_get_children_count::<T>);
child_proxy_iface.child_added = Some(child_proxy_child_added::<T>);
child_proxy_iface.child_removed = Some(child_proxy_child_removed::<T>);
}
pub fn register<T: ObjectSubclass + ChildProxyImpl>(type_: glib::subclass::InitializingType<T>) {
unsafe {
let iface_info = gobject_ffi::GInterfaceInfo {
interface_init: Some(child_proxy_init::<T>),
interface_finalize: None,
interface_data: ptr::null_mut(),
};
gobject_ffi::g_type_add_interface_static(
type_.to_glib(),
ffi::gst_child_proxy_get_type(),
&iface_info,
);
}
}

View file

@ -8,7 +8,6 @@
use ffi;
use glib_ffi;
use gobject_ffi;
use glib;
use glib::prelude::*;
@ -18,8 +17,6 @@ use glib::subclass::prelude::*;
use libc;
use std::ptr;
use URIHandler;
use URIType;
@ -30,6 +27,31 @@ pub trait URIHandlerImpl: super::element::ElementImpl + Send + Sync + 'static {
fn get_protocols() -> Vec<String>;
}
unsafe impl<T: ObjectSubclass + URIHandlerImpl> IsImplementable<T> for URIHandler {
unsafe extern "C" fn interface_init(
iface: glib_ffi::gpointer,
_iface_data: glib_ffi::gpointer,
) {
let uri_handler_iface = &mut *(iface as *mut ffi::GstURIHandlerInterface);
// Store the protocols in the interface data for later use
let mut data = T::type_data();
let protocols = T::get_protocols();
let protocols: *mut *const libc::c_char = protocols.to_glib_full();
let data = data.as_mut();
if data.interface_data.is_null() {
data.interface_data = Box::into_raw(Box::new(Vec::new()));
}
(*(data.interface_data as *mut Vec<(glib_ffi::GType, glib_ffi::gpointer)>))
.push((URIHandler::static_type().to_glib(), protocols as *mut _));
uri_handler_iface.get_type = Some(uri_handler_get_type::<T>);
uri_handler_iface.get_protocols = Some(uri_handler_get_protocols::<T>);
uri_handler_iface.get_uri = Some(uri_handler_get_uri::<T>);
uri_handler_iface.set_uri = Some(uri_handler_set_uri::<T>);
}
}
unsafe extern "C" fn uri_handler_get_type<T: ObjectSubclass>(
_type_: glib_ffi::GType,
) -> ffi::GstURIType
@ -83,43 +105,3 @@ where
}
}
}
unsafe extern "C" fn uri_handler_init<T: ObjectSubclass>(
iface: glib_ffi::gpointer,
_iface_data: glib_ffi::gpointer,
) where
T: URIHandlerImpl,
{
let uri_handler_iface = &mut *(iface as *mut ffi::GstURIHandlerInterface);
// Store the protocols in the interface data for later use
let mut data = T::type_data();
let protocols = T::get_protocols();
let protocols: *mut *const libc::c_char = protocols.to_glib_full();
let data = &mut *data.as_mut();
if data.interface_data.is_null() {
data.interface_data = Box::into_raw(Box::new(Vec::new()));
}
(*(data.interface_data as *mut Vec<(glib_ffi::GType, glib_ffi::gpointer)>))
.push((URIHandler::static_type().to_glib(), protocols as *mut _));
uri_handler_iface.get_type = Some(uri_handler_get_type::<T>);
uri_handler_iface.get_protocols = Some(uri_handler_get_protocols::<T>);
uri_handler_iface.get_uri = Some(uri_handler_get_uri::<T>);
uri_handler_iface.set_uri = Some(uri_handler_set_uri::<T>);
}
pub fn register<T: ObjectSubclass + URIHandlerImpl>(type_: &glib::subclass::InitializingType<T>) {
unsafe {
let iface_info = gobject_ffi::GInterfaceInfo {
interface_init: Some(uri_handler_init::<T>),
interface_finalize: None,
interface_data: ptr::null_mut(),
};
gobject_ffi::g_type_add_interface_static(
type_.to_glib(),
ffi::gst_uri_handler_get_type(),
&iface_info,
);
}
}