Port Bin subclassing

This commit is contained in:
Sebastian Dröge 2018-11-19 18:37:00 +02:00
parent 0a8d34bc23
commit 55dac7eeb4
6 changed files with 170 additions and 3 deletions

View file

@ -83,6 +83,7 @@ conversion_type = "scalar"
[[object]]
name = "Gst.Bin"
subclassing = true
status = "generate"
trait_name = "GstBinExt"
[[object.function]]

View file

@ -2,6 +2,7 @@
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT
use BinClass;
use ChildProxy;
use Element;
#[cfg(any(feature = "v1_10", feature = "dox"))]
@ -26,7 +27,7 @@ use std::mem::transmute;
use std::ptr;
glib_wrapper! {
pub struct Bin(Object<ffi::GstBin, ffi::GstBinClass>): Element, Object, ChildProxy;
pub struct Bin(Object<ffi::GstBin, ffi::GstBinClass, BinClass>): Element, Object, ChildProxy;
match fn {
get_type => || ffi::gst_bin_get_type(),

View file

@ -11,10 +11,11 @@ use Element;
use glib;
use glib::translate::{from_glib, from_glib_full, FromGlibPtrContainer, ToGlib, ToGlibPtr};
use glib::IsA;
use glib::{IsA, IsClassFor};
use ffi;
use std::ops;
use std::path;
pub trait GstBinExtManual {
@ -124,6 +125,30 @@ impl<O: IsA<Bin>> GstBinExtManual for O {
}
}
#[repr(C)]
pub struct BinClass(ffi::GstBinClass);
unsafe impl IsClassFor for BinClass {
type Instance = ::Bin;
}
unsafe impl Send for BinClass {}
unsafe impl Sync for BinClass {}
impl ops::Deref for BinClass {
type Target = ::ElementClass;
fn deref(&self) -> &Self::Target {
self.upcast_ref()
}
}
impl ops::DerefMut for BinClass {
fn deref_mut(&mut self) -> &mut Self::Target {
self.upcast_ref_mut()
}
}
#[cfg(test)]
mod tests {
use super::*;

View file

@ -145,11 +145,13 @@ mod promise;
#[cfg(any(feature = "v1_14", feature = "dox"))]
pub use promise::*;
mod bin;
mod bus;
mod element;
pub use element::ElementClass;
mod bin;
pub use bin::BinClass;
// OS dependent Bus extensions (also import the other plateform mod for doc)
#[cfg(any(feature = "v1_14", feature = "dox"))]
cfg_if! {

View file

@ -0,0 +1,136 @@
// Copyright (C) 2017,2018 Sebastian Dröge <sebastian@centricular.com>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use ffi;
use glib_ffi;
use glib::translate::*;
use super::prelude::*;
use glib::subclass::prelude::*;
use Bin;
use BinClass;
use Element;
use Message;
pub trait BinImpl: ElementImpl + Send + Sync + 'static {
fn add_element(&self, bin: &Bin, element: &Element) -> bool {
self.parent_add_element(bin, element)
}
fn remove_element(&self, bin: &Bin, element: &Element) -> bool {
self.parent_remove_element(bin, element)
}
fn handle_message(&self, bin: &Bin, message: Message) {
self.parent_handle_message(bin, message)
}
fn parent_add_element(&self, bin: &Bin, element: &Element) -> bool {
unsafe {
let data = self.get_type_data();
let parent_class = data.as_ref().get_parent_class() as *mut ffi::GstBinClass;
(*parent_class)
.add_element
.map(|f| from_glib(f(bin.to_glib_none().0, element.to_glib_none().0)))
.unwrap_or(false)
}
}
fn parent_remove_element(&self, bin: &Bin, element: &Element) -> bool {
unsafe {
let data = self.get_type_data();
let parent_class = data.as_ref().get_parent_class() as *mut ffi::GstBinClass;
(*parent_class)
.remove_element
.map(|f| from_glib(f(bin.to_glib_none().0, element.to_glib_none().0)))
.unwrap_or(false)
}
}
fn parent_handle_message(&self, bin: &Bin, message: Message) {
unsafe {
let data = self.get_type_data();
let parent_class = data.as_ref().get_parent_class() as *mut ffi::GstBinClass;
(*parent_class)
.handle_message
.map(move |f| f(bin.to_glib_none().0, message.into_ptr()));
}
}
}
unsafe impl<T: ObjectSubclass + BinImpl> IsSubclassable<T> for BinClass
where
<T as ObjectSubclass>::Instance: PanicPoison,
{
fn override_vfuncs(&mut self) {
<::ElementClass as IsSubclassable<T>>::override_vfuncs(self);
unsafe {
let klass = &mut *(self as *const Self as *mut ffi::GstBinClass);
klass.add_element = Some(bin_add_element::<T>);
klass.remove_element = Some(bin_remove_element::<T>);
klass.handle_message = Some(bin_handle_message::<T>);
}
}
}
unsafe extern "C" fn bin_add_element<T: ObjectSubclass>(
ptr: *mut ffi::GstBin,
element: *mut ffi::GstElement,
) -> glib_ffi::gboolean
where
T: BinImpl,
T::Instance: PanicPoison,
{
glib_floating_reference_guard!(ptr);
let instance = &*(ptr as *mut T::Instance);
let imp = instance.get_impl();
let wrap: Bin = from_glib_borrow(ptr);
gst_panic_to_error!(&wrap, &instance.panicked(), false, {
imp.add_element(&wrap, &from_glib_borrow(element))
})
.to_glib()
}
unsafe extern "C" fn bin_remove_element<T: ObjectSubclass>(
ptr: *mut ffi::GstBin,
element: *mut ffi::GstElement,
) -> glib_ffi::gboolean
where
T: BinImpl,
T::Instance: PanicPoison,
{
glib_floating_reference_guard!(ptr);
let instance = &*(ptr as *mut T::Instance);
let imp = instance.get_impl();
let wrap: Bin = from_glib_borrow(ptr);
gst_panic_to_error!(&wrap, &instance.panicked(), false, {
imp.remove_element(&wrap, &from_glib_borrow(element))
})
.to_glib()
}
unsafe extern "C" fn bin_handle_message<T: ObjectSubclass>(
ptr: *mut ffi::GstBin,
message: *mut ffi::GstMessage,
) where
T: BinImpl,
T::Instance: PanicPoison,
{
glib_floating_reference_guard!(ptr);
let instance = &*(ptr as *mut T::Instance);
let imp = instance.get_impl();
let wrap: Bin = from_glib_borrow(ptr);
gst_panic_to_error!(&wrap, &instance.panicked(), (), {
imp.handle_message(&wrap, from_glib_full(message))
});
}

View file

@ -13,11 +13,13 @@ pub mod error;
#[macro_use]
pub mod plugin;
pub mod bin;
pub mod child_proxy;
pub mod element;
pub mod uri_handler;
pub mod prelude {
pub use super::bin::BinImpl;
pub use super::child_proxy::ChildProxyImpl;
pub use super::element::{ElementClassSubclassExt, ElementImpl, ElementImplExt};
pub use super::uri_handler::URIHandlerImpl;